Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions packages/federation-sdk/src/services/state.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,12 +210,6 @@ export class StateService {

return events.concat(eventsFromStore);
},

getEventsByHashes: async (
_hashes: string[],
): Promise<PersistentEventBase[]> => {
throw new Error('Not implemented');
},
};
}

Expand Down
12 changes: 6 additions & 6 deletions packages/room/src/authorizartion-rules/rules.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { it, describe, expect, afterEach } from 'bun:test';
import { PersistentEventBase } from '../manager/event-wrapper';
import { PersistentEventFactory } from '../manager/factory';
import { type PduJoinRuleEventContent, type PduType } from '../types/v1';
import { Pdu, PduContent, type PduType } from '../types/v3-11';
import { checkEventAuthWithoutState, checkEventAuthWithState } from './rules';
import type { EventStore } from '../state_resolution/definitions/definitions';
import type { PduPowerLevelsEventV10Content, PduV10 } from '../types/v10';
import { type StateMapKey } from '../types/_common';

function getStateMapKey(event: PersistentEventBase): StateMapKey {
Expand All @@ -28,7 +27,7 @@ class MockStore implements EventStore {
}

class FakeStateEventCreator {
protected _event!: PduV10;
protected _event!: Pdu;
constructor() {
this._event = {
state_key: '', // always a state
Expand All @@ -38,7 +37,7 @@ class FakeStateEventCreator {
prev_events: [],
room_id: '',
sender: '',
} as unknown as PduV10;
} as unknown as Pdu;
}

withStateKey(stateKey: string) {
Expand All @@ -47,7 +46,8 @@ class FakeStateEventCreator {
}

withType(type: PduType | 'test') {
this._event.type = type as PduType;
// @ts-ignore breaking due to type and content conflict, doesn't matter here
this._event.type = type;
return this;
}

Expand Down Expand Up @@ -772,7 +772,7 @@ describe('authorization rules', () => {
state.set(powerLevel.getUniqueStateIdentifier(), powerLevel);
};

const createPowerLevel = (content: PduPowerLevelsEventV10Content) => {
const createPowerLevel = (content: PduContent) => {
return new FakeStateEventCreator()
.asPowerLevel()
.withRoomId(roomId)
Expand Down
2 changes: 1 addition & 1 deletion packages/room/src/authorizartion-rules/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
type PduMembershipEventContent,
type PduPowerLevelsEventContent,
type PduType,
} from '../types/v1';
} from '../types/v3-11';

import {
getStateMapKey,
Expand Down
3 changes: 1 addition & 2 deletions packages/room/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@ export * from './manager/event-wrapper';
export * from './manager/type';
export * from './state_resolution/definitions/definitions';

export * from './types/v1';
export * from './types/v3';
export * from './types/v3-11';
export * from './types/_common';
19 changes: 1 addition & 18 deletions packages/room/src/manager/event-wrapper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ describe('[EventWrapper] Redaction', () => {
signatures: {},
unsigned: {},
};
// @ts-expect-error our types are production types
runTest(a, b);
}
});
Expand Down Expand Up @@ -319,24 +320,6 @@ describe('[EventWrapper] Redaction', () => {
});

it('member, Member events have changed behavior in MSC3375 and MSC3821.', () => {
const a = {
type: 'm.room.member',
event_id: '$test:domain',
content: {
membership: 'join',
join_authorised_via_users_server: '@user:domain',
other_key: 'stripped',
},
};
const b = {
type: 'm.room.member',
event_id: '$test:domain',
content: { membership: 'join' },
signatures: {},
unsigned: {},
};
// @ts-expect-error just redactions
runTest(a, b, '1');
const a2 = {
type: 'm.room.member',
content: {
Expand Down
37 changes: 14 additions & 23 deletions packages/room/src/manager/event-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,28 @@ import {
PduTypeRoomJoinRules,
PduTypeRoomMember,
PduTypeRoomPowerLevels,
type PduV1,
type PduMembershipEventContent,
type PduJoinRuleEventContent,
Signature,
PduV1Content,
PduType,
} from '../types/v1';
import type { PduV3 } from '../types/v3';
Pdu,
PduContent,
} from '../types/v3-11';
import crypto from 'node:crypto';
import {
getStateMapKey,
type EventStore,
} from '../state_resolution/definitions/definitions';
import { PowerLevelEvent } from './power-level-event-wrapper';
import { PduVersionForRoomVersion, RoomVersion } from './type';
import { type RoomVersion } from './type';

function extractDomain(identifier: string) {
return identifier.split(':').pop();
}

type AnyPdu = PduV1 | PduV3;

type AnyPduBareMinimum<T extends AnyPdu> = Omit<T, 'hashes' | 'signatures'> & {
// TODO: decide on whether v1 needs to be in code or not, this helps a lot with typing
auth_events: string[];
prev_events: string[];
// ----
hashes?: T['hashes'];
signatures?: T['signatures'];
type PduWithHashesAndSignaturesOptional = Omit<Pdu, 'hashes' | 'signatures'> & {
hashes?: Pdu['hashes'];
signatures?: Pdu['signatures'];
};

export const REDACT_ALLOW_ALL_KEYS: unique symbol = Symbol.for('all');
Expand All @@ -46,7 +39,7 @@ export abstract class PersistentEventBase<T extends RoomVersion = '11'> {
private signatures: Signature = {};

constructor(
protected rawEvent: AnyPduBareMinimum<PduVersionForRoomVersion<T>>,
protected rawEvent: PduWithHashesAndSignaturesOptional,
freeze = false,
) {
if (freeze) {
Expand Down Expand Up @@ -128,7 +121,7 @@ export abstract class PersistentEventBase<T extends RoomVersion = '11'> {
// v1 should have this already, others, generates it
abstract get eventId(): string;

getContent<T extends (PduV1 | PduV3)['content']>(): T {
getContent<T extends PduContent>(): T {
return this.rawEvent.content as T;
}

Expand Down Expand Up @@ -194,16 +187,14 @@ export abstract class PersistentEventBase<T extends RoomVersion = '11'> {
string[] | typeof REDACT_ALLOW_ALL_KEYS
>;

private _getRedactedEvent(
event: AnyPduBareMinimum<PduVersionForRoomVersion<T>>,
) {
type KeysExceptContent = Exclude<keyof AnyPdu, 'content'>;
private _getRedactedEvent(event: PduWithHashesAndSignaturesOptional) {
type KeysExceptContent = Exclude<keyof Pdu, 'content'>;

// it is expected to have everything in this event ready by this point
const topLevelAllowedKeysExceptContent =
this.getAllowedKeys() as KeysExceptContent[];

const dict = {} as Record<KeysExceptContent, AnyPdu[KeysExceptContent]>;
const dict = {} as Record<KeysExceptContent, Pdu[KeysExceptContent]>;

for (const key of topLevelAllowedKeysExceptContent) {
if (key in event) {
Expand All @@ -213,11 +204,11 @@ export abstract class PersistentEventBase<T extends RoomVersion = '11'> {

const currentContent = this.getContent();

let newContent = {} as Partial<PduV1Content>;
let newContent = {} as Partial<PduContent>;

// m.room.member allows keys membership, join_authorised_via_users_server. Additionally, it allows the signed key of the third_party_invite key.
const allowedContentKeys = this.getAllowedContentKeys()[this.type] as
| (keyof PduV1Content)[]
| (keyof PduContent)[]
| typeof REDACT_ALLOW_ALL_KEYS;

if (allowedContentKeys) {
Expand Down
34 changes: 16 additions & 18 deletions packages/room/src/manager/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,23 @@ import {
PduTypeRoomMember,
PduTypeRoomPowerLevels,
type PduCreateEventContent,
type PduV1,
PduTypeRoomName,
type PduRoomNameEventContent,
PduTypeRoomJoinRules,
type PduJoinRuleEventContent,
PduPowerLevelsEventContent,
} from '../types/v1';
import type { PduV3 } from '../types/v3';
Pdu,
} from '../types/v3-11';

import { PersistentEventV1 } from './v1';
import { PersistentEventV3 } from './v3';

import type { PduVersionForRoomVersion, RoomVersion } from './type';
import type { RoomVersion } from './type';
import type { PersistentEventBase } from './event-wrapper';
import { PersistentEventV6 } from './v6';
import { PersistentEventV8 } from './v8';
import { PersistentEventV9 } from './v9';
import { PersistentEventV11 } from './v11';
import { PduV3ForType } from '../types/_common';
import { PduForType } from '../types/_common';

// Utility function to create a random ID for room creation
function createRoomIdPrefix(length: number) {
Expand Down Expand Up @@ -53,18 +51,18 @@ export class PersistentEventFactory {
return PersistentEventFactory.supportedRoomVersions.includes(roomVersion);
}

static createFromRawEvent<T extends RoomVersion>(
rawEvent: Omit<PduVersionForRoomVersion<T>, 'signatures' | 'hashes'> & {
signatures?: PduVersionForRoomVersion<T>['signatures'];
hashes?: PduVersionForRoomVersion<T>['hashes'];
static createFromRawEvent(
rawEvent: Omit<Pdu, 'signatures' | 'hashes'> & {
signatures?: Pdu['signatures'];
hashes?: Pdu['hashes'];
},
roomVersion: RoomVersion,
): PersistentEventBase<RoomVersion> {
if (roomVersion === '1' || roomVersion === '2') {
return new PersistentEventV1(rawEvent as any, false);
if (!PersistentEventFactory.isSupportedRoomVersion(roomVersion)) {
throw new Error(`Room version ${roomVersion} is not supported`);
}

const event = rawEvent as PduV3;
const event = rawEvent as Pdu;

switch (roomVersion) {
case '3':
Expand Down Expand Up @@ -104,7 +102,7 @@ export class PersistentEventFactory {
const roomId = `!${createRoomIdPrefix(8)}:${domain}`;

const eventPartial: Omit<
PduV3ForType<typeof PduTypeRoomCreate>,
PduForType<typeof PduTypeRoomCreate>,
'signatures' | 'hashes'
> = {
type: PduTypeRoomCreate,
Expand Down Expand Up @@ -152,7 +150,7 @@ export class PersistentEventFactory {
};

const eventPartial: Omit<
PduV3ForType<typeof PduTypeRoomMember>,
PduForType<typeof PduTypeRoomMember>,
'signatures' | 'hashes'
> = {
type: PduTypeRoomMember,
Expand Down Expand Up @@ -180,7 +178,7 @@ export class PersistentEventFactory {
}

const eventPartial: Omit<
PduV3ForType<typeof PduTypeRoomPowerLevels>,
PduForType<typeof PduTypeRoomPowerLevels>,
'signatures' | 'hashes'
> = {
type: PduTypeRoomPowerLevels,
Expand Down Expand Up @@ -208,7 +206,7 @@ export class PersistentEventFactory {
}

const eventPartial: Omit<
PduV3ForType<typeof PduTypeRoomName>,
PduForType<typeof PduTypeRoomName>,
'signatures' | 'hashes'
> = {
type: PduTypeRoomName,
Expand Down Expand Up @@ -237,7 +235,7 @@ export class PersistentEventFactory {
}

const eventPartial: Omit<
PduV3ForType<typeof PduTypeRoomJoinRules>,
PduForType<typeof PduTypeRoomJoinRules>,
'signatures' | 'hashes'
> = {
type: PduTypeRoomJoinRules,
Expand Down
2 changes: 1 addition & 1 deletion packages/room/src/manager/power-level-event-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
PduTypeRoomMessage,
type PduPowerLevelsEventContent,
type PduType,
} from '../types/v1';
} from '../types/v3-11';
import { PersistentEventBase } from './event-wrapper';

// centralize all power level values here
Expand Down
10 changes: 0 additions & 10 deletions packages/room/src/manager/type.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import type { PduV1 } from '../types/v1';
import type { PduV3 } from '../types/v3';

export type RoomVersion1And2 = '1' | '2';

export type RoomVersion3To11 =
Expand All @@ -15,10 +12,3 @@ export type RoomVersion3To11 =
| '11';

export type RoomVersion = RoomVersion1And2 | RoomVersion3To11;

export type PduVersionForRoomVersion<T extends RoomVersion> =
T extends RoomVersion1And2
? PduV1
: T extends RoomVersion3To11
? PduV3
: never;
Loading