vector-im-hydrogen-web/src/matrix/room/summary.js

158 lines
4.0 KiB
JavaScript
Raw Normal View History

2019-02-10 21:25:29 +01:00
// import SummaryMembers from "./members";
2018-12-21 14:35:24 +01:00
2019-02-10 21:25:29 +01:00
export default class RoomSummary {
constructor(roomId) {
2019-02-10 21:25:29 +01:00
// this._members = new SummaryMembers();
2018-12-21 14:35:24 +01:00
this._roomId = roomId;
2019-02-10 21:25:29 +01:00
this._name = null;
this._lastMessage = null;
this._unreadCount = null;
this._mentionCount = null;
this._isEncrypted = null;
this._isDirectMessage = null;
this._membership = null;
2018-12-21 14:35:24 +01:00
this._inviteCount = 0;
this._joinCount = 0;
}
get name() {
2019-02-10 21:25:29 +01:00
return this._name || "Room without a name";
2018-12-21 14:35:24 +01:00
}
get lastMessage() {
return this._lastMessageBody;
}
get inviteCount() {
return this._inviteCount;
}
get joinCount() {
return this._joinCount;
}
2019-02-10 21:25:29 +01:00
async applySync(roomResponse, membership, txn) {
const changed = this._processSyncResponse(roomResponse, membership);
2018-12-21 14:35:24 +01:00
if (changed) {
2019-02-10 21:25:29 +01:00
await this._persist(txn);
2018-12-21 14:35:24 +01:00
}
return changed;
}
2019-02-10 21:25:29 +01:00
async load(summary) {
2018-12-21 14:35:24 +01:00
this._roomId = summary.roomId;
2019-02-10 21:25:29 +01:00
this._name = summary.name;
this._lastMessage = summary.lastMessage;
this._unreadCount = summary.unreadCount;
this._mentionCount = summary.mentionCount;
this._isEncrypted = summary.isEncrypted;
this._isDirectMessage = summary.isDirectMessage;
this._membership = summary.membership;
2018-12-21 14:35:24 +01:00
this._inviteCount = summary.inviteCount;
this._joinCount = summary.joinCount;
}
2019-02-10 21:25:29 +01:00
_persist(txn) {
2018-12-21 14:35:24 +01:00
const summary = {
roomId: this._roomId,
heroes: this._heroes,
inviteCount: this._inviteCount,
joinCount: this._joinCount,
2019-02-10 21:25:29 +01:00
name: this._name,
lastMessageBody: this._lastMessageBody
2018-12-21 14:35:24 +01:00
};
2019-02-10 21:25:29 +01:00
return txn.roomSummary.set(summary);
2018-12-21 14:35:24 +01:00
}
2019-02-10 21:25:29 +01:00
_processSyncResponse(roomResponse, membership) {
2018-12-21 14:35:24 +01:00
// lets not do lazy loading for now
// if (roomResponse.summary) {
// this._updateSummary(roomResponse.summary);
// }
let changed = false;
2019-02-10 21:25:29 +01:00
if (membership !== this._membership) {
this._membership = membership;
changed = true;
}
if (roomResponse.state_events) {
2018-12-21 14:35:24 +01:00
changed = roomResponse.state_events.events.reduce((changed, e) => {
return this._processEvent(e) || changed;
}, changed);
}
2019-02-10 21:25:29 +01:00
if (roomResponse.timeline) {
changed = roomResponse.timeline.events.reduce((changed, e) => {
return this._processEvent(e) || changed;
}, changed);
}
2018-12-21 14:35:24 +01:00
return changed;
}
_processEvent(event) {
if (event.type === "m.room.name") {
const newName = event.content && event.content.name;
2019-02-10 21:25:29 +01:00
if (newName !== this._name) {
this._name = newName;
2018-12-21 14:35:24 +01:00
return true;
}
} else if (event.type === "m.room.member") {
return this._processMembership(event);
} else if (event.type === "m.room.message") {
const content = event.content;
const body = content && content.body;
const msgtype = content && content.msgtype;
if (msgtype === "m.text") {
this._lastMessageBody = body;
return true;
}
}
return false;
}
_processMembership(event) {
let changed = false;
const prevMembership = event.prev_content && event.prev_content.membership;
2019-02-10 21:25:29 +01:00
if (!event.content) {
return changed;
}
const content = event.content;
const membership = content.membership;
2018-12-21 14:35:24 +01:00
// danger of a replayed event getting the count out of sync
// but summary api will solve this.
// otherwise we'd have to store all the member ids in here
if (membership !== prevMembership) {
switch (prevMembership) {
2019-02-10 21:25:29 +01:00
case "invite": --this._inviteCount; break;
case "join": --this._joinCount; break;
2018-12-21 14:35:24 +01:00
}
switch (membership) {
2019-02-10 21:25:29 +01:00
case "invite": ++this._inviteCount; break;
case "join": ++this._joinCount; break;
2018-12-21 14:35:24 +01:00
}
changed = true;
}
2019-02-10 21:25:29 +01:00
// if (membership === "join" && content.name) {
// // TODO: avatar_url
// changed = this._members.applyMember(content.name, content.state_key) || changed;
// }
2018-12-21 14:35:24 +01:00
return changed;
}
_updateSummary(summary) {
const heroes = summary["m.heroes"];
const inviteCount = summary["m.joined_member_count"];
const joinCount = summary["m.invited_member_count"];
if (heroes) {
this._heroes = heroes;
}
if (Number.isInteger(inviteCount)) {
this._inviteCount = inviteCount;
}
if (Number.isInteger(joinCount)) {
this._joinCount = joinCount;
}
// this._recaculateNameIfNoneSet();
}
}