auth: a room's notice is a state event (org.gamecult.notice), not a pinned message
Heimdall's ruling: type org.gamecult.notice, state_key '', content {version, scope}.
version is a positive integer, scope exactly show, study or train; absent or
malformed means no train line. ROOM_NOTICE_CONTENT_KEY becomes
ROOM_NOTICE_EVENT_TYPE; RoomTrainingLine reads the state event through useStateEvent.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
3fcf1cf9dd
commit
c2673a698d
4 changed files with 62 additions and 63 deletions
|
|
@ -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<RoomNotice>();
|
||||
|
||||
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 (
|
||||
<Text
|
||||
|
|
|
|||
|
|
@ -1,12 +1,12 @@
|
|||
import { describe, expect, it } from 'vitest';
|
||||
import {
|
||||
ROOM_NOTICE_CONTENT_KEY,
|
||||
ROOM_NOTICE_EVENT_TYPE,
|
||||
TRAINING_NOTICE_TEXT,
|
||||
parseRoomNotice,
|
||||
roomTrainingLine,
|
||||
} from './trainingNotice';
|
||||
|
||||
const pinned = (notice: unknown) => ({ 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();
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -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<string, unknown> | 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;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -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',
|
||||
|
|
|
|||
Loading…
Reference in a new issue