Skip to content

Commit 10b6e0a

Browse files
committed
v3.12.0
1 parent 489e029 commit 10b6e0a

File tree

5 files changed

+151
-107
lines changed

5 files changed

+151
-107
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@gnuxie/matrix-protection-suite-for-matrix-bot-sdk",
3-
"version": "3.11.0",
3+
"version": "3.12.0",
44
"main": "dist/index.js",
55
"types": "dist/index.d.ts",
66
"files": [
@@ -40,7 +40,7 @@
4040
"jest": "^29.4.3",
4141
"lint-staged": "^13.1.2",
4242
"matrix-bot-sdk": ">=0.6.4",
43-
"matrix-protection-suite": "npm:@gnuxie/matrix-protection-suite@3.11.0",
43+
"matrix-protection-suite": "npm:@gnuxie/matrix-protection-suite@3.12.0",
4444
"postcss": "^8.4.21",
4545
"prettier": "^2.8.4",
4646
"ts-jest": "^29.0.5",
@@ -75,7 +75,7 @@
7575
"@sinclair/typebox": "0.34.13",
7676
"@the-draupnir-project/matrix-basic-types": "1.3.0",
7777
"matrix-bot-sdk": ">=0.6.4",
78-
"matrix-protection-suite": "npm:@gnuxie/matrix-protection-suite@3.11.0"
78+
"matrix-protection-suite": "npm:@gnuxie/matrix-protection-suite@3.12.0"
7979
},
8080
"publishConfig": {
8181
"@gnuxie:registry": "https://registry.npmjs.org"

src/Client/BotSDKBaseClient.ts

Lines changed: 138 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,13 @@ import {
1414
RoomCreateOptions,
1515
RoomCreator,
1616
RoomEventRedacter,
17-
RoomEventRelationsGetter,
1817
RoomJoiner,
1918
RoomKicker,
2019
RoomStateEventSender,
21-
PaginationError,
2220
Value,
2321
isError,
2422
RoomEvent,
25-
doPagination,
2623
EventDecoder,
27-
RoomEventRelationsOptions,
28-
StandardChunkPage,
2924
RoomMessageSender,
3025
MessageContent,
3126
ClientRooms,
@@ -35,9 +30,19 @@ import {
3530
MultipleErrors,
3631
ClientCapabilitiesNegotiation,
3732
ClientCapabilitiesResponse,
33+
RoomMessages,
34+
PaginationIterator,
35+
RoomMessagesOptions,
36+
RoomMessagesPaginator,
37+
PaginationChunk,
38+
RoomMessagesResponse,
39+
StandardPaginationIterator,
40+
RoomEventRelations,
41+
RoomEventRelationsPaginator,
42+
RoomEventRelationsOptions,
43+
RoomEventRelationsResponse,
3844
} from 'matrix-protection-suite';
3945
import { MatrixSendClient } from '../MatrixEmitter';
40-
import { getRelationsForEvent } from './PaginationAPIs';
4146
import {
4247
MatrixRoomID,
4348
StringRoomID,
@@ -155,9 +160,10 @@ export class BotSDKBaseClient
155160
RoomCreator,
156161
RoomEventGetter,
157162
RoomEventRedacter,
158-
RoomEventRelationsGetter,
163+
RoomEventRelations,
159164
RoomJoiner,
160165
RoomKicker,
166+
RoomMessages,
161167
RoomMessageSender,
162168
RoomReactionSender,
163169
RoomStateEventSender,
@@ -342,24 +348,72 @@ export class BotSDKBaseClient
342348
resultifyBotSDKRequestError
343349
);
344350
}
345-
public async forEachRelation<ChunkItem = RoomEvent>(
351+
352+
public toRoomEventRelationsPaginator<TEvent extends RoomEvent = RoomEvent>(
353+
roomID: StringRoomID,
354+
eventID: StringEventID
355+
): RoomEventRelationsPaginator<TEvent> {
356+
return Object.freeze({
357+
client: this.client,
358+
eventDecoder: this.eventDecoder,
359+
async fetchPage(
360+
ergonomicOptions: RoomEventRelationsOptions
361+
): Promise<ActionResult<PaginationChunk<TEvent>>> {
362+
const options = {
363+
...ergonomicOptions,
364+
dir: ergonomicOptions.direction === 'forwards' ? 'f' : 'b',
365+
};
366+
return this.client
367+
.doRequest(
368+
'GET',
369+
`/_matrix/client/v1/rooms/${encodeURIComponent(
370+
roomID
371+
)}/relations/${encodeURIComponent(eventID)}`,
372+
options
373+
)
374+
.then((response) => {
375+
const firstSchemaPass = Value.Decode(
376+
RoomEventRelationsResponse,
377+
response
378+
);
379+
if (isError(firstSchemaPass)) {
380+
return firstSchemaPass.elaborate(
381+
'The response for /relations is severly malformed'
382+
);
383+
}
384+
const parsedEvents: TEvent[] = [];
385+
for (const event of firstSchemaPass.ok.chunk) {
386+
const decodedEvent = this.eventDecoder.decodeEvent(event);
387+
if (isError(decodedEvent)) {
388+
return decodedEvent.elaborate(
389+
'Failed to decode an event in the /relations response'
390+
);
391+
}
392+
parsedEvents.push(decodedEvent.ok as TEvent);
393+
}
394+
return Ok({
395+
previousToken: firstSchemaPass.ok.prev_batch,
396+
nextToken: firstSchemaPass.ok.next_batch,
397+
chunk: parsedEvents,
398+
hasNext: firstSchemaPass.ok.next_batch !== undefined,
399+
hasPrevious: true,
400+
} satisfies PaginationChunk<TEvent>);
401+
});
402+
},
403+
});
404+
}
405+
406+
toRoomEventRelationsIterator<TEvent extends RoomEvent = RoomEvent>(
346407
roomID: StringRoomID,
347408
eventID: StringEventID,
348-
options: RoomEventRelationsOptions<ChunkItem>
349-
): Promise<ActionResult<void, PaginationError>> {
350-
const startingPage = StandardChunkPage.createFirstPage<ChunkItem>(
351-
async () =>
352-
await getRelationsForEvent<ChunkItem>(
353-
this.client,
354-
this.eventDecoder,
355-
roomID,
356-
eventID,
357-
options
358-
),
359-
options
409+
options: RoomEventRelationsOptions
410+
): PaginationIterator<TEvent> {
411+
return new StandardPaginationIterator(
412+
options,
413+
this.toRoomEventRelationsPaginator(roomID, eventID)
360414
);
361-
return await doPagination(startingPage, options);
362415
}
416+
363417
public async getAllState<T extends StateEvent>(
364418
room: MatrixRoomID | StringRoomID
365419
): Promise<ActionResult<T[]>> {
@@ -415,4 +469,67 @@ export class BotSDKBaseClient
415469
resultifyBotSDKRequestError
416470
);
417471
}
472+
473+
toRoomMessagesPaginator<TEvent extends RoomEvent = RoomEvent>(
474+
roomID: StringRoomID
475+
): RoomMessagesPaginator<TEvent> {
476+
return Object.freeze({
477+
client: this.client,
478+
eventDecoder: this.eventDecoder,
479+
async fetchPage(
480+
ergonomicOptions: RoomMessagesOptions
481+
): Promise<ActionResult<PaginationChunk<TEvent>>> {
482+
const options = {
483+
...ergonomicOptions,
484+
filter: ergonomicOptions.filter
485+
? JSON.stringify(ergonomicOptions.filter)
486+
: undefined,
487+
dir: ergonomicOptions.direction === 'forwards' ? 'f' : 'b',
488+
};
489+
return this.client
490+
.doRequest(
491+
'GET',
492+
`/_matrix/client/v3/rooms/${encodeURIComponent(roomID)}/messages`,
493+
options
494+
)
495+
.then((response) => {
496+
const firstSchemaPass = Value.Decode(
497+
RoomMessagesResponse,
498+
response
499+
);
500+
if (isError(firstSchemaPass)) {
501+
return firstSchemaPass.elaborate(
502+
'The response for /messages is severly malformed'
503+
);
504+
}
505+
const parsedEvents: TEvent[] = [];
506+
for (const event of firstSchemaPass.ok.chunk) {
507+
const decodedEvent = this.eventDecoder.decodeEvent(event);
508+
if (isError(decodedEvent)) {
509+
return decodedEvent.elaborate(
510+
'Failed to decode an event in the /messages response'
511+
);
512+
}
513+
parsedEvents.push(decodedEvent.ok as TEvent);
514+
}
515+
return Ok({
516+
previousToken: firstSchemaPass.ok.start,
517+
nextToken: firstSchemaPass.ok.end,
518+
chunk: parsedEvents,
519+
hasNext: firstSchemaPass.ok.end !== undefined,
520+
hasPrevious: true,
521+
} satisfies PaginationChunk<TEvent>);
522+
});
523+
},
524+
});
525+
}
526+
toRoomMessagesIterator<TEvent extends RoomEvent = RoomEvent>(
527+
roomID: StringRoomID,
528+
options: RoomMessagesOptions
529+
): PaginationIterator<TEvent> {
530+
return new StandardPaginationIterator(
531+
options,
532+
this.toRoomMessagesPaginator(roomID)
533+
);
534+
}
418535
}

src/Client/BotSDKClientPlatform.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ import {
88
RoomBanner,
99
RoomCreator,
1010
RoomEventRedacter,
11-
RoomEventRelationsGetter,
11+
RoomEventRelations,
1212
RoomInviter,
1313
RoomJoiner,
1414
RoomKicker,
15+
RoomMessages,
1516
RoomMessageSender,
1617
RoomResolver,
1718
RoomStateEventSender,
@@ -37,7 +38,7 @@ export class BotSDKClientPlatform implements ClientPlatform {
3738
toRoomEventRedacter(): RoomEventRedacter {
3839
return this.allClient;
3940
}
40-
toRoomEventRelationsGetter(): RoomEventRelationsGetter {
41+
toRoomEventRelations(): RoomEventRelations {
4142
return this.allClient;
4243
}
4344
toRoomEventGetter(): RoomEventGetter {
@@ -70,6 +71,9 @@ export class BotSDKClientPlatform implements ClientPlatform {
7071
toRoomReactionSender(): RoomReactionSender {
7172
return this.allClient;
7273
}
74+
toRoomMessages(): RoomMessages {
75+
return this.allClient;
76+
}
7377
toRoomMessageSender(): RoomMessageSender {
7478
return this.allClient;
7579
}

src/Client/PaginationAPIs.ts

Lines changed: 0 additions & 77 deletions
This file was deleted.

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3164,10 +3164,10 @@ matrix-bot-sdk@>=0.6.4:
31643164
request-promise "^4.2.6"
31653165
sanitize-html "^2.8.0"
31663166

3167-
"matrix-protection-suite@npm:@gnuxie/matrix-protection-suite@3.11.0":
3168-
version "3.11.0"
3169-
resolved "https://registry.yarnpkg.com/@gnuxie/matrix-protection-suite/-/matrix-protection-suite-3.11.0.tgz#2ed31e8bd0c68b89927117090f049b5905c9ed82"
3170-
integrity sha512-Y/on4KzPN8413dNv1zLBP1LaD9ijgZSR7qXPJUZRZHqezEruQp1bg8q2YIOdzsrI5dk/TK2ftTnEC/lbz2aWYA==
3167+
"matrix-protection-suite@npm:@gnuxie/matrix-protection-suite@3.12.0":
3168+
version "3.12.0"
3169+
resolved "https://registry.yarnpkg.com/@gnuxie/matrix-protection-suite/-/matrix-protection-suite-3.12.0.tgz#d89d73b8ff90d0eb7b9c8f9d1771acf042e97bc7"
3170+
integrity sha512-f/nuzHYaCB/uVwN7UZy4BatcIvmQL/pTzRE4O2TGMrdv2fDQPsSvc3HZyJTJI/t8iU5g+k/58tk2TyJmLzMS6A==
31713171
dependencies:
31723172
"@gnuxie/typescript-result" "^1.0.0"
31733173
await-lock "^2.2.2"

0 commit comments

Comments
 (0)