2022-02-09 23:32:51 +05:30
|
|
|
/*
|
|
|
|
Copyright 2020 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
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 {ViewModel} from "../ViewModel.js";
|
|
|
|
import {imageToInfo} from "./common.js";
|
2022-02-10 21:09:54 +05:30
|
|
|
import {RoomType} from "../../matrix/room/common";
|
2022-02-09 23:32:51 +05:30
|
|
|
|
|
|
|
export class CreateRoomViewModel extends ViewModel {
|
|
|
|
constructor(options) {
|
|
|
|
super(options);
|
|
|
|
const {session} = options;
|
|
|
|
this._session = session;
|
2022-02-10 18:39:18 +05:30
|
|
|
this._name = undefined;
|
|
|
|
this._topic = undefined;
|
|
|
|
this._roomAlias = undefined;
|
2022-02-09 23:32:51 +05:30
|
|
|
this._isPublic = false;
|
|
|
|
this._isEncrypted = true;
|
2022-02-10 18:39:18 +05:30
|
|
|
this._isAdvancedShown = false;
|
|
|
|
this._isFederationDisabled = false;
|
2022-02-09 23:32:51 +05:30
|
|
|
this._avatarScaledBlob = undefined;
|
|
|
|
this._avatarFileName = undefined;
|
|
|
|
this._avatarInfo = undefined;
|
|
|
|
}
|
|
|
|
|
2022-02-10 18:39:18 +05:30
|
|
|
get isPublic() { return this._isPublic; }
|
|
|
|
get isEncrypted() { return this._isEncrypted; }
|
|
|
|
get canCreate() { return !!this._name; }
|
|
|
|
avatarUrl() { return this._avatarScaledBlob.url; }
|
|
|
|
get avatarTitle() { return this._name; }
|
|
|
|
get avatarLetter() { return ""; }
|
|
|
|
get avatarColorNumber() { return 0; }
|
|
|
|
get hasAvatar() { return !!this._avatarScaledBlob; }
|
|
|
|
get isFederationDisabled() { return this._isFederationDisabled; }
|
|
|
|
get isAdvancedShown() { return this._isAdvancedShown; }
|
|
|
|
|
2022-02-09 23:32:51 +05:30
|
|
|
setName(name) {
|
|
|
|
this._name = name;
|
2022-02-10 18:39:18 +05:30
|
|
|
this.emitChange("canCreate");
|
2022-02-09 23:32:51 +05:30
|
|
|
}
|
|
|
|
|
2022-02-10 18:39:18 +05:30
|
|
|
setRoomAlias(roomAlias) {
|
|
|
|
this._roomAlias = roomAlias;
|
|
|
|
}
|
2022-02-09 23:32:51 +05:30
|
|
|
|
|
|
|
setTopic(topic) {
|
|
|
|
this._topic = topic;
|
|
|
|
}
|
|
|
|
|
|
|
|
setPublic(isPublic) {
|
|
|
|
this._isPublic = isPublic;
|
|
|
|
this.emitChange("isPublic");
|
|
|
|
}
|
|
|
|
|
|
|
|
setEncrypted(isEncrypted) {
|
|
|
|
this._isEncrypted = isEncrypted;
|
|
|
|
this.emitChange("isEncrypted");
|
|
|
|
}
|
|
|
|
|
2022-02-10 18:39:18 +05:30
|
|
|
setFederationDisabled(disable) {
|
|
|
|
this._isFederationDisabled = disable;
|
|
|
|
this.emitChange("isFederationDisabled");
|
|
|
|
}
|
2022-02-09 23:32:51 +05:30
|
|
|
|
2022-02-10 18:39:18 +05:30
|
|
|
toggleAdvancedShown() {
|
|
|
|
this._isAdvancedShown = !this._isAdvancedShown;
|
|
|
|
this.emitChange("isAdvancedShown");
|
2022-02-09 23:32:51 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
create() {
|
|
|
|
let avatar;
|
|
|
|
if (this._avatarScaledBlob) {
|
|
|
|
avatar = {
|
|
|
|
info: this._avatarInfo,
|
|
|
|
name: this._avatarFileName,
|
|
|
|
blob: this._avatarScaledBlob
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const roomBeingCreated = this._session.createRoom({
|
|
|
|
type: this.isPublic ? RoomType.Public : RoomType.Private,
|
2022-02-10 18:39:18 +05:30
|
|
|
name: this._name ?? undefined,
|
2022-02-09 23:32:51 +05:30
|
|
|
topic: this.topic ?? undefined,
|
|
|
|
isEncrypted: !this.isPublic && this._isEncrypted,
|
2022-02-10 18:39:18 +05:30
|
|
|
isFederationDisabled: this._isFederationDisabled,
|
|
|
|
alias: this.isPublic ? ensureAliasIsLocalPart(this._roomAlias) : undefined,
|
2022-02-09 23:32:51 +05:30
|
|
|
avatar,
|
|
|
|
});
|
2022-02-10 15:33:52 +05:30
|
|
|
this.navigation.push("room", roomBeingCreated.id);
|
2022-02-09 23:32:51 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
async selectAvatar() {
|
|
|
|
if (!this.platform.hasReadPixelPermission()) {
|
|
|
|
alert("Please allow canvas image data access, so we can scale your images down.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (this._avatarScaledBlob) {
|
|
|
|
this._avatarScaledBlob.dispose();
|
|
|
|
}
|
|
|
|
this._avatarScaledBlob = undefined;
|
|
|
|
this._avatarFileName = undefined;
|
|
|
|
this._avatarInfo = undefined;
|
|
|
|
|
|
|
|
const file = await this.platform.openFile("image/*");
|
|
|
|
if (!file || !file.blob.mimeType.startsWith("image/")) {
|
|
|
|
// allow to clear the avatar by not selecting an image
|
|
|
|
this.emitChange("hasAvatar");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let image = await this.platform.loadImage(file.blob);
|
|
|
|
const limit = 800;
|
|
|
|
if (image.maxDimension > limit) {
|
|
|
|
const scaledImage = await image.scale(limit);
|
|
|
|
image.dispose();
|
|
|
|
image = scaledImage;
|
|
|
|
}
|
|
|
|
this._avatarScaledBlob = image.blob;
|
|
|
|
this._avatarInfo = imageToInfo(image);
|
|
|
|
this._avatarFileName = file.name;
|
|
|
|
this.emitChange("hasAvatar");
|
|
|
|
}
|
|
|
|
}
|
2022-02-10 18:39:18 +05:30
|
|
|
|
|
|
|
function ensureAliasIsLocalPart(roomAliasLocalPart) {
|
|
|
|
if (roomAliasLocalPart.startsWith("#")) {
|
|
|
|
roomAliasLocalPart = roomAliasLocalPart.substr(1);
|
|
|
|
}
|
|
|
|
const colonIdx = roomAliasLocalPart.indexOf(":");
|
|
|
|
if (colonIdx !== -1) {
|
|
|
|
roomAliasLocalPart = roomAliasLocalPart.substr(0, colonIdx);
|
|
|
|
}
|
|
|
|
return roomAliasLocalPart;
|
|
|
|
}
|