增加一次性群邀请链接和二维码
This commit is contained in:
1 parent
dfeb5bfefa
commit
5ec9c81f90
18 files changed
+727
-14
No files matched your search
@@ -0,0 +1,5 @@
|
||||
CREATE TABLE group_invite_links (
|
||||
group_id uuid PRIMARY KEY REFERENCES chat_groups(id) ON DELETE CASCADE,
|
||||
token_hash bytea NOT NULL UNIQUE CHECK (octet_length(token_hash) = 32),
|
||||
expires_at timestamptz NOT NULL
|
||||
);
|
||||
@@ -40,6 +40,9 @@ var groupPreferencesMigration string
|
||||
//go:embed 011_group_seen.sql
|
||||
var groupSeenMigration string
|
||||
|
||||
//go:embed 012_group_invite_link.sql
|
||||
var groupInviteLinkMigration string
|
||||
|
||||
func Migrate(ctx context.Context, pool *pgxpool.Pool) error {
|
||||
tx, err := pool.Begin(ctx)
|
||||
if err != nil {
|
||||
@@ -55,7 +58,7 @@ func Migrate(ctx context.Context, pool *pgxpool.Pool) error {
|
||||
for index, migration := range []string{
|
||||
identityMigration, contactsMigration, deliveryMigration, keyPackagesMigration, mlsCredentialsMigration,
|
||||
attachmentsMigration, groupsMigration, groupTransportMigration, groupAttachmentsMigration, groupPreferencesMigration,
|
||||
groupSeenMigration,
|
||||
groupSeenMigration, groupInviteLinkMigration,
|
||||
} {
|
||||
version := index + 1
|
||||
var applied bool
|
||||
|
||||
@@ -0,0 +1,97 @@
|
||||
package groups
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
func (s *Store) SetInviteLink(ctx context.Context, group, user, device string, digest []byte) (time.Time, error) {
|
||||
var expiry time.Time
|
||||
tx, err := s.db.Begin(ctx)
|
||||
if err != nil {
|
||||
return expiry, err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
_, owner, err := lockTransport(ctx, tx, group, user, device)
|
||||
if err != nil {
|
||||
return expiry, err
|
||||
}
|
||||
if !owner {
|
||||
return expiry, ErrAction
|
||||
}
|
||||
if digest == nil {
|
||||
_, err = tx.Exec(ctx, "DELETE FROM group_invite_links WHERE group_id=$1", group)
|
||||
} else {
|
||||
err = tx.QueryRow(ctx, `
|
||||
INSERT INTO group_invite_links(group_id,token_hash,expires_at) VALUES($1,$2,now()+interval '24 hours')
|
||||
ON CONFLICT(group_id) DO UPDATE SET token_hash=EXCLUDED.token_hash, expires_at=EXCLUDED.expires_at
|
||||
RETURNING expires_at
|
||||
`, group, digest).Scan(&expiry)
|
||||
}
|
||||
if err != nil {
|
||||
return expiry, err
|
||||
}
|
||||
return expiry, tx.Commit(ctx)
|
||||
}
|
||||
|
||||
func (s *Store) RedeemInviteLink(ctx context.Context, user, device string, digest []byte) (string, []string, error) {
|
||||
tx, err := s.db.Begin(ctx)
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
var group, owner string
|
||||
// All membership changes lock the group row before touching its invitation.
|
||||
err = tx.QueryRow(ctx, `
|
||||
SELECT g.id,g.owner_id FROM chat_groups g JOIN group_invite_links l ON l.group_id=g.id
|
||||
WHERE l.token_hash=$1 AND l.expires_at>now() FOR UPDATE OF g
|
||||
`, digest).Scan(&group, &owner)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return "", nil, ErrUnavailable
|
||||
}
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
// Recheck the token after acquiring the group lock so rotation and redemption serialize.
|
||||
var valid bool
|
||||
err = tx.QueryRow(ctx, `
|
||||
SELECT EXISTS(SELECT 1 FROM group_invite_links WHERE group_id=$1 AND token_hash=$2 AND expires_at>clock_timestamp())
|
||||
`, group, digest).Scan(&valid)
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
if !valid {
|
||||
return "", nil, ErrUnavailable
|
||||
}
|
||||
var currentDevice string
|
||||
err = tx.QueryRow(ctx, "SELECT id FROM devices WHERE id=$1 AND user_id=$2 AND revoked_at IS NULL FOR UPDATE", device, user).Scan(¤tDevice)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return "", nil, ErrUnavailable
|
||||
}
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
if err = checkMemberLimit(ctx, tx, group); err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
tag, err := tx.Exec(ctx, `
|
||||
INSERT INTO group_members(group_id,user_id,state) VALUES($1,$2,'invited') ON CONFLICT DO NOTHING
|
||||
`, group, user)
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
if tag.RowsAffected() == 0 {
|
||||
return "", nil, ErrAction
|
||||
}
|
||||
if _, err = tx.Exec(ctx, "DELETE FROM group_invite_links WHERE group_id=$1", group); err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
users, err := groupChangeAudience(ctx, tx, group, owner, "managers")
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
return group, append(users, user), tx.Commit(ctx)
|
||||
}
|
||||
@@ -289,6 +289,10 @@ func changeActive(ctx context.Context, tx pgx.Tx, user, id, owner, role string,
|
||||
return false, ErrAction
|
||||
}
|
||||
_, err = tx.Exec(ctx, "UPDATE group_members SET role='member' WHERE group_id=$1 AND user_id IN ($2,$3)", id, owner, change.Target)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
_, err = tx.Exec(ctx, "DELETE FROM group_invite_links WHERE group_id=$1", id)
|
||||
// The new owner establishes a new authenticated MLS session for the same roster.
|
||||
return true, err
|
||||
case "remove", "promote", "demote":
|
||||
@@ -302,13 +306,9 @@ func changeActive(ctx context.Context, tx pgx.Tx, user, id, owner, role string,
|
||||
}
|
||||
|
||||
func invite(ctx context.Context, tx pgx.Tx, user, id, target string) error {
|
||||
var count int
|
||||
if err := tx.QueryRow(ctx, "SELECT count(*) FROM group_members WHERE group_id=$1", id).Scan(&count); err != nil {
|
||||
if err := checkMemberLimit(ctx, tx, id); err != nil {
|
||||
return err
|
||||
}
|
||||
if count >= 32 {
|
||||
return ErrLimit
|
||||
}
|
||||
tag, err := tx.Exec(ctx, `
|
||||
INSERT INTO group_members(group_id,user_id,state)
|
||||
SELECT $1,$3,'invited' FROM contact_relationships
|
||||
@@ -322,6 +322,17 @@ func invite(ctx context.Context, tx pgx.Tx, user, id, target string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func checkMemberLimit(ctx context.Context, tx pgx.Tx, id string) error {
|
||||
var count int
|
||||
if err := tx.QueryRow(ctx, "SELECT count(*) FROM group_members WHERE group_id=$1", id).Scan(&count); err != nil {
|
||||
return err
|
||||
}
|
||||
if count >= 32 {
|
||||
return ErrLimit
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func changeMember(ctx context.Context, tx pgx.Tx, id, role string, change Change) (bool, error) {
|
||||
if change.Action == "remove" {
|
||||
var state string
|
||||
|
||||
@@ -66,6 +66,9 @@ func New(accounts *account.Store, contactStore *contacts.Store, envelopeStore *d
|
||||
mux.HandleFunc("POST /api/v1/groups/{id}", a.authorized(a.changeGroup))
|
||||
mux.HandleFunc("POST /api/v1/groups/{id}/preferences", a.authorized(a.setGroupPreferences))
|
||||
mux.HandleFunc("POST /api/v1/groups/{id}/seen", a.authorized(a.markGroupSeen))
|
||||
mux.HandleFunc("POST /api/v1/groups/{id}/invite-link", a.authorized(a.issueGroupInviteLink))
|
||||
mux.HandleFunc("DELETE /api/v1/groups/{id}/invite-link", a.authorized(a.revokeGroupInviteLink))
|
||||
mux.HandleFunc("POST /api/v1/group-invites/redeem", a.authorized(a.redeemGroupInviteLink))
|
||||
mux.HandleFunc("GET /api/v1/groups/{id}/epochs", a.authorized(a.groupEpochs))
|
||||
mux.HandleFunc("POST /api/v1/groups/{id}/epochs", a.authorized(a.publishGroupEpoch))
|
||||
mux.HandleFunc("GET /api/v1/groups/{id}/messages", a.authorized(a.groupInbox))
|
||||
|
||||
@@ -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