adjust m.direct when creating a DM

This commit is contained in:
Bruno Windels 2022-02-10 19:54:15 +01:00
parent 3adb2c3254
commit ff46d382ac
3 changed files with 39 additions and 1 deletions

View File

@ -624,8 +624,8 @@ export class Session {
// the room id. Replace the room being created with the synced room.
if (roomBeingCreated.roomId && !!this.rooms.get(roomBeingCreated.roomId)) {
this._tryReplaceRoomBeingCreated(roomBeingCreated.roomId, log);
await roomBeingCreated.adjustDirectMessageMapIfNeeded(this._user, this._storage, this._hsApi, log);
}
// TODO: if type is DM, then adjust the m.direct account data
});
return roomBeingCreated;
}

View File

@ -286,6 +286,9 @@ export class HomeServerApi {
return this._post(`/createRoom`, {}, payload, options);
}
setAccountData(ownUserId: string, type: string, content: Record<string, any>, options?: IRequestOptions): IHomeServerRequest {
return this._put(`/user/${encodeURIComponent(ownUserId)}/account_data/${encodeURIComponent(type)}`, {}, content, options);
}
}
import {Request as MockRequest} from "../../mocks/Request.js";

View File

@ -26,6 +26,8 @@ import type {HomeServerApi} from "../net/HomeServerApi";
import type {ILogItem} from "../../logging/types";
import type {Platform} from "../../platform/web/Platform";
import type {IBlobHandle} from "../../platform/types/types";
import type {User} from "../User";
import type {Storage} from "../storage/idb/Storage";
type CreateRoomPayload = {
is_direct?: boolean;
@ -217,4 +219,37 @@ export class RoomBeingCreated extends EventEmitter<{change: never}> {
this.options.avatar.blob.dispose();
}
}
async adjustDirectMessageMapIfNeeded(user: User, storage: Storage, hsApi: HomeServerApi, log: ILogItem): Promise<void> {
if (!this.options.invites || this.options.type !== RoomType.DirectMessage) {
return;
}
const userId = this.options.invites[0];
const DM_MAP_TYPE = "m.direct";
await log.wrap("set " + DM_MAP_TYPE, async log => {
try {
const txn = await storage.readWriteTxn([storage.storeNames.accountData]);
let mapEntry;
try {
mapEntry = await txn.accountData.get(DM_MAP_TYPE);
if (!mapEntry) {
mapEntry = {type: DM_MAP_TYPE, content: {}};
}
const map = mapEntry.content;
const userRooms = map[userId];
// this is a new room id so no need to check if it's already there
userRooms.push(this._roomId);
txn.accountData.set(mapEntry);
await txn.complete();
} catch (err) {
txn.abort();
throw err;
}
await hsApi.setAccountData(user.id, DM_MAP_TYPE, mapEntry.content, {log}).response();
} catch (err) {
// we can't really do anything else but logging here
log.catch(err);
}
});
}
}