增加群聊置顶、免打扰和应用内提醒
This commit is contained in:
1 parent
ced6e9030d
commit
f54077ce46
17 files changed
+228
-42
No files matched your search
@@ -64,6 +64,7 @@ func New(accounts *account.Store, contactStore *contacts.Store, envelopeStore *d
|
||||
mux.HandleFunc("POST /api/v1/groups", a.authorized(a.createGroup))
|
||||
mux.HandleFunc("GET /api/v1/groups/{id}", a.authorized(a.groupDetail))
|
||||
mux.HandleFunc("POST /api/v1/groups/{id}", a.authorized(a.changeGroup))
|
||||
mux.HandleFunc("POST /api/v1/groups/{id}/preferences", a.authorized(a.setGroupPreferences))
|
||||
mux.HandleFunc("GET /api/v1/groups/{id}/epochs", a.authorized(a.groupEpochs))
|
||||
mux.HandleFunc("POST /api/v1/groups/{id}/epochs", a.authorized(a.publishGroupEpoch))
|
||||
mux.HandleFunc("GET /api/v1/groups/{id}/messages", a.authorized(a.groupInbox))
|
||||
|
||||
@@ -97,11 +97,11 @@ func (a *API) publishGroupEpoch(w http.ResponseWriter, r *http.Request, _ string
|
||||
groupError(w, err)
|
||||
return
|
||||
}
|
||||
devices := make([]string, 0, len(input.Devices))
|
||||
devices := make([]groups.EpochDevice, 0, len(input.Devices))
|
||||
for _, device := range input.Devices {
|
||||
devices = append(devices, device.Device)
|
||||
devices = append(devices, groups.EpochDevice{ID: device.Device})
|
||||
}
|
||||
a.hintGroup(r.Context(), id, devices)
|
||||
a.hintGroup(r.Context(), id, devices, "", false)
|
||||
w.WriteHeader(204)
|
||||
}
|
||||
|
||||
@@ -129,21 +129,26 @@ 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, devices, err := a.groups.Send(r.Context(), id, actor.User.ID, actor.Device.ID, message)
|
||||
sequence, devices, created, 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)
|
||||
a.hintGroup(r.Context(), id, devices, actor.Device.ID, created)
|
||||
respond(w, 200, map[string]string{"sequence": sequence})
|
||||
}
|
||||
|
||||
func (a *API) hintGroup(ctx context.Context, group string, devices []string) {
|
||||
func (a *API) hintGroup(ctx context.Context, group string, devices []groups.EpochDevice, sender string, newMessage bool) {
|
||||
ctx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 250*time.Millisecond)
|
||||
defer cancel()
|
||||
pipe := a.redis.Pipeline()
|
||||
group = strings.ToLower(group)
|
||||
for _, device := range devices {
|
||||
pipe.Publish(ctx, "xhchat:inbox:"+device, "group:"+strings.ToLower(group))
|
||||
prefix := "group:"
|
||||
if newMessage && !device.Muted && sender != device.ID {
|
||||
prefix = "group-notify:"
|
||||
}
|
||||
pipe.Publish(ctx, "xhchat:inbox:"+device.ID, prefix+group)
|
||||
}
|
||||
// Hints are best effort. The durable cursor is also polled by clients.
|
||||
_, _ = pipe.Exec(ctx)
|
||||
|
||||
@@ -134,3 +134,32 @@ func (a *API) hintGroupList(ctx context.Context, users []string) {
|
||||
}
|
||||
_, _ = pipe.Exec(ctx)
|
||||
}
|
||||
|
||||
func (a *API) setGroupPreferences(w http.ResponseWriter, r *http.Request, _ string, actor account.Identity) {
|
||||
group := r.PathValue("id")
|
||||
if !uuidPattern.MatchString(group) {
|
||||
groupError(w, groups.ErrUnavailable)
|
||||
return
|
||||
}
|
||||
var input struct {
|
||||
Pinned *bool `json:"pinned"`
|
||||
Muted *bool `json:"muted"`
|
||||
}
|
||||
if !parse(w, r, &input) {
|
||||
return
|
||||
}
|
||||
if input.Pinned == nil && input.Muted == nil {
|
||||
problem(w, 400, "invalid_group_preferences", "请选择要修改的群设置")
|
||||
return
|
||||
}
|
||||
if !a.limit(w, r, "group-change:"+actor.User.ID, 60) {
|
||||
return
|
||||
}
|
||||
result, err := a.groups.SetPreferences(r.Context(), group, actor.User.ID, actor.Device.ID, input.Pinned, input.Muted)
|
||||
if err != nil {
|
||||
groupError(w, err)
|
||||
return
|
||||
}
|
||||
a.hintGroupList(r.Context(), []string{actor.User.ID})
|
||||
respond(w, 200, result)
|
||||
}
|
||||
@@ -103,15 +103,19 @@ func (a *API) realtime(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
writeCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
event := map[string]string{"type": "contacts_changed"}
|
||||
event := map[string]any{"type": "contacts_changed"}
|
||||
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) {
|
||||
kind, group, _ := strings.Cut(change.Payload, ":")
|
||||
if (kind == "group" || kind == "group-notify") && uuidPattern.MatchString(group) {
|
||||
event["type"] = "group_changed"
|
||||
event["groupId"] = group
|
||||
if kind == "group-notify" {
|
||||
event["notify"] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
err := wsjson.Write(writeCtx, conn, event)
|
||||
|
||||
Reference in new issue
Block a user