2024-02-03 11:34:26 +01:00
|
|
|
// Copyright 2022 The Gitea Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package websocket
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2024-02-03 16:41:20 +01:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2024-02-03 11:34:26 +01:00
|
|
|
notify_service "code.gitea.io/gitea/services/notify"
|
2024-02-03 14:11:05 +01:00
|
|
|
"github.com/olahol/melody"
|
2024-02-03 11:34:26 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type webhookNotifier struct {
|
|
|
|
notify_service.NullNotifier
|
2024-02-03 14:11:05 +01:00
|
|
|
m *melody.Melody
|
2024-02-03 11:34:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ notify_service.Notifier = &webhookNotifier{}
|
|
|
|
|
|
|
|
// NewNotifier create a new webhooksNotifier notifier
|
2024-02-03 14:11:05 +01:00
|
|
|
func NewNotifier(m *melody.Melody) notify_service.Notifier {
|
|
|
|
return &webhookNotifier{
|
|
|
|
m: m,
|
|
|
|
}
|
2024-02-03 11:34:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *webhookNotifier) CreateIssueComment(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, issue *issues_model.Issue, comment *issues_model.Comment, mentions []*user_model.User) {
|
2024-02-03 14:11:05 +01:00
|
|
|
// TODO: use proper message
|
|
|
|
msg := []byte("<div hx-swap-oob=\"beforebegin:.timeline-item.comment.form\"><div class=\"hello\">hello world!</div></div>")
|
|
|
|
|
2024-02-03 16:41:20 +01:00
|
|
|
err := n.m.BroadcastFilter(msg, func(s *melody.Session) bool {
|
2024-02-03 14:11:05 +01:00
|
|
|
sessionData, err := getSessionData(s)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if sessionData.uid == doer.ID {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, mention := range mentions {
|
|
|
|
if mention.ID == sessionData.uid {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
})
|
2024-02-03 16:41:20 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Failed to broadcast message: %v", err)
|
|
|
|
}
|
2024-02-03 11:34:26 +01:00
|
|
|
}
|