package httpapi import ( "errors" "net/http" "strconv" "strings" "xhchat/server/internal/account" "xhchat/server/internal/groups" ) 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 } 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"` } if !parseLimit(w, r, &input, 96<<10) { return } if !uuidPattern.MatchString(input.ID) || !uuidPattern.MatchString(input.Epoch) || !mlsFrame(input.Wire) || input.Wire[3] != 2 { problem(w, 400, "invalid_group_message", "群消息密文无效") return } sequence, err := a.groups.Send(r.Context(), id, actor.User.ID, actor.Device.ID, groups.Message{ID: input.ID, Epoch: input.Epoch, Wire: input.Wire}) if err != nil { groupError(w, err) return } respond(w, 200, map[string]string{"sequence": sequence}) } 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) { 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) }