This repository has been archived on 2022-08-19. You can view files and clone it, but cannot push or open issues or pull requests.
hydrogen-web/src/domain/session/room/RoomViewModel.js

87 lines
2.5 KiB
JavaScript
Raw Normal View History

2020-04-21 01:05:53 +05:30
import {EventEmitter} from "../../../utils/EventEmitter.js";
import {TimelineViewModel} from "./timeline/TimelineViewModel.js";
import {avatarInitials} from "../avatar.js";
export class RoomViewModel extends EventEmitter {
constructor({room, ownUserId, closeCallback}) {
super();
this._room = room;
2019-06-16 14:23:23 +05:30
this._ownUserId = ownUserId;
this._timeline = null;
this._timelineVM = null;
this._onRoomChange = this._onRoomChange.bind(this);
this._timelineError = null;
this._sendError = null;
this._closeCallback = closeCallback;
}
async load() {
this._room.on("change", this._onRoomChange);
try {
this._timeline = await this._room.openTimeline();
2020-03-22 04:10:40 +05:30
this._timelineVM = new TimelineViewModel(this._room, this._timeline, this._ownUserId);
2019-06-02 18:29:30 +05:30
this.emit("change", "timelineViewModel");
} catch (err) {
2019-06-02 18:29:30 +05:30
console.error(`room.openTimeline(): ${err.message}:\n${err.stack}`);
this._timelineError = err;
this.emit("change", "error");
}
}
dispose() {
// this races with enable, on the await openTimeline()
if (this._timeline) {
// will stop the timeline from delivering updates on entries
this._timeline.close();
}
}
close() {
this._closeCallback();
}
// room doesn't tell us yet which fields changed,
// so emit all fields originating from summary
_onRoomChange() {
this.emit("change", "name");
}
get name() {
return this._room.name;
}
get timelineViewModel() {
return this._timelineVM;
}
get error() {
if (this._timelineError) {
return `Something went wrong loading the timeline: ${this._timelineError.message}`;
}
if (this._sendError) {
return `Something went wrong sending your message: ${this._sendError.message}`;
}
return "";
}
get avatarInitials() {
return avatarInitials(this._room.name);
}
async sendMessage(message) {
2019-07-29 23:24:21 +05:30
if (message) {
2019-09-15 15:53:26 +05:30
try {
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}`);
this._sendError = err;
this._timelineError = null;
2019-09-15 15:53:26 +05:30
this.emit("change", "error");
return false;
}
return true;
2019-07-29 23:24:21 +05:30
}
2019-09-15 15:53:26 +05:30
return false;
}
}