1
0
Fork 0

fix(bot): rewrite the Friday notice in the claimed state

The claim path appended a credit line to the invitation, so a claimed notice
still said the slot was open, that nobody had picked it up, and that Jason
would host if nobody did.

- renderClaimedNotice(): the whole body for the claimed state, role ping
  dropped since there is nothing left to ask for
- compare the live message against the rendered body each poll, so a message
  left contradictory by an older build heals on the next tick
- a cancelled claim still restores the full invitation, ping included
- cover both bodies and the claim parser in the notice integration test
This commit is contained in:
Jason Fraley 2026-11-20 20:11:53 -05:00
parent 5418d6563e
commit b90247a288
3 changed files with 92 additions and 26 deletions

View file

@ -99,7 +99,7 @@ Once a mission is evaluable (`Completed`, or `Scheduled`/`Active` past its start
## Feature flow: Friday open-slot notice
At 8 PM Eastern on Wednesday (`America/New_York`, evaluated via `isNoticeDue` in `src/lib/intelligence/fridayOpsNotice.ts`; catch-up until ET midnight, a fully missed Wednesday skips to next week), if no non-cancelled mission starts on that week's upcoming Friday, `fridayOpsNotice` posts once in #side-ops pinging the Community Zeus role: the slot is open, hosting is welcome, and if nobody picks it up the slot stays empty or Jason hosts it himself. The slot window is Friday 00:00–23:59:59.999 **America/New_York** computed from the Eastern calendar date, never the server clock (the UTC deploy container once treated Wed 8 PM ET as Thursday and missed Friday-evening ops entirely); when it is already Friday Eastern, that day itself is the slot, so the dedup token stays stable from Wednesday through the op day and late claims still credit. When a non-superuser creator later claims the slot (`getFridayClaimant`, earliest-created mission wins; superuser/staff-created missions never claim), the notice is edited to append "This slot was claimed by \<user\>. Thank you for hosting for our community!"; if that op is later cancelled or deleted, the claim line is stripped and the notice reverts to the open-slot state (a between-polls claim swap re-credits the new claimant). Role: `DISCORD_COMMUNITY_ZEUS_ROLE_ID` or resolved by name in the guild; channel: `DISCORD_SIDE_OPS_CHANNEL_ID`. Dedup without DB state: the message embeds a stable per-Friday Eastern date label (e.g. `January 15, 2027`) and the service scans recent channel messages for it (claim idempotency via a marker in the message content). Never use Discord's `<t:epoch:D>` for this date: it renders in each viewer's timezone, so an epoch at Friday 00:00 Eastern displayed as Thursday for members in Central/Mountain/Pacific/Hawaii. Notices still carrying the old `<t:epoch:D>` token are matched and rewritten in place to the Eastern label. Drafts (Concept) count as the slot being taken.
At 8 PM Eastern on Wednesday (`America/New_York`, evaluated via `isNoticeDue` in `src/lib/intelligence/fridayOpsNotice.ts`; catch-up until ET midnight, a fully missed Wednesday skips to next week), if no non-cancelled mission starts on that week's upcoming Friday, `fridayOpsNotice` posts once in #side-ops pinging the Community Zeus role: the slot is open, hosting is welcome, and if nobody picks it up the slot stays empty or Jason hosts it himself. The slot window is Friday 00:00–23:59:59.999 **America/New_York** computed from the Eastern calendar date, never the server clock (the UTC deploy container once treated Wed 8 PM ET as Thursday and missed Friday-evening ops entirely); when it is already Friday Eastern, that day itself is the slot, so the dedup token stays stable from Wednesday through the op day and late claims still credit. When a non-superuser creator later claims the slot (`getFridayClaimant`, earliest-created mission wins; superuser/staff-created missions never claim), the notice is rewritten to the claimed state: the invitation and its role ping are replaced with "The Friday op slot (<date>) is taken." plus "This slot was claimed by \<user\>. Thank you for hosting for our community!" (leaving the invitation in place would contradict the credit line). If that op is later cancelled or deleted, the claim line is stripped and the whole invitation, role ping included, is restored (a between-polls claim swap re-credits the new claimant; the service compares the rendered body each poll, so a message left in a contradictory state by an older build heals itself). Role: `DISCORD_COMMUNITY_ZEUS_ROLE_ID` or resolved by name in the guild; channel: `DISCORD_SIDE_OPS_CHANNEL_ID`. Dedup without DB state: the message embeds a stable per-Friday Eastern date label (e.g. `January 15, 2027`) and the service scans recent channel messages for it (claim idempotency via a marker in the message content). Never use Discord's `<t:epoch:D>` for this date: it renders in each viewer's timezone, so an epoch at Friday 00:00 Eastern displayed as Thursday for members in Central/Mountain/Pacific/Hawaii. Notices still carrying the old `<t:epoch:D>` token are matched and rewritten in place to the Eastern label. Drafts (Concept) count as the slot being taken.
## Feature flow: assignment transfers

View file

@ -21,12 +21,13 @@ import {
* scheduled for that week's Friday yet, post once in #side-ops pinging the
* Community Zeus role, offering the slot.
*
* When a non-superuser (a community zeus) later claims the slot, the notice
* is edited to credit them instead; if that claim op is later cancelled (or
* deleted), the notice reverts to the open-slot state. Dedup needs no DB
* state: the notice embeds a stable per-Friday Eastern date label and the
* service scans recent channel messages for it (and for the claim line)
* before posting or editing, so bot restarts never double-post or re-credit.
* When a non-superuser (a community zeus) later claims the slot, the notice is
* rewritten to the claimed state (the invitation and its role ping replaced by
* the credit line); if that claim op is later cancelled (or deleted), the
* notice reverts to the open-slot invitation. Dedup needs no DB state: the
* notice embeds a stable per-Friday Eastern date label and the service scans
* recent channel messages for it before posting or editing, so bot restarts
* never double-post or re-credit.
*/
const MESSAGE_SCAN_LIMIT = 100;
@ -72,7 +73,19 @@ const CLAIM_MARKER = "This slot was claimed by";
const CLAIM_LINE_PATTERN =
/\n\nThis slot was claimed by (.+)\. Thank you for hosting for our community!$/;
const creditedNameOf = (content: string): string | null =>
/**
* The same notice once a community zeus has claimed the slot. The invitation
* above is replaced rather than kept: "the slot is still open ... if nobody
* picks it up" directly contradicts the credit line under it, so the whole
* body is rewritten to the claimed state (and the role ping dropped, since
* there is nothing left to ask for).
*/
export const renderClaimedNotice = (userName: string, now = new Date()): string =>
`The Friday op slot (${fridayDateLabel(now)}) is taken.\n\n` +
`${CLAIM_MARKER} ${userName}. Thank you for hosting for our community!`;
/** The credited host, when the message body is in the claimed state. */
export const claimedByName = (content: string): string | null =>
content.match(CLAIM_LINE_PATTERN)?.[1] ?? null;
/**
@ -128,34 +141,37 @@ const processNotice = async (
return "posted";
}
// A notice posted before the Eastern label existed still carries the old
// `<t:epoch:D>` token (which reads as Thursday west of Eastern). Upgrade it
// in place so the visible date is correct for everyone.
let content = notice.content;
if (!content.includes(token) && content.includes(legacyToken)) {
content = content.replace(legacyToken, token);
await notice.edit(content);
// Once a host has claimed the slot the message must read as claimed end to
// end. Comparing against the rendered body instead of appending a line also
// heals a message an older build left with the invitation still in it.
if (claim) {
const desired = renderClaimedNotice(claim.userName, now);
if (notice.content === desired) return "idle";
await notice.edit(desired);
payload.logger.info(
`[Bot] Friday open-slot notice for ${token}: upgraded the legacy date token to the Eastern label.`,
`[Bot] Friday open-slot notice for ${token} claimed by ${claim.userName}; notice rewritten to the claimed state.`,
);
return "edited";
}
const credited = creditedNameOf(content);
if (!claim) {
if (!credited) return "idle";
await notice.edit(content.replace(CLAIM_LINE_PATTERN, ""));
// The claim went away (op cancelled or deleted): restore the full invitation,
// including the role ping, so the slot reads as open again.
const credited = claimedByName(notice.content);
if (credited) {
const zeusRoleId = await resolveZeusRoleId(client);
await notice.edit(renderNotice(zeusRoleId, now));
payload.logger.info(
`[Bot] Friday open-slot notice for ${token}: claim by ${credited} was cancelled; notice reverted to the open-slot state.`,
);
return "edited";
}
if (credited !== claim.userName) {
const base = credited ? content.replace(CLAIM_LINE_PATTERN, "") : content;
await notice.edit(
`${base}\n\n${CLAIM_MARKER} ${claim.userName}. Thank you for hosting for our community!`,
);
// Open and unclaimed: the only repair left is the legacy Discord timestamp
// token, which reads as Thursday for viewers west of Eastern.
if (!notice.content.includes(token) && notice.content.includes(legacyToken)) {
await notice.edit(notice.content.replace(legacyToken, token));
payload.logger.info(
`[Bot] Friday open-slot notice for ${token} claimed by ${claim.userName}; notice updated.`,
`[Bot] Friday open-slot notice for ${token}: upgraded the legacy date token to the Eastern label.`,
);
return "edited";
}

View file

@ -342,3 +342,53 @@ describe("Friday open-slot notice", () => {
await expect(getFridayClaimant(payload, WEDNESDAY)).resolves.toBeNull();
});
});
/**
* Message copy for the notice, exercised through the bot service that sends it.
* The service validates its env at import, so give it dummies (nothing here
* connects to Discord: the render helpers are pure).
*/
describe("Friday open-slot notice copy", () => {
process.env.DISCORD_TOKEN ||= "notice-copy-test-token";
process.env.DISCORD_GUILD_ID ||= "123456789012345678";
process.env.APP_URL ||= "https://example.test";
const loadService = () => import("@/bot/services/fridayOpsNotice");
it("invites a host, with the role ping, while the slot is open", async () => {
const { renderNotice } = await loadService();
const body = renderNotice("987654321098765432", WEDNESDAY);
expect(body.startsWith("<@&987654321098765432> ")).toBe(true);
expect(body).toContain("is still open");
expect(body).toContain(fridayDateLabel(WEDNESDAY));
expect(body).toContain(`${process.env.APP_URL?.trim()}/intelligence/missions/calendar`);
expect(body).not.toContain("is taken");
});
it("drops the invitation and the role ping once the slot is claimed", async () => {
const { claimedByName, renderClaimedNotice } = await loadService();
const body = renderClaimedNotice("J. Goad", WEDNESDAY);
// The credit line on its own is not enough: the invitation left above it
// ("still open ... if nobody picks it up") reads as the opposite claim.
expect(body).toContain("is taken");
expect(body).not.toContain("still open");
expect(body).not.toContain("schedule one");
expect(body).not.toContain("<@&");
expect(body).toContain(fridayDateLabel(WEDNESDAY));
expect(body).toContain(
"This slot was claimed by J. Goad. Thank you for hosting for our community!",
);
expect(claimedByName(body)).toBe("J. Goad");
});
it("reads an open notice as unclaimed and round-trips a claimed one", async () => {
const { claimedByName, renderClaimedNotice, renderNotice } = await loadService();
expect(claimedByName(renderNotice(null, WEDNESDAY))).toBeNull();
// Comparing rendered bodies is how the service decides to heal a message,
// so the claim parser must accept exactly what the renderer writes.
expect(claimedByName(renderClaimedNotice("Z. LAMPERT", FRIDAY))).toBe("Z. LAMPERT");
});
});