Add Discord guild ID configuration and enhance event announcement options
This commit introduces the `DISCORD_GUILD_ID` environment variable to the configuration files, allowing for better integration with Discord for role synchronization. The event announcement functionality has been updated to include an option for the `@everyone` mention, which can be toggled during event creation. The backend logic has been modified to handle this new option, and corresponding updates have been made to the frontend to allow users to control the mention behavior. Additionally, tests have been added to ensure the correct functionality of these features.
This commit is contained in:
60
backend/internal/application/discord_roles.go
Normal file
60
backend/internal/application/discord_roles.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package application
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"mixmaker/backend/internal/domain"
|
||||
)
|
||||
|
||||
const (
|
||||
DiscordRoleScopeGlobal = "global"
|
||||
DiscordRoleScopeEvent = "event"
|
||||
|
||||
DiscordRoleKindTank = "tank"
|
||||
DiscordRoleKindDamage = "damage"
|
||||
DiscordRoleKindSupport = "support"
|
||||
DiscordRoleKindTeam = "team"
|
||||
DiscordRoleKindCaptain = "captain"
|
||||
|
||||
DiscordRoleActionReconcile = "reconcile"
|
||||
DiscordRoleActionTeardown = "teardown"
|
||||
)
|
||||
|
||||
type DiscordManagedRole struct {
|
||||
Scope string
|
||||
EventID string
|
||||
TeamID string
|
||||
Kind string
|
||||
DiscordRoleID string
|
||||
RoleName string
|
||||
}
|
||||
|
||||
type DiscordRoleSyncJob struct {
|
||||
ID int64
|
||||
EventID string
|
||||
Action string
|
||||
Generation int64
|
||||
RoleSnapshot []string
|
||||
Attempts int
|
||||
}
|
||||
|
||||
type DiscordRoleRoster struct {
|
||||
Roster domain.RosterDraft
|
||||
PlayerDiscordIDs map[string]string
|
||||
}
|
||||
|
||||
type DiscordRoleStore interface {
|
||||
SeedDiscordRoleSyncJobs(context.Context) error
|
||||
ClaimDiscordRoleSyncJob(context.Context) (DiscordRoleSyncJob, error)
|
||||
CompleteDiscordRoleSyncJob(context.Context, int64, string) error
|
||||
RetryDiscordRoleSyncJob(context.Context, int64, string, time.Time) error
|
||||
ListDiscordManagedRoles(context.Context, string) ([]DiscordManagedRole, error)
|
||||
UpsertDiscordManagedRole(context.Context, DiscordManagedRole) error
|
||||
DeleteDiscordManagedRole(context.Context, DiscordManagedRole) error
|
||||
ListDiscordRoleAssignments(context.Context, string) ([]string, error)
|
||||
UpsertDiscordRoleAssignment(context.Context, string, string) error
|
||||
DeleteDiscordRoleAssignment(context.Context, string, string) error
|
||||
GetDiscordRoleRoster(context.Context, string) (DiscordRoleRoster, error)
|
||||
ListActiveDiscordRoleRosters(context.Context) ([]DiscordRoleRoster, error)
|
||||
}
|
||||
@@ -21,6 +21,7 @@ type Store interface {
|
||||
DeleteSession(context.Context, string) error
|
||||
UpdatePlayer(context.Context, domain.Player) (domain.Player, error)
|
||||
ListPlayers(context.Context) ([]domain.Player, error)
|
||||
EnqueueDiscordRoleSync(context.Context, string, string, int64) error
|
||||
CreateParticipant(context.Context, domain.Player, domain.RSVP) (domain.Player, domain.RSVP, error)
|
||||
CreateEvent(context.Context, domain.Event) (domain.Event, error)
|
||||
GetEvent(context.Context, string) (domain.Event, error)
|
||||
@@ -59,7 +60,15 @@ type Store interface {
|
||||
type Publisher interface{ Publish(topic string, value any) }
|
||||
|
||||
type EventAnnouncer interface {
|
||||
AnnounceEventCreated(context.Context, domain.Event) error
|
||||
AnnounceEventCreated(context.Context, domain.Event, bool) error
|
||||
}
|
||||
|
||||
type CreateEventOptions struct {
|
||||
PingEveryone bool
|
||||
}
|
||||
|
||||
func (s *Service) enqueueDiscordRoleSync(ctx context.Context, eventID, action string) {
|
||||
_ = s.Store.EnqueueDiscordRoleSync(ctx, eventID, action, s.Now().UnixNano())
|
||||
}
|
||||
|
||||
type Service struct {
|
||||
@@ -159,7 +168,7 @@ func (s *Service) UpdateOwnProfile(ctx context.Context, actor domain.Account, cu
|
||||
return out, err
|
||||
}
|
||||
|
||||
func (s *Service) CreateEvent(ctx context.Context, actor domain.Account, event domain.Event) (domain.Event, error) {
|
||||
func (s *Service) CreateEvent(ctx context.Context, actor domain.Account, event domain.Event, options CreateEventOptions) (domain.Event, error) {
|
||||
if !actor.IsStaff() {
|
||||
return domain.Event{}, domain.ErrForbidden
|
||||
}
|
||||
@@ -177,7 +186,7 @@ func (s *Service) CreateEvent(ctx context.Context, actor domain.Account, event d
|
||||
s.Bus.Publish("events", out)
|
||||
if s.EventAnnouncer != nil {
|
||||
announcementCtx, cancel := context.WithTimeout(ctx, s.AnnouncementTimeout)
|
||||
_ = s.EventAnnouncer.AnnounceEventCreated(announcementCtx, out)
|
||||
_ = s.EventAnnouncer.AnnounceEventCreated(announcementCtx, out, options.PingEveryone)
|
||||
cancel()
|
||||
}
|
||||
}
|
||||
@@ -381,6 +390,7 @@ func (s *Service) RenameTeam(ctx context.Context, actor domain.Account, player d
|
||||
_ = s.Store.AppendAudit(ctx, actor.ID, "team.renamed", teamID, map[string]string{"name": name})
|
||||
s.Bus.Publish("team:"+teamID, team)
|
||||
s.Bus.Publish("event:"+team.EventID, team)
|
||||
s.enqueueDiscordRoleSync(ctx, team.EventID, DiscordRoleActionReconcile)
|
||||
}
|
||||
return team, err
|
||||
}
|
||||
|
||||
@@ -33,12 +33,14 @@ func (p *recordingPublisher) Publish(topic string, _ any) {
|
||||
|
||||
type recordingAnnouncer struct {
|
||||
events []domain.Event
|
||||
pings []bool
|
||||
err error
|
||||
hasDeadline bool
|
||||
}
|
||||
|
||||
func (a *recordingAnnouncer) AnnounceEventCreated(ctx context.Context, event domain.Event) error {
|
||||
func (a *recordingAnnouncer) AnnounceEventCreated(ctx context.Context, event domain.Event, pingEveryone bool) error {
|
||||
a.events = append(a.events, event)
|
||||
a.pings = append(a.pings, pingEveryone)
|
||||
_, a.hasDeadline = ctx.Deadline()
|
||||
return a.err
|
||||
}
|
||||
@@ -51,7 +53,7 @@ func TestCreateEventAnnouncesAfterPersistence(t *testing.T) {
|
||||
now := time.Date(2026, time.July, 19, 8, 0, 0, 0, time.UTC)
|
||||
service.Now = func() time.Time { return now }
|
||||
|
||||
created, err := service.CreateEvent(context.Background(), domain.Account{ID: "admin", Role: domain.RoleAdmin}, validEvent(now))
|
||||
created, err := service.CreateEvent(context.Background(), domain.Account{ID: "admin", Role: domain.RoleAdmin}, validEvent(now), CreateEventOptions{PingEveryone: true})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -64,6 +66,9 @@ func TestCreateEventAnnouncesAfterPersistence(t *testing.T) {
|
||||
if !announcer.hasDeadline {
|
||||
t.Fatal("announcement context must have a timeout")
|
||||
}
|
||||
if len(announcer.pings) != 1 || !announcer.pings[0] {
|
||||
t.Fatalf("expected @everyone option, got %v", announcer.pings)
|
||||
}
|
||||
if len(bus.topics) != 1 || bus.topics[0] != "events" {
|
||||
t.Fatalf("unexpected published topics: %v", bus.topics)
|
||||
}
|
||||
@@ -77,7 +82,7 @@ func TestCreateEventDoesNotAnnouncePersistenceFailure(t *testing.T) {
|
||||
now := time.Date(2026, time.July, 19, 8, 0, 0, 0, time.UTC)
|
||||
service.Now = func() time.Time { return now }
|
||||
|
||||
_, err := service.CreateEvent(context.Background(), domain.Account{ID: "admin", Role: domain.RoleAdmin}, validEvent(now))
|
||||
_, err := service.CreateEvent(context.Background(), domain.Account{ID: "admin", Role: domain.RoleAdmin}, validEvent(now), CreateEventOptions{PingEveryone: true})
|
||||
if !errors.Is(err, persistenceErr) {
|
||||
t.Fatalf("expected persistence error, got %v", err)
|
||||
}
|
||||
@@ -93,7 +98,7 @@ func TestCreateEventIgnoresAnnouncementFailure(t *testing.T) {
|
||||
now := time.Date(2026, time.July, 19, 8, 0, 0, 0, time.UTC)
|
||||
service.Now = func() time.Time { return now }
|
||||
|
||||
created, err := service.CreateEvent(context.Background(), domain.Account{ID: "admin", Role: domain.RoleAdmin}, validEvent(now))
|
||||
created, err := service.CreateEvent(context.Background(), domain.Account{ID: "admin", Role: domain.RoleAdmin}, validEvent(now), CreateEventOptions{PingEveryone: true})
|
||||
if err != nil {
|
||||
t.Fatalf("announcement failure must not fail event creation: %v", err)
|
||||
}
|
||||
@@ -102,6 +107,20 @@ func TestCreateEventIgnoresAnnouncementFailure(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateEventCanAnnounceWithoutEveryonePing(t *testing.T) {
|
||||
announcer := &recordingAnnouncer{}
|
||||
service := New(&createEventStore{}, &recordingPublisher{}, announcer)
|
||||
now := time.Date(2026, time.July, 19, 8, 0, 0, 0, time.UTC)
|
||||
service.Now = func() time.Time { return now }
|
||||
|
||||
if _, err := service.CreateEvent(context.Background(), domain.Account{ID: "admin", Role: domain.RoleAdmin}, validEvent(now), CreateEventOptions{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(announcer.events) != 1 || len(announcer.pings) != 1 || announcer.pings[0] {
|
||||
t.Fatalf("expected one announcement without @everyone, events=%d pings=%v", len(announcer.events), announcer.pings)
|
||||
}
|
||||
}
|
||||
|
||||
func validEvent(now time.Time) domain.Event {
|
||||
return domain.Event{
|
||||
Name: "Sunday Mix",
|
||||
|
||||
@@ -39,6 +39,7 @@ func (s *Service) CancelEvent(ctx context.Context, actor domain.Account, eventID
|
||||
_ = s.Store.AppendAudit(ctx, actor.ID, "event.cancelled", eventID, event)
|
||||
s.Bus.Publish("event:"+eventID, event)
|
||||
s.Bus.Publish("events", event)
|
||||
s.enqueueDiscordRoleSync(ctx, eventID, DiscordRoleActionTeardown)
|
||||
}
|
||||
return event, err
|
||||
}
|
||||
@@ -321,6 +322,9 @@ func (s *Service) SubstituteRoster(ctx context.Context, actor domain.Account, ev
|
||||
}
|
||||
_ = s.Store.AppendAudit(ctx, actor.ID, action, eventID, map[string]string{"outgoingPlayerId": outgoingID, "incomingPlayerId": reserveID})
|
||||
s.Bus.Publish("event:"+eventID, roster)
|
||||
if emergency {
|
||||
s.enqueueDiscordRoleSync(ctx, eventID, DiscordRoleActionReconcile)
|
||||
}
|
||||
}
|
||||
return roster, err
|
||||
}
|
||||
@@ -389,6 +393,7 @@ func (s *Service) ConfirmRosters(ctx context.Context, actor domain.Account, even
|
||||
if err == nil {
|
||||
_ = s.Store.AppendAudit(ctx, actor.ID, "roster.confirmed", eventID, roster)
|
||||
s.Bus.Publish("event:"+eventID, event)
|
||||
s.enqueueDiscordRoleSync(ctx, eventID, DiscordRoleActionReconcile)
|
||||
}
|
||||
return event, err
|
||||
}
|
||||
@@ -571,6 +576,9 @@ func (s *Service) RevertWorkflowStage(ctx context.Context, actor domain.Account,
|
||||
if err == nil {
|
||||
_ = s.Store.AppendAudit(ctx, actor.ID, "workflow.reverted", eventID, map[string]domain.EventState{"state": previous})
|
||||
s.Bus.Publish("event:"+eventID, event)
|
||||
if previous == domain.RostersDraft {
|
||||
s.enqueueDiscordRoleSync(ctx, eventID, DiscordRoleActionTeardown)
|
||||
}
|
||||
}
|
||||
return event, err
|
||||
}
|
||||
@@ -684,6 +692,9 @@ func (s *Service) RecordSeriesResult(ctx context.Context, actor domain.Account,
|
||||
old := event.Version
|
||||
event.State, event.Version, event.UpdatedAt = domain.Completed, event.Version+1, s.Now()
|
||||
_, getErr = s.Store.SaveEventWorkflow(ctx, event, old)
|
||||
if getErr == nil {
|
||||
s.enqueueDiscordRoleSync(ctx, out.EventID, DiscordRoleActionTeardown)
|
||||
}
|
||||
}
|
||||
return out, getErr
|
||||
}
|
||||
@@ -736,6 +747,9 @@ func (s *Service) RecordSeriesResult(ctx context.Context, actor domain.Account,
|
||||
old := event.Version
|
||||
event.State, event.Version, event.UpdatedAt = domain.Completed, event.Version+1, s.Now()
|
||||
_, getErr = s.Store.SaveEventWorkflow(ctx, event, old)
|
||||
if getErr == nil {
|
||||
s.enqueueDiscordRoleSync(ctx, out.EventID, DiscordRoleActionTeardown)
|
||||
}
|
||||
}
|
||||
err = getErr
|
||||
}
|
||||
@@ -799,6 +813,9 @@ func (s *Service) advanceGraphTournament(ctx context.Context, completed domain.S
|
||||
old := event.Version
|
||||
event.State, event.Version, event.UpdatedAt = domain.Completed, event.Version+1, s.Now()
|
||||
_, err = s.Store.SaveEventWorkflow(ctx, event, old)
|
||||
if err == nil {
|
||||
s.enqueueDiscordRoleSync(ctx, completed.EventID, DiscordRoleActionTeardown)
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user