2022-02-02 14:49:49 +05:30
|
|
|
/*
|
|
|
|
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 22:27:35 +05:30
|
|
|
import {MediaRepository} from "../net/MediaRepository";
|
2022-02-02 14:49:49 +05:30
|
|
|
import {EventEmitter} from "../../utils/EventEmitter";
|
2022-02-09 23:28:30 +05:30
|
|
|
import {AttachmentUpload} from "./AttachmentUpload";
|
2022-02-02 14:49:49 +05:30
|
|
|
|
|
|
|
import type {HomeServerApi} from "../net/HomeServerApi";
|
|
|
|
import type {ILogItem} from "../../logging/types";
|
2022-02-09 23:28:30 +05:30
|
|
|
import type {Platform} from "../../platform/web/Platform";
|
|
|
|
import type {IBlobHandle} from "../../platform/types/types";
|
2022-02-02 14:49:49 +05:30
|
|
|
|
|
|
|
type CreateRoomPayload = {
|
|
|
|
is_direct?: boolean;
|
|
|
|
preset?: string;
|
|
|
|
name?: string;
|
|
|
|
topic?: string;
|
|
|
|
invite?: string[];
|
2022-02-09 23:28:30 +05:30
|
|
|
room_alias_name?: string;
|
|
|
|
initial_state: {type: string; state_key: string; content: Record<string, any>}[]
|
|
|
|
}
|
|
|
|
|
|
|
|
type ImageInfo = {
|
|
|
|
w: number;
|
|
|
|
h: number;
|
|
|
|
mimetype: string;
|
|
|
|
size: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
type Avatar = {
|
|
|
|
info: ImageInfo;
|
|
|
|
blob: IBlobHandle;
|
|
|
|
name: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
type Options = {
|
|
|
|
type: RoomType;
|
|
|
|
isEncrypted?: boolean;
|
|
|
|
name?: string;
|
|
|
|
topic?: string;
|
|
|
|
invites?: string[];
|
|
|
|
avatar?: Avatar;
|
|
|
|
alias?: string;
|
2022-02-02 14:49:49 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
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 22:27:35 +05:30
|
|
|
export class RoomBeingCreated extends EventEmitter<{change: never}> {
|
2022-02-02 14:49:49 +05:30
|
|
|
private _roomId?: string;
|
|
|
|
private profiles: Profile[] = [];
|
|
|
|
|
|
|
|
public readonly isEncrypted: boolean;
|
2022-02-09 23:28:30 +05:30
|
|
|
private _calculatedName: string;
|
2022-02-07 21:00:44 +05:30
|
|
|
private _error?: Error;
|
2022-02-02 14:49:49 +05:30
|
|
|
|
|
|
|
constructor(
|
2022-02-03 22:27:35 +05:30
|
|
|
public readonly localId: string,
|
2022-02-09 23:28:30 +05:30
|
|
|
private readonly options: Options,
|
2022-02-08 19:28:29 +05:30
|
|
|
private readonly updateCallback: (self: RoomBeingCreated, params: string | undefined) => void,
|
2022-02-03 22:27:35 +05:30
|
|
|
public readonly mediaRepository: MediaRepository,
|
2022-02-09 23:28:30 +05:30
|
|
|
public readonly platform: Platform,
|
2022-02-02 14:49:49 +05:30
|
|
|
log: ILogItem
|
|
|
|
) {
|
|
|
|
super();
|
2022-02-09 23:28:30 +05:30
|
|
|
this.isEncrypted = options.isEncrypted === undefined ? defaultE2EEStatusForType(options.type) : options.isEncrypted;
|
|
|
|
if (options.name) {
|
|
|
|
this._calculatedName = options.name;
|
2022-02-02 14:49:49 +05:30
|
|
|
} else {
|
|
|
|
const summaryData = {
|
|
|
|
joinCount: 1, // ourselves
|
2022-02-09 23:28:30 +05:30
|
|
|
inviteCount: (options.invites?.length || 0)
|
2022-02-02 14:49:49 +05:30
|
|
|
};
|
2022-02-09 23:28:30 +05:30
|
|
|
const userIdProfiles = (options.invites || []).map(userId => new UserIdProfile(userId));
|
|
|
|
this._calculatedName = calculateRoomName(userIdProfiles, summaryData, log);
|
2022-02-02 14:49:49 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-08 19:28:29 +05:30
|
|
|
/** @internal */
|
2022-02-07 23:28:43 +05:30
|
|
|
async create(hsApi: HomeServerApi, log: ILogItem): Promise<void> {
|
2022-02-09 23:28:30 +05:30
|
|
|
let avatarEventContent;
|
|
|
|
if (this.options.avatar) {
|
|
|
|
const {avatar} = this.options;
|
|
|
|
const attachment = new AttachmentUpload({filename: avatar.name, blob: avatar.blob, platform: this.platform});
|
|
|
|
await attachment.upload(hsApi, () => {}, log);
|
|
|
|
avatarEventContent = {
|
|
|
|
info: avatar.info
|
|
|
|
};
|
|
|
|
attachment.applyToContent("url", avatarEventContent);
|
|
|
|
}
|
|
|
|
const createOptions: CreateRoomPayload = {
|
|
|
|
is_direct: this.options.type === RoomType.DirectMessage,
|
|
|
|
preset: presetForType(this.options.type),
|
|
|
|
initial_state: []
|
2022-02-02 14:49:49 +05:30
|
|
|
};
|
2022-02-09 23:28:30 +05:30
|
|
|
if (this.options.name) {
|
|
|
|
createOptions.name = this.options.name;
|
2022-02-02 14:49:49 +05:30
|
|
|
}
|
2022-02-09 23:28:30 +05:30
|
|
|
if (this.options.topic) {
|
|
|
|
createOptions.topic = this.options.topic;
|
2022-02-02 14:49:49 +05:30
|
|
|
}
|
2022-02-09 23:28:30 +05:30
|
|
|
if (this.options.invites) {
|
|
|
|
createOptions.invite = this.options.invites;
|
|
|
|
}
|
|
|
|
if (this.options.alias) {
|
|
|
|
createOptions.room_alias_name = this.options.alias;
|
2022-02-02 14:49:49 +05:30
|
|
|
}
|
|
|
|
if (this.isEncrypted) {
|
2022-02-09 23:28:30 +05:30
|
|
|
createOptions.initial_state.push(createRoomEncryptionEvent());
|
|
|
|
}
|
|
|
|
if (avatarEventContent) {
|
|
|
|
createOptions.initial_state.push({
|
|
|
|
type: "m.room.avatar",
|
|
|
|
state_key: "",
|
|
|
|
content: avatarEventContent
|
|
|
|
});
|
2022-02-02 14:49:49 +05:30
|
|
|
}
|
2022-02-07 21:00:44 +05:30
|
|
|
try {
|
2022-02-09 23:28:30 +05:30
|
|
|
const response = await hsApi.createRoom(createOptions, {log}).response();
|
2022-02-07 21:00:44 +05:30
|
|
|
this._roomId = response["room_id"];
|
|
|
|
} catch (err) {
|
|
|
|
this._error = err;
|
|
|
|
}
|
2022-02-08 19:28:29 +05:30
|
|
|
this.emitChange();
|
2022-02-02 14:49:49 +05:30
|
|
|
}
|
|
|
|
|
2022-02-07 23:28:43 +05:30
|
|
|
/** requests the profiles of the invitees if needed to give an accurate
|
|
|
|
* estimated room name in case an explicit room name is not set.
|
|
|
|
* The room is being created in the background whether this is called
|
|
|
|
* or not, and this just gives a more accurate name while that request
|
|
|
|
* is running. */
|
2022-02-08 19:28:29 +05:30
|
|
|
/** @internal */
|
2022-02-07 23:28:43 +05:30
|
|
|
async loadProfiles(hsApi: HomeServerApi, log: ILogItem): Promise<void> {
|
2022-02-02 14:49:49 +05:30
|
|
|
// only load profiles if we need it for the room name and avatar
|
2022-02-09 23:28:30 +05:30
|
|
|
if (!this.options.name && this.options.invites) {
|
|
|
|
this.profiles = await loadProfiles(this.options.invites, hsApi, log);
|
2022-02-03 22:27:35 +05:30
|
|
|
const summaryData = {
|
|
|
|
joinCount: 1, // ourselves
|
2022-02-09 23:28:30 +05:30
|
|
|
inviteCount: this.options.invites.length
|
2022-02-03 22:27:35 +05:30
|
|
|
};
|
2022-02-09 23:28:30 +05:30
|
|
|
this._calculatedName = calculateRoomName(this.profiles, summaryData, log);
|
2022-02-03 22:27:35 +05:30
|
|
|
this.emitChange();
|
2022-02-02 14:49:49 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-08 19:28:29 +05:30
|
|
|
private emitChange(params?: string) {
|
|
|
|
this.updateCallback(this, params);
|
2022-02-03 22:27:35 +05:30
|
|
|
this.emit("change");
|
|
|
|
}
|
|
|
|
|
2022-02-09 23:28:30 +05:30
|
|
|
get avatarColorId(): string { return this.options.invites?.[0] ?? this._roomId ?? this.localId; }
|
|
|
|
get avatarUrl(): string | undefined { return this.profiles?.[0].avatarUrl; }
|
|
|
|
get avatarBlobUrl(): string | undefined { return this.options.avatar?.blob?.url; }
|
2022-02-07 21:00:44 +05:30
|
|
|
get roomId(): string | undefined { return this._roomId; }
|
2022-02-09 23:28:30 +05:30
|
|
|
get name() { return this._calculatedName; }
|
2022-02-03 22:27:35 +05:30
|
|
|
get isBeingCreated(): boolean { return true; }
|
2022-02-07 21:00:44 +05:30
|
|
|
get error(): Error | undefined { return this._error; }
|
2022-02-10 14:09:27 +05:30
|
|
|
get id() { return this.localId; }
|
|
|
|
|
2022-02-07 21:00:44 +05:30
|
|
|
cancel() {
|
2022-02-08 19:28:29 +05:30
|
|
|
// TODO: remove from collection somehow
|
2022-02-07 21:00:44 +05:30
|
|
|
}
|
2022-02-09 23:28:30 +05:30
|
|
|
|
|
|
|
dispose() {
|
|
|
|
if (this.options.avatar) {
|
|
|
|
this.options.avatar.blob.dispose();
|
|
|
|
}
|
|
|
|
}
|
2022-02-02 14:49:49 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
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 22:27:35 +05:30
|
|
|
get displayName(): string | undefined;
|
|
|
|
get avatarUrl(): string | undefined;
|
2022-02-02 14:49:49 +05:30
|
|
|
get name(): string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class Profile implements IProfile {
|
|
|
|
constructor(
|
|
|
|
public readonly userId: string,
|
|
|
|
public readonly displayName: string,
|
2022-02-03 22:27:35 +05:30
|
|
|
public readonly avatarUrl: string | undefined
|
2022-02-02 14:49:49 +05:30
|
|
|
) {}
|
|
|
|
|
|
|
|
get name() { return this.displayName || this.userId; }
|
|
|
|
}
|
2022-02-03 22:27:35 +05:30
|
|
|
|
|
|
|
class UserIdProfile implements IProfile {
|
|
|
|
constructor(public readonly userId: string) {}
|
|
|
|
get displayName() { return undefined; }
|
|
|
|
get name() { return this.userId; }
|
|
|
|
get avatarUrl() { return undefined; }
|
|
|
|
}
|