mirror of
https://github.com/go-gitea/gitea
synced 2025-02-06 20:47:00 +01:00
cleanup
This commit is contained in:
parent
d9c4554108
commit
ea06a25d72
@ -6,26 +6,16 @@ package websocket
|
||||
import (
|
||||
"code.gitea.io/gitea/modules/context"
|
||||
"code.gitea.io/gitea/modules/web"
|
||||
notify_service "code.gitea.io/gitea/services/notify"
|
||||
"code.gitea.io/gitea/services/websocket"
|
||||
|
||||
"github.com/olahol/melody"
|
||||
)
|
||||
|
||||
var m *melody.Melody
|
||||
|
||||
func Init(r *web.Route) {
|
||||
m = melody.New()
|
||||
r.Any("/-/ws", webSocket)
|
||||
m.HandleConnect(websocket.HandleConnect)
|
||||
m.HandleMessage(websocket.HandleMessage)
|
||||
m.HandleDisconnect(websocket.HandleDisconnect)
|
||||
notify_service.RegisterNotifier(websocket.NewNotifier(m))
|
||||
}
|
||||
m := websocket.Init()
|
||||
|
||||
func webSocket(ctx *context.Context) {
|
||||
err := m.HandleRequest(ctx.Resp, ctx.Req)
|
||||
if err != nil {
|
||||
ctx.ServerError("HandleRequest", err)
|
||||
}
|
||||
r.Any("/-/ws", func(ctx *context.Context) {
|
||||
err := m.HandleRequest(ctx.Resp, ctx.Req)
|
||||
if err != nil {
|
||||
ctx.ServerError("HandleRequest", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -8,33 +8,35 @@ import (
|
||||
"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/modules/log"
|
||||
"github.com/olahol/melody"
|
||||
)
|
||||
|
||||
func (n *websocketNotifier) filterIssueSessions(repo *repo_model.Repository, issue *issues_model.Issue) []*melody.Session {
|
||||
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
|
||||
}
|
||||
|
||||
// if the repo is public, the user will get notifications
|
||||
if !repo.IsPrivate {
|
||||
return true
|
||||
// if the repo is private, the user will get notifications if they have access to the repo
|
||||
hasAccess, err := access.HasAccessUnit(ctx, data.user, repo, unit.TypeIssues, perm.AccessModeNone)
|
||||
if err != nil {
|
||||
log.Error("Failed to check access: %v", err)
|
||||
return false
|
||||
}
|
||||
|
||||
// if the repo is private, the user will get notifications if they have access to the repo
|
||||
|
||||
// TODO: check if the user has access to the repo
|
||||
return data.userID == issue.PosterID
|
||||
return hasAccess
|
||||
})
|
||||
}
|
||||
|
||||
func (n *websocketNotifier) DeleteComment(ctx context.Context, doer *user_model.User, c *issues_model.Comment) {
|
||||
sessions := n.filterIssueSessions(c.Issue.Repo, c.Issue)
|
||||
sessions := n.filterIssueSessions(ctx, c.Issue.Repo, c.Issue)
|
||||
|
||||
for _, s := range sessions {
|
||||
msg := fmt.Sprintf(htmxRemoveElement, fmt.Sprintf("#%s", c.HashTag()))
|
||||
|
@ -17,7 +17,7 @@ type websocketNotifier struct {
|
||||
}
|
||||
|
||||
// NewNotifier create a new webhooksNotifier notifier
|
||||
func NewNotifier(m *melody.Melody) notify_service.Notifier {
|
||||
func newNotifier(m *melody.Melody) notify_service.Notifier {
|
||||
return &websocketNotifier{
|
||||
m: m,
|
||||
rnd: templates.HTMLRenderer(),
|
||||
|
@ -8,12 +8,13 @@ import (
|
||||
"net/url"
|
||||
|
||||
"github.com/olahol/melody"
|
||||
|
||||
user_model "code.gitea.io/gitea/models/user"
|
||||
)
|
||||
|
||||
type sessionData struct {
|
||||
userID int64
|
||||
isSigned bool
|
||||
onURL string
|
||||
user *user_model.User
|
||||
onURL string
|
||||
}
|
||||
|
||||
func (d *sessionData) isOnURL(_u1 string) bool {
|
||||
|
@ -6,12 +6,15 @@ package websocket
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/olahol/melody"
|
||||
|
||||
"code.gitea.io/gitea/modules/context"
|
||||
"code.gitea.io/gitea/modules/json"
|
||||
|
||||
"github.com/olahol/melody"
|
||||
notify_service "code.gitea.io/gitea/services/notify"
|
||||
)
|
||||
|
||||
var m *melody.Melody
|
||||
|
||||
type websocketMessage struct {
|
||||
Action string `json:"action"`
|
||||
Data string `json:"data"`
|
||||
@ -21,14 +24,21 @@ type subscribeMessageData struct {
|
||||
URL string `json:"url"`
|
||||
}
|
||||
|
||||
func HandleConnect(s *melody.Session) {
|
||||
func Init() *melody.Melody {
|
||||
m = melody.New()
|
||||
m.HandleConnect(handleConnect)
|
||||
m.HandleMessage(handleMessage)
|
||||
m.HandleDisconnect(handleDisconnect)
|
||||
notify_service.RegisterNotifier(newNotifier(m))
|
||||
return m
|
||||
}
|
||||
|
||||
func handleConnect(s *melody.Session) {
|
||||
ctx := context.GetWebContext(s.Request)
|
||||
|
||||
data := &sessionData{}
|
||||
|
||||
if ctx.IsSigned {
|
||||
data.isSigned = true
|
||||
data.userID = ctx.Doer.ID
|
||||
data.user = ctx.Doer
|
||||
}
|
||||
|
||||
s.Set("data", data)
|
||||
@ -36,7 +46,7 @@ func HandleConnect(s *melody.Session) {
|
||||
// TODO: handle logouts
|
||||
}
|
||||
|
||||
func HandleMessage(s *melody.Session, _msg []byte) {
|
||||
func handleMessage(s *melody.Session, _msg []byte) {
|
||||
data, err := getSessionData(s)
|
||||
if err != nil {
|
||||
return
|
||||
@ -67,6 +77,5 @@ func handleSubscribeMessage(data *sessionData, _data any) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func HandleDisconnect(s *melody.Session) {
|
||||
// TODO: Handle disconnect
|
||||
func handleDisconnect(s *melody.Session) {
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user