Skip to content

Commit

Permalink
Add safe membership event parsing by default
Browse files Browse the repository at this point in the history
  • Loading branch information
Gnuxie committed Apr 8, 2024
1 parent 88415be commit 103edc1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
24 changes: 10 additions & 14 deletions src/MatrixTypes/MembershipEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
import { Static, StaticDecode, Type } from '@sinclair/typebox';
import { StateEvent, StrippedStateEvent, UnsignedData } from './Events';
import { StringUserID } from './StringlyTypedMatrix';
import { registerDefaultDecoder } from './EventDecoder';
import { Value } from '../Interface/Value';

export type MembershipEventUnsigned = Static<typeof MembershipEventUnsigned>;
export const MembershipEventUnsigned = Type.Composite([
Expand Down Expand Up @@ -93,22 +91,20 @@ export const MembershipEventContent = Type.Object({
),
});

export type MembershipEvent = StaticDecode<typeof MembershipEvent>;
export const MembershipEvent = Type.Composite([
Type.Omit(StateEvent(MembershipEventContent), [
'content',
'state_key',
'unsigned',
'type',
]),
export type BaseMembershipEvent = StaticDecode<typeof BaseMembershipEvent>;
export const BaseMembershipEvent = Type.Composite([
Type.Omit(StateEvent(Type.Object({})), ['state_key', 'unsigned', 'type']),
Type.Object({
content: MembershipEventContent,
state_key: StringUserID,
type: Type.Literal('m.room.member'),
unsigned: Type.Optional(MembershipEventUnsigned),
}),
]);

registerDefaultDecoder('m.room.member', (event) =>
Value.Decode(MembershipEvent, event)
);
export type MembershipEvent = StaticDecode<typeof MembershipEvent>;
export const MembershipEvent = Type.Composite([
Type.Omit(BaseMembershipEvent, ['content']),
Type.Object({
content: MembershipEventContent,
}),
]);
24 changes: 22 additions & 2 deletions src/SafeMatrixEvents/SafeMembershipEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
//
// SPDX-License-Identifier: AFL-3.0

import { ActionResult, Ok, isOk } from '../Interface/Action';
import { ActionResult, Ok, isError, isOk } from '../Interface/Action';
import { DecodeException, Value } from '../Interface/Value';
import { MembershipEventContent } from '../MatrixTypes/MembershipEvent';
import { registerDefaultDecoder } from '../MatrixTypes/EventDecoder';
import {
BaseMembershipEvent,
MembershipEventContent,
} from '../MatrixTypes/MembershipEvent';
import { ValuePointer } from '@sinclair/typebox/value';

/**
Expand Down Expand Up @@ -94,3 +98,19 @@ export const SafeMembershipEventMirror = Object.freeze({
});

export type SafeMembershipEventMirror = typeof SafeMembershipEventMirror;

registerDefaultDecoder('m.room.member', (event) => {
const baseEventResult = Value.Decode(BaseMembershipEvent, event);
if (isError(baseEventResult)) {
return baseEventResult;
}
const safeContentResult = SafeMembershipEventMirror.parse(
baseEventResult.ok.content
);
if (isError(safeContentResult)) {
return safeContentResult;
}
const completeEvent = baseEventResult.ok;
completeEvent.content = safeContentResult.ok;
return Ok(completeEvent);
});

0 comments on commit 103edc1

Please sign in to comment.