diff --git a/src/app/features/room/RoomTrainingLine.tsx b/src/app/features/room/RoomTrainingLine.tsx index 8855f0ff..147932de 100644 --- a/src/app/features/room/RoomTrainingLine.tsx +++ b/src/app/features/room/RoomTrainingLine.tsx @@ -1,43 +1,18 @@ import { Text, color, config } from 'folds'; import { Room } from 'matrix-js-sdk'; -import React, { useEffect, useState } from 'react'; -import { useMatrixClient } from '../../hooks/useMatrixClient'; -import { useRoomPinnedEvents } from '../../hooks/useRoomPinnedEvents'; -import { RoomNotice, parseRoomNotice, roomTrainingLine } from '../../utils/trainingNotice'; - -// The room's own pinned notice decides the line, newest pin first; no notice, no line. The version -// is read by the collector from the same pinned event, so nothing is stored in the browser. -const useRoomNotice = (room: Room): RoomNotice | undefined => { - const mx = useMatrixClient(); - const pinned = useRoomPinnedEvents(room); - const [notice, setNotice] = useState(); - - useEffect(() => { - let live = true; - setNotice(undefined); - (async () => { - for (let i = pinned.length - 1; i >= 0; i -= 1) { - const id = pinned[i]; - const local = room.findEventById(id)?.getContent(); - // eslint-disable-next-line no-await-in-loop - const content = local ?? (await mx.fetchRoomEvent(room.roomId, id).catch(() => undefined))?.content; - const found = parseRoomNotice(content); - if (found) { - if (live) setNotice(found); - return; - } - } - })(); - return () => { - live = false; - }; - }, [mx, room, pinned]); - - return notice; -}; +import React from 'react'; +import { useStateEvent } from '../../hooks/useStateEvent'; +import { + ROOM_NOTICE_EVENT_TYPE, + parseRoomNotice, + roomTrainingLine, +} from '../../utils/trainingNotice'; +// The room's notice state event decides the line; no event, or a malformed one, no line. The +// collector reads the version from the same event, so nothing is stored in the browser. export function RoomTrainingLine({ room }: { room: Room }) { - const line = roomTrainingLine(useRoomNotice(room)); + const event = useStateEvent(room, ROOM_NOTICE_EVENT_TYPE); + const line = roomTrainingLine(parseRoomNotice(event?.getContent())); if (!line) return null; return ( ({ body: 'sample', [ROOM_NOTICE_CONTENT_KEY]: notice }); +const lineFor = (content: unknown) => roomTrainingLine(parseRoomNotice(content)); describe('training notice', () => { it('says exactly the ruled sentence and never mentions Discord', () => { @@ -16,19 +16,42 @@ describe('training notice', () => { expect(TRAINING_NOTICE_TEXT.toLowerCase()).not.toContain('discord'); }); - it('shows the train line only where the room declares train', () => { - const train = parseRoomNotice(pinned({ version: 2, scope: 'train' })); - expect(train).toEqual({ version: 2, scope: 'train' }); - expect(roomTrainingLine(train)).toBe(TRAINING_NOTICE_TEXT); - expect(roomTrainingLine(parseRoomNotice(pinned({ version: 1, scope: 'study' })))).toBeUndefined(); - expect(roomTrainingLine(parseRoomNotice(pinned({ version: 1, scope: 'show' })))).toBeUndefined(); + it('reads the notice from the org.gamecult.notice state event', () => { + expect(ROOM_NOTICE_EVENT_TYPE).toBe('org.gamecult.notice'); }); - it('shows nothing where no notice is declared, and never infers train', () => { - expect(roomTrainingLine(undefined)).toBeUndefined(); - expect(parseRoomNotice({ body: 'plain message' })).toBeUndefined(); - expect(parseRoomNotice(pinned({ scope: 'train' }))).toBeUndefined(); - expect(parseRoomNotice(pinned({ version: 0, scope: 'train' }))).toBeUndefined(); - expect(parseRoomNotice(pinned({ version: 1, scope: 'everything' }))).toBeUndefined(); + it('shows the train line only where the room declares train', () => { + expect(parseRoomNotice({ version: 2, scope: 'train' })).toEqual({ version: 2, scope: 'train' }); + expect(lineFor({ version: 2, scope: 'train' })).toBe(TRAINING_NOTICE_TEXT); + expect(lineFor({ version: 1, scope: 'study' })).toBeUndefined(); + expect(lineFor({ version: 1, scope: 'show' })).toBeUndefined(); + }); + + it('shows nothing where the notice is absent', () => { + expect(lineFor(undefined)).toBeUndefined(); + expect(lineFor(null)).toBeUndefined(); + expect(lineFor({})).toBeUndefined(); + }); + + it('shows nothing where the notice is malformed in any way', () => { + expect(lineFor('train')).toBeUndefined(); + expect(lineFor({ scope: 'train' })).toBeUndefined(); + expect(lineFor({ version: 1 })).toBeUndefined(); + expect(lineFor({ version: 0, scope: 'train' })).toBeUndefined(); + expect(lineFor({ version: -1, scope: 'train' })).toBeUndefined(); + expect(lineFor({ version: 1.5, scope: 'train' })).toBeUndefined(); + expect(lineFor({ version: '1', scope: 'train' })).toBeUndefined(); + expect(lineFor({ version: null, scope: 'train' })).toBeUndefined(); + }); + + it('rejects a version written with leading zeros', () => { + expect(lineFor({ version: '01', scope: 'train' })).toBeUndefined(); + expect(lineFor({ version: '007', scope: 'train' })).toBeUndefined(); + }); + + it('rejects an unknown or wrongly cased scope', () => { + expect(lineFor({ version: 1, scope: 'everything' })).toBeUndefined(); + expect(lineFor({ version: 1, scope: 'Train' })).toBeUndefined(); + expect(lineFor({ version: 1, scope: ['train'] })).toBeUndefined(); }); }); diff --git a/src/app/utils/trainingNotice.ts b/src/app/utils/trainingNotice.ts index c5cd7aa1..84918920 100644 --- a/src/app/utils/trainingNotice.ts +++ b/src/app/utils/trainingNotice.ts @@ -1,28 +1,28 @@ +import { StateEvent } from '../../types/matrix/room'; + export const TRAINING_NOTICE_TEXT = "Everything said here may be used to train GameCult's AI stewards."; /** - * Where a room declares its notice. Heimdall's answer-collector-consent.md says the notice is pinned - * with the sample and carries `notice_version` and a scope, but does not fix the pinned event's - * encoding; this key is this client's provisional reading of it, and the one place to change. + * Heimdall's ruling: a room's notice is a room state event, not a pinned message. Type below, + * state_key '', content `{version, scope}`; version a positive integer, scope exactly one of + * show, study or train. Absent or malformed in any way means no train line. */ -export const ROOM_NOTICE_CONTENT_KEY = 'org.gamecult.notice'; +export const ROOM_NOTICE_EVENT_TYPE = StateEvent.GameCultNotice; export type RoomNotice = { version: number; scope: 'show' | 'study' | 'train' }; -/** The notice a pinned event declares, or undefined: no scope is ever inferred. */ +/** The notice a state event's content declares, or undefined: no scope is ever inferred. */ export const parseRoomNotice = (content: unknown): RoomNotice | undefined => { - const raw = (content as Record | undefined)?.[ROOM_NOTICE_CONTENT_KEY] as - | { version?: unknown; scope?: unknown } - | undefined; + if (typeof content !== 'object' || content === null) return undefined; + const { version, scope } = content as { version?: unknown; scope?: unknown }; if ( - raw && - typeof raw.version === 'number' && - Number.isInteger(raw.version) && - raw.version > 0 && - (raw.scope === 'show' || raw.scope === 'study' || raw.scope === 'train') + typeof version === 'number' && + Number.isSafeInteger(version) && + version > 0 && + (scope === 'show' || scope === 'study' || scope === 'train') ) { - return { version: raw.version, scope: raw.scope }; + return { version, scope }; } return undefined; }; diff --git a/src/types/matrix/room.ts b/src/types/matrix/room.ts index c73c0f0b..8d6b89dd 100644 --- a/src/types/matrix/room.ts +++ b/src/types/matrix/room.ts @@ -23,6 +23,7 @@ export enum StateEvent { RoomMember = 'm.room.member', RoomThirdPartyInvite = 'm.room.third_party_invite', RoomPowerLevels = 'm.room.power_levels', + GameCultNotice = 'org.gamecult.notice', RoomName = 'm.room.name', RoomTopic = 'm.room.topic', RoomAvatar = 'm.room.avatar',