2020-08-05 22:08:55 +05:30
|
|
|
/*
|
|
|
|
Copyright 2020 Bruno Windels <bruno@windels.cloud>
|
|
|
|
|
|
|
|
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-08-28 18:06:00 +05:30
|
|
|
import {MEGOLM_ALGORITHM} from "../e2ee/common.js";
|
|
|
|
|
2020-09-14 20:04:07 +05:30
|
|
|
|
2020-09-23 22:41:11 +05:30
|
|
|
function applyTimelineEntries(data, timelineEntries, isInitialSync, canMarkUnread, ownUserId) {
|
2020-09-14 20:04:07 +05:30
|
|
|
if (timelineEntries.length) {
|
|
|
|
data = timelineEntries.reduce((data, entry) => {
|
|
|
|
return processTimelineEvent(data, entry,
|
2020-09-23 22:41:11 +05:30
|
|
|
isInitialSync, canMarkUnread, ownUserId);
|
2020-09-14 20:04:07 +05:30
|
|
|
}, data);
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2021-05-10 22:11:43 +05:30
|
|
|
export function reduceStateEvents(roomResponse, callback, value) {
|
|
|
|
const stateEvents = roomResponse?.state?.events;
|
|
|
|
// state comes before timeline
|
|
|
|
if (Array.isArray(stateEvents)) {
|
|
|
|
value = stateEvents.reduce(callback, value);
|
|
|
|
}
|
|
|
|
const timelineEvents = roomResponse?.timeline?.events;
|
|
|
|
// and after that state events in the timeline
|
|
|
|
if (Array.isArray(timelineEvents)) {
|
|
|
|
value = timelineEvents.reduce((data, event) => {
|
|
|
|
if (typeof event.state_key === "string") {
|
|
|
|
value = callback(value, event);
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}, value);
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
2020-09-14 20:04:07 +05:30
|
|
|
|
2021-05-10 22:11:43 +05:30
|
|
|
function applySyncResponse(data, roomResponse, membership) {
|
2020-03-15 01:16:49 +05:30
|
|
|
if (roomResponse.summary) {
|
|
|
|
data = updateSummary(data, roomResponse.summary);
|
|
|
|
}
|
|
|
|
if (membership !== data.membership) {
|
|
|
|
data = data.cloneIfNeeded();
|
|
|
|
data.membership = membership;
|
|
|
|
}
|
2020-08-28 00:22:51 +05:30
|
|
|
if (roomResponse.account_data) {
|
|
|
|
data = roomResponse.account_data.events.reduce(processRoomAccountData, data);
|
|
|
|
}
|
2021-05-10 22:11:43 +05:30
|
|
|
// process state events in state and in timeline.
|
2020-09-14 20:04:07 +05:30
|
|
|
// non-state events are handled by applyTimelineEntries
|
|
|
|
// so decryption is handled properly
|
2021-05-10 22:11:43 +05:30
|
|
|
data = reduceStateEvents(roomResponse, processStateEvent, data);
|
2020-08-20 20:37:02 +05:30
|
|
|
const unreadNotifications = roomResponse.unread_notifications;
|
|
|
|
if (unreadNotifications) {
|
2021-05-04 17:03:30 +05:30
|
|
|
data = processNotificationCounts(data, unreadNotifications);
|
2020-08-20 20:37:02 +05:30
|
|
|
}
|
2020-03-15 01:16:49 +05:30
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2021-05-04 17:03:30 +05:30
|
|
|
function processNotificationCounts(data, unreadNotifications) {
|
|
|
|
const highlightCount = unreadNotifications.highlight_count || 0;
|
|
|
|
if (highlightCount !== data.highlightCount) {
|
|
|
|
data = data.cloneIfNeeded();
|
|
|
|
data.highlightCount = highlightCount;
|
|
|
|
}
|
|
|
|
const notificationCount = unreadNotifications.notification_count;
|
|
|
|
if (notificationCount !== data.notificationCount) {
|
|
|
|
data = data.cloneIfNeeded();
|
|
|
|
data.notificationCount = notificationCount;
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2020-08-28 00:22:51 +05:30
|
|
|
function processRoomAccountData(data, event) {
|
|
|
|
if (event?.type === "m.tag") {
|
|
|
|
let tags = event?.content?.tags;
|
|
|
|
if (!tags || Array.isArray(tags) || typeof tags !== "object") {
|
|
|
|
tags = null;
|
|
|
|
}
|
|
|
|
data = data.cloneIfNeeded();
|
|
|
|
data.tags = tags;
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2021-04-20 19:41:21 +05:30
|
|
|
export function processStateEvent(data, event) {
|
2020-03-15 01:16:49 +05:30
|
|
|
if (event.type === "m.room.encryption") {
|
2020-08-28 18:06:00 +05:30
|
|
|
const algorithm = event.content?.algorithm;
|
|
|
|
if (!data.encryption && algorithm === MEGOLM_ALGORITHM) {
|
2020-03-15 01:16:49 +05:30
|
|
|
data = data.cloneIfNeeded();
|
2020-08-28 18:06:00 +05:30
|
|
|
data.encryption = event.content;
|
2020-03-15 01:16:49 +05:30
|
|
|
}
|
2020-08-21 15:25:25 +05:30
|
|
|
} else if (event.type === "m.room.name") {
|
2020-08-20 20:32:51 +05:30
|
|
|
const newName = event.content?.name;
|
2020-03-15 01:16:49 +05:30
|
|
|
if (newName !== data.name) {
|
|
|
|
data = data.cloneIfNeeded();
|
|
|
|
data.name = newName;
|
|
|
|
}
|
2020-08-21 15:25:25 +05:30
|
|
|
} else if (event.type === "m.room.avatar") {
|
2020-08-20 20:32:51 +05:30
|
|
|
const newUrl = event.content?.url;
|
|
|
|
if (newUrl !== data.avatarUrl) {
|
|
|
|
data = data.cloneIfNeeded();
|
|
|
|
data.avatarUrl = newUrl;
|
|
|
|
}
|
2020-08-21 18:05:23 +05:30
|
|
|
} else if (event.type === "m.room.canonical_alias") {
|
|
|
|
const content = event.content;
|
|
|
|
data = data.cloneIfNeeded();
|
|
|
|
data.canonicalAlias = content.alias;
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2020-09-23 22:41:11 +05:30
|
|
|
function processTimelineEvent(data, eventEntry, isInitialSync, canMarkUnread, ownUserId) {
|
2020-09-14 17:31:47 +05:30
|
|
|
if (eventEntry.eventType === "m.room.message") {
|
2020-09-14 20:04:07 +05:30
|
|
|
if (!data.lastMessageTimestamp || eventEntry.timestamp > data.lastMessageTimestamp) {
|
|
|
|
data = data.cloneIfNeeded();
|
|
|
|
data.lastMessageTimestamp = eventEntry.timestamp;
|
|
|
|
}
|
2020-09-23 22:41:11 +05:30
|
|
|
if (!isInitialSync && eventEntry.sender !== ownUserId && canMarkUnread) {
|
2020-09-14 20:04:07 +05:30
|
|
|
data = data.cloneIfNeeded();
|
2020-08-21 17:15:38 +05:30
|
|
|
data.isUnread = true;
|
|
|
|
}
|
2020-09-22 21:52:37 +05:30
|
|
|
}
|
2020-03-15 01:16:49 +05:30
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateSummary(data, summary) {
|
|
|
|
const heroes = summary["m.heroes"];
|
2020-08-21 21:41:26 +05:30
|
|
|
const joinCount = summary["m.joined_member_count"];
|
|
|
|
const inviteCount = summary["m.invited_member_count"];
|
2020-08-31 19:39:38 +05:30
|
|
|
// TODO: we could easily calculate if all members are available here and set hasFetchedMembers?
|
|
|
|
// so we can avoid calling /members...
|
|
|
|
// we'd need to do a count query in the roomMembers store though ...
|
2020-08-21 21:41:07 +05:30
|
|
|
if (heroes && Array.isArray(heroes)) {
|
2020-03-15 01:16:49 +05:30
|
|
|
data = data.cloneIfNeeded();
|
|
|
|
data.heroes = heroes;
|
|
|
|
}
|
|
|
|
if (Number.isInteger(inviteCount)) {
|
|
|
|
data = data.cloneIfNeeded();
|
|
|
|
data.inviteCount = inviteCount;
|
|
|
|
}
|
|
|
|
if (Number.isInteger(joinCount)) {
|
|
|
|
data = data.cloneIfNeeded();
|
|
|
|
data.joinCount = joinCount;
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2021-04-20 21:09:46 +05:30
|
|
|
function applyInvite(data, invite) {
|
|
|
|
if (data.isDirectMessage !== invite.isDirectMessage) {
|
|
|
|
data = data.cloneIfNeeded();
|
|
|
|
data.isDirectMessage = invite.isDirectMessage;
|
2021-05-05 20:41:53 +05:30
|
|
|
if (invite.isDirectMessage) {
|
|
|
|
data.dmUserId = invite.inviter?.userId;
|
|
|
|
} else {
|
|
|
|
data.dmUserId = null;
|
|
|
|
}
|
2021-04-20 21:09:46 +05:30
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2021-04-20 19:41:21 +05:30
|
|
|
export class SummaryData {
|
2020-03-15 01:16:49 +05:30
|
|
|
constructor(copy, roomId) {
|
|
|
|
this.roomId = copy ? copy.roomId : roomId;
|
|
|
|
this.name = copy ? copy.name : null;
|
2020-08-21 15:25:47 +05:30
|
|
|
this.lastMessageTimestamp = copy ? copy.lastMessageTimestamp : null;
|
2020-08-21 17:40:53 +05:30
|
|
|
this.isUnread = copy ? copy.isUnread : false;
|
2020-08-28 18:06:00 +05:30
|
|
|
this.encryption = copy ? copy.encryption : null;
|
2020-03-15 01:16:49 +05:30
|
|
|
this.membership = copy ? copy.membership : null;
|
|
|
|
this.inviteCount = copy ? copy.inviteCount : 0;
|
|
|
|
this.joinCount = copy ? copy.joinCount : 0;
|
|
|
|
this.heroes = copy ? copy.heroes : null;
|
|
|
|
this.canonicalAlias = copy ? copy.canonicalAlias : null;
|
2020-06-27 02:56:24 +05:30
|
|
|
this.hasFetchedMembers = copy ? copy.hasFetchedMembers : false;
|
2020-08-31 12:23:47 +05:30
|
|
|
this.isTrackingMembers = copy ? copy.isTrackingMembers : false;
|
2020-08-20 20:32:51 +05:30
|
|
|
this.avatarUrl = copy ? copy.avatarUrl : null;
|
2020-08-20 20:37:02 +05:30
|
|
|
this.notificationCount = copy ? copy.notificationCount : 0;
|
|
|
|
this.highlightCount = copy ? copy.highlightCount : 0;
|
2020-08-28 00:22:51 +05:30
|
|
|
this.tags = copy ? copy.tags : null;
|
2021-04-20 21:09:46 +05:30
|
|
|
this.isDirectMessage = copy ? copy.isDirectMessage : false;
|
|
|
|
this.dmUserId = copy ? copy.dmUserId : null;
|
2020-03-15 01:16:49 +05:30
|
|
|
this.cloned = copy ? true : false;
|
|
|
|
}
|
|
|
|
|
2021-03-03 01:59:32 +05:30
|
|
|
diff(other) {
|
|
|
|
const props = Object.getOwnPropertyNames(this);
|
|
|
|
return props.reduce((diff, prop) => {
|
|
|
|
if (prop !== "cloned") {
|
|
|
|
if (this[prop] !== other[prop]) {
|
|
|
|
diff[prop] = this[prop];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return diff;
|
|
|
|
}, {});
|
|
|
|
}
|
|
|
|
|
2020-03-15 01:16:49 +05:30
|
|
|
cloneIfNeeded() {
|
|
|
|
if (this.cloned) {
|
|
|
|
return this;
|
|
|
|
} else {
|
|
|
|
return new SummaryData(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
serialize() {
|
2021-05-04 17:01:04 +05:30
|
|
|
return Object.entries(this).reduce((obj, [key, value]) => {
|
|
|
|
if (key !== "cloned" && value !== null) {
|
|
|
|
obj[key] = value;
|
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
}, {});
|
2020-03-15 01:16:49 +05:30
|
|
|
}
|
2018-12-21 19:05:24 +05:30
|
|
|
|
2020-09-23 22:41:11 +05:30
|
|
|
applyTimelineEntries(timelineEntries, isInitialSync, canMarkUnread, ownUserId) {
|
|
|
|
return applyTimelineEntries(this, timelineEntries, isInitialSync, canMarkUnread, ownUserId);
|
2020-08-21 21:41:07 +05:30
|
|
|
}
|
|
|
|
|
2021-05-10 22:11:43 +05:30
|
|
|
applySyncResponse(roomResponse, membership) {
|
|
|
|
return applySyncResponse(this, roomResponse, membership);
|
2020-08-28 18:06:00 +05:30
|
|
|
}
|
|
|
|
|
2021-04-20 21:09:46 +05:30
|
|
|
applyInvite(invite) {
|
|
|
|
return applyInvite(this, invite);
|
|
|
|
}
|
|
|
|
|
2020-08-21 22:33:21 +05:30
|
|
|
get needsHeroes() {
|
2020-09-23 17:56:14 +05:30
|
|
|
return !this.name && !this.canonicalAlias && this.heroes && this.heroes.length > 0;
|
2020-08-21 15:25:47 +05:30
|
|
|
}
|
2021-05-06 18:56:48 +05:30
|
|
|
|
|
|
|
isNewJoin(oldData) {
|
|
|
|
return this.membership === "join" && oldData.membership !== "join";
|
|
|
|
}
|
2020-09-23 17:56:14 +05:30
|
|
|
}
|
2020-08-21 15:25:47 +05:30
|
|
|
|
2020-09-23 17:56:14 +05:30
|
|
|
export class RoomSummary {
|
|
|
|
constructor(roomId) {
|
2020-09-24 20:10:51 +05:30
|
|
|
this._data = null;
|
|
|
|
this.applyChanges(new SummaryData(null, roomId));
|
2018-12-21 19:05:24 +05:30
|
|
|
}
|
|
|
|
|
2020-09-23 17:56:14 +05:30
|
|
|
get data() {
|
|
|
|
return this._data;
|
2020-09-22 21:52:37 +05:30
|
|
|
}
|
|
|
|
|
2020-08-21 15:26:10 +05:30
|
|
|
writeClearUnread(txn) {
|
|
|
|
const data = new SummaryData(this._data);
|
|
|
|
data.isUnread = false;
|
|
|
|
data.notificationCount = 0;
|
|
|
|
data.highlightCount = 0;
|
|
|
|
txn.roomSummary.set(data.serialize());
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2020-06-27 02:56:24 +05:30
|
|
|
writeHasFetchedMembers(value, txn) {
|
|
|
|
const data = new SummaryData(this._data);
|
|
|
|
data.hasFetchedMembers = value;
|
|
|
|
txn.roomSummary.set(data.serialize());
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2020-08-31 12:23:47 +05:30
|
|
|
writeIsTrackingMembers(value, txn) {
|
|
|
|
const data = new SummaryData(this._data);
|
|
|
|
data.isTrackingMembers = value;
|
|
|
|
txn.roomSummary.set(data.serialize());
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2020-09-23 17:56:14 +05:30
|
|
|
writeData(data, txn) {
|
2020-03-15 01:16:49 +05:30
|
|
|
if (data !== this._data) {
|
|
|
|
txn.roomSummary.set(data.serialize());
|
|
|
|
return data;
|
2018-12-21 19:05:24 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-05 20:33:22 +05:30
|
|
|
/** move summary to archived store when leaving the room */
|
2021-05-06 18:56:48 +05:30
|
|
|
writeArchivedData(data, txn) {
|
2021-05-05 20:33:22 +05:30
|
|
|
if (data !== this._data) {
|
|
|
|
txn.archivedRoomSummary.set(data.serialize());
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-23 17:56:14 +05:30
|
|
|
async writeAndApplyData(data, storage) {
|
|
|
|
if (data === this._data) {
|
2020-09-23 22:40:25 +05:30
|
|
|
return false;
|
2020-09-23 17:56:14 +05:30
|
|
|
}
|
2021-03-05 00:17:02 +05:30
|
|
|
const txn = await storage.readWriteTxn([
|
2020-09-14 20:04:07 +05:30
|
|
|
storage.storeNames.roomSummary,
|
|
|
|
]);
|
|
|
|
try {
|
|
|
|
txn.roomSummary.set(data.serialize());
|
|
|
|
} catch (err) {
|
|
|
|
txn.abort();
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
await txn.complete();
|
|
|
|
this.applyChanges(data);
|
2020-09-23 22:40:25 +05:30
|
|
|
return true;
|
2020-09-14 20:04:07 +05:30
|
|
|
}
|
|
|
|
|
2020-06-27 02:56:24 +05:30
|
|
|
applyChanges(data) {
|
2020-03-15 01:16:49 +05:30
|
|
|
this._data = data;
|
2020-09-23 17:56:14 +05:30
|
|
|
// clear cloned flag, so cloneIfNeeded makes a copy and
|
|
|
|
// this._data is not modified if any field is changed.
|
|
|
|
this._data.cloned = false;
|
2020-03-15 01:16:49 +05:30
|
|
|
}
|
2018-12-21 19:05:24 +05:30
|
|
|
|
2020-03-15 01:16:49 +05:30
|
|
|
async load(summary) {
|
2020-09-24 20:10:51 +05:30
|
|
|
this.applyChanges(new SummaryData(summary));
|
2018-12-21 19:05:24 +05:30
|
|
|
}
|
2020-03-15 01:16:49 +05:30
|
|
|
}
|
2018-12-21 19:05:24 +05:30
|
|
|
|
2020-03-15 01:16:49 +05:30
|
|
|
export function tests() {
|
|
|
|
return {
|
2021-05-04 17:01:04 +05:30
|
|
|
"serialize doesn't include null fields or cloned": assert => {
|
|
|
|
const roomId = "!123:hs.tld";
|
|
|
|
const data = new SummaryData(null, roomId);
|
|
|
|
const clone = data.cloneIfNeeded();
|
|
|
|
const serialized = clone.serialize();
|
|
|
|
assert.strictEqual(serialized.cloned, undefined);
|
|
|
|
assert.equal(serialized.roomId, roomId);
|
|
|
|
const nullCount = Object.values(serialized).reduce((count, value) => count + value === null ? 1 : 0, 0);
|
|
|
|
assert.strictEqual(nullCount, 0);
|
2019-10-13 11:18:33 +05:30
|
|
|
}
|
2020-03-15 01:16:49 +05:30
|
|
|
}
|
2018-12-21 19:05:24 +05:30
|
|
|
}
|