2020-04-20 21:26:39 +02:00
|
|
|
import {TimelineViewModel} from "./timeline/TimelineViewModel.js";
|
2019-06-16 15:21:20 +02:00
|
|
|
import {avatarInitials} 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);
|
|
|
|
const {room, ownUserId, closeCallback} = options;
|
2019-02-27 22:50:08 +01:00
|
|
|
this._room = room;
|
2019-06-16 10:53:23 +02:00
|
|
|
this._ownUserId = ownUserId;
|
2019-02-27 22:50:08 +01:00
|
|
|
this._timeline = null;
|
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;
|
2019-06-26 23:14:39 +02:00
|
|
|
this._closeCallback = closeCallback;
|
2020-05-04 22:23:43 +02:00
|
|
|
this._composerVM = new ComposerViewModel(this);
|
2019-02-27 22:50:08 +01: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 {
|
|
|
|
this._timeline = await this._room.openTimeline();
|
2020-05-04 19:23:11 +02:00
|
|
|
this._timelineVM = new TimelineViewModel(this.childOptions({
|
|
|
|
room: this._room,
|
|
|
|
timeline: this._timeline,
|
|
|
|
ownUserId: this._ownUserId,
|
|
|
|
}));
|
|
|
|
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
|
|
|
}
|
2019-02-27 22:50:08 +01:00
|
|
|
}
|
|
|
|
|
2019-06-26 23:14:39 +02:00
|
|
|
dispose() {
|
|
|
|
// this races with enable, on the await openTimeline()
|
2019-02-27 22:50:08 +01:00
|
|
|
if (this._timeline) {
|
2019-06-01 18:29:23 +02:00
|
|
|
// will stop the timeline from delivering updates on entries
|
2019-02-27 22:50:08 +01:00
|
|
|
this._timeline.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-26 23:14:39 +02:00
|
|
|
close() {
|
|
|
|
this._closeCallback();
|
|
|
|
}
|
|
|
|
|
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() {
|
2020-05-04 19:23:11 +02:00
|
|
|
this.emitChange("name");
|
2019-02-27 22:50:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
get name() {
|
|
|
|
return this._room.name;
|
|
|
|
}
|
|
|
|
|
2019-06-01 18:29:23 +02:00
|
|
|
get timelineViewModel() {
|
|
|
|
return this._timelineVM;
|
2019-02-27 22:50:08 +01: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 "";
|
|
|
|
}
|
|
|
|
|
|
|
|
get avatarInitials() {
|
|
|
|
return avatarInitials(this._room.name);
|
2019-03-09 00:43:43 +01:00
|
|
|
}
|
2019-07-27 10:40:56 +02:00
|
|
|
|
2020-05-04 22:23:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
async _sendMessage(message) {
|
2019-07-29 19:54:21 +02:00
|
|
|
if (message) {
|
2019-09-15 12:23:26 +02:00
|
|
|
try {
|
2020-03-30 21:33:04 +02:00
|
|
|
await this._room.sendEvent("m.room.message", {msgtype: "m.text", 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
|
|
|
|
|
|
|
get composerViewModel() {
|
|
|
|
return this._composerVM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ComposerViewModel {
|
|
|
|
constructor(roomVM) {
|
|
|
|
this._roomVM = roomVM;
|
|
|
|
}
|
|
|
|
|
|
|
|
sendMessage(message) {
|
|
|
|
return this._roomVM._sendMessage(message);
|
|
|
|
}
|
2019-02-27 22:50:08 +01:00
|
|
|
}
|