@@ -5,22 +5,45 @@ package websocket
55
66import (
77 "context"
8- "encoding/json"
98 "fmt"
109
1110 issues_model "code.gitea.io/gitea/models/issues"
11+ "code.gitea.io/gitea/models/perm"
12+ "code.gitea.io/gitea/models/perm/access"
13+ repo_model "code.gitea.io/gitea/models/repo"
14+ "code.gitea.io/gitea/models/unit"
1215 user_model "code.gitea.io/gitea/models/user"
13- "code.gitea.io/gitea/services/pubsub"
16+ "code.gitea.io/gitea/modules/log"
17+
18+ "github.com/olahol/melody"
1419)
1520
16- func (n * websocketNotifier ) DeleteComment (ctx context.Context , doer * user_model.User , c * issues_model.Comment ) {
17- d , err := json .Marshal (c )
18- if err != nil {
19- return
20- }
21+ func (n * websocketNotifier ) filterIssueSessions (ctx context.Context , repo * repo_model.Repository , issue * issues_model.Issue ) []* melody.Session {
22+ return n .filterSessions (func (s * melody.Session , data * sessionData ) bool {
23+ // if the user is watching the issue, they will get notifications
24+ if ! data .isOnURL (fmt .Sprintf ("/%s/%s/issues/%d" , repo .Owner .Name , repo .Name , issue .Index )) {
25+ return false
26+ }
2127
22- topic := fmt .Sprintf ("repo:%s/%s" , c .RefRepo .OwnerName , c .RefRepo .Name )
23- n .pubsub .Publish (ctx , topic , pubsub.Message {
24- Data : d ,
28+ // the user will get notifications if they have access to the repos issues
29+ hasAccess , err := access .HasAccessUnit (ctx , data .user , repo , unit .TypeIssues , perm .AccessModeRead )
30+ if err != nil {
31+ log .Error ("Failed to check access: %v" , err )
32+ return false
33+ }
34+
35+ return hasAccess
2536 })
2637}
38+
39+ func (n * websocketNotifier ) DeleteComment (ctx context.Context , doer * user_model.User , c * issues_model.Comment ) {
40+ sessions := n .filterIssueSessions (ctx , c .Issue .Repo , c .Issue )
41+
42+ for _ , s := range sessions {
43+ msg := fmt .Sprintf (htmxRemoveElement , fmt .Sprintf ("#%s" , c .HashTag ()))
44+ err := s .Write ([]byte (msg ))
45+ if err != nil {
46+ log .Error ("Failed to write to session: %v" , err )
47+ }
48+ }
49+ }
0 commit comments