package httpapi import ( "context" "errors" "net/http" "strconv" "strings" "time" "xhchat/server/internal/account" "xhchat/server/internal/groups" ) func (a *API) groupDevices(w http.ResponseWriter, r *http.Request, _ string, actor account.Identity) { id := r.PathValue("id") if !uuidPattern.MatchString(id) { groupError(w, groups.ErrUnavailable) return } result, err := a.groups.Devices(r.Context(), id, actor.User.ID, actor.Device.ID) if err != nil { groupError(w, err) return } respond(w, 200, result) } func (a *API) claimGroupPackage(w http.ResponseWriter, r *http.Request, _ string, actor account.Identity) { id, target := r.PathValue("id"), strings.ToLower(r.PathValue("device")) if !uuidPattern.MatchString(id) || !uuidPattern.MatchString(target) { groupError(w, groups.ErrUnavailable) return } if !a.limit(w, r, "group-claim:"+actor.Device.ID, 64) { return } pack, err := a.groups.Claim(r.Context(), id, actor.User.ID, actor.Device.ID, target) if err != nil { groupError(w, err) return } respond(w, 200, pack) } func (a *API) groupEpochs(w http.ResponseWriter, r *http.Request, _ string, actor account.Identity) { id := r.PathValue("id") if !uuidPattern.MatchString(id) { groupError(w, groups.ErrUnavailable) return } epochs, err := a.groups.Epochs(r.Context(), id, actor.User.ID, actor.Device.ID) if err != nil { groupError(w, err) return } respond(w, 200, epochs) } func (a *API) publishGroupEpoch(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-epoch:"+actor.Device.ID, 12) { return } var input groups.Epoch if !parseLimit(w, r, &input, 3<<20) { return } version, err := strconv.ParseInt(input.Version, 10, 64) if !uuidPattern.MatchString(input.ID) || err != nil || version < 1 || strconv.FormatInt(version, 10) != input.Version || len(input.Devices) < 1 || len(input.Devices) > 32 { problem(w, 400, "invalid_group_epoch", "群加密世代信息无效") return } seen := make(map[string]bool, len(input.Devices)) for index := range input.Devices { device := &input.Devices[index] device.Device = strings.ToLower(device.Device) if !uuidPattern.MatchString(device.Device) || seen[device.Device] || (device.Device == actor.Device.ID && len(device.Wire) != 0) || (device.Device != actor.Device.ID && (!mlsFrame(device.Wire) || device.Wire[3] != 3)) { problem(w, 400, "invalid_group_epoch", "群设备或 Welcome 无效") return } seen[device.Device] = true } if !seen[actor.Device.ID] { problem(w, 400, "invalid_group_epoch", "创建设备必须加入群加密世代") return } if err := a.groups.PublishEpoch(r.Context(), id, actor.User.ID, actor.Device.ID, input); err != nil { groupError(w, err) return } devices := make([]groups.EpochDevice, 0, len(input.Devices)) for _, device := range input.Devices { devices = append(devices, groups.EpochDevice{ID: device.Device}) } a.hintGroup(r.Context(), id, devices, "", false) w.WriteHeader(204) } func (a *API) sendGroupMessage(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-send:"+actor.Device.ID, 180) { return } var input struct { ID string `json:"id"` Epoch string `json:"epoch"` Wire []byte `json:"wire"` AttachmentID *string `json:"attachmentId,omitempty"` } if !parseLimit(w, r, &input, 96<<10) { return } if !uuidPattern.MatchString(input.ID) || !uuidPattern.MatchString(input.Epoch) || !mlsFrame(input.Wire) || input.Wire[3] != 2 || input.AttachmentID != nil && !uuidPattern.MatchString(*input.AttachmentID) { problem(w, 400, "invalid_group_message", "群消息密文无效") return } message := groups.Message{ID: input.ID, Epoch: input.Epoch, Wire: input.Wire, AttachmentID: input.AttachmentID} 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, actor.Device.ID, created) respond(w, 200, map[string]string{"sequence": sequence}) } 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 { 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) } 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, 240) { return } after := int64(0) if raw := r.URL.Query().Get("after"); raw != "" { var err error after, err = strconv.ParseInt(raw, 10, 64) if err != nil || after < 0 { problem(w, 400, "invalid_cursor", "无效的投递游标") return } } items, err := a.groups.Inbox(r.Context(), id, actor.User.ID, actor.Device.ID, after) if errors.Is(err, groups.ErrUnavailable) { groupError(w, err) return } if err != nil { unavailable(w, err) return } respond(w, 200, items) } func (a *API) markGroupSeen(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 { Sequence string `json:"sequence"` } if !parse(w, r, &input) { return } sequence, err := strconv.ParseInt(input.Sequence, 10, 64) if err != nil || sequence <= 0 || strconv.FormatInt(sequence, 10) != input.Sequence { problem(w, 400, "invalid_cursor", "无效的已查看游标") return } if !a.limit(w, r, "group-seen:"+actor.Device.ID, 240) { return } if err = a.groups.MarkSeen(r.Context(), group, actor.User.ID, actor.Device.ID, sequence); err != nil { groupError(w, err) return } w.WriteHeader(204) }