166 lines
4.4 KiB
Go
166 lines
4.4 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
"unicode"
|
|
"unicode/utf8"
|
|
|
|
"xhchat/server/internal/account"
|
|
"xhchat/server/internal/groups"
|
|
)
|
|
|
|
func groupName(name string) bool {
|
|
return name != "" && utf8.RuneCountInString(name) <= 64 && !strings.ContainsFunc(name, unicode.IsControl)
|
|
}
|
|
|
|
func groupError(w http.ResponseWriter, err error) {
|
|
switch {
|
|
case errors.Is(err, groups.ErrUnavailable):
|
|
problem(w, 404, "group_not_found", "群聊不可用")
|
|
case errors.Is(err, groups.ErrAction):
|
|
problem(w, 409, "group_action_unavailable", "当前无法执行此群操作")
|
|
case errors.Is(err, groups.ErrLimit):
|
|
problem(w, 409, "group_member_limit", "群成员与待处理邀请已达上限")
|
|
case errors.Is(err, groups.ErrVersion):
|
|
problem(w, 409, "group_epoch_changed", "群成员或设备版本已变更")
|
|
case errors.Is(err, groups.ErrAttachment):
|
|
problem(w, 409, "group_attachment_conflict", "群附件不可用或已绑定到其他消息")
|
|
default:
|
|
unavailable(w, err)
|
|
}
|
|
}
|
|
|
|
func (a *API) createGroup(w http.ResponseWriter, r *http.Request, _ string, id account.Identity) {
|
|
var body struct {
|
|
Name string `json:"name"`
|
|
}
|
|
if !parse(w, r, &body) {
|
|
return
|
|
}
|
|
name := strings.TrimSpace(body.Name)
|
|
if !groupName(name) {
|
|
problem(w, 400, "invalid_group_name", "群名称须为 1 至 64 个字符")
|
|
return
|
|
}
|
|
if !a.limit(w, r, "group-create:"+id.User.ID, 10) {
|
|
return
|
|
}
|
|
group, err := a.groups.Create(r.Context(), id.User.ID, name)
|
|
if err != nil {
|
|
groupError(w, err)
|
|
return
|
|
}
|
|
a.hintGroupList(r.Context(), []string{id.User.ID})
|
|
respond(w, 201, group)
|
|
}
|
|
|
|
func (a *API) listGroups(w http.ResponseWriter, r *http.Request, _ string, id account.Identity) {
|
|
result, err := a.groups.List(r.Context(), id.User.ID, id.Device.ID)
|
|
if err != nil {
|
|
groupError(w, err)
|
|
return
|
|
}
|
|
respond(w, 200, result)
|
|
}
|
|
|
|
func (a *API) groupDetail(w http.ResponseWriter, r *http.Request, _ string, id account.Identity) {
|
|
group := r.PathValue("id")
|
|
if !uuidPattern.MatchString(group) {
|
|
groupError(w, groups.ErrUnavailable)
|
|
return
|
|
}
|
|
result, err := a.groups.Detail(r.Context(), id.User.ID, group)
|
|
if err != nil {
|
|
groupError(w, err)
|
|
return
|
|
}
|
|
respond(w, 200, result)
|
|
}
|
|
|
|
func (a *API) changeGroup(w http.ResponseWriter, r *http.Request, _ string, id account.Identity) {
|
|
group := r.PathValue("id")
|
|
if !uuidPattern.MatchString(group) {
|
|
groupError(w, groups.ErrUnavailable)
|
|
return
|
|
}
|
|
var change groups.Change
|
|
if !parse(w, r, &change) {
|
|
return
|
|
}
|
|
switch change.Action {
|
|
case "invite", "remove", "promote", "demote", "transfer":
|
|
if !uuidPattern.MatchString(change.Target) {
|
|
problem(w, 400, "invalid_member", "群成员 ID 无效")
|
|
return
|
|
}
|
|
change.Target = strings.ToLower(change.Target)
|
|
case "rename":
|
|
change.Name = strings.TrimSpace(change.Name)
|
|
if !groupName(change.Name) {
|
|
problem(w, 400, "invalid_group_name", "群名称须为 1 至 64 个字符")
|
|
return
|
|
}
|
|
case "accept", "decline", "leave", "disband":
|
|
default:
|
|
problem(w, 400, "invalid_group_action", "群操作无效")
|
|
return
|
|
}
|
|
if !a.limit(w, r, "group-change:"+id.User.ID, 60) {
|
|
return
|
|
}
|
|
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)
|
|
}
|
|
|
|
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)
|
|
}
|