Skip to content

Commit f2001a2

Browse files
committed
fix
1 parent edb0f9c commit f2001a2

File tree

3 files changed

+37
-18
lines changed

3 files changed

+37
-18
lines changed

services/pubsub/notifier.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
notify_service "code.gitea.io/gitea/services/notify"
1414
)
1515

16-
func Init() Broker {
16+
func InitWithNotifier() Broker {
1717
broker := NewMemory() // TODO: allow for other pubsub implementations
1818
notify_service.RegisterNotifier(newNotifier(broker))
1919
return broker

services/websocket/comment_notifier.go

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,45 @@ package websocket
55

66
import (
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+
}

services/websocket/websocket.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,12 @@ type subscribeMessageData struct {
3131

3232
func Init() *melody.Melody {
3333
m = melody.New()
34-
hub := &hub{
35-
pubsub: pubsub.NewMemory(),
36-
}
34+
hub := &hub{}
3735
m.HandleConnect(hub.handleConnect)
3836
m.HandleMessage(hub.handleMessage)
3937
m.HandleDisconnect(hub.handleDisconnect)
4038

41-
broker := pubsub.NewMemory() // TODO: allow for other pubsub implementations
39+
broker := pubsub.InitWithNotifier()
4240
notifier := newNotifier(m)
4341

4442
ctx, unsubscribe := context.WithCancel(context.Background())
@@ -77,9 +75,7 @@ func Init() *melody.Melody {
7775
return m
7876
}
7977

80-
type hub struct {
81-
pubsub pubsub.Broker
82-
}
78+
type hub struct{}
8379

8480
func (h *hub) handleConnect(s *melody.Session) {
8581
ctx := gitea_context.GetWebContext(s.Request)

0 commit comments

Comments
 (0)