Skip to content

Commit 7daafae

Browse files
committed
Merge branch 'issue-updates' into pubsub
2 parents 63f4e6c + 0262846 commit 7daafae

File tree

6 files changed

+36
-45
lines changed

6 files changed

+36
-45
lines changed

assets/go-licenses.json

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go.mod

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,10 +235,7 @@ require (
235235
github.com/gorilla/securecookie v1.1.2 // indirect
236236
github.com/gorilla/websocket v1.5.0 // indirect
237237
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 // indirect
238-
github.com/hashicorp/errwrap v1.1.0 // indirect
239238
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
240-
github.com/hashicorp/go-multierror v1.1.1 // indirect
241-
github.com/hashicorp/go-retryablehttp v0.7.7 // indirect
242239
github.com/hashicorp/hcl v1.0.0 // indirect
243240
github.com/imdario/mergo v0.3.16 // indirect
244241
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -476,8 +476,6 @@ github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brv
476476
github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ=
477477
github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48=
478478
github.com/hashicorp/go-hclog v1.6.3 h1:Qr2kF+eVWjTiYmU7Y31tYlP1h0q/X3Nl3tPGdaB11/k=
479-
github.com/hashicorp/go-hclog v1.6.3/go.mod h1:W4Qnvbt70Wk/zYJryRzDRU/4r0kIg0PVHBcfoyhpF5M=
480-
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
481479
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
482480
github.com/hashicorp/go-retryablehttp v0.7.7 h1:C8hUCYzor8PIfXHa4UrZkU4VvK8o9ISHxT2Q8+VepXU=
483481
github.com/hashicorp/go-retryablehttp v0.7.7/go.mod h1:pkQpWZeYWskR+D1tR2O5OcBFOxfA7DoAO6xtkuQnHTk=

services/websocket/comment_notifier.go

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

66
import (
77
"context"
8+
"encoding/json"
89
"fmt"
910

1011
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"
1512
user_model "code.gitea.io/gitea/models/user"
16-
"code.gitea.io/gitea/modules/log"
17-
"github.com/olahol/melody"
13+
"code.gitea.io/gitea/services/pubsub"
1814
)
1915

20-
func (n *websocketNotifier) filterIssueSessions(ctx context.Context, repo *repo_model.Repository, issue *issues_model.Issue) []*melody.Session {
21-
return n.filterSessions(func(s *melody.Session, data *sessionData) bool {
22-
// if the user is watching the issue, they will get notifications
23-
if !data.isOnURL(fmt.Sprintf("/%s/%s/issues/%d", repo.Owner.Name, repo.Name, issue.Index)) {
24-
return false
25-
}
26-
27-
// the user will get notifications if they have access to the repos issues
28-
hasAccess, err := access.HasAccessUnit(ctx, data.user, repo, unit.TypeIssues, perm.AccessModeRead)
29-
if err != nil {
30-
log.Error("Failed to check access: %v", err)
31-
return false
32-
}
33-
34-
return hasAccess
35-
})
36-
}
37-
3816
func (n *websocketNotifier) DeleteComment(ctx context.Context, doer *user_model.User, c *issues_model.Comment) {
39-
sessions := n.filterIssueSessions(ctx, c.Issue.Repo, c.Issue)
40-
41-
for _, s := range sessions {
42-
msg := fmt.Sprintf(htmxRemoveElement, fmt.Sprintf("#%s", c.HashTag()))
43-
err := s.Write([]byte(msg))
44-
if err != nil {
45-
log.Error("Failed to write to session: %v", err)
46-
}
17+
d, err := json.Marshal(c)
18+
if err != nil {
19+
return
4720
}
21+
22+
topic := fmt.Sprintf("repo:%s/%s", c.RefRepo.OwnerName, c.RefRepo.Name)
23+
n.pubsub.Publish(ctx, topic, pubsub.Message{
24+
Data: d,
25+
})
4826
}

services/websocket/notifier.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"code.gitea.io/gitea/modules/log"
88
"code.gitea.io/gitea/modules/templates"
99
notify_service "code.gitea.io/gitea/services/notify"
10+
1011
"github.com/olahol/melody"
1112
)
1213

services/websocket/websocket.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,12 @@ type subscribeMessageData struct {
3131

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

3841
broker := pubsub.NewMemory() // TODO: allow for other pubsub implementations
3942
notifier := newNotifier(m)
@@ -74,7 +77,11 @@ func Init() *melody.Melody {
7477
return m
7578
}
7679

77-
func handleConnect(s *melody.Session) {
80+
type hub struct {
81+
pubsub pubsub.Broker
82+
}
83+
84+
func (h *hub) handleConnect(s *melody.Session) {
7885
ctx := gitea_context.GetWebContext(s.Request)
7986

8087
data := &sessionData{}
@@ -87,7 +94,7 @@ func handleConnect(s *melody.Session) {
8794
// TODO: handle logouts
8895
}
8996

90-
func handleMessage(s *melody.Session, _msg []byte) {
97+
func (h *hub) handleMessage(s *melody.Session, _msg []byte) {
9198
data, err := getSessionData(s)
9299
if err != nil {
93100
return
@@ -101,14 +108,14 @@ func handleMessage(s *melody.Session, _msg []byte) {
101108

102109
switch msg.Action {
103110
case "subscribe":
104-
err := handleSubscribeMessage(data, msg.Data)
111+
err := h.handleSubscribeMessage(data, msg.Data)
105112
if err != nil {
106113
return
107114
}
108115
}
109116
}
110117

111-
func handleSubscribeMessage(data *sessionData, _data any) error {
118+
func (h *hub) handleSubscribeMessage(data *sessionData, _data any) error {
112119
msgData := &subscribeMessageData{}
113120
err := mapstructure.Decode(_data, &msgData)
114121
if err != nil {
@@ -119,5 +126,5 @@ func handleSubscribeMessage(data *sessionData, _data any) error {
119126
return nil
120127
}
121128

122-
func handleDisconnect(s *melody.Session) {
129+
func (h *hub) handleDisconnect(s *melody.Session) {
123130
}

0 commit comments

Comments
 (0)