feat: auto-friend invite bound users

This commit is contained in:
richjohnrambo
2026-09-11 10:46:20 +08:00
parent 926b4bbd81
commit b9dc56eccf
4 changed files with 104 additions and 23 deletions
@@ -9,12 +9,18 @@ import (
mysqldriver "github.com/go-sql-driver/mysql"
)
type ignoreSQLError struct {
sql string
code uint16
}
func ensureIMRegistrationInviteControlTables(exec sqlExecutor) error {
statements := []string{
`CREATE TABLE IF NOT EXISTS im_registration_invite_batches(
id BIGINT NOT NULL,
tenant_id BIGINT NOT NULL,
quantity INT UNSIGNED NOT NULL,
max_uses_per_code INT UNSIGNED NOT NULL DEFAULT 0,
idempotency_key VARCHAR(128) NOT NULL,
expires_at DATETIME(3) NOT NULL,
created_by BIGINT NOT NULL,
@@ -32,6 +38,9 @@ func ensureIMRegistrationInviteControlTables(exec sqlExecutor) error {
code CHAR(5) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
status VARCHAR(16) NOT NULL,
expires_at DATETIME(3) NOT NULL,
max_uses INT UNSIGNED NOT NULL DEFAULT 0,
bound_user_id BIGINT NULL,
bound_user_label VARCHAR(128) NOT NULL DEFAULT '',
usage_count INT UNSIGNED NOT NULL DEFAULT 0,
last_used_at DATETIME(3) NULL,
created_by BIGINT NOT NULL,
@@ -40,6 +49,7 @@ func ensureIMRegistrationInviteControlTables(exec sqlExecutor) error {
updated_at DATETIME(3) NOT NULL,
PRIMARY KEY(id),
UNIQUE INDEX uk_im_registration_invite_code_tenant(tenant_id,code),
INDEX idx_im_registration_invite_tenant_bound_user(tenant_id,bound_user_id),
INDEX idx_im_registration_invite_tenant_status_expires(tenant_id,status,expires_at),
INDEX idx_im_registration_invite_tenant_batch(tenant_id,batch_id),
INDEX idx_im_registration_invite_tenant_created(tenant_id,created_at)
@@ -50,6 +60,29 @@ func ensureIMRegistrationInviteControlTables(exec sqlExecutor) error {
return err
}
}
alterStatements := []ignoreSQLError{
{`ALTER TABLE im_registration_invite_codes ADD COLUMN max_uses INT UNSIGNED NOT NULL DEFAULT 0 AFTER expires_at`, 1060},
{`ALTER TABLE im_registration_invite_codes ADD COLUMN bound_user_id BIGINT NULL AFTER max_uses`, 1060},
{`ALTER TABLE im_registration_invite_codes ADD COLUMN bound_user_label VARCHAR(128) NOT NULL DEFAULT '' AFTER bound_user_id`, 1060},
{`ALTER TABLE im_registration_invite_codes ADD INDEX idx_im_registration_invite_tenant_bound_user(tenant_id,bound_user_id)`, 1061},
{`ALTER TABLE im_registration_invite_batches ADD COLUMN max_uses_per_code INT UNSIGNED NOT NULL DEFAULT 0 AFTER quantity`, 1060},
}
for _, stmt := range alterStatements {
if err := execIgnoreMySQLError(exec, stmt.sql, stmt.code); err != nil {
return err
}
}
return nil
}
func execIgnoreMySQLError(exec sqlExecutor, statement string, code uint16) error {
if _, err := exec.Exec(statement); err != nil {
var mysqlErr *mysqldriver.MySQLError
if errors.As(err, &mysqlErr) && mysqlErr.Number == code {
return nil
}
return err
}
return nil
}