接入群消息实时提醒和重连补拉
This commit is contained in:
1 parent
5ace82b0d7
commit
b2e04e8174
10 files changed
+122
-33
No files matched your search
@@ -1,10 +1,12 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"xhchat/server/internal/account"
|
||||
"xhchat/server/internal/groups"
|
||||
@@ -95,6 +97,11 @@ func (a *API) publishGroupEpoch(w http.ResponseWriter, r *http.Request, _ string
|
||||
groupError(w, err)
|
||||
return
|
||||
}
|
||||
devices := make([]string, 0, len(input.Devices))
|
||||
for _, device := range input.Devices {
|
||||
devices = append(devices, device.Device)
|
||||
}
|
||||
a.hintGroup(r.Context(), id, devices)
|
||||
w.WriteHeader(204)
|
||||
}
|
||||
|
||||
@@ -122,21 +129,33 @@ func (a *API) sendGroupMessage(w http.ResponseWriter, r *http.Request, _ string,
|
||||
return
|
||||
}
|
||||
message := groups.Message{ID: input.ID, Epoch: input.Epoch, Wire: input.Wire, AttachmentID: input.AttachmentID}
|
||||
sequence, err := a.groups.Send(r.Context(), id, actor.User.ID, actor.Device.ID, message)
|
||||
sequence, devices, err := a.groups.Send(r.Context(), id, actor.User.ID, actor.Device.ID, message)
|
||||
if err != nil {
|
||||
groupError(w, err)
|
||||
return
|
||||
}
|
||||
a.hintGroup(r.Context(), id, devices)
|
||||
respond(w, 200, map[string]string{"sequence": sequence})
|
||||
}
|
||||
|
||||
func (a *API) hintGroup(ctx context.Context, group string, devices []string) {
|
||||
ctx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 250*time.Millisecond)
|
||||
defer cancel()
|
||||
pipe := a.redis.Pipeline()
|
||||
for _, device := range devices {
|
||||
pipe.Publish(ctx, "xhchat:inbox:"+device, "group:"+strings.ToLower(group))
|
||||
}
|
||||
// Hints are best effort. The durable cursor is also polled by clients.
|
||||
_, _ = pipe.Exec(ctx)
|
||||
}
|
||||
|
||||
func (a *API) groupInbox(w http.ResponseWriter, r *http.Request, _ string, actor account.Identity) {
|
||||
id := r.PathValue("id")
|
||||
if !uuidPattern.MatchString(id) {
|
||||
groupError(w, groups.ErrUnavailable)
|
||||
return
|
||||
}
|
||||
if !a.limit(w, r, "group-inbox:"+actor.Device.ID, 180) {
|
||||
if !a.limit(w, r, "group-inbox:"+actor.Device.ID, 240) {
|
||||
return
|
||||
}
|
||||
after := int64(0)
|
||||
|
||||
@@ -100,11 +100,15 @@ func (a *API) realtime(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
writeCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
event := "contacts_changed"
|
||||
event := map[string]string{"type": "contacts_changed"}
|
||||
if change.Channel == inboxChannel {
|
||||
event = "inbox_changed"
|
||||
event["type"] = "inbox_changed"
|
||||
if group, found := strings.CutPrefix(change.Payload, "group:"); found && uuidPattern.MatchString(group) {
|
||||
event["type"] = "group_changed"
|
||||
event["groupId"] = group
|
||||
}
|
||||
}
|
||||
err := wsjson.Write(writeCtx, conn, map[string]string{"type": event})
|
||||
err := wsjson.Write(writeCtx, conn, event)
|
||||
cancel()
|
||||
if err != nil {
|
||||
return
|
||||
|
||||
Reference in new issue
Block a user