2020-08-05 18:38:55 +02:00
|
|
|
/*
|
|
|
|
Copyright 2020 Bruno Windels <bruno@windels.cloud>
|
2020-08-17 16:34:25 +02:00
|
|
|
Copyright 2020 The Matrix.org Foundation C.I.C.
|
2020-08-05 18:38:55 +02:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2020-04-20 21:26:39 +02:00
|
|
|
import {TimelineViewModel} from "./timeline/TimelineViewModel.js";
|
2021-04-28 14:10:54 +02:00
|
|
|
import {avatarInitials, getIdentifierColorNumber, getAvatarHttpUrl} from "../../avatar.js";
|
2020-05-04 19:23:11 +02:00
|
|
|
import {ViewModel} from "../../ViewModel.js";
|
2019-02-27 22:50:08 +01:00
|
|
|
|
2020-05-04 19:23:11 +02:00
|
|
|
export class RoomViewModel extends ViewModel {
|
|
|
|
constructor(options) {
|
|
|
|
super(options);
|
2021-05-31 15:18:44 +02:00
|
|
|
const {room} = options;
|
2019-02-27 22:50:08 +01:00
|
|
|
this._room = room;
|
2019-06-01 18:29:23 +02:00
|
|
|
this._timelineVM = null;
|
2019-02-27 22:50:08 +01:00
|
|
|
this._onRoomChange = this._onRoomChange.bind(this);
|
2019-03-09 00:43:43 +01:00
|
|
|
this._timelineError = null;
|
2020-03-30 21:33:04 +02:00
|
|
|
this._sendError = null;
|
2021-05-11 13:11:11 +02:00
|
|
|
this._composerVM = null;
|
|
|
|
if (room.isArchived) {
|
|
|
|
this._composerVM = new ArchivedViewModel(this.childOptions({archivedRoom: room}));
|
|
|
|
} else {
|
|
|
|
this._composerVM = new ComposerViewModel(this);
|
|
|
|
}
|
2020-08-21 11:57:49 +02:00
|
|
|
this._clearUnreadTimout = null;
|
2020-10-16 13:02:21 +02:00
|
|
|
this._closeUrl = this.urlCreator.urlUntilSegment("session");
|
2021-06-04 14:28:44 +02:00
|
|
|
this._detailsLink = this.urlCreator.urlForSegment("details");
|
2020-10-14 14:53:52 +02:00
|
|
|
}
|
|
|
|
|
2019-06-26 23:14:39 +02:00
|
|
|
async load() {
|
2019-02-27 22:50:08 +01:00
|
|
|
this._room.on("change", this._onRoomChange);
|
2019-03-09 00:43:43 +01:00
|
|
|
try {
|
2021-02-24 11:21:04 +01:00
|
|
|
const timeline = await this._room.openTimeline();
|
2020-10-19 13:39:19 +02:00
|
|
|
const timelineVM = this.track(new TimelineViewModel(this.childOptions({
|
2020-05-04 19:23:11 +02:00
|
|
|
room: this._room,
|
2021-02-24 11:21:04 +01:00
|
|
|
timeline,
|
2020-10-19 12:57:21 +02:00
|
|
|
})));
|
2020-10-19 13:39:19 +02:00
|
|
|
this._timelineVM = timelineVM;
|
2020-05-04 19:23:11 +02:00
|
|
|
this.emitChange("timelineViewModel");
|
2019-03-09 00:43:43 +01:00
|
|
|
} catch (err) {
|
2019-06-02 14:59:30 +02:00
|
|
|
console.error(`room.openTimeline(): ${err.message}:\n${err.stack}`);
|
2019-03-09 00:43:43 +01:00
|
|
|
this._timelineError = err;
|
2020-05-04 19:23:11 +02:00
|
|
|
this.emitChange("error");
|
2019-03-09 00:43:43 +01:00
|
|
|
}
|
2020-10-07 12:30:46 +02:00
|
|
|
this._clearUnreadAfterDelay();
|
|
|
|
}
|
|
|
|
|
|
|
|
async _clearUnreadAfterDelay() {
|
2021-05-11 13:11:11 +02:00
|
|
|
if (this._room.isArchived || this._clearUnreadTimout) {
|
2020-10-07 12:30:46 +02:00
|
|
|
return;
|
|
|
|
}
|
2020-08-21 11:57:49 +02:00
|
|
|
this._clearUnreadTimout = this.clock.createTimeout(2000);
|
|
|
|
try {
|
|
|
|
await this._clearUnreadTimout.elapsed();
|
|
|
|
await this._room.clearUnread();
|
2020-10-07 12:30:46 +02:00
|
|
|
this._clearUnreadTimout = null;
|
2020-08-21 11:57:49 +02:00
|
|
|
} catch (err) {
|
|
|
|
if (err.name !== "AbortError") {
|
|
|
|
throw err;
|
2021-04-23 18:06:38 +02:00
|
|
|
}
|
2020-08-21 11:57:49 +02:00
|
|
|
}
|
2019-02-27 22:50:08 +01:00
|
|
|
}
|
|
|
|
|
2020-10-07 12:30:46 +02:00
|
|
|
focus() {
|
|
|
|
this._clearUnreadAfterDelay();
|
|
|
|
}
|
|
|
|
|
2019-06-26 23:14:39 +02:00
|
|
|
dispose() {
|
2020-09-10 17:43:01 +02:00
|
|
|
super.dispose();
|
2020-10-14 11:26:10 +02:00
|
|
|
this._room.off("change", this._onRoomChange);
|
2021-05-11 16:09:58 +02:00
|
|
|
if (this._room.isArchived) {
|
|
|
|
this._room.release();
|
|
|
|
}
|
2020-08-21 11:57:49 +02:00
|
|
|
if (this._clearUnreadTimout) {
|
|
|
|
this._clearUnreadTimout.abort();
|
|
|
|
this._clearUnreadTimout = null;
|
|
|
|
}
|
2019-02-27 22:50:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// room doesn't tell us yet which fields changed,
|
|
|
|
// so emit all fields originating from summary
|
|
|
|
_onRoomChange() {
|
2021-05-11 13:11:11 +02:00
|
|
|
if (this._room.isArchived) {
|
|
|
|
this._composerVM.emitChange();
|
|
|
|
}
|
2021-05-07 13:11:17 +02:00
|
|
|
this.emitChange();
|
2019-02-27 22:50:08 +01:00
|
|
|
}
|
|
|
|
|
2021-04-21 15:45:51 +02:00
|
|
|
get kind() { return "room"; }
|
2021-04-21 15:34:35 +02:00
|
|
|
get closeUrl() { return this._closeUrl; }
|
|
|
|
get name() { return this._room.name || this.i18n`Empty Room`; }
|
|
|
|
get id() { return this._room.id; }
|
|
|
|
get timelineViewModel() { return this._timelineVM; }
|
|
|
|
get isEncrypted() { return this._room.isEncrypted; }
|
2021-06-04 14:28:44 +02:00
|
|
|
get detailsLink() { return this._detailsLink; }
|
2020-09-11 11:35:53 +02:00
|
|
|
|
2019-03-09 00:43:43 +01:00
|
|
|
get error() {
|
|
|
|
if (this._timelineError) {
|
|
|
|
return `Something went wrong loading the timeline: ${this._timelineError.message}`;
|
|
|
|
}
|
2020-03-30 21:33:04 +02:00
|
|
|
if (this._sendError) {
|
|
|
|
return `Something went wrong sending your message: ${this._sendError.message}`;
|
|
|
|
}
|
2019-06-16 15:21:20 +02:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2020-08-20 17:33:08 +02:00
|
|
|
get avatarLetter() {
|
2020-08-21 18:14:32 +02:00
|
|
|
return avatarInitials(this.name);
|
2019-03-09 00:43:43 +01:00
|
|
|
}
|
2019-07-27 10:40:56 +02:00
|
|
|
|
2020-08-13 12:41:00 +02:00
|
|
|
get avatarColorNumber() {
|
|
|
|
return getIdentifierColorNumber(this._room.id)
|
|
|
|
}
|
2020-08-20 17:33:08 +02:00
|
|
|
|
2021-04-28 14:10:54 +02:00
|
|
|
avatarUrl(size) {
|
|
|
|
return getAvatarHttpUrl(this._room.avatarUrl, size, this.platform, this._room.mediaRepository);
|
2020-08-20 17:33:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
get avatarTitle() {
|
|
|
|
return this.name;
|
|
|
|
}
|
2021-05-12 13:59:08 +02:00
|
|
|
|
|
|
|
get canLeave() {
|
|
|
|
return this._room.isJoined;
|
|
|
|
}
|
|
|
|
|
|
|
|
leaveRoom() {
|
|
|
|
this._room.leave();
|
|
|
|
}
|
2021-05-12 15:38:54 +02:00
|
|
|
|
|
|
|
get canForget() {
|
|
|
|
return this._room.isArchived;
|
|
|
|
}
|
|
|
|
|
|
|
|
forgetRoom() {
|
|
|
|
this._room.forget();
|
|
|
|
}
|
2021-05-18 11:07:46 +02:00
|
|
|
|
|
|
|
get canRejoin() {
|
|
|
|
return this._room.isArchived;
|
|
|
|
}
|
|
|
|
|
|
|
|
rejoinRoom() {
|
|
|
|
this._room.join();
|
|
|
|
}
|
2020-05-04 22:23:43 +02:00
|
|
|
|
|
|
|
async _sendMessage(message) {
|
2021-05-11 13:11:11 +02:00
|
|
|
if (!this._room.isArchived && message) {
|
2019-09-15 12:23:26 +02:00
|
|
|
try {
|
2020-11-10 15:13:31 +01:00
|
|
|
let msgtype = "m.text";
|
2020-11-16 19:29:29 +01:00
|
|
|
if (message.startsWith("/me ")) {
|
|
|
|
message = message.substr(4).trim();
|
2020-11-10 15:13:31 +01:00
|
|
|
msgtype = "m.emote";
|
|
|
|
}
|
|
|
|
await this._room.sendEvent("m.room.message", {msgtype, body: message});
|
2019-09-15 12:23:26 +02:00
|
|
|
} catch (err) {
|
|
|
|
console.error(`room.sendMessage(): ${err.message}:\n${err.stack}`);
|
2020-03-30 21:33:04 +02:00
|
|
|
this._sendError = err;
|
|
|
|
this._timelineError = null;
|
2020-05-04 19:23:11 +02:00
|
|
|
this.emitChange("error");
|
2019-09-15 12:23:26 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2019-07-29 19:54:21 +02:00
|
|
|
}
|
2019-09-15 12:23:26 +02:00
|
|
|
return false;
|
2019-07-27 10:40:56 +02:00
|
|
|
}
|
2020-05-04 22:23:43 +02:00
|
|
|
|
2020-11-18 20:07:31 +01:00
|
|
|
async _pickAndSendFile() {
|
2020-11-11 10:47:55 +01:00
|
|
|
try {
|
2020-11-18 20:07:31 +01:00
|
|
|
const file = await this.platform.openFile();
|
|
|
|
if (!file) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return this._sendFile(file);
|
2020-11-11 10:47:55 +01:00
|
|
|
} catch (err) {
|
2020-11-18 20:07:31 +01:00
|
|
|
console.error(err);
|
2020-11-10 22:36:26 +01:00
|
|
|
}
|
2020-11-18 20:07:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async _sendFile(file) {
|
2020-11-10 22:36:26 +01:00
|
|
|
const content = {
|
|
|
|
body: file.name,
|
2020-11-13 19:13:35 +01:00
|
|
|
msgtype: "m.file"
|
2020-11-10 22:36:26 +01:00
|
|
|
};
|
2020-11-13 19:13:35 +01:00
|
|
|
await this._room.sendEvent("m.room.message", content, {
|
|
|
|
"url": this._room.createAttachment(file.blob, file.name)
|
|
|
|
});
|
2020-11-10 22:36:26 +01:00
|
|
|
}
|
|
|
|
|
2021-03-09 19:35:25 +01:00
|
|
|
async _pickAndSendVideo() {
|
|
|
|
try {
|
|
|
|
if (!this.platform.hasReadPixelPermission()) {
|
|
|
|
alert("Please allow canvas image data access, so we can scale your images down.");
|
|
|
|
return;
|
|
|
|
}
|
2021-03-10 13:43:55 +01:00
|
|
|
const file = await this.platform.openFile("video/*");
|
2021-03-09 19:35:25 +01:00
|
|
|
if (!file) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!file.blob.mimeType.startsWith("video/")) {
|
|
|
|
return this._sendFile(file);
|
|
|
|
}
|
2021-03-10 13:43:55 +01:00
|
|
|
let video;
|
|
|
|
try {
|
|
|
|
video = await this.platform.loadVideo(file.blob);
|
|
|
|
} catch (err) {
|
|
|
|
// TODO: extract platform dependent code from view model
|
|
|
|
if (err instanceof window.MediaError && err.code === 4) {
|
|
|
|
throw new Error(`this browser does not support videos of type ${file?.blob.mimeType}.`);
|
|
|
|
} else {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
2021-03-09 19:35:25 +01:00
|
|
|
const content = {
|
|
|
|
body: file.name,
|
|
|
|
msgtype: "m.video",
|
|
|
|
info: videoToInfo(video)
|
|
|
|
};
|
|
|
|
const attachments = {
|
|
|
|
"url": this._room.createAttachment(video.blob, file.name),
|
|
|
|
};
|
|
|
|
|
|
|
|
const limit = await this.platform.settingsStorage.getInt("sentImageSizeLimit");
|
|
|
|
const maxDimension = limit || Math.min(video.maxDimension, 800);
|
|
|
|
const thumbnail = await video.scale(maxDimension);
|
|
|
|
content.info.thumbnail_info = imageToInfo(thumbnail);
|
|
|
|
attachments["info.thumbnail_url"] =
|
|
|
|
this._room.createAttachment(thumbnail.blob, file.name);
|
|
|
|
await this._room.sendEvent("m.room.message", content, attachments);
|
|
|
|
} catch (err) {
|
2021-03-10 13:43:55 +01:00
|
|
|
this._sendError = err;
|
2021-03-09 19:35:25 +01:00
|
|
|
this.emitChange("error");
|
|
|
|
console.error(err.stack);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-18 20:07:31 +01:00
|
|
|
async _pickAndSendPicture() {
|
2020-11-13 19:15:03 +01:00
|
|
|
try {
|
2020-11-18 20:07:31 +01:00
|
|
|
if (!this.platform.hasReadPixelPermission()) {
|
|
|
|
alert("Please allow canvas image data access, so we can scale your images down.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const file = await this.platform.openFile("image/*");
|
|
|
|
if (!file) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!file.blob.mimeType.startsWith("image/")) {
|
|
|
|
return this._sendFile(file);
|
|
|
|
}
|
2020-11-20 15:51:16 +01:00
|
|
|
let image = await this.platform.loadImage(file.blob);
|
|
|
|
const limit = await this.platform.settingsStorage.getInt("sentImageSizeLimit");
|
|
|
|
if (limit && image.maxDimension > limit) {
|
|
|
|
image = await image.scale(limit);
|
|
|
|
}
|
2020-11-18 20:07:31 +01:00
|
|
|
const content = {
|
|
|
|
body: file.name,
|
|
|
|
msgtype: "m.image",
|
|
|
|
info: imageToInfo(image)
|
|
|
|
};
|
|
|
|
const attachments = {
|
|
|
|
"url": this._room.createAttachment(image.blob, file.name),
|
|
|
|
};
|
|
|
|
if (image.maxDimension > 600) {
|
|
|
|
const thumbnail = await image.scale(400);
|
|
|
|
content.info.thumbnail_info = imageToInfo(thumbnail);
|
|
|
|
attachments["info.thumbnail_url"] =
|
|
|
|
this._room.createAttachment(thumbnail.blob, file.name);
|
|
|
|
}
|
|
|
|
await this._room.sendEvent("m.room.message", content, attachments);
|
2020-11-13 19:15:03 +01:00
|
|
|
} catch (err) {
|
2021-03-09 19:35:25 +01:00
|
|
|
this._sendError = err;
|
|
|
|
this.emitChange("error");
|
|
|
|
console.error(err.stack);
|
2020-11-13 19:15:03 +01:00
|
|
|
}
|
|
|
|
}
|
2021-05-27 20:27:28 +02:00
|
|
|
|
2020-05-04 22:23:43 +02:00
|
|
|
get composerViewModel() {
|
|
|
|
return this._composerVM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-13 18:00:19 +02:00
|
|
|
class ComposerViewModel extends ViewModel {
|
2020-05-04 22:23:43 +02:00
|
|
|
constructor(roomVM) {
|
2020-08-13 18:00:19 +02:00
|
|
|
super();
|
2020-05-04 22:23:43 +02:00
|
|
|
this._roomVM = roomVM;
|
2020-08-13 18:00:19 +02:00
|
|
|
this._isEmpty = true;
|
2020-05-04 22:23:43 +02:00
|
|
|
}
|
|
|
|
|
2020-09-11 11:35:53 +02:00
|
|
|
get isEncrypted() {
|
|
|
|
return this._roomVM.isEncrypted;
|
|
|
|
}
|
|
|
|
|
2020-05-04 22:23:43 +02:00
|
|
|
sendMessage(message) {
|
2020-08-13 18:00:19 +02:00
|
|
|
const success = this._roomVM._sendMessage(message);
|
|
|
|
if (success) {
|
|
|
|
this._isEmpty = true;
|
|
|
|
this.emitChange("canSend");
|
|
|
|
}
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2020-11-13 19:15:03 +01:00
|
|
|
sendPicture() {
|
2020-11-18 20:07:31 +01:00
|
|
|
this._roomVM._pickAndSendPicture();
|
2020-11-13 19:15:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sendFile() {
|
2020-11-18 20:07:31 +01:00
|
|
|
this._roomVM._pickAndSendFile();
|
2020-11-11 11:47:05 +01:00
|
|
|
}
|
|
|
|
|
2021-03-09 19:35:25 +01:00
|
|
|
sendVideo() {
|
|
|
|
this._roomVM._pickAndSendVideo();
|
|
|
|
}
|
|
|
|
|
2020-08-13 18:00:19 +02:00
|
|
|
get canSend() {
|
|
|
|
return !this._isEmpty;
|
|
|
|
}
|
|
|
|
|
2020-11-06 23:43:02 +01:00
|
|
|
async setInput(text) {
|
|
|
|
const wasEmpty = this._isEmpty;
|
2020-08-13 18:00:19 +02:00
|
|
|
this._isEmpty = text.length === 0;
|
2020-11-10 14:02:07 +01:00
|
|
|
if (wasEmpty && !this._isEmpty) {
|
|
|
|
this._roomVM._room.ensureMessageKeyIsShared();
|
2020-11-06 23:43:02 +01:00
|
|
|
}
|
|
|
|
if (wasEmpty !== this._isEmpty) {
|
|
|
|
this.emitChange("canSend");
|
|
|
|
}
|
2020-05-04 22:23:43 +02:00
|
|
|
}
|
2021-05-11 13:11:11 +02:00
|
|
|
|
|
|
|
get kind() {
|
|
|
|
return "composer";
|
|
|
|
}
|
2019-02-27 22:50:08 +01:00
|
|
|
}
|
2020-11-13 19:15:03 +01:00
|
|
|
|
|
|
|
function imageToInfo(image) {
|
|
|
|
return {
|
|
|
|
w: image.width,
|
|
|
|
h: image.height,
|
|
|
|
mimetype: image.blob.mimeType,
|
|
|
|
size: image.blob.size
|
|
|
|
};
|
|
|
|
}
|
2021-03-09 19:35:25 +01:00
|
|
|
|
|
|
|
function videoToInfo(video) {
|
|
|
|
const info = imageToInfo(video);
|
|
|
|
info.duration = video.duration;
|
|
|
|
return info;
|
|
|
|
}
|
2021-05-11 13:11:11 +02:00
|
|
|
|
|
|
|
class ArchivedViewModel extends ViewModel {
|
|
|
|
constructor(options) {
|
|
|
|
super(options);
|
|
|
|
this._archivedRoom = options.archivedRoom;
|
|
|
|
}
|
|
|
|
|
|
|
|
get description() {
|
|
|
|
if (this._archivedRoom.isKicked) {
|
|
|
|
if (this._archivedRoom.kickReason) {
|
|
|
|
return this.i18n`You were kicked from the room by ${this._archivedRoom.kickedBy.name} because: ${this._archivedRoom.kickReason}`;
|
|
|
|
} else {
|
|
|
|
return this.i18n`You were kicked from the room by ${this._archivedRoom.kickedBy.name}.`;
|
|
|
|
}
|
|
|
|
} else if (this._archivedRoom.isBanned) {
|
|
|
|
if (this._archivedRoom.kickReason) {
|
|
|
|
return this.i18n`You were banned from the room by ${this._archivedRoom.kickedBy.name} because: ${this._archivedRoom.kickReason}`;
|
|
|
|
} else {
|
|
|
|
return this.i18n`You were banned from the room by ${this._archivedRoom.kickedBy.name}.`;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return this.i18n`You left this room`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get kind() {
|
|
|
|
return "archived";
|
|
|
|
}
|
2021-05-26 13:10:50 +02:00
|
|
|
}
|