增加一次性群邀请链接和二维码
This commit is contained in:
1 parent
dfeb5bfefa
commit
5ec9c81f90
18 files changed
+727
-14
No files matched your search
@@ -0,0 +1,69 @@
|
||||
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})
|
||||
}
|
||||
Reference in new issue
Block a user