2022-02-02 10:19:49 +01:00
|
|
|
/*
|
|
|
|
Copyright 2020 Bruno Windels <bruno@windels.cloud>
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import {calculateRoomName} from "./members/Heroes";
|
|
|
|
import {createRoomEncryptionEvent} from "../e2ee/common";
|
2022-02-03 17:57:35 +01:00
|
|
|
import {MediaRepository} from "../net/MediaRepository";
|
2022-02-02 10:19:49 +01:00
|
|
|
import {EventEmitter} from "../../utils/EventEmitter";
|
|
|
|
|
|
|
|
import type {StateEvent} from "../storage/types";
|
|
|
|
import type {HomeServerApi} from "../net/HomeServerApi";
|
|
|
|
import type {ILogItem} from "../../logging/types";
|
|
|
|
|
|
|
|
type CreateRoomPayload = {
|
|
|
|
is_direct?: boolean;
|
|
|
|
preset?: string;
|
|
|
|
name?: string;
|
|
|
|
topic?: string;
|
|
|
|
invite?: string[];
|
|
|
|
initial_state?: StateEvent[]
|
|
|
|
}
|
|
|
|
|
|
|
|
export enum RoomType {
|
|
|
|
DirectMessage,
|
|
|
|
Private,
|
|
|
|
Public
|
|
|
|
}
|
|
|
|
|
|
|
|
function defaultE2EEStatusForType(type: RoomType): boolean {
|
|
|
|
switch (type) {
|
|
|
|
case RoomType.DirectMessage:
|
|
|
|
case RoomType.Private:
|
|
|
|
return true;
|
|
|
|
case RoomType.Public:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function presetForType(type: RoomType): string {
|
|
|
|
switch (type) {
|
|
|
|
case RoomType.DirectMessage:
|
|
|
|
return "trusted_private_chat";
|
|
|
|
case RoomType.Private:
|
|
|
|
return "private_chat";
|
|
|
|
case RoomType.Public:
|
|
|
|
return "public_chat";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-03 17:57:35 +01:00
|
|
|
export class RoomBeingCreated extends EventEmitter<{change: never}> {
|
2022-02-02 10:19:49 +01:00
|
|
|
private _roomId?: string;
|
|
|
|
private profiles: Profile[] = [];
|
|
|
|
|
|
|
|
public readonly isEncrypted: boolean;
|
2022-02-03 17:57:35 +01:00
|
|
|
private _name: string;
|
2022-02-02 10:19:49 +01:00
|
|
|
|
|
|
|
constructor(
|
2022-02-03 17:57:35 +01:00
|
|
|
public readonly localId: string,
|
2022-02-02 10:19:49 +01:00
|
|
|
private readonly type: RoomType,
|
|
|
|
isEncrypted: boolean | undefined,
|
|
|
|
private readonly explicitName: string | undefined,
|
|
|
|
private readonly topic: string | undefined,
|
|
|
|
private readonly inviteUserIds: string[] | undefined,
|
2022-02-03 17:57:35 +01:00
|
|
|
private readonly updateCallback,
|
|
|
|
public readonly mediaRepository: MediaRepository,
|
2022-02-02 10:19:49 +01:00
|
|
|
log: ILogItem
|
|
|
|
) {
|
|
|
|
super();
|
|
|
|
this.isEncrypted = isEncrypted === undefined ? defaultE2EEStatusForType(this.type) : isEncrypted;
|
|
|
|
if (explicitName) {
|
2022-02-03 17:57:35 +01:00
|
|
|
this._name = explicitName;
|
2022-02-02 10:19:49 +01:00
|
|
|
} else {
|
|
|
|
const summaryData = {
|
|
|
|
joinCount: 1, // ourselves
|
|
|
|
inviteCount: (this.inviteUserIds?.length || 0)
|
|
|
|
};
|
2022-02-03 17:57:35 +01:00
|
|
|
const userIdProfiles = (inviteUserIds || []).map(userId => new UserIdProfile(userId));
|
|
|
|
this._name = calculateRoomName(userIdProfiles, summaryData, log);
|
2022-02-02 10:19:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public async start(hsApi: HomeServerApi, log: ILogItem): Promise<void> {
|
|
|
|
await Promise.all([
|
|
|
|
this.loadProfiles(hsApi, log),
|
|
|
|
this.create(hsApi, log),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
private async create(hsApi: HomeServerApi, log: ILogItem): Promise<void> {
|
|
|
|
const options: CreateRoomPayload = {
|
|
|
|
is_direct: this.type === RoomType.DirectMessage,
|
|
|
|
preset: presetForType(this.type)
|
|
|
|
};
|
|
|
|
if (this.explicitName) {
|
|
|
|
options.name = this.explicitName;
|
|
|
|
}
|
|
|
|
if (this.topic) {
|
|
|
|
options.topic = this.topic;
|
|
|
|
}
|
|
|
|
if (this.inviteUserIds) {
|
|
|
|
options.invite = this.inviteUserIds;
|
|
|
|
}
|
|
|
|
if (this.isEncrypted) {
|
|
|
|
options.initial_state = [createRoomEncryptionEvent()];
|
|
|
|
}
|
2022-02-03 17:57:35 +01:00
|
|
|
console.log("going to create the room now");
|
2022-02-02 10:19:49 +01:00
|
|
|
const response = await hsApi.createRoom(options, {log}).response();
|
|
|
|
this._roomId = response["room_id"];
|
2022-02-03 17:57:35 +01:00
|
|
|
console.log("done creating the room now", this._roomId);
|
|
|
|
// TODO: somehow check in Session if we need to replace this with a joined room
|
|
|
|
// in case the room appears first in sync, and this request returns later
|
|
|
|
this.emitChange();
|
2022-02-02 10:19:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private async loadProfiles(hsApi: HomeServerApi, log: ILogItem): Promise<void> {
|
|
|
|
// only load profiles if we need it for the room name and avatar
|
|
|
|
if (!this.explicitName && this.inviteUserIds) {
|
|
|
|
this.profiles = await loadProfiles(this.inviteUserIds, hsApi, log);
|
2022-02-03 17:57:35 +01:00
|
|
|
console.log("loaded the profiles", this.profiles);
|
|
|
|
const summaryData = {
|
|
|
|
joinCount: 1, // ourselves
|
|
|
|
inviteCount: this.inviteUserIds.length
|
|
|
|
};
|
|
|
|
this._name = calculateRoomName(this.profiles, summaryData, log);
|
|
|
|
console.log("loaded the profiles and the new name", this.name);
|
|
|
|
this.emitChange();
|
2022-02-02 10:19:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-03 17:57:35 +01:00
|
|
|
private emitChange() {
|
|
|
|
this.updateCallback(this);
|
|
|
|
this.emit("change");
|
|
|
|
}
|
|
|
|
|
|
|
|
get avatarColorId(): string {
|
|
|
|
return this.inviteUserIds?.[0] ?? this._roomId ?? this.localId;
|
2022-02-02 10:19:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
get avatarUrl(): string | undefined {
|
2022-02-03 17:57:35 +01:00
|
|
|
const result = this.profiles[0]?.avatarUrl;
|
|
|
|
console.log("RoomBeingCreated.avatarUrl", this.profiles, result);
|
|
|
|
return result;
|
2022-02-02 10:19:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
get roomId(): string | undefined {
|
|
|
|
return this._roomId;
|
|
|
|
}
|
2022-02-03 17:57:35 +01:00
|
|
|
|
|
|
|
get name() { return this._name; }
|
|
|
|
|
|
|
|
get isBeingCreated(): boolean { return true; }
|
2022-02-02 10:19:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function loadProfiles(userIds: string[], hsApi: HomeServerApi, log: ILogItem): Promise<Profile[]> {
|
|
|
|
const profiles = await Promise.all(userIds.map(async userId => {
|
|
|
|
const response = await hsApi.profile(userId, {log}).response();
|
|
|
|
return new Profile(userId, response.displayname as string, response.avatar_url as string);
|
|
|
|
}));
|
|
|
|
profiles.sort((a, b) => a.name.localeCompare(b.name));
|
|
|
|
return profiles;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface IProfile {
|
|
|
|
get userId(): string;
|
2022-02-03 17:57:35 +01:00
|
|
|
get displayName(): string | undefined;
|
|
|
|
get avatarUrl(): string | undefined;
|
2022-02-02 10:19:49 +01:00
|
|
|
get name(): string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class Profile implements IProfile {
|
|
|
|
constructor(
|
|
|
|
public readonly userId: string,
|
|
|
|
public readonly displayName: string,
|
2022-02-03 17:57:35 +01:00
|
|
|
public readonly avatarUrl: string | undefined
|
2022-02-02 10:19:49 +01:00
|
|
|
) {}
|
|
|
|
|
|
|
|
get name() { return this.displayName || this.userId; }
|
|
|
|
}
|
2022-02-03 17:57:35 +01:00
|
|
|
|
|
|
|
class UserIdProfile implements IProfile {
|
|
|
|
constructor(public readonly userId: string) {}
|
|
|
|
get displayName() { return undefined; }
|
|
|
|
get name() { return this.userId; }
|
|
|
|
get avatarUrl() { return undefined; }
|
|
|
|
|
|
|
|
}
|