Source code for sidequest_common.schemas.discord_events

  1"""Two-way guild event <-> Discord scheduled event sync: the bot-facing contract.
  2
  3The API decides *what* Discord should show (``DiscordEventPush``); the bot does every Discord call and reports back
  4what Discord actually holds (``DiscordEventState``). See ``sidequest_common.models.events.GuildEvent`` for the hash
  5columns that keep the two sides from echoing each other.
  6"""
  7
  8from datetime import datetime  # pydantic needs it at runtime
  9from enum import StrEnum
 10from typing import Literal
 11from uuid import UUID  # pydantic needs it at runtime
 12
 13from pydantic import BaseModel, Field
 14
 15from sidequest_common.schemas.events import EventCounts, EventKind
 16from sidequest_common.schemas.recruitment import RecruitmentTally, RecruitShoutout
 17from sidequest_common.schemas.weekly_board import BoardSignup
 18
 19
[docs] 20class DiscordEventStatus(StrEnum): 21 """A Discord scheduled event's status, named the way Discord's ``EventStatus`` is.""" 22 23 SCHEDULED = "scheduled" 24 ACTIVE = "active" 25 COMPLETED = "completed" 26 CANCELLED = "cancelled"
27 28
[docs] 29class DiscordEventState(BaseModel): 30 """What Discord currently holds for one scheduled event, as the bot read it (gateway payload or REST response).""" 31 32 id: int 33 name: str 34 description: str | None = None 35 starts_at: datetime 36 ends_at: datetime | None = None 37 status: DiscordEventStatus 38 location: str | None = Field(default=None, description="External events only.") 39 channel_id: int | None = Field(default=None, description="Voice/stage events only.")
40 41 42PushAction = Literal["upsert", "cancel", "complete"] 43"""``upsert``: create the event if ``discord_event_id`` is ``None`` (or it no longer exists), else edit it. 44``cancel``: cancel a scheduled event (end an active one). ``complete``: end an active event, delete a scheduled one. 45After ``cancel``/``complete`` the bot acks with ``event=None``: the guild event is no longer linked.""" 46 47
[docs] 48class DiscordEventPush(BaseModel): 49 """One guild event whose Discord event is out of date. Fields are Discord-ready (within Discord's limits).""" 50 51 event_id: UUID 52 discord_event_id: int | None 53 thread_id: int | None = Field(default=None, description="The event's forum thread, if one has been created.") 54 kind: EventKind 55 counts: EventCounts = Field( 56 default_factory=lambda: EventCounts(tank=0, healer=0, dps=0, signed=0, tentative=0, absent=0, interested=0), 57 description="Current headcounts, for the forum starter post's live counts line.", 58 ) 59 action: PushAction 60 name: str = Field(max_length=100) 61 title: str = Field(description="Full, untruncated title (``name`` is Discord's 100-char-limited version).") 62 description: str = Field(max_length=1000) 63 location: str = Field(max_length=100, description="Used for external events; voice/stage events keep theirs.") 64 starts_at: datetime 65 ends_at: datetime = Field(description="Discord requires an end for external events.") 66 signup_url: str 67 push_hash: str 68 board_channel_id: int | None = Field(default=None, description="The channel of the event's weekly-board card.") 69 board_message_id: int | None = Field(default=None, description="The event's weekly-board card, if it has one.") 70 board_signups: list[BoardSignup] = Field( 71 default_factory=list, 72 description="Every sign-up, in sign-up order, for the weekly-board card; empty when there is no card.", 73 ) 74 recruitment: RecruitmentTally | None = Field( 75 default=None, description="Set only for recruitment drives: the live leaderboard for the forum starter post." 76 )
77 78
[docs] 79class SignupActivity(BaseModel): 80 """One unposted sign-up activity row, queued to post as a forum comment on ``thread_id``.""" 81 82 id: UUID 83 thread_id: int 84 display_name: str 85 action: str 86 detail: str | None = None 87 discord_id: int | None = Field(default=None, description="Member to @-mention; None for rows from before.") 88 comment: str | None = None
89 90
[docs] 91class DiscordSyncPending(BaseModel): 92 """``GET /api/guilds/{guild_id}/event-sync``: everything the bot should write to Discord now.""" 93 94 pushes: list[DiscordEventPush] 95 deletions: list[int] = Field(description="Discord event ids whose guild event was deleted on the site.") 96 activity: list[SignupActivity] = Field( 97 default_factory=list, description="Unposted sign-up activity for events with a Discord thread, oldest first." 98 ) 99 recruit_shoutouts: list[RecruitShoutout] = Field( 100 default_factory=list, description="Qualified recruits whose shout-out hasn't posted yet, oldest first." 101 )
102 103
[docs] 104class DiscordPushAck(BaseModel): 105 """``POST /api/guilds/{guild_id}/event-sync/{event_id}/ack``: the bot applied ``push_hash``. 106 107 ``event`` is what Discord returned, or ``None`` when no Discord event is linked any more (ended, deleted, or the 108 push was skipped because the start time had passed). ``thread_id`` is the event's forum thread, if any. 109 """ 110 111 push_hash: str 112 event: DiscordEventState | None 113 thread_id: int | None = None
114 115
[docs] 116class DiscordEventReconcile(BaseModel): 117 """``POST /api/guilds/{guild_id}/event-sync/reconcile``: every scheduled event currently in the guild. 118 119 Applied like individual updates, then any linked guild event whose event isn't listed is treated as deleted in 120 Discord, and any tombstone whose event isn't listed is dropped. 121 """ 122 123 events: list[DiscordEventState]
124 125
[docs] 126class DiscordInterestReport(BaseModel): 127 """``PUT /api/guilds/{guild_id}/event-sync/interest/{discord_event_id}``: the full interested-user set now. 128 129 Replaces the previously reported set for the guild event linked to ``discord_event_id``; a no-op (204) if no 130 guild event is linked to it. 131 """ 132 133 user_ids: list[int]
134 135
[docs] 136class ActivityAck(BaseModel): 137 """``POST /api/guilds/{guild_id}/event-sync/activity/ack``: the bot posted these sign-up activity rows.""" 138 139 ids: list[UUID]