This commit is contained in:
Anbraten 2024-09-20 10:29:36 +02:00
parent edb0f9c188
commit f2001a2e7a
No known key found for this signature in database
GPG Key ID: B1222603899C6B25
3 changed files with 37 additions and 18 deletions

View File

@ -13,7 +13,7 @@ import (
notify_service "code.gitea.io/gitea/services/notify"
)
func Init() Broker {
func InitWithNotifier() Broker {
broker := NewMemory() // TODO: allow for other pubsub implementations
notify_service.RegisterNotifier(newNotifier(broker))
return broker

View File

@ -5,22 +5,45 @@ package websocket
import (
"context"
"encoding/json"
"fmt"
issues_model "code.gitea.io/gitea/models/issues"
"code.gitea.io/gitea/models/perm"
"code.gitea.io/gitea/models/perm/access"
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unit"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/services/pubsub"
"code.gitea.io/gitea/modules/log"
"github.com/olahol/melody"
)
func (n *websocketNotifier) DeleteComment(ctx context.Context, doer *user_model.User, c *issues_model.Comment) {
d, err := json.Marshal(c)
if err != nil {
return
}
func (n *websocketNotifier) filterIssueSessions(ctx context.Context, repo *repo_model.Repository, issue *issues_model.Issue) []*melody.Session {
return n.filterSessions(func(s *melody.Session, data *sessionData) bool {
// if the user is watching the issue, they will get notifications
if !data.isOnURL(fmt.Sprintf("/%s/%s/issues/%d", repo.Owner.Name, repo.Name, issue.Index)) {
return false
}
topic := fmt.Sprintf("repo:%s/%s", c.RefRepo.OwnerName, c.RefRepo.Name)
n.pubsub.Publish(ctx, topic, pubsub.Message{
Data: d,
// the user will get notifications if they have access to the repos issues
hasAccess, err := access.HasAccessUnit(ctx, data.user, repo, unit.TypeIssues, perm.AccessModeRead)
if err != nil {
log.Error("Failed to check access: %v", err)
return false
}
return hasAccess
})
}
func (n *websocketNotifier) DeleteComment(ctx context.Context, doer *user_model.User, c *issues_model.Comment) {
sessions := n.filterIssueSessions(ctx, c.Issue.Repo, c.Issue)
for _, s := range sessions {
msg := fmt.Sprintf(htmxRemoveElement, fmt.Sprintf("#%s", c.HashTag()))
err := s.Write([]byte(msg))
if err != nil {
log.Error("Failed to write to session: %v", err)
}
}
}

View File

@ -31,14 +31,12 @@ type subscribeMessageData struct {
func Init() *melody.Melody {
m = melody.New()
hub := &hub{
pubsub: pubsub.NewMemory(),
}
hub := &hub{}
m.HandleConnect(hub.handleConnect)
m.HandleMessage(hub.handleMessage)
m.HandleDisconnect(hub.handleDisconnect)
broker := pubsub.NewMemory() // TODO: allow for other pubsub implementations
broker := pubsub.InitWithNotifier()
notifier := newNotifier(m)
ctx, unsubscribe := context.WithCancel(context.Background())
@ -77,9 +75,7 @@ func Init() *melody.Melody {
return m
}
type hub struct {
pubsub pubsub.Broker
}
type hub struct{}
func (h *hub) handleConnect(s *melody.Session) {
ctx := gitea_context.GetWebContext(s.Request)