mirror of
https://github.com/vector-im/hydrogen-web.git
synced 2025-01-11 12:37:22 +01:00
Add setAccountData and getAccountData
This commit is contained in:
parent
9c0a1ac305
commit
9cafb8cd26
@ -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
|
||||
|
@ -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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user