70 lines
2.0 KiB
Go
70 lines
2.0 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"net/http"
|
|
"time"
|
|
|
|
"xhchat/server/internal/account"
|
|
"xhchat/server/internal/groups"
|
|
)
|
|
|
|
func (a *API) issueGroupInviteLink(w http.ResponseWriter, r *http.Request, _ string, actor account.Identity) {
|
|
group := r.PathValue("id")
|
|
if !uuidPattern.MatchString(group) {
|
|
groupError(w, groups.ErrUnavailable)
|
|
return
|
|
}
|
|
if !a.limit(w, r, "group-invite-issue:"+actor.User.ID, 20) {
|
|
return
|
|
}
|
|
secret := account.Secret()
|
|
digest := sha256.Sum256([]byte(secret))
|
|
expiry, err := a.groups.SetInviteLink(r.Context(), group, actor.User.ID, actor.Device.ID, digest[:])
|
|
if err != nil {
|
|
groupError(w, err)
|
|
return
|
|
}
|
|
respond(w, 201, struct {
|
|
Code string `json:"code"`
|
|
ExpiresAt time.Time `json:"expiresAt"`
|
|
}{secret, expiry})
|
|
}
|
|
|
|
func (a *API) revokeGroupInviteLink(w http.ResponseWriter, r *http.Request, _ string, actor account.Identity) {
|
|
group := r.PathValue("id")
|
|
if !uuidPattern.MatchString(group) {
|
|
groupError(w, groups.ErrUnavailable)
|
|
return
|
|
}
|
|
if !a.limit(w, r, "group-invite-issue:"+actor.User.ID, 20) {
|
|
return
|
|
}
|
|
if _, err := a.groups.SetInviteLink(r.Context(), group, actor.User.ID, actor.Device.ID, nil); err != nil {
|
|
groupError(w, err)
|
|
return
|
|
}
|
|
w.WriteHeader(204)
|
|
}
|
|
|
|
func (a *API) redeemGroupInviteLink(w http.ResponseWriter, r *http.Request, _ string, actor account.Identity) {
|
|
var input struct {
|
|
Code string `json:"code"`
|
|
}
|
|
if !parse(w, r, &input) || !a.limit(w, r, "group-invite-redeem:"+actor.User.ID, 30) {
|
|
return
|
|
}
|
|
if decoded, err := account.Encoding.Strict().DecodeString(input.Code); err != nil || len(decoded) != 32 || len(input.Code) != 43 {
|
|
problem(w, 400, "invalid_group_invite", "群邀请链接格式无效")
|
|
return
|
|
}
|
|
digest := sha256.Sum256([]byte(input.Code))
|
|
group, users, err := a.groups.RedeemInviteLink(r.Context(), actor.User.ID, actor.Device.ID, digest[:])
|
|
if err != nil {
|
|
groupError(w, err)
|
|
return
|
|
}
|
|
a.hintGroupList(r.Context(), users)
|
|
respond(w, 200, map[string]string{"groupId": group})
|
|
}
|