2020-08-05 22:08:55 +05:30
|
|
|
/*
|
|
|
|
Copyright 2020 Bruno Windels <bruno@windels.cloud>
|
2020-08-17 20:04:25 +05:30
|
|
|
Copyright 2020 The Matrix.org Foundation C.I.C.
|
2020-08-05 22:08:55 +05:30
|
|
|
|
|
|
|
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-21 00:56:39 +05:30
|
|
|
import {TimelineViewModel} from "./timeline/TimelineViewModel.js";
|
2020-08-14 18:03:13 +05:30
|
|
|
import {avatarInitials, getIdentifierColorNumber} from "../../avatar.js";
|
2020-05-04 22:53:11 +05:30
|
|
|
import {ViewModel} from "../../ViewModel.js";
|
2019-02-28 03:20:08 +05:30
|
|
|
|
2020-05-04 22:53:11 +05:30
|
|
|
export class RoomViewModel extends ViewModel {
|
|
|
|
constructor(options) {
|
|
|
|
super(options);
|
|
|
|
const {room, ownUserId, closeCallback} = options;
|
2019-02-28 03:20:08 +05:30
|
|
|
this._room = room;
|
2019-06-16 14:23:23 +05:30
|
|
|
this._ownUserId = ownUserId;
|
2019-02-28 03:20:08 +05:30
|
|
|
this._timeline = null;
|
2019-06-01 21:59:23 +05:30
|
|
|
this._timelineVM = null;
|
2019-02-28 03:20:08 +05:30
|
|
|
this._onRoomChange = this._onRoomChange.bind(this);
|
2019-03-09 05:13:43 +05:30
|
|
|
this._timelineError = null;
|
2020-03-31 01:03:04 +05:30
|
|
|
this._sendError = null;
|
2019-06-27 02:44:39 +05:30
|
|
|
this._closeCallback = closeCallback;
|
2020-05-05 01:53:43 +05:30
|
|
|
this._composerVM = new ComposerViewModel(this);
|
2019-02-28 03:20:08 +05:30
|
|
|
}
|
|
|
|
|
2019-06-27 02:44:39 +05:30
|
|
|
async load() {
|
2019-02-28 03:20:08 +05:30
|
|
|
this._room.on("change", this._onRoomChange);
|
2019-03-09 05:13:43 +05:30
|
|
|
try {
|
|
|
|
this._timeline = await this._room.openTimeline();
|
2020-05-04 22:53:11 +05:30
|
|
|
this._timelineVM = new TimelineViewModel(this.childOptions({
|
|
|
|
room: this._room,
|
|
|
|
timeline: this._timeline,
|
|
|
|
ownUserId: this._ownUserId,
|
|
|
|
}));
|
|
|
|
this.emitChange("timelineViewModel");
|
2019-03-09 05:13:43 +05:30
|
|
|
} catch (err) {
|
2019-06-02 18:29:30 +05:30
|
|
|
console.error(`room.openTimeline(): ${err.message}:\n${err.stack}`);
|
2019-03-09 05:13:43 +05:30
|
|
|
this._timelineError = err;
|
2020-05-04 22:53:11 +05:30
|
|
|
this.emitChange("error");
|
2019-03-09 05:13:43 +05:30
|
|
|
}
|
2019-02-28 03:20:08 +05:30
|
|
|
}
|
|
|
|
|
2019-06-27 02:44:39 +05:30
|
|
|
dispose() {
|
|
|
|
// this races with enable, on the await openTimeline()
|
2019-02-28 03:20:08 +05:30
|
|
|
if (this._timeline) {
|
2019-06-01 21:59:23 +05:30
|
|
|
// will stop the timeline from delivering updates on entries
|
2019-02-28 03:20:08 +05:30
|
|
|
this._timeline.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-27 02:44:39 +05:30
|
|
|
close() {
|
|
|
|
this._closeCallback();
|
|
|
|
}
|
|
|
|
|
2019-02-28 03:20:08 +05:30
|
|
|
// room doesn't tell us yet which fields changed,
|
|
|
|
// so emit all fields originating from summary
|
|
|
|
_onRoomChange() {
|
2020-05-04 22:53:11 +05:30
|
|
|
this.emitChange("name");
|
2019-02-28 03:20:08 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
get name() {
|
|
|
|
return this._room.name;
|
|
|
|
}
|
|
|
|
|
2019-06-01 21:59:23 +05:30
|
|
|
get timelineViewModel() {
|
|
|
|
return this._timelineVM;
|
2019-02-28 03:20:08 +05:30
|
|
|
}
|
2019-03-09 05:13:43 +05:30
|
|
|
|
|
|
|
get error() {
|
|
|
|
if (this._timelineError) {
|
|
|
|
return `Something went wrong loading the timeline: ${this._timelineError.message}`;
|
|
|
|
}
|
2020-03-31 01:03:04 +05:30
|
|
|
if (this._sendError) {
|
|
|
|
return `Something went wrong sending your message: ${this._sendError.message}`;
|
|
|
|
}
|
2019-06-16 18:51:20 +05:30
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
get avatarInitials() {
|
|
|
|
return avatarInitials(this._room.name);
|
2019-03-09 05:13:43 +05:30
|
|
|
}
|
2019-07-27 14:10:56 +05:30
|
|
|
|
2020-08-13 16:11:00 +05:30
|
|
|
get avatarColorNumber() {
|
|
|
|
return getIdentifierColorNumber(this._room.id)
|
|
|
|
}
|
2020-05-05 01:53:43 +05:30
|
|
|
|
|
|
|
async _sendMessage(message) {
|
2019-07-29 23:24:21 +05:30
|
|
|
if (message) {
|
2019-09-15 15:53:26 +05:30
|
|
|
try {
|
2020-03-31 01:03:04 +05:30
|
|
|
await this._room.sendEvent("m.room.message", {msgtype: "m.text", body: message});
|
2019-09-15 15:53:26 +05:30
|
|
|
} catch (err) {
|
|
|
|
console.error(`room.sendMessage(): ${err.message}:\n${err.stack}`);
|
2020-03-31 01:03:04 +05:30
|
|
|
this._sendError = err;
|
|
|
|
this._timelineError = null;
|
2020-05-04 22:53:11 +05:30
|
|
|
this.emitChange("error");
|
2019-09-15 15:53:26 +05:30
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2019-07-29 23:24:21 +05:30
|
|
|
}
|
2019-09-15 15:53:26 +05:30
|
|
|
return false;
|
2019-07-27 14:10:56 +05:30
|
|
|
}
|
2020-05-05 01:53:43 +05:30
|
|
|
|
|
|
|
get composerViewModel() {
|
|
|
|
return this._composerVM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-13 21:30:19 +05:30
|
|
|
class ComposerViewModel extends ViewModel {
|
2020-05-05 01:53:43 +05:30
|
|
|
constructor(roomVM) {
|
2020-08-13 21:30:19 +05:30
|
|
|
super();
|
2020-05-05 01:53:43 +05:30
|
|
|
this._roomVM = roomVM;
|
2020-08-13 21:30:19 +05:30
|
|
|
this._isEmpty = true;
|
2020-05-05 01:53:43 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
sendMessage(message) {
|
2020-08-13 21:30:19 +05:30
|
|
|
const success = this._roomVM._sendMessage(message);
|
|
|
|
if (success) {
|
|
|
|
this._isEmpty = true;
|
|
|
|
this.emitChange("canSend");
|
|
|
|
}
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
get canSend() {
|
|
|
|
return !this._isEmpty;
|
|
|
|
}
|
|
|
|
|
|
|
|
setInput(text) {
|
|
|
|
this._isEmpty = text.length === 0;
|
|
|
|
this.emitChange("canSend");
|
2020-05-05 01:53:43 +05:30
|
|
|
}
|
2019-02-28 03:20:08 +05:30
|
|
|
}
|