Skip to content

Commit 7641aba

Browse files
committed
Implement PersistentConfigBackend.
1 parent e2d748e commit 7641aba

File tree

2 files changed

+55
-10
lines changed

2 files changed

+55
-10
lines changed

src/Client/BotSDKBaseClient.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,17 +79,26 @@ function unknownError(error: unknown): never {
7979
);
8080
}
8181

82+
export function is404(error: unknown): boolean {
83+
return (
84+
typeof error === 'object' &&
85+
error !== null &&
86+
'statusCode' in error &&
87+
error.statusCode === 404
88+
);
89+
}
90+
8291
// Either i'm really tired right now or stupid.
8392
// But I can't think of a way to share this definition with
8493
// `resultifyBotSDKRequestError` without having never | undefined
8594
// clients need to just have `never` when 404 isn't being checked!
8695
export function resultifyBotSDKRequestErrorWith404AsUndefined(
8796
error: unknown
8897
): ActionResult<undefined, ActionException> {
98+
if (is404(error)) {
99+
return Ok(undefined);
100+
}
89101
if (error instanceof MatrixError) {
90-
if (error.statusCode === 404) {
91-
return Ok(undefined);
92-
}
93102
return matrixExceptionFromMatrixError(error);
94103
} else if (Value.Check(WeakError, error)) {
95104
return actionExceptionFromWeakError(error);

src/Interface/MatrixData.ts

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,55 @@ import {
99
MatrixAccountData,
1010
MatrixStateData,
1111
Ok,
12+
PersistentConfigBackend,
1213
RoomStateRevisionIssuer,
1314
Value,
1415
isError,
1516
} from 'matrix-protection-suite';
1617
import { MatrixSendClient } from '../MatrixEmitter';
18+
import {
19+
is404,
20+
resultifyBotSDKRequestError,
21+
resultifyBotSDKRequestErrorWith404AsUndefined,
22+
} from '../Client/BotSDKBaseClient';
1723

18-
function is404(error: unknown): boolean {
19-
return (
20-
typeof error === 'object' &&
21-
error !== null &&
22-
'statusCode' in error &&
23-
error.statusCode === 404
24-
);
24+
export class BotSDKAccountDataConfigBackend<T>
25+
implements PersistentConfigBackend<T>
26+
{
27+
constructor(
28+
private readonly client: MatrixSendClient,
29+
private readonly eventType: string,
30+
private readonly eventSchema: Parameters<(typeof Value)['Decode']>[0]
31+
) {
32+
// nothing to do.
33+
}
34+
35+
public async requestConfig(): Promise<
36+
ActionResult<Record<string, unknown> | undefined>
37+
> {
38+
return await this.client
39+
.getAccountData(this.eventType)
40+
.then(
41+
(data) => Ok(data as Record<string, undefined>),
42+
resultifyBotSDKRequestErrorWith404AsUndefined
43+
);
44+
}
45+
public async saveConfig(data: T): Promise<ActionResult<void>> {
46+
const encodeData = Value.Encode(this.eventSchema, data);
47+
if (isError(encodeData)) {
48+
return encodeData;
49+
}
50+
return await this.client
51+
.setAccountData(this.eventType, encodeData.ok)
52+
.then((_) => Ok(undefined), resultifyBotSDKRequestError);
53+
}
54+
public async saveUnparsedConfig(
55+
data: Record<string, unknown>
56+
): Promise<ActionResult<void>> {
57+
return await this.client
58+
.setAccountData(this.eventType, data)
59+
.then((_) => Ok(undefined), resultifyBotSDKRequestError);
60+
}
2561
}
2662

2763
export class BotSDKMatrixAccountData<T> implements MatrixAccountData<T> {

0 commit comments

Comments
 (0)