增加群组创建、邀请与成员权限管理
This commit is contained in:
1 parent
b1f55df263
commit
99a741e6da
13 files changed
+620
-5
No files matched your search
@@ -16,6 +16,7 @@ import (
|
||||
"xhchat/server/internal/account"
|
||||
"xhchat/server/internal/contacts"
|
||||
"xhchat/server/internal/delivery"
|
||||
"xhchat/server/internal/groups"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/redis/go-redis/v9"
|
||||
@@ -25,14 +26,15 @@ type API struct {
|
||||
accounts *account.Store
|
||||
contacts *contacts.Store
|
||||
delivery *delivery.Store
|
||||
groups *groups.Store
|
||||
redis *redis.Client
|
||||
origins map[string]bool
|
||||
hashSlots chan struct{}
|
||||
ready func(context.Context) error
|
||||
}
|
||||
|
||||
func New(accounts *account.Store, contactStore *contacts.Store, envelopeStore *delivery.Store, cache *redis.Client, origins map[string]bool, ready func(context.Context) error) http.Handler {
|
||||
a := &API{accounts: accounts, contacts: contactStore, delivery: envelopeStore, redis: cache, origins: origins, hashSlots: make(chan struct{}, 4), ready: ready}
|
||||
func New(accounts *account.Store, contactStore *contacts.Store, envelopeStore *delivery.Store, groupStore *groups.Store, cache *redis.Client, origins map[string]bool, ready func(context.Context) error) http.Handler {
|
||||
a := &API{accounts: accounts, contacts: contactStore, delivery: envelopeStore, groups: groupStore, redis: cache, origins: origins, hashSlots: make(chan struct{}, 4), ready: ready}
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("GET /healthz", func(w http.ResponseWriter, _ *http.Request) { respond(w, 200, map[string]string{"status": "ok"}) })
|
||||
mux.HandleFunc("GET /readyz", a.readiness)
|
||||
@@ -58,6 +60,10 @@ func New(accounts *account.Store, contactStore *contacts.Store, envelopeStore *d
|
||||
mux.HandleFunc("POST /api/v1/attachments/{id}", a.authorized(a.uploadAttachment))
|
||||
mux.HandleFunc("GET /api/v1/attachments/{id}", a.authorized(a.downloadAttachment))
|
||||
mux.HandleFunc("GET /api/v1/mls-local-key", a.authorized(a.localMLSKey))
|
||||
mux.HandleFunc("GET /api/v1/groups", a.authorized(a.listGroups))
|
||||
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))
|
||||
return a.boundary(mux)
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,113 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"strings"
|
||||
"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", "群成员与待处理邀请已达上限")
|
||||
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
|
||||
}
|
||||
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)
|
||||
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":
|
||||
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
|
||||
}
|
||||
if err := a.groups.Change(r.Context(), id.User.ID, group, change); err != nil {
|
||||
groupError(w, err)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(204)
|
||||
}
|
||||
Reference in new issue
Block a user