接入群成员与邀请实时同步

This commit is contained in:
xhgit committed 2026-11-14 15:04:47 +08:00
1 parent b2e04e8174
commit 5cae19a256
9 files changed
+175 -38

No files matched your search

+20 -1
View File
@@ -1,9 +1,11 @@
package httpapi
import (
"context"
"errors"
"net/http"
"strings"
"time"
"unicode"
"unicode/utf8"
@@ -52,6 +54,7 @@ func (a *API) createGroup(w http.ResponseWriter, r *http.Request, _ string, id a
groupError(w, err)
return
}
a.hintGroupList(r.Context(), []string{id.User.ID})
respond(w, 201, group)
}
@@ -109,9 +112,25 @@ func (a *API) changeGroup(w http.ResponseWriter, r *http.Request, _ string, id a
if !a.limit(w, r, "group-change:"+id.User.ID, 60) {
return
}
if err := a.groups.Change(r.Context(), id.User.ID, group, change); err != nil {
users, err := a.groups.Change(r.Context(), id.User.ID, group, change)
if err != nil {
groupError(w, err)
return
}
a.hintGroupList(r.Context(), users)
w.WriteHeader(204)
}
func (a *API) hintGroupList(ctx context.Context, users []string) {
ctx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 250*time.Millisecond)
defer cancel()
pipe := a.redis.Pipeline()
seen := make(map[string]bool, len(users))
for _, user := range users {
if !seen[user] {
pipe.Publish(ctx, "xhchat:groups:"+user, "changed")
seen[user] = true
}
}
_, _ = pipe.Exec(ctx)
}
+11 -5
View File
@@ -64,11 +64,14 @@ func (a *API) realtime(w http.ResponseWriter, r *http.Request) {
return
}
inboxChannel := "xhchat:inbox:" + identity.Device.ID
subscription := a.redis.Subscribe(r.Context(), "xhchat:contacts:"+identity.User.ID, inboxChannel)
groupsChannel := "xhchat:groups:" + identity.User.ID
subscription := a.redis.Subscribe(r.Context(), "xhchat:contacts:"+identity.User.ID, inboxChannel, groupsChannel)
defer subscription.Close()
if _, err = subscription.Receive(r.Context()); err != nil {
unavailable(w, err)
return
for range 3 {
if _, err = subscription.Receive(r.Context()); err != nil {
unavailable(w, err)
return
}
}
changes := subscription.Channel()
// The exact Origin was checked before consuming the ticket.
@@ -101,7 +104,10 @@ func (a *API) realtime(w http.ResponseWriter, r *http.Request) {
}
writeCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
event := map[string]string{"type": "contacts_changed"}
if change.Channel == inboxChannel {
switch change.Channel {
case groupsChannel:
event["type"] = "group_list_changed"
case inboxChannel:
event["type"] = "inbox_changed"
if group, found := strings.CutPrefix(change.Payload, "group:"); found && uuidPattern.MatchString(group) {
event["type"] = "group_changed"