2024-02-03 10:34:26 +00:00
|
|
|
// Copyright 2022 The Gitea Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package websocket
|
|
|
|
|
|
|
|
import (
|
2024-02-03 19:30:04 +00:00
|
|
|
"bytes"
|
2024-02-03 10:34:26 +00:00
|
|
|
"context"
|
2024-02-03 19:30:04 +00:00
|
|
|
"fmt"
|
|
|
|
"html/template"
|
2024-02-03 10:34:26 +00:00
|
|
|
|
|
|
|
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 19:30:04 +00:00
|
|
|
"code.gitea.io/gitea/modules/base"
|
2024-02-03 15:41:20 +00:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2024-02-03 10:34:26 +00:00
|
|
|
notify_service "code.gitea.io/gitea/services/notify"
|
2024-02-03 13:11:05 +00:00
|
|
|
"github.com/olahol/melody"
|
2024-02-03 10:34:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type webhookNotifier struct {
|
|
|
|
notify_service.NullNotifier
|
2024-02-03 13:11:05 +00:00
|
|
|
m *melody.Melody
|
2024-02-03 10:34:26 +00:00
|
|
|
}
|
|
|
|
|
2024-02-03 19:30:04 +00:00
|
|
|
var (
|
|
|
|
_ notify_service.Notifier = &webhookNotifier{}
|
|
|
|
tplIssueComment base.TplName = "repo/issue/view"
|
|
|
|
)
|
2024-02-03 10:34:26 +00:00
|
|
|
|
|
|
|
// NewNotifier create a new webhooksNotifier notifier
|
2024-02-03 13:11:05 +00:00
|
|
|
func NewNotifier(m *melody.Melody) notify_service.Notifier {
|
|
|
|
return &webhookNotifier{
|
|
|
|
m: m,
|
|
|
|
}
|
2024-02-03 10:34:26 +00:00
|
|
|
}
|
|
|
|
|
2024-02-03 19:30:04 +00:00
|
|
|
var addElementHTML = "<div hx-swap-oob=\"beforebegin:%s\">%s</div>"
|
|
|
|
|
2024-02-03 10:34:26 +00: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 13:11:05 +00:00
|
|
|
// TODO: use proper message
|
2024-02-03 19:30:04 +00:00
|
|
|
var content bytes.Buffer
|
|
|
|
|
|
|
|
tmpl := new(template.Template)
|
|
|
|
if err := tmpl.ExecuteTemplate(&content, string(tplIssueComment), comment); err != nil {
|
|
|
|
log.Error("Template: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := fmt.Sprintf(addElementHTML, ".timeline-item.comment.form", "test")
|
2024-02-03 13:11:05 +00:00
|
|
|
|
2024-02-03 19:30:04 +00:00
|
|
|
err := n.m.BroadcastFilter([]byte(msg), func(s *melody.Session) bool {
|
2024-02-03 13:11:05 +00: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 15:41:20 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Failed to broadcast message: %v", err)
|
|
|
|
}
|
2024-02-03 10:34:26 +00:00
|
|
|
}
|