Add setAccountData and getAccountData

This commit is contained in:
Robert Long 2022-09-15 21:21:21 -07:00
parent 9c0a1ac305
commit 9cafb8cd26
2 changed files with 45 additions and 0 deletions

View File

@ -963,6 +963,42 @@ export class Session {
return this._roomStateHandler.subscribe(roomStateHandler);
}
async setAccountData(type, content) {
const txn = await this._storage.readWriteTxn([this._storage.storeNames.accountData]);
if (txn) {
txn.accountData.set({ type, content });
await txn.complete();
}
await this.hsApi.setAccountData(this.userId, type, content).response();
}
async getAccountData(type) {
const txn = await this._storage.readWriteTxn([this._storage.storeNames.accountData]);
const entry = await txn.accountData.get(type);
if (entry) {
await txn.complete();
return entry.content;
} else {
try {
const content = await this.hsApi.accountData(this.userId, type).response();
if (content) {
txn.accountData.set({ type, content });
await txn.complete();
}
return content;
} catch (error) {
txn.abort();
return undefined;
}
}
}
/**
Creates an empty (summary isn't loaded) the archived room if it isn't
loaded already, assuming sync will either remove it (when rejoining) or

View File

@ -360,6 +360,15 @@ export class HomeServerApi {
createRoom(payload: Record<string, any>, options?: BaseRequestOptions): IHomeServerRequest {
return this._post(`/createRoom`, {}, payload, options);
}
accountData(ownUserId: string, type: string, options?: BaseRequestOptions): IHomeServerRequest {
return this._get(
`/user/${encodeURIComponent(ownUserId)}/account_data/${encodeURIComponent(type)}`,
undefined,
undefined,
options,
);
}
setAccountData(ownUserId: string, type: string, content: Record<string, any>, options?: BaseRequestOptions): IHomeServerRequest {
return this._put(`/user/${encodeURIComponent(ownUserId)}/account_data/${encodeURIComponent(type)}`, {}, content, options);