-
Notifications
You must be signed in to change notification settings - Fork 1
/
Events.ts
103 lines (94 loc) · 3.96 KB
/
Events.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright 2023 Gnuxie <Gnuxie@protonmail.com>
// Copyright 2018 New Vector Ltd
//
// SPDX-License-Identifier: AFL-3.0 AND Apache-2.0
//
// SPDX-FileAttributionText: <text>
// This modified file incorporates work from matrix-spec
// https://github.com/matrix-org/matrix-spec
// </text>
import { Static, StaticDecode, TSchema, Type } from '@sinclair/typebox';
import {
StringEventID,
StringRoomID,
StringUserID,
} from './StringlyTypedMatrix';
const TContent = Type.Object({});
export type Event<Content extends TSchema = typeof TContent> = Static<
ReturnType<typeof Event<Content>>
>;
export const Event = <Content extends TSchema>(Content: Content) =>
Type.Object({
content: Content,
type: Type.String({
description:
"The type of event. This SHOULD be namespaced similar to Java package naming conventions e.g. 'com.example.subdomain.event.type'",
}),
});
export type UnsignedData = Static<typeof UnsignedData>;
export const UnsignedData = Type.Object({
age: Type.Optional(
Type.Number({
description:
'The time in milliseconds that has elapsed since the event was sent. This field is generated by the local homeserver, and may be incorrect if the local time on at least one of the two servers is out of sync, which can cause the age to either be negative or greater than it actually is.',
format: 'int64',
example: 1567437,
})
),
redacted_because: Type.Optional(Type.Unknown()),
transaction_id: Type.Optional(
Type.String({
description:
'The client-supplied [transaction ID](/client-server-api/#transaction-identifiers), for example, provided via\n`PUT /_matrix/client/v3/rooms/{roomId}/send/{eventType}/{txnId}`,\nif the client being given the event is the same one which sent it.\n',
})
),
prev_content: Type.Optional(Type.Unknown()),
});
export type SyncRoomEvent<Content extends TSchema = typeof TContent> = Static<
ReturnType<typeof SyncRoomEvent<Content>>
>;
export const SyncRoomEvent = <Content extends TSchema>(Content: Content) =>
Type.Composite([
Event(Content),
Type.Object({
event_id: StringEventID,
sender: StringUserID,
origin_server_ts: Type.Number({
description:
'Timestamp in milliseconds on originating homeserver when this event was sent.',
format: 'int64',
}),
unsigned: Type.Optional(UnsignedData),
}),
]);
export type RoomEvent<Content extends TSchema = typeof TContent> = StaticDecode<
ReturnType<typeof RoomEvent<Content>>
>;
export const RoomEvent = <Content extends TSchema>(Content: Content) =>
Type.Composite([
SyncRoomEvent(Content),
Type.Object({
room_id: StringRoomID,
}),
]);
const StateKey = Type.Object({
state_key: Type.String({
description:
"A unique key which defines the overwriting semantics for this piece of room state. This value is often a zero-length string. The presence of this key makes this event a State Event.\nState keys starting with an `@` are reserved for referencing user IDs, such as room members. With the exception of a few events, state events set with a given user's ID as the state key MUST only be set by that user.",
}),
});
export type SyncStateEvent<Content extends TSchema = typeof TContent> =
StaticDecode<ReturnType<typeof SyncStateEvent<Content>>>;
export const SyncStateEvent = <Content extends TSchema>(Content: Content) =>
Type.Composite([SyncRoomEvent(Content), StateKey]);
export type StateEvent<Content extends TSchema = typeof TContent> =
StaticDecode<ReturnType<typeof StateEvent<Content>>>;
export const StateEvent = <Content extends TSchema>(Content: Content) =>
Type.Composite([RoomEvent(Content), StateKey]);
export type StrippedStateEvent = StaticDecode<typeof StrippedStateEvent>;
export const StrippedStateEvent = Type.Object({
content: Type.Unknown(),
state_key: Type.String({ description: 'The `state_key` for the event.' }),
type: Type.String({ description: 'The `type` for the event.' }),
sender: StringUserID,
});