2024-02-11 14:26:53 +01:00
|
|
|
// Copyright 2024 The Gitea Authors. All rights reserved.
|
2024-02-03 11:34:26 +01:00
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package websocket
|
|
|
|
|
|
|
|
import (
|
2024-02-03 16:41:20 +01:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2024-02-03 23:24:40 +01:00
|
|
|
"code.gitea.io/gitea/modules/templates"
|
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
|
|
|
)
|
|
|
|
|
2024-02-11 14:11:51 +01:00
|
|
|
type websocketNotifier struct {
|
2024-02-03 11:34:26 +01:00
|
|
|
notify_service.NullNotifier
|
2024-02-03 23:24:40 +01:00
|
|
|
m *melody.Melody
|
|
|
|
rnd *templates.HTMLRender
|
2024-02-03 11:34:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewNotifier create a new webhooksNotifier notifier
|
2024-02-16 17:19:50 +01:00
|
|
|
func newNotifier(m *melody.Melody) notify_service.Notifier {
|
2024-02-11 14:11:51 +01:00
|
|
|
return &websocketNotifier{
|
2024-02-03 23:24:40 +01:00
|
|
|
m: m,
|
|
|
|
rnd: templates.HTMLRenderer(),
|
2024-02-03 14:11:05 +01:00
|
|
|
}
|
2024-02-03 11:34:26 +01:00
|
|
|
}
|
|
|
|
|
2024-02-11 14:26:53 +01:00
|
|
|
// htmxAddElementEnd = "<div hx-swap-oob=\"beforebegin:%s\">%s</div>"
|
|
|
|
// htmxUpdateElement = "<div hx-swap-oob=\"outerHTML:%s\">%s</div>"
|
|
|
|
|
|
|
|
var htmxRemoveElement = "<div hx-swap-oob=\"delete:%s\"></div>"
|
2024-02-03 20:30:04 +01:00
|
|
|
|
2024-02-11 14:11:51 +01:00
|
|
|
func (n *websocketNotifier) filterSessions(fn func(*melody.Session, *sessionData) bool) []*melody.Session {
|
2024-02-03 23:24:40 +01:00
|
|
|
sessions, err := n.m.Sessions()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Failed to get sessions: %v", err)
|
|
|
|
return nil
|
|
|
|
}
|
2024-02-03 20:30:04 +01:00
|
|
|
|
2024-02-03 23:24:40 +01:00
|
|
|
_sessions := make([]*melody.Session, 0, len(sessions))
|
|
|
|
for _, s := range sessions {
|
2024-02-11 14:11:51 +01:00
|
|
|
data, err := getSessionData(s)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if fn(s, data) {
|
2024-02-03 23:24:40 +01:00
|
|
|
_sessions = append(_sessions, s)
|
|
|
|
}
|
2024-02-03 20:30:04 +01:00
|
|
|
}
|
|
|
|
|
2024-02-03 23:24:40 +01:00
|
|
|
return _sessions
|
|
|
|
}
|