@@ -9,19 +9,55 @@ import {
99 MatrixAccountData ,
1010 MatrixStateData ,
1111 Ok ,
12+ PersistentConfigBackend ,
1213 RoomStateRevisionIssuer ,
1314 Value ,
1415 isError ,
1516} from 'matrix-protection-suite' ;
1617import { 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
2763export class BotSDKMatrixAccountData < T > implements MatrixAccountData < T > {
0 commit comments