Add global role synchronization for Discord with configurable interval
Some checks failed
CI / backend (push) Has been cancelled
CI / frontend (push) Has been cancelled
CI / compose (push) Has been cancelled

This commit introduces a new feature for global role synchronization in Discord, allowing for periodic reconciliation of managed roles. A new environment variable, `DISCORD_GLOBAL_SYNC_INTERVAL`, has been added to configure the synchronization interval, defaulting to 5 minutes. The `RoleWorker` has been updated to schedule global sync jobs, ensuring that missing managed roles are restored and extra assignments are removed without affecting unrelated server roles. Database schema changes support the new synchronization logic, and tests have been added to validate the functionality of the global reconciliation process.
This commit is contained in:
2026-07-19 12:04:33 +03:00
parent e5646ba33d
commit 6b189bfce4
14 changed files with 568 additions and 42 deletions

View File

@@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/http/httptest"
"sort"
@@ -25,6 +26,8 @@ type roleStoreFake struct {
registrations map[string]application.DiscordRoleRegistration
active []application.DiscordRoleRoster
warnings []string
retries int
globalBuckets []int64
}
func newRoleStoreFake() *roleStoreFake {
@@ -38,6 +41,11 @@ func newRoleStoreFake() *roleStoreFake {
func (s *roleStoreFake) SeedDiscordRoleSyncJobs(context.Context) error { return nil }
func (s *roleStoreFake) ScheduleGlobalDiscordRoleSync(_ context.Context, bucket int64) error {
s.globalBuckets = append(s.globalBuckets, bucket)
return nil
}
func (s *roleStoreFake) ClaimDiscordRoleSyncJob(context.Context) (application.DiscordRoleSyncJob, error) {
s.mu.Lock()
defer s.mu.Unlock()
@@ -57,6 +65,7 @@ func (s *roleStoreFake) CompleteDiscordRoleSyncJob(_ context.Context, _ int64, w
func (s *roleStoreFake) RetryDiscordRoleSyncJob(_ context.Context, _ int64, message string, _ time.Time) error {
s.warnings = append(s.warnings, message)
s.retries++
return nil
}
@@ -70,6 +79,14 @@ func (s *roleStoreFake) ListDiscordManagedRoles(_ context.Context, eventID strin
return out, nil
}
func (s *roleStoreFake) ListAllDiscordManagedRoles(context.Context) ([]application.DiscordManagedRole, error) {
out := make([]application.DiscordManagedRole, 0, len(s.roles))
for _, role := range s.roles {
out = append(out, role)
}
return out, nil
}
func (s *roleStoreFake) UpsertDiscordManagedRole(_ context.Context, role application.DiscordManagedRole) error {
s.roles[managedRoleKey(role)] = role
return nil
@@ -102,6 +119,14 @@ func (s *roleStoreFake) DeleteDiscordRoleAssignment(_ context.Context, roleID, u
return nil
}
func (s *roleStoreFake) SetDiscordRoleAssignments(_ context.Context, roleID string, userIDs []string) error {
s.assignments[roleID] = make(map[string]bool, len(userIDs))
for _, userID := range userIDs {
s.assignments[roleID][userID] = true
}
return nil
}
func (s *roleStoreFake) GetDiscordRoleRoster(_ context.Context, eventID string) (application.DiscordRoleRoster, error) {
roster, ok := s.rosters[eventID]
if !ok {
@@ -122,6 +147,14 @@ func (s *roleStoreFake) GetDiscordRoleRegistration(_ context.Context, eventID st
return registration, nil
}
func (s *roleStoreFake) ListActiveDiscordRoleRegistrations(context.Context) ([]application.DiscordRoleRegistration, error) {
out := make([]application.DiscordRoleRegistration, 0, len(s.registrations))
for _, registration := range s.registrations {
out = append(out, registration)
}
return out, nil
}
func TestRoleWorkerReconcilesIdempotentlyAndRenames(t *testing.T) {
var mu sync.Mutex
roleNames := make(map[string]string)
@@ -360,7 +393,115 @@ func TestRoleWorkerReconcilesRSVPStatusAndEventRename(t *testing.T) {
}
}
func TestRoleWorkerStoresMissingGuildMemberWarning(t *testing.T) {
func TestGlobalReconcileRestoresExactManagedAssignments(t *testing.T) {
type actualRole struct {
Name string
Hoist bool
Position int
}
actualRoles := map[string]actualRole{
"going-role": {Name: "Идёт: Sunday Mix"},
"maybe-role": {Name: "Возможно: Sunday Mix"},
"not-going-role": {Name: "Не идёт: Sunday Mix"},
}
memberRoles := map[string]map[string]bool{
"user-1": {"foreign-role": true, "maybe-role": true},
"user-2": {"going-role": true},
}
mutations := 0
server := httptest.NewServer(http.HandlerFunc(func(response http.ResponseWriter, request *http.Request) {
parts := strings.Split(strings.Trim(request.URL.Path, "/"), "/")
if request.Method == http.MethodGet && strings.HasSuffix(request.URL.Path, "/roles") {
payload := make([]map[string]any, 0, len(actualRoles))
for id, role := range actualRoles {
payload = append(payload, map[string]any{"id": id, "name": role.Name, "hoist": role.Hoist, "position": role.Position})
}
_ = json.NewEncoder(response).Encode(payload)
return
}
if request.Method == http.MethodGet && strings.Contains(request.URL.Path, "/members") {
payload := make([]map[string]any, 0, len(memberRoles))
for userID, roles := range memberRoles {
roleIDs := make([]string, 0, len(roles))
for roleID := range roles {
roleIDs = append(roleIDs, roleID)
}
payload = append(payload, map[string]any{"user": map[string]string{"id": userID}, "roles": roleIDs})
}
_ = json.NewEncoder(response).Encode(payload)
return
}
mutations++
if request.Method == http.MethodPost && strings.HasSuffix(request.URL.Path, "/roles") {
var payload map[string]any
_ = json.NewDecoder(request.Body).Decode(&payload)
actualRoles["recreated-registered"] = actualRole{
Name: payload["name"].(string), Hoist: payload["hoist"].(bool),
}
_ = json.NewEncoder(response).Encode(map[string]string{"id": "recreated-registered"})
return
}
if len(parts) >= 6 && parts[2] == "members" && parts[4] == "roles" {
userID, roleID := parts[3], parts[5]
if memberRoles[userID] == nil {
memberRoles[userID] = make(map[string]bool)
}
if request.Method == http.MethodPut {
memberRoles[userID][roleID] = true
} else if request.Method == http.MethodDelete {
delete(memberRoles[userID], roleID)
}
response.WriteHeader(http.StatusNoContent)
return
}
response.WriteHeader(http.StatusNoContent)
}))
defer server.Close()
store := newRoleStoreFake()
for _, role := range []application.DiscordManagedRole{
{Scope: application.DiscordRoleScopeEvent, EventID: "event-1", Kind: application.DiscordRoleKindRegistered, DiscordRoleID: "deleted-registered", RoleName: "Зарегистрирован: Sunday Mix", Hoist: true},
{Scope: application.DiscordRoleScopeEvent, EventID: "event-1", Kind: application.DiscordRoleKindGoing, DiscordRoleID: "going-role", RoleName: "Идёт: Sunday Mix"},
{Scope: application.DiscordRoleScopeEvent, EventID: "event-1", Kind: application.DiscordRoleKindMaybe, DiscordRoleID: "maybe-role", RoleName: "Возможно: Sunday Mix"},
{Scope: application.DiscordRoleScopeEvent, EventID: "event-1", Kind: application.DiscordRoleKindNotGoing, DiscordRoleID: "not-going-role", RoleName: "Не идёт: Sunday Mix"},
} {
store.roles[managedRoleKey(role)] = role
}
store.registrations["event-1"] = testDiscordRegistration("Sunday Mix", domain.Going)
store.jobs = []application.DiscordRoleSyncJob{{ID: 1, EventID: "__global__", Action: application.DiscordRoleActionFullReconcile}}
worker, err := NewRoleWorker(store, RoleSyncConfig{
BotToken: "token", GuildID: "guild", Locale: "ru", APIBaseURL: server.URL, HTTPClient: server.Client(),
})
if err != nil {
t.Fatal(err)
}
if _, err = worker.ProcessNext(context.Background()); err != nil {
t.Fatal(err)
}
registeredID := eventRoleID(store.roles, "event-1", application.DiscordRoleKindRegistered)
if registeredID != "recreated-registered" {
t.Fatalf("deleted managed role was not recreated: %q", registeredID)
}
if !memberRoles["user-1"][registeredID] || !memberRoles["user-1"]["going-role"] {
t.Fatalf("missing desired roles were not restored: %v", memberRoles["user-1"])
}
if memberRoles["user-1"]["maybe-role"] || memberRoles["user-2"]["going-role"] {
t.Fatalf("extra managed assignments were not removed: %v", memberRoles)
}
if !memberRoles["user-1"]["foreign-role"] {
t.Fatal("foreign server role was modified")
}
firstMutations := mutations
store.jobs = []application.DiscordRoleSyncJob{{ID: 2, EventID: "__global__", Action: application.DiscordRoleActionFullReconcile}}
if _, err = worker.ProcessNext(context.Background()); err != nil {
t.Fatal(err)
}
if mutations != firstMutations {
t.Fatalf("idempotent global reconcile made %d mutations", mutations-firstMutations)
}
}
func TestRoleWorkerRetriesMissingGuildMember(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(response http.ResponseWriter, request *http.Request) {
if request.Method == http.MethodPost {
_ = json.NewEncoder(response).Encode(map[string]string{"id": strings.ReplaceAll(request.URL.Path, "/", "-") + time.Now().String()})
@@ -389,6 +530,12 @@ func TestRoleWorkerStoresMissingGuildMemberWarning(t *testing.T) {
if len(store.warnings) != 1 || !strings.Contains(store.warnings[0], "not in the guild") {
t.Fatalf("missing member warning was not stored: %v", store.warnings)
}
if store.retries != 1 {
t.Fatalf("missing guild member job was not retried: %d", store.retries)
}
if delay := retryDelay(1, &missingGuildMembersError{warnings: []string{"missing"}}); delay != 5*time.Minute {
t.Fatalf("unexpected missing member retry delay: %s", delay)
}
}
func TestRoleWorkerTeardownPreservesOtherEventGlobalAssignments(t *testing.T) {
@@ -489,6 +636,53 @@ func TestRoleManagerReturnsServerError(t *testing.T) {
}
}
func TestRoleManagerPaginatesGuildMembers(t *testing.T) {
requests := 0
server := httptest.NewServer(http.HandlerFunc(func(response http.ResponseWriter, request *http.Request) {
requests++
if request.URL.Query().Get("after") == "" {
page := make([]map[string]any, 1000)
for index := range page {
page[index] = map[string]any{"user": map[string]string{"id": fmt.Sprintf("user-%04d", index)}, "roles": []string{}}
}
_ = json.NewEncoder(response).Encode(page)
return
}
_ = json.NewEncoder(response).Encode([]map[string]any{{
"user": map[string]string{"id": "user-1000"}, "roles": []string{},
}})
}))
defer server.Close()
manager, err := newRoleManager(RoleSyncConfig{BotToken: "token", GuildID: "guild", APIBaseURL: server.URL, HTTPClient: server.Client()})
if err != nil {
t.Fatal(err)
}
members, err := manager.ListGuildMembers(context.Background())
if err != nil {
t.Fatal(err)
}
if len(members) != 1001 || requests != 2 {
t.Fatalf("unexpected pagination result: members=%d requests=%d", len(members), requests)
}
}
func TestRoleWorkerSchedulesStableGlobalBucket(t *testing.T) {
store := newRoleStoreFake()
worker, err := NewRoleWorker(store, RoleSyncConfig{
BotToken: "token", GuildID: "guild", GlobalSyncInterval: 5 * time.Minute,
})
if err != nil {
t.Fatal(err)
}
worker.now = func() time.Time { return time.Unix(1_000, 0).UTC() }
worker.scheduleGlobalSync(context.Background())
worker.now = func() time.Time { return time.Unix(1_001, 0).UTC() }
worker.scheduleGlobalSync(context.Background())
if len(store.globalBuckets) != 2 || store.globalBuckets[0] != store.globalBuckets[1] {
t.Fatalf("same interval produced different buckets: %v", store.globalBuckets)
}
}
func testDiscordRoster(name string) application.DiscordRoleRoster {
slots := []domain.Slot{
{PlayerID: "player-1", Role: domain.Tank},