refactor(DP15): centralize sendStateEvent as-any cast in typed helper

Add a typed `sendStateEvent(mx, roomId, eventType, content, stateKey?)`
helper in utils/room.ts that mirrors the DP16 account-data helper pattern.
The SDK's typed `sendStateEvent` overload rejects the fork's custom
`StateEvent` enum values, so every call site cast arg 2 to `any` (which
also collapsed the content type). The single `as any` cast now lives inside
the helper; a generic `content: T extends object` keeps each call site's
content type checked.

Route all 32 `mx.sendStateEvent(..., StateEvent.X as any, ...)` casts across
20 files through the helper. The 2 dynamic-string casts in developer-tools
(SendRoomEvent, StateEventEditor) pass a runtime string, not an enum value,
so they stay as-is.

No behavior change.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-07-07 23:32:50 -04:00
parent 4fc3f7a35f
commit 101e4116e8
21 changed files with 83 additions and 43 deletions
+4 -2
View File
@@ -8,6 +8,7 @@ import {
} from 'matrix-js-sdk'; } from 'matrix-js-sdk';
import { RoomJoinRulesEventContent } from 'matrix-js-sdk/lib/types'; import { RoomJoinRulesEventContent } from 'matrix-js-sdk/lib/types';
import { RoomType, StateEvent } from '../../../types/matrix/room'; import { RoomType, StateEvent } from '../../../types/matrix/room';
import { sendStateEvent } from '../../utils/room';
import { getViaServers } from '../../plugins/via-servers'; import { getViaServers } from '../../plugins/via-servers';
import { getMxIdServer } from '../../utils/matrix'; import { getMxIdServer } from '../../utils/matrix';
import { CreateRoomAccess } from './types'; import { CreateRoomAccess } from './types';
@@ -150,9 +151,10 @@ export const createRoom = async (mx: MatrixClient, data: CreateRoomData): Promis
const result = await mx.createRoom(options); const result = await mx.createRoom(options);
if (data.parent) { if (data.parent) {
await mx.sendStateEvent( await sendStateEvent(
mx,
data.parent.roomId, data.parent.roomId,
StateEvent.SpaceChild as any, StateEvent.SpaceChild,
{ {
auto_join: false, auto_join: false,
suggested: false, suggested: false,
@@ -38,7 +38,7 @@ import { mDirectAtom } from '../../state/mDirectList';
import { roomToParentsAtom } from '../../state/room/roomToParents'; import { roomToParentsAtom } from '../../state/room/roomToParents';
import { useAllJoinedRoomsSet, useGetRoom } from '../../hooks/useGetRoom'; import { useAllJoinedRoomsSet, useGetRoom } from '../../hooks/useGetRoom';
import { VirtualTile } from '../../components/virtualizer'; import { VirtualTile } from '../../components/virtualizer';
import { getDirectRoomAvatarUrl, getRoomAvatarUrl } from '../../utils/room'; import { getDirectRoomAvatarUrl, getRoomAvatarUrl, sendStateEvent } from '../../utils/room';
import { RoomAvatar, RoomIcon } from '../../components/room-avatar'; import { RoomAvatar, RoomIcon } from '../../components/room-avatar';
import { nameInitials } from '../../utils/common'; import { nameInitials } from '../../utils/common';
import { useMediaAuthentication } from '../../hooks/useMediaAuthentication'; import { useMediaAuthentication } from '../../hooks/useMediaAuthentication';
@@ -136,9 +136,10 @@ export function AddExistingModal({ parentId, space, requestClose }: AddExistingM
await rateLimitedActions(selectedRooms, async (room) => { await rateLimitedActions(selectedRooms, async (room) => {
const via = getViaServers(room); const via = getViaServers(room);
await mx.sendStateEvent( await sendStateEvent(
mx,
parentId, parentId,
StateEvent.SpaceChild as any, StateEvent.SpaceChild,
{ {
auto_join: false, auto_join: false,
suggested: false, suggested: false,
@@ -35,6 +35,7 @@ import { mxcUrlToHttp } from '../../../utils/matrix';
import { useMediaAuthentication } from '../../../hooks/useMediaAuthentication'; import { useMediaAuthentication } from '../../../hooks/useMediaAuthentication';
import { usePowerLevels } from '../../../hooks/usePowerLevels'; import { usePowerLevels } from '../../../hooks/usePowerLevels';
import { StateEvent } from '../../../../types/matrix/room'; import { StateEvent } from '../../../../types/matrix/room';
import { sendStateEvent } from '../../../utils/room';
import { suffixRename } from '../../../utils/common'; import { suffixRename } from '../../../utils/common';
import { AsyncStatus, useAsyncCallback } from '../../../hooks/useAsyncCallback'; import { AsyncStatus, useAsyncCallback } from '../../../hooks/useAsyncCallback';
import { useAlive } from '../../../hooks/useAlive'; import { useAlive } from '../../../hooks/useAlive';
@@ -57,7 +58,7 @@ function CreatePackTile({ packs, roomId }: CreatePackTileProps) {
display_name: name, display_name: name,
}, },
}; };
await mx.sendStateEvent(roomId, StateEvent.PoniesRoomEmotes as any, content, stateKey); await sendStateEvent(mx, roomId, StateEvent.PoniesRoomEmotes, content, stateKey);
}, },
[mx, roomId], [mx, roomId],
), ),
@@ -164,7 +165,7 @@ export function RoomPacks({ onViewPack }: RoomPacksProps) {
for (let i = 0; i < removedPacks.length; i += 1) { for (let i = 0; i < removedPacks.length; i += 1) {
const addr = removedPacks[i]; const addr = removedPacks[i];
// eslint-disable-next-line no-await-in-loop // eslint-disable-next-line no-await-in-loop
await mx.sendStateEvent(room.roomId, StateEvent.PoniesRoomEmotes as any, {}, addr.stateKey); await sendStateEvent(mx, room.roomId, StateEvent.PoniesRoomEmotes, {}, addr.stateKey);
} }
}, [mx, room, removedPacks]), }, [mx, room, removedPacks]),
); );
@@ -23,6 +23,7 @@ import { SequenceCardStyle } from '../../room-settings/styles.css';
import { SettingTile } from '../../../components/setting-tile'; import { SettingTile } from '../../../components/setting-tile';
import { useMatrixClient } from '../../../hooks/useMatrixClient'; import { useMatrixClient } from '../../../hooks/useMatrixClient';
import { StateEvent } from '../../../../types/matrix/room'; import { StateEvent } from '../../../../types/matrix/room';
import { sendStateEvent } from '../../../utils/room';
import { AsyncStatus, useAsyncCallback } from '../../../hooks/useAsyncCallback'; import { AsyncStatus, useAsyncCallback } from '../../../hooks/useAsyncCallback';
import { useRoom } from '../../../hooks/useRoom'; import { useRoom } from '../../../hooks/useRoom';
import { useStateEvent } from '../../../hooks/useStateEvent'; import { useStateEvent } from '../../../hooks/useStateEvent';
@@ -48,7 +49,7 @@ export function RoomEncryption({ permissions }: RoomEncryptionProps) {
const [enableState, enable] = useAsyncCallback( const [enableState, enable] = useAsyncCallback(
useCallback(async () => { useCallback(async () => {
await mx.sendStateEvent(room.roomId, StateEvent.RoomEncryption as any, { await sendStateEvent(mx, room.roomId, StateEvent.RoomEncryption, {
algorithm: ROOM_ENC_ALGO, algorithm: ROOM_ENC_ALGO,
}); });
}, [mx, room.roomId]), }, [mx, room.roomId]),
@@ -21,6 +21,7 @@ import { SettingTile } from '../../../components/setting-tile';
import { useMatrixClient } from '../../../hooks/useMatrixClient'; import { useMatrixClient } from '../../../hooks/useMatrixClient';
import { useRoom } from '../../../hooks/useRoom'; import { useRoom } from '../../../hooks/useRoom';
import { StateEvent } from '../../../../types/matrix/room'; import { StateEvent } from '../../../../types/matrix/room';
import { sendStateEvent } from '../../../utils/room';
import { AsyncStatus, useAsyncCallback } from '../../../hooks/useAsyncCallback'; import { AsyncStatus, useAsyncCallback } from '../../../hooks/useAsyncCallback';
import { useStateEvent } from '../../../hooks/useStateEvent'; import { useStateEvent } from '../../../hooks/useStateEvent';
import { stopPropagation } from '../../../utils/keyboard'; import { stopPropagation } from '../../../utils/keyboard';
@@ -76,7 +77,7 @@ export function RoomHistoryVisibility({ permissions }: RoomHistoryVisibilityProp
const content: RoomHistoryVisibilityEventContent = { const content: RoomHistoryVisibilityEventContent = {
history_visibility: visibility, history_visibility: visibility,
}; };
await mx.sendStateEvent(room.roomId, StateEvent.RoomHistoryVisibility as any, content); await sendStateEvent(mx, room.roomId, StateEvent.RoomHistoryVisibility, content);
}, },
[mx, room.roomId], [mx, room.roomId],
), ),
@@ -18,7 +18,7 @@ import { StateEvent } from '../../../../types/matrix/room';
import { useStateEvent } from '../../../hooks/useStateEvent'; import { useStateEvent } from '../../../hooks/useStateEvent';
import { useSpaceOptionally } from '../../../hooks/useSpace'; import { useSpaceOptionally } from '../../../hooks/useSpace';
import { AsyncStatus, useAsyncCallback } from '../../../hooks/useAsyncCallback'; import { AsyncStatus, useAsyncCallback } from '../../../hooks/useAsyncCallback';
import { getStateEvents } from '../../../utils/room'; import { getStateEvents, sendStateEvent } from '../../../utils/room';
import { import {
useRecursiveChildSpaceScopeFactory, useRecursiveChildSpaceScopeFactory,
useSpaceChildren, useSpaceChildren,
@@ -111,7 +111,7 @@ export function RoomJoinRules({ permissions }: RoomJoinRulesProps) {
join_rule: joinRule as JoinRule, join_rule: joinRule as JoinRule,
}; };
if (allow.length > 0) c.allow = allow; if (allow.length > 0) c.allow = allow;
await mx.sendStateEvent(room.roomId, StateEvent.RoomJoinRules as any, c); await sendStateEvent(mx, room.roomId, StateEvent.RoomJoinRules, c);
}, },
[mx, room, space, subspaces, roomIdToParents], [mx, room, space, subspaces, roomIdToParents],
), ),
@@ -39,6 +39,7 @@ import { mxcUrlToHttp } from '../../../utils/matrix';
import { useMatrixClient } from '../../../hooks/useMatrixClient'; import { useMatrixClient } from '../../../hooks/useMatrixClient';
import { useMediaAuthentication } from '../../../hooks/useMediaAuthentication'; import { useMediaAuthentication } from '../../../hooks/useMediaAuthentication';
import { StateEvent } from '../../../../types/matrix/room'; import { StateEvent } from '../../../../types/matrix/room';
import { sendStateEvent } from '../../../utils/room';
import { CompactUploadCardRenderer } from '../../../components/upload-card'; import { CompactUploadCardRenderer } from '../../../components/upload-card';
import { useObjectURL } from '../../../hooks/useObjectURL'; import { useObjectURL } from '../../../hooks/useObjectURL';
import { createUploadAtom, UploadSuccess } from '../../../state/upload'; import { createUploadAtom, UploadSuccess } from '../../../state/upload';
@@ -152,16 +153,16 @@ export function RoomProfileEdit({
useCallback( useCallback(
async (roomAvatarMxc?: string | null, roomName?: string, roomTopic?: string) => { async (roomAvatarMxc?: string | null, roomName?: string, roomTopic?: string) => {
if (roomAvatarMxc !== undefined) { if (roomAvatarMxc !== undefined) {
await mx.sendStateEvent(room.roomId, StateEvent.RoomAvatar as any, { await sendStateEvent(mx, room.roomId, StateEvent.RoomAvatar, {
url: roomAvatarMxc, url: roomAvatarMxc,
}); });
} }
if (roomName !== undefined) { if (roomName !== undefined) {
await mx.sendStateEvent(room.roomId, StateEvent.RoomName as any, { name: roomName }); await sendStateEvent(mx, room.roomId, StateEvent.RoomName, { name: roomName });
} }
if (roomTopic !== undefined) { if (roomTopic !== undefined) {
const topicContent = buildTopicContent(roomTopic); const topicContent = buildTopicContent(roomTopic);
await mx.sendStateEvent(room.roomId, StateEvent.RoomTopic as any, topicContent); await sendStateEvent(mx, room.roomId, StateEvent.RoomTopic, topicContent);
} }
}, },
[mx, room.roomId], [mx, room.roomId],
@@ -7,6 +7,7 @@ import { SettingsSelect } from '../../../components/settings-select/SettingsSele
import { useMatrixClient } from '../../../hooks/useMatrixClient'; import { useMatrixClient } from '../../../hooks/useMatrixClient';
import { useRoom } from '../../../hooks/useRoom'; import { useRoom } from '../../../hooks/useRoom';
import { StateEvent } from '../../../../types/matrix/room'; import { StateEvent } from '../../../../types/matrix/room';
import { sendStateEvent } from '../../../utils/room';
import { useStateEvent } from '../../../hooks/useStateEvent'; import { useStateEvent } from '../../../hooks/useStateEvent';
import { RoomPermissionsAPI } from '../../../hooks/useRoomPermissions'; import { RoomPermissionsAPI } from '../../../hooks/useRoomPermissions';
import { AsyncStatus, useAsyncCallback } from '../../../hooks/useAsyncCallback'; import { AsyncStatus, useAsyncCallback } from '../../../hooks/useAsyncCallback';
@@ -55,7 +56,7 @@ export function RoomQuality({ permissions }: RoomQualityProps) {
useCallback( useCallback(
async (next: RoomQualityContent) => { async (next: RoomQualityContent) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any // eslint-disable-next-line @typescript-eslint/no-explicit-any
await mx.sendStateEvent(room.roomId, StateEvent.LotusRoomQuality as any, next); await sendStateEvent(mx, room.roomId, StateEvent.LotusRoomQuality, next);
}, },
[mx, room.roomId], [mx, room.roomId],
), ),
@@ -7,6 +7,7 @@ import { SettingTile } from '../../../components/setting-tile';
import { useMatrixClient } from '../../../hooks/useMatrixClient'; import { useMatrixClient } from '../../../hooks/useMatrixClient';
import { useRoom } from '../../../hooks/useRoom'; import { useRoom } from '../../../hooks/useRoom';
import { StateEvent } from '../../../../types/matrix/room'; import { StateEvent } from '../../../../types/matrix/room';
import { sendStateEvent } from '../../../utils/room';
import { AsyncStatus, useAsyncCallback } from '../../../hooks/useAsyncCallback'; import { AsyncStatus, useAsyncCallback } from '../../../hooks/useAsyncCallback';
import { useStateEvent } from '../../../hooks/useStateEvent'; import { useStateEvent } from '../../../hooks/useStateEvent';
import { RoomPermissionsAPI } from '../../../hooks/useRoomPermissions'; import { RoomPermissionsAPI } from '../../../hooks/useRoomPermissions';
@@ -31,7 +32,7 @@ export function RoomRetention({ permissions }: RoomRetentionProps) {
// Lotus custom-state convention: cast the type key (RoomRetention isn't a // Lotus custom-state convention: cast the type key (RoomRetention isn't a
// typed key in the SDK's StateEvents map). // typed key in the SDK's StateEvents map).
// eslint-disable-next-line @typescript-eslint/no-explicit-any // eslint-disable-next-line @typescript-eslint/no-explicit-any
await mx.sendStateEvent(room.roomId, StateEvent.RoomRetention as any, content); await sendStateEvent(mx, room.roomId, StateEvent.RoomRetention, content);
}, },
[mx, room.roomId], [mx, room.roomId],
), ),
@@ -7,6 +7,7 @@ import { SettingTile } from '../../../components/setting-tile';
import { useMatrixClient } from '../../../hooks/useMatrixClient'; import { useMatrixClient } from '../../../hooks/useMatrixClient';
import { useRoom } from '../../../hooks/useRoom'; import { useRoom } from '../../../hooks/useRoom';
import { StateEvent } from '../../../../types/matrix/room'; import { StateEvent } from '../../../../types/matrix/room';
import { sendStateEvent } from '../../../utils/room';
import { AsyncStatus, useAsyncCallback } from '../../../hooks/useAsyncCallback'; import { AsyncStatus, useAsyncCallback } from '../../../hooks/useAsyncCallback';
import { useStateEvent } from '../../../hooks/useStateEvent'; import { useStateEvent } from '../../../hooks/useStateEvent';
import { RoomPermissionsAPI } from '../../../hooks/useRoomPermissions'; import { RoomPermissionsAPI } from '../../../hooks/useRoomPermissions';
@@ -31,7 +32,7 @@ export function RoomVoiceLimit({ permissions }: RoomVoiceLimitProps) {
useCallback( useCallback(
async (value: number) => { async (value: number) => {
const content: VoiceLimitContent = value > 0 ? { max_users: value } : {}; const content: VoiceLimitContent = value > 0 ? { max_users: value } : {};
await mx.sendStateEvent(room.roomId, StateEvent.LotusVoiceLimit as any, content); await sendStateEvent(mx, room.roomId, StateEvent.LotusVoiceLimit, content);
}, },
[mx, room.roomId], [mx, room.roomId],
), ),
@@ -15,6 +15,7 @@ import { getPowerLevelTag, getPowers, usePowerLevelTags } from '../../../hooks/u
import { useRoom } from '../../../hooks/useRoom'; import { useRoom } from '../../../hooks/useRoom';
import { useMatrixClient } from '../../../hooks/useMatrixClient'; import { useMatrixClient } from '../../../hooks/useMatrixClient';
import { StateEvent } from '../../../../types/matrix/room'; import { StateEvent } from '../../../../types/matrix/room';
import { sendStateEvent } from '../../../utils/room';
import { PowerSwitcher } from '../../../components/power'; import { PowerSwitcher } from '../../../components/power';
import { AsyncStatus, useAsyncCallback } from '../../../hooks/useAsyncCallback'; import { AsyncStatus, useAsyncCallback } from '../../../hooks/useAsyncCallback';
import { useAlive } from '../../../hooks/useAlive'; import { useAlive } from '../../../hooks/useAlive';
@@ -84,7 +85,7 @@ export function PermissionGroups({
return draftPowerLevels; return draftPowerLevels;
}); });
await mx.sendStateEvent(room.roomId, StateEvent.RoomPowerLevels as any, editedPowerLevels); await sendStateEvent(mx, room.roomId, StateEvent.RoomPowerLevels, editedPowerLevels);
}, [mx, room, powerLevels, permissionUpdate, permissionGroups]), }, [mx, room, powerLevels, permissionUpdate, permissionGroups]),
); );
@@ -45,6 +45,7 @@ import { CompactUploadCardRenderer } from '../../../components/upload-card';
import { createUploadAtom, UploadSuccess } from '../../../state/upload'; import { createUploadAtom, UploadSuccess } from '../../../state/upload';
import { AsyncStatus, useAsyncCallback } from '../../../hooks/useAsyncCallback'; import { AsyncStatus, useAsyncCallback } from '../../../hooks/useAsyncCallback';
import { MemberPowerTag, MemberPowerTagIcon, StateEvent } from '../../../../types/matrix/room'; import { MemberPowerTag, MemberPowerTagIcon, StateEvent } from '../../../../types/matrix/room';
import { sendStateEvent } from '../../../utils/room';
import { useAlive } from '../../../hooks/useAlive'; import { useAlive } from '../../../hooks/useAlive';
import { BetaNoticeBadge } from '../../../components/BetaNoticeBadge'; import { BetaNoticeBadge } from '../../../components/BetaNoticeBadge';
import { getPowerTagIconSrc } from '../../../hooks/useMemberPowerTag'; import { getPowerTagIconSrc } from '../../../hooks/useMemberPowerTag';
@@ -335,7 +336,7 @@ export function PowersEditor({ powerLevels, requestClose }: PowersEditorProps) {
deleted.forEach((power) => { deleted.forEach((power) => {
delete content[power]; delete content[power];
}); });
await mx.sendStateEvent(room.roomId, StateEvent.PowerLevelTags as any, content); await sendStateEvent(mx, room.roomId, StateEvent.PowerLevelTags, content);
}, [mx, room, powerLevelTags, editedPowerTags, deleted]), }, [mx, room, powerLevelTags, editedPowerTags, deleted]),
); );
+3 -2
View File
@@ -18,6 +18,7 @@ import {
import { HierarchyItem } from '../../hooks/useSpaceHierarchy'; import { HierarchyItem } from '../../hooks/useSpaceHierarchy';
import { useMatrixClient } from '../../hooks/useMatrixClient'; import { useMatrixClient } from '../../hooks/useMatrixClient';
import { MSpaceChildContent, StateEvent } from '../../../types/matrix/room'; import { MSpaceChildContent, StateEvent } from '../../../types/matrix/room';
import { sendStateEvent } from '../../utils/room';
import { AsyncStatus, useAsyncCallback } from '../../hooks/useAsyncCallback'; import { AsyncStatus, useAsyncCallback } from '../../hooks/useAsyncCallback';
import { UseStateProvider } from '../../components/UseStateProvider'; import { UseStateProvider } from '../../components/UseStateProvider';
import { LeaveSpacePrompt } from '../../components/leave-space-prompt'; import { LeaveSpacePrompt } from '../../components/leave-space-prompt';
@@ -48,7 +49,7 @@ function SuggestMenuItem({
const [toggleState, handleToggleSuggested] = useAsyncCallback( const [toggleState, handleToggleSuggested] = useAsyncCallback(
useCallback(() => { useCallback(() => {
const newContent: MSpaceChildContent = { ...content, suggested: !content.suggested }; const newContent: MSpaceChildContent = { ...content, suggested: !content.suggested };
return mx.sendStateEvent(parentId, StateEvent.SpaceChild as any, newContent, roomId); return sendStateEvent(mx, parentId, StateEvent.SpaceChild, newContent, roomId);
}, [mx, parentId, roomId, content]), }, [mx, parentId, roomId, content]),
); );
@@ -85,7 +86,7 @@ function RemoveMenuItem({
const [removeState, handleRemove] = useAsyncCallback( const [removeState, handleRemove] = useAsyncCallback(
useCallback( useCallback(
() => mx.sendStateEvent(parentId, StateEvent.SpaceChild as any, {}, roomId), () => sendStateEvent(mx, parentId, StateEvent.SpaceChild, {}, roomId),
[mx, parentId, roomId], [mx, parentId, roomId],
), ),
); );
+9 -7
View File
@@ -40,7 +40,7 @@ import { getSpaceRoomPath, withSearchParam } from '../../pages/pathUtils';
import { StateEvent } from '../../../types/matrix/room'; import { StateEvent } from '../../../types/matrix/room';
import { CanDropCallback, useDnDMonitor } from './DnD'; import { CanDropCallback, useDnDMonitor } from './DnD';
import { ASCIILexicalTable, orderKeys } from '../../utils/ASCIILexicalTable'; import { ASCIILexicalTable, orderKeys } from '../../utils/ASCIILexicalTable';
import { getStateEvent } from '../../utils/room'; import { getStateEvent, sendStateEvent } from '../../utils/room';
import { setAccountData } from '../../utils/accountData'; import { setAccountData } from '../../utils/accountData';
import { useClosedLobbyCategoriesAtom } from '../../state/hooks/closedLobbyCategories'; import { useClosedLobbyCategoriesAtom } from '../../state/hooks/closedLobbyCategories';
import { import {
@@ -272,9 +272,10 @@ export function Lobby() {
if (reorders) { if (reorders) {
await rateLimitedActions(reorders, async (reorder) => { await rateLimitedActions(reorders, async (reorder) => {
if (!reorder.item.parentId) return; if (!reorder.item.parentId) return;
await mx.sendStateEvent( await sendStateEvent(
mx,
reorder.item.parentId, reorder.item.parentId,
StateEvent.SpaceChild as any, StateEvent.SpaceChild,
{ ...reorder.item.content, order: reorder.orderKey }, { ...reorder.item.content, order: reorder.orderKey },
reorder.item.roomId, reorder.item.roomId,
); );
@@ -299,7 +300,7 @@ export function Lobby() {
// remove from current space // remove from current space
if (item.parentId !== containerParentId) { if (item.parentId !== containerParentId) {
mx.sendStateEvent(item.parentId, StateEvent.SpaceChild as any, {}, item.roomId); sendStateEvent(mx, item.parentId, StateEvent.SpaceChild, {}, item.roomId);
} }
if ( if (
@@ -319,7 +320,7 @@ export function Lobby() {
joinRuleContent.allow?.filter((allowRule) => allowRule.room_id !== item.parentId) ?? joinRuleContent.allow?.filter((allowRule) => allowRule.room_id !== item.parentId) ??
[]; [];
allow.push({ type: RestrictedAllowType.RoomMembership, room_id: containerParentId }); allow.push({ type: RestrictedAllowType.RoomMembership, room_id: containerParentId });
mx.sendStateEvent(itemRoom.roomId, StateEvent.RoomJoinRules as any, { sendStateEvent(mx, itemRoom.roomId, StateEvent.RoomJoinRules, {
...joinRuleContent, ...joinRuleContent,
allow, allow,
}); });
@@ -359,9 +360,10 @@ export function Lobby() {
if (reorders) { if (reorders) {
await rateLimitedActions(reorders, async (reorder) => { await rateLimitedActions(reorders, async (reorder) => {
await mx.sendStateEvent( await sendStateEvent(
mx,
containerParentId, containerParentId,
StateEvent.SpaceChild as any, StateEvent.SpaceChild,
{ ...reorder.item.content, order: reorder.orderKey }, { ...reorder.item.content, order: reorder.orderKey },
reorder.item.roomId, reorder.item.roomId,
); );
@@ -24,6 +24,7 @@ import { useMatrixClient } from '../../hooks/useMatrixClient';
import { useRoom } from '../../hooks/useRoom'; import { useRoom } from '../../hooks/useRoom';
import { useStateEvent } from '../../hooks/useStateEvent'; import { useStateEvent } from '../../hooks/useStateEvent';
import { StateEvent } from '../../../types/matrix/room'; import { StateEvent } from '../../../types/matrix/room';
import { sendStateEvent } from '../../utils/room';
import { usePowerLevels, readPowerLevel } from '../../hooks/usePowerLevels'; import { usePowerLevels, readPowerLevel } from '../../hooks/usePowerLevels';
import { useRoomCreators } from '../../hooks/useRoomCreators'; import { useRoomCreators } from '../../hooks/useRoomCreators';
import { useRoomPermissions } from '../../hooks/useRoomPermissions'; import { useRoomPermissions } from '../../hooks/useRoomPermissions';
@@ -250,7 +251,7 @@ export function RoomServerACL({ requestClose }: RoomServerACLProps) {
// Save handler // Save handler
const [saveState, save] = useAsyncCallback( const [saveState, save] = useAsyncCallback(
useCallback(async () => { useCallback(async () => {
await mx.sendStateEvent(room.roomId, StateEvent.RoomServerAcl as any, { await sendStateEvent(mx, room.roomId, StateEvent.RoomServerAcl, {
allow: allowList, allow: allowList,
deny: denyList, deny: denyList,
allow_ip_literals: allowIpLiterals, allow_ip_literals: allowIpLiterals,
+2 -1
View File
@@ -54,6 +54,7 @@ import {
getEventEdits, getEventEdits,
getMemberAvatarMxc, getMemberAvatarMxc,
getMemberName, getMemberName,
sendStateEvent,
} from '../../../utils/room'; } from '../../../utils/room';
import { mxcUrlToHttp } from '../../../utils/matrix'; import { mxcUrlToHttp } from '../../../utils/matrix';
import { messageAriaLabel } from '../../../utils/a11y'; import { messageAriaLabel } from '../../../utils/a11y';
@@ -428,7 +429,7 @@ export const MessagePinItem = as<
if (!isPinned && eventId) { if (!isPinned && eventId) {
pinContent.pinned.push(eventId); pinContent.pinned.push(eventId);
} }
mx.sendStateEvent(room.roomId, StateEvent.RoomPinnedEvents as any, pinContent); sendStateEvent(mx, room.roomId, StateEvent.RoomPinnedEvents, pinContent);
onClose?.(); onClose?.();
}; };
@@ -47,6 +47,7 @@ import {
getMemberAvatarMxc, getMemberAvatarMxc,
getMemberName, getMemberName,
getStateEvent, getStateEvent,
sendStateEvent,
} from '../../../utils/room'; } from '../../../utils/room';
import { GetContentCallback, MessageEvent, StateEvent } from '../../../../types/matrix/room'; import { GetContentCallback, MessageEvent, StateEvent } from '../../../../types/matrix/room';
import { useMentionClickHandler } from '../../../hooks/useMentionClickHandler'; import { useMentionClickHandler } from '../../../hooks/useMentionClickHandler';
@@ -122,7 +123,7 @@ function PinnedMessage({
pinned: content.pinned.filter((id) => id !== eventId), pinned: content.pinned.filter((id) => id !== eventId),
}; };
return mx.sendStateEvent(room.roomId, StateEvent.RoomPinnedEvents as any, newContent); return sendStateEvent(mx, room.roomId, StateEvent.RoomPinnedEvents, newContent);
}, [room, eventId, mx]), }, [room, eventId, mx]),
); );
@@ -26,6 +26,7 @@ import { usePowerLevelsContext } from '../../../hooks/usePowerLevels';
import { useRoomCreators } from '../../../hooks/useRoomCreators'; import { useRoomCreators } from '../../../hooks/useRoomCreators';
import { useRoomPermissions } from '../../../hooks/useRoomPermissions'; import { useRoomPermissions } from '../../../hooks/useRoomPermissions';
import { StateEvent } from '../../../../types/matrix/room'; import { StateEvent } from '../../../../types/matrix/room';
import { sendStateEvent } from '../../../utils/room';
import { generateWidgetId, validateWidgetUrl, WidgetUrlError } from './widgetUtils'; import { generateWidgetId, validateWidgetUrl, WidgetUrlError } from './widgetUtils';
const urlErrorMessage = (err: WidgetUrlError): string => { const urlErrorMessage = (err: WidgetUrlError): string => {
@@ -84,7 +85,7 @@ export function WidgetsPanel({ room, requestClose }: WidgetsPanelProps) {
}; };
try { try {
// eslint-disable-next-line @typescript-eslint/no-explicit-any // eslint-disable-next-line @typescript-eslint/no-explicit-any
await mx.sendStateEvent(room.roomId, StateEvent.Widget as any, content as any, id); await sendStateEvent(mx, room.roomId, StateEvent.Widget, content, id);
setAdding(false); setAdding(false);
} catch (e) { } catch (e) {
setError((e as Error).message); setError((e as Error).message);
@@ -96,7 +97,7 @@ export function WidgetsPanel({ room, requestClose }: WidgetsPanelProps) {
const handleRemove = (id: string) => { const handleRemove = (id: string) => {
if (viewingId === id) setViewingId(null); if (viewingId === id) setViewingId(null);
// eslint-disable-next-line @typescript-eslint/no-explicit-any // eslint-disable-next-line @typescript-eslint/no-explicit-any
mx.sendStateEvent(room.roomId, StateEvent.Widget as any, {} as any, id).catch(() => undefined); sendStateEvent(mx, room.roomId, StateEvent.Widget, {}, id).catch(() => undefined);
}; };
return ( return (
+8 -6
View File
@@ -24,7 +24,7 @@ import {
} from '../utils/matrix'; } from '../utils/matrix';
import { useRoomNavigate } from './useRoomNavigate'; import { useRoomNavigate } from './useRoomNavigate';
import { Membership, StateEvent } from '../../types/matrix/room'; import { Membership, StateEvent } from '../../types/matrix/room';
import { getStateEvent } from '../utils/room'; import { getStateEvent, sendStateEvent } from '../utils/room';
import { splitWithSpace } from '../utils/common'; import { splitWithSpace } from '../utils/common';
import { createRoomEncryptionState } from '../components/create-room'; import { createRoomEncryptionState } from '../components/create-room';
@@ -368,9 +368,10 @@ export const useCommands = (mx: MatrixClient, room: Room): CommandRecord => {
?.getStateEvents(StateEvent.RoomMember, mx.getSafeUserId()); ?.getStateEvents(StateEvent.RoomMember, mx.getSafeUserId());
const content = mEvent?.getContent(); const content = mEvent?.getContent();
if (!content) return; if (!content) return;
await mx.sendStateEvent( await sendStateEvent(
mx,
room.roomId, room.roomId,
StateEvent.RoomMember as any, StateEvent.RoomMember,
{ {
...content, ...content,
displayname: nick, displayname: nick,
@@ -390,9 +391,10 @@ export const useCommands = (mx: MatrixClient, room: Room): CommandRecord => {
?.getStateEvents(StateEvent.RoomMember, mx.getSafeUserId()); ?.getStateEvents(StateEvent.RoomMember, mx.getSafeUserId());
const content = mEvent?.getContent(); const content = mEvent?.getContent();
if (!content) return; if (!content) return;
await mx.sendStateEvent( await sendStateEvent(
mx,
room.roomId, room.roomId,
StateEvent.RoomMember as any, StateEvent.RoomMember,
{ {
...content, ...content,
avatar_url: payload, avatar_url: payload,
@@ -532,7 +534,7 @@ export const useCommands = (mx: MatrixClient, room: Room): CommandRecord => {
aclContent.allow?.sort(); aclContent.allow?.sort();
aclContent.deny?.sort(); aclContent.deny?.sort();
await mx.sendStateEvent(room.roomId, StateEvent.RoomServerAcl as any, aclContent); await sendStateEvent(mx, room.roomId, StateEvent.RoomServerAcl, aclContent);
}, },
}, },
}), }),
+4 -4
View File
@@ -6,7 +6,7 @@ import { useMatrixClient } from './useMatrixClient';
import { useAlive } from './useAlive'; import { useAlive } from './useAlive';
import { useStateEvent } from './useStateEvent'; import { useStateEvent } from './useStateEvent';
import { StateEvent } from '../../types/matrix/room'; import { StateEvent } from '../../types/matrix/room';
import { getStateEvent } from '../utils/room'; import { getStateEvent, sendStateEvent } from '../utils/room';
export const usePublishedAliases = (room: Room): [string | undefined, string[]] => { export const usePublishedAliases = (room: Room): [string | undefined, string[]] => {
const aliasContent = useStateEvent( const aliasContent = useStateEvent(
@@ -56,7 +56,7 @@ export const useSetMainAlias = (room: Room): ((alias: string | undefined) => Pro
alt_aliases: altAliases, alt_aliases: altAliases,
}; };
await mx.sendStateEvent(room.roomId, StateEvent.RoomCanonicalAlias as any, newContent); await sendStateEvent(mx, room.roomId, StateEvent.RoomCanonicalAlias, newContent);
}, },
[mx, room], [mx, room],
); );
@@ -90,7 +90,7 @@ export const usePublishUnpublishAliases = (
alt_aliases: altAliases, alt_aliases: altAliases,
}; };
await mx.sendStateEvent(room.roomId, StateEvent.RoomCanonicalAlias as any, newContent); await sendStateEvent(mx, room.roomId, StateEvent.RoomCanonicalAlias, newContent);
}, },
[mx, room], [mx, room],
); );
@@ -114,7 +114,7 @@ export const usePublishUnpublishAliases = (
alt_aliases: altAliases, alt_aliases: altAliases,
}; };
await mx.sendStateEvent(room.roomId, StateEvent.RoomCanonicalAlias as any, newContent); await sendStateEvent(mx, room.roomId, StateEvent.RoomCanonicalAlias, newContent);
}, },
[mx, room], [mx, room],
); );
+19
View File
@@ -8,6 +8,7 @@ import {
IPowerLevelsContent, IPowerLevelsContent,
IPushRule, IPushRule,
IPushRules, IPushRules,
ISendEventResponse,
JoinRule, JoinRule,
MatrixClient, MatrixClient,
MatrixEvent, MatrixEvent,
@@ -43,6 +44,24 @@ export const getStateEvent = (
export const getStateEvents = (room: Room, eventType: StateEvent): MatrixEvent[] => export const getStateEvents = (room: Room, eventType: StateEvent): MatrixEvent[] =>
room.getLiveTimeline().getState(EventTimeline.FORWARDS)?.getStateEvents(eventType) ?? []; room.getLiveTimeline().getState(EventTimeline.FORWARDS)?.getStateEvents(eventType) ?? [];
// Typed state-event write helper.
//
// matrix-js-sdk's `sendStateEvent` typed overload rejects the fork's custom
// `StateEvent` enum values (the `io.lotus.*` / `im.ponies.*` / etc. values in
// `types/matrix/room.ts`), and casting the event type to `any` at each call site
// also collapses the `content` argument to `any`. We centralize the single
// `as any` cast here so every call site keeps its own `content` type checked.
export function sendStateEvent<T extends object>(
mx: MatrixClient,
roomId: string,
eventType: StateEvent,
content: T,
stateKey = '',
): Promise<ISendEventResponse> {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return mx.sendStateEvent(roomId, eventType as any, content, stateKey);
}
export const getAccountData = ( export const getAccountData = (
mx: MatrixClient, mx: MatrixClient,
eventType: AccountDataEvent | string, eventType: AccountDataEvent | string,