44package websocket
55
66import (
7+ "context"
8+
9+ issues_model "code.gitea.io/gitea/models/issues"
10+ user_model "code.gitea.io/gitea/models/user"
11+ "code.gitea.io/gitea/modules/graceful"
712 "code.gitea.io/gitea/modules/json"
8- "code.gitea.io/gitea/services/context "
9- notify_service "code.gitea.io/gitea/services/notify "
13+ "code.gitea.io/gitea/modules/log "
14+ gitea_context "code.gitea.io/gitea/services/context "
1015 "code.gitea.io/gitea/services/pubsub"
1116
1217 "github.com/mitchellh/mapstructure"
@@ -31,12 +36,46 @@ func Init() *melody.Melody {
3136 m .HandleDisconnect (handleDisconnect )
3237
3338 broker := pubsub .NewMemory () // TODO: allow for other pubsub implementations
34- notify_service .RegisterNotifier (newNotifier (m , broker ))
39+ notifier := newNotifier (m )
40+
41+ ctx , unsubscribe := context .WithCancel (context .Background ())
42+ graceful .GetManager ().RunAtShutdown (ctx , func () {
43+ unsubscribe ()
44+ })
45+
46+ broker .Subscribe (ctx , "notify" , func (msg []byte ) {
47+ data := struct {
48+ Function string
49+ }{}
50+
51+ err := json .Unmarshal (msg , & data )
52+ if err != nil {
53+ log .Error ("Failed to unmarshal message: %v" , err )
54+ return
55+ }
56+
57+ switch data .Function {
58+ case "DeleteComment" :
59+ var data struct {
60+ Comment * issues_model.Comment
61+ Doer * user_model.User
62+ }
63+
64+ err := json .Unmarshal (msg , & data )
65+ if err != nil {
66+ log .Error ("Failed to unmarshal message: %v" , err )
67+ return
68+ }
69+
70+ notifier .DeleteComment (context .Background (), data .Doer , data .Comment )
71+ }
72+ })
73+
3574 return m
3675}
3776
3877func handleConnect (s * melody.Session ) {
39- ctx := context .GetWebContext (s .Request )
78+ ctx := gitea_context .GetWebContext (s .Request )
4079
4180 data := & sessionData {}
4281 if ctx .IsSigned {
0 commit comments