2018-01-26 00:16:03 +03:00
|
|
|
/*
|
2019-02-14 05:49:28 +03:00
|
|
|
Copyright 2018, 2019 New Vector Ltd
|
2018-01-26 00:16:03 +03:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
import {Store} from 'flux/utils';
|
|
|
|
import dis from '../dispatcher';
|
|
|
|
import DMRoomMap from '../utils/DMRoomMap';
|
2018-02-06 18:15:47 +03:00
|
|
|
import Unread from '../Unread';
|
2018-10-12 23:35:54 +03:00
|
|
|
import SettingsStore from "../settings/SettingsStore";
|
2018-01-26 00:16:03 +03:00
|
|
|
|
2019-02-14 05:49:28 +03:00
|
|
|
/*
|
|
|
|
Room sorting algorithm:
|
|
|
|
* Always prefer to have red > grey > bold > idle
|
|
|
|
* The room being viewed should be sticky (not jump down to the idle list)
|
|
|
|
* When switching to a new room, sort the last sticky room to the top of the idle list.
|
|
|
|
|
|
|
|
The approach taken by the store is to generate an initial representation of all the
|
|
|
|
tagged lists (accepting that it'll take a little bit longer to calculate) and make
|
|
|
|
small changes to that over time. This results in quick changes to the room list while
|
|
|
|
also having update operations feel more like popping/pushing to a stack.
|
|
|
|
*/
|
|
|
|
|
|
|
|
const CATEGORY_RED = "red"; // Mentions in the room
|
|
|
|
const CATEGORY_GREY = "grey"; // Unread notified messages (not mentions)
|
|
|
|
const CATEGORY_BOLD = "bold"; // Unread messages (not notified, 'Mentions Only' rooms)
|
|
|
|
const CATEGORY_IDLE = "idle"; // Nothing of interest
|
2019-02-13 23:00:35 +03:00
|
|
|
|
|
|
|
const CATEGORY_ORDER = [CATEGORY_RED, CATEGORY_GREY, CATEGORY_BOLD, CATEGORY_IDLE];
|
|
|
|
const LIST_ORDERS = {
|
|
|
|
"m.favourite": "manual",
|
|
|
|
"im.vector.fake.invite": "recent",
|
|
|
|
"im.vector.fake.recent": "recent",
|
|
|
|
"im.vector.fake.direct": "recent",
|
|
|
|
"m.lowpriority": "recent",
|
|
|
|
"im.vector.fake.archived": "recent",
|
|
|
|
};
|
|
|
|
|
2019-02-26 22:43:10 +03:00
|
|
|
/**
|
|
|
|
* Identifier for the "breadcrumb" (or "sort by most important room first") algorithm.
|
|
|
|
* Includes a provision for keeping the currently open room from flying down the room
|
|
|
|
* list.
|
|
|
|
* @type {string}
|
|
|
|
*/
|
|
|
|
const ALGO_IMPORTANCE = "importance";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Identifier for classic sorting behaviour: sort by the most recent message first.
|
|
|
|
* @type {string}
|
|
|
|
*/
|
|
|
|
const ALGO_RECENT = "recent";
|
|
|
|
|
2018-01-26 00:16:03 +03:00
|
|
|
/**
|
|
|
|
* A class for storing application state for categorising rooms in
|
|
|
|
* the RoomList.
|
|
|
|
*/
|
|
|
|
class RoomListStore extends Store {
|
|
|
|
constructor() {
|
|
|
|
super(dis);
|
|
|
|
|
|
|
|
this._init();
|
2018-02-06 18:15:47 +03:00
|
|
|
this._getManualComparator = this._getManualComparator.bind(this);
|
|
|
|
this._recentsComparator = this._recentsComparator.bind(this);
|
2018-01-26 00:16:03 +03:00
|
|
|
}
|
|
|
|
|
2019-02-23 02:57:41 +03:00
|
|
|
/**
|
2019-02-26 22:43:10 +03:00
|
|
|
* Changes the sorting algorithm used by the RoomListStore.
|
|
|
|
* @param {string} algorithm The new algorithm to use. Should be one of the ALGO_* constants.
|
2019-02-23 02:57:41 +03:00
|
|
|
*/
|
2019-02-26 22:43:10 +03:00
|
|
|
updateSortingAlgorithm(algorithm) {
|
|
|
|
// Dev note: We only have two algorithms at the moment, but it isn't impossible that we want
|
|
|
|
// multiple in the future. Also constants make things slightly clearer.
|
|
|
|
const byImportance = algorithm === ALGO_IMPORTANCE;
|
|
|
|
console.log("Updating room sorting algorithm: sortByImportance=" + byImportance);
|
|
|
|
this._setState({orderRoomsByImportance: byImportance});
|
|
|
|
|
|
|
|
// Trigger a resort of the entire list to reflect the change in algorithm
|
|
|
|
this._generateInitialRoomLists();
|
2019-02-23 02:57:41 +03:00
|
|
|
}
|
|
|
|
|
2018-01-26 00:16:03 +03:00
|
|
|
_init() {
|
|
|
|
// Initialise state
|
2019-02-13 23:00:35 +03:00
|
|
|
const defaultLists = {
|
|
|
|
"m.server_notice": [/* { room: js-sdk room, category: string } */],
|
|
|
|
"im.vector.fake.invite": [],
|
|
|
|
"m.favourite": [],
|
|
|
|
"im.vector.fake.recent": [],
|
|
|
|
"im.vector.fake.direct": [],
|
|
|
|
"m.lowpriority": [],
|
|
|
|
"im.vector.fake.archived": [],
|
|
|
|
};
|
2018-01-26 00:16:03 +03:00
|
|
|
this._state = {
|
2019-02-13 23:00:35 +03:00
|
|
|
// The rooms in these arrays are ordered according to either the
|
|
|
|
// 'recents' behaviour or 'manual' behaviour.
|
|
|
|
lists: defaultLists,
|
|
|
|
presentationLists: defaultLists, // like `lists`, but with arrays of rooms instead
|
2018-01-26 00:16:03 +03:00
|
|
|
ready: false,
|
2019-02-13 23:00:35 +03:00
|
|
|
stickyRoomId: null,
|
2019-02-23 02:57:41 +03:00
|
|
|
orderRoomsByImportance: true,
|
2018-01-26 00:16:03 +03:00
|
|
|
};
|
2019-02-23 02:57:41 +03:00
|
|
|
|
|
|
|
SettingsStore.monitorSetting('RoomList.orderByImportance', null);
|
2019-02-26 22:43:10 +03:00
|
|
|
SettingsStore.monitorSetting('feature_custom_tags', null);
|
2018-01-26 00:16:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
_setState(newState) {
|
2019-02-14 05:49:28 +03:00
|
|
|
// If we're changing the lists, transparently change the presentation lists (which
|
|
|
|
// is given to requesting components). This dramatically simplifies our code elsewhere
|
|
|
|
// while also ensuring we don't need to update all the calling components to support
|
|
|
|
// categories.
|
2019-02-13 23:00:35 +03:00
|
|
|
if (newState['lists']) {
|
|
|
|
const presentationLists = {};
|
|
|
|
for (const key of Object.keys(newState['lists'])) {
|
|
|
|
presentationLists[key] = newState['lists'][key].map((e) => e.room);
|
|
|
|
}
|
|
|
|
newState['presentationLists'] = presentationLists;
|
|
|
|
}
|
2018-01-26 00:16:03 +03:00
|
|
|
this._state = Object.assign(this._state, newState);
|
|
|
|
this.__emitChange();
|
|
|
|
}
|
|
|
|
|
2019-02-14 05:35:01 +03:00
|
|
|
__onDispatch(payload) {
|
2019-02-13 23:00:35 +03:00
|
|
|
const logicallyReady = this._matrixClient && this._state.ready;
|
2018-01-26 00:16:03 +03:00
|
|
|
switch (payload.action) {
|
2019-02-23 02:57:41 +03:00
|
|
|
case 'setting_updated': {
|
2019-02-25 23:39:08 +03:00
|
|
|
if (payload.settingName === 'RoomList.orderByImportance') {
|
2019-02-26 22:43:10 +03:00
|
|
|
this.updateSortingAlgorithm(payload.newValue === true ? ALGO_IMPORTANCE : ALGO_RECENT);
|
2019-02-25 23:39:08 +03:00
|
|
|
} else if (payload.settingName === 'feature_custom_tags') {
|
2019-02-26 22:43:10 +03:00
|
|
|
this._setState({tagsEnabled: payload.newValue});
|
|
|
|
this._generateInitialRoomLists(); // Tags means we have to start from scratch
|
2019-02-25 23:39:08 +03:00
|
|
|
}
|
2019-02-23 02:57:41 +03:00
|
|
|
}
|
|
|
|
break;
|
2018-01-26 00:16:03 +03:00
|
|
|
// Initialise state after initial sync
|
|
|
|
case 'MatrixActions.sync': {
|
|
|
|
if (!(payload.prevState !== 'PREPARED' && payload.state === 'PREPARED')) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-02-25 23:39:08 +03:00
|
|
|
this._setState({tagsEnabled: SettingsStore.isFeatureEnabled("feature_custom_tags")});
|
|
|
|
|
2018-02-05 20:29:22 +03:00
|
|
|
this._matrixClient = payload.matrixClient;
|
2019-02-26 22:43:10 +03:00
|
|
|
|
|
|
|
const algorithm = SettingsStore.getValue("RoomList.orderByImportance")
|
|
|
|
? ALGO_IMPORTANCE : ALGO_RECENT;
|
|
|
|
this.updateSortingAlgorithm(algorithm);
|
2018-01-26 00:16:03 +03:00
|
|
|
}
|
|
|
|
break;
|
2019-02-14 03:03:27 +03:00
|
|
|
case 'MatrixActions.Room.receipt': {
|
|
|
|
if (!logicallyReady) break;
|
|
|
|
|
2019-02-14 05:16:11 +03:00
|
|
|
// First see if the receipt event is for our own user. If it was, trigger
|
|
|
|
// a room update (we probably read the room on a different device).
|
2019-02-14 03:03:27 +03:00
|
|
|
const myUserId = this._matrixClient.getUserId();
|
|
|
|
for (const eventId of Object.keys(payload.event.getContent())) {
|
|
|
|
const receiptUsers = Object.keys(payload.event.getContent()[eventId]['m.read'] || {});
|
|
|
|
if (receiptUsers.includes(myUserId)) {
|
|
|
|
this._roomUpdateTriggered(payload.room.roomId);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2018-01-26 00:16:03 +03:00
|
|
|
case 'MatrixActions.Room.tags': {
|
2019-02-13 23:00:35 +03:00
|
|
|
if (!logicallyReady) break;
|
2019-02-14 00:22:00 +03:00
|
|
|
// TODO: Figure out which rooms changed in the tag and only change those.
|
|
|
|
// This is very blunt and wipes out the sticky room stuff
|
|
|
|
this._generateInitialRoomLists();
|
2018-01-26 00:16:03 +03:00
|
|
|
}
|
|
|
|
break;
|
2018-02-16 17:16:50 +03:00
|
|
|
case 'MatrixActions.Room.timeline': {
|
2019-02-13 23:00:35 +03:00
|
|
|
if (!logicallyReady ||
|
2018-02-16 17:16:50 +03:00
|
|
|
!payload.isLiveEvent ||
|
2018-02-19 12:56:03 +03:00
|
|
|
!payload.isLiveUnfilteredRoomTimelineEvent ||
|
2018-02-16 17:16:50 +03:00
|
|
|
!this._eventTriggersRecentReorder(payload.event)
|
2019-02-13 23:00:35 +03:00
|
|
|
) {
|
|
|
|
break;
|
|
|
|
}
|
2018-11-02 01:28:13 +03:00
|
|
|
|
2019-02-13 23:00:35 +03:00
|
|
|
this._roomUpdateTriggered(payload.event.getRoomId());
|
2018-02-16 17:16:50 +03:00
|
|
|
}
|
|
|
|
break;
|
2018-02-23 13:32:33 +03:00
|
|
|
// When an event is decrypted, it could mean we need to reorder the room
|
|
|
|
// list because we now know the type of the event.
|
|
|
|
case 'MatrixActions.Event.decrypted': {
|
2019-02-13 23:00:35 +03:00
|
|
|
if (!logicallyReady) break;
|
2018-02-23 17:54:00 +03:00
|
|
|
|
|
|
|
const roomId = payload.event.getRoomId();
|
|
|
|
|
|
|
|
// We may have decrypted an event without a roomId (e.g to_device)
|
|
|
|
if (!roomId) break;
|
|
|
|
|
|
|
|
const room = this._matrixClient.getRoom(roomId);
|
|
|
|
|
|
|
|
// We somehow decrypted an event for a room our client is unaware of
|
|
|
|
if (!room) break;
|
|
|
|
|
2018-02-23 13:32:33 +03:00
|
|
|
const liveTimeline = room.getLiveTimeline();
|
|
|
|
const eventTimeline = room.getTimelineForEvent(payload.event.getId());
|
|
|
|
|
2018-02-23 17:54:00 +03:00
|
|
|
// Either this event was not added to the live timeline (e.g. pagination)
|
|
|
|
// or it doesn't affect the ordering of the room list.
|
2019-02-13 23:00:35 +03:00
|
|
|
if (liveTimeline !== eventTimeline || !this._eventTriggersRecentReorder(payload.event)) {
|
|
|
|
break;
|
|
|
|
}
|
2018-11-02 01:28:13 +03:00
|
|
|
|
2019-02-13 23:00:35 +03:00
|
|
|
this._roomUpdateTriggered(roomId);
|
2018-02-23 13:32:33 +03:00
|
|
|
}
|
|
|
|
break;
|
2018-02-05 21:06:29 +03:00
|
|
|
case 'MatrixActions.accountData': {
|
2019-02-13 23:00:35 +03:00
|
|
|
if (!logicallyReady) break;
|
2018-02-05 21:06:29 +03:00
|
|
|
if (payload.event_type !== 'm.direct') break;
|
2019-02-14 00:22:00 +03:00
|
|
|
// TODO: Figure out which rooms changed in the direct chat and only change those.
|
|
|
|
// This is very blunt and wipes out the sticky room stuff
|
|
|
|
this._generateInitialRoomLists();
|
2018-11-05 05:47:24 +03:00
|
|
|
}
|
|
|
|
break;
|
2018-09-17 20:14:52 +03:00
|
|
|
case 'MatrixActions.Room.myMembership': {
|
2019-02-13 23:00:35 +03:00
|
|
|
if (!logicallyReady) break;
|
2019-02-25 05:44:47 +03:00
|
|
|
this._roomUpdateTriggered(payload.room.roomId, true);
|
2018-02-05 21:27:50 +03:00
|
|
|
}
|
|
|
|
break;
|
2018-02-20 20:57:46 +03:00
|
|
|
// This could be a new room that we've been invited to, joined or created
|
|
|
|
// we won't get a RoomMember.membership for these cases if we're not already
|
|
|
|
// a member.
|
|
|
|
case 'MatrixActions.Room': {
|
2019-02-13 23:00:35 +03:00
|
|
|
if (!logicallyReady) break;
|
2019-02-25 05:44:47 +03:00
|
|
|
this._roomUpdateTriggered(payload.room.roomId, true);
|
2018-02-20 20:57:46 +03:00
|
|
|
}
|
|
|
|
break;
|
2019-02-14 00:08:19 +03:00
|
|
|
// TODO: Re-enable optimistic updates when we support dragging again
|
|
|
|
// case 'RoomListActions.tagRoom.pending': {
|
|
|
|
// if (!logicallyReady) break;
|
|
|
|
// // XXX: we only show one optimistic update at any one time.
|
|
|
|
// // Ideally we should be making a list of in-flight requests
|
|
|
|
// // that are backed by transaction IDs. Until the js-sdk
|
|
|
|
// // supports this, we're stuck with only being able to use
|
|
|
|
// // the most recent optimistic update.
|
|
|
|
// console.log("!! Optimistic tag: ", payload);
|
|
|
|
// }
|
|
|
|
// break;
|
|
|
|
// case 'RoomListActions.tagRoom.failure': {
|
|
|
|
// if (!logicallyReady) break;
|
|
|
|
// // Reset state according to js-sdk
|
|
|
|
// console.log("!! Optimistic tag failure: ", payload);
|
|
|
|
// }
|
2019-02-14 00:22:00 +03:00
|
|
|
// break;
|
2018-01-26 00:16:03 +03:00
|
|
|
case 'on_logged_out': {
|
|
|
|
// Reset state without pushing an update to the view, which generally assumes that
|
|
|
|
// the matrix client isn't `null` and so causing a re-render will cause NPEs.
|
|
|
|
this._init();
|
2018-02-06 18:15:47 +03:00
|
|
|
this._matrixClient = null;
|
2018-01-26 00:16:03 +03:00
|
|
|
}
|
|
|
|
break;
|
2019-02-13 23:00:35 +03:00
|
|
|
case 'view_room': {
|
|
|
|
if (!logicallyReady) break;
|
|
|
|
|
|
|
|
// Note: it is important that we set a new stickyRoomId before setting the old room
|
|
|
|
// to IDLE. If we don't, the wrong room gets counted as sticky.
|
2019-02-14 05:49:28 +03:00
|
|
|
const currentStickyId = this._state.stickyRoomId;
|
2019-02-13 23:00:35 +03:00
|
|
|
this._setState({stickyRoomId: payload.room_id});
|
2019-02-14 05:49:28 +03:00
|
|
|
if (currentStickyId) {
|
|
|
|
this._setRoomCategory(this._matrixClient.getRoom(currentStickyId), CATEGORY_IDLE);
|
2019-02-13 23:00:35 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2019-02-14 05:49:28 +03:00
|
|
|
}
|
2019-02-13 23:00:35 +03:00
|
|
|
|
2019-02-25 05:44:47 +03:00
|
|
|
_roomUpdateTriggered(roomId, ignoreSticky) {
|
2019-02-14 05:49:28 +03:00
|
|
|
// We don't calculate categories for sticky rooms because we have a moderate
|
|
|
|
// interest in trying to maintain the category that they were last in before
|
|
|
|
// being artificially flagged as IDLE. Also, this reduces the amount of time
|
|
|
|
// we spend in _setRoomCategory ever so slightly.
|
2019-02-25 05:44:47 +03:00
|
|
|
if (this._state.stickyRoomId !== roomId || ignoreSticky) {
|
2019-02-20 00:56:56 +03:00
|
|
|
// Micro optimization: Only look up the room if we're confident we'll need it.
|
|
|
|
const room = this._matrixClient.getRoom(roomId);
|
|
|
|
if (!room) return;
|
|
|
|
|
2019-02-14 03:03:27 +03:00
|
|
|
const category = this._calculateCategory(room);
|
|
|
|
this._setRoomCategory(room, category);
|
2018-01-26 00:16:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-25 23:39:08 +03:00
|
|
|
_filterTags(tags) {
|
|
|
|
tags = tags ? Object.keys(tags) : [];
|
|
|
|
if (this._state.tagsEnabled) return tags;
|
|
|
|
return tags.filter((t) => !!LIST_ORDERS[t]);
|
|
|
|
}
|
|
|
|
|
2019-02-25 05:44:47 +03:00
|
|
|
_getRecommendedTagsForRoom(room) {
|
|
|
|
const tags = [];
|
|
|
|
|
|
|
|
const myMembership = room.getMyMembership();
|
|
|
|
if (myMembership === 'join' || myMembership === 'invite') {
|
|
|
|
// Stack the user's tags on top
|
2019-02-25 23:39:08 +03:00
|
|
|
tags.push(...this._filterTags(room.tags));
|
2019-02-25 05:44:47 +03:00
|
|
|
|
2019-02-27 09:40:07 +03:00
|
|
|
// Order matters here: The DMRoomMap updates before invites
|
|
|
|
// are accepted, so we check to see if the room is an invite
|
|
|
|
// first, then if it is a direct chat, and finally default
|
|
|
|
// to the "recents" list.
|
2019-02-25 05:44:47 +03:00
|
|
|
const dmRoomMap = DMRoomMap.shared();
|
2019-02-27 09:40:07 +03:00
|
|
|
if (myMembership === 'invite') {
|
2019-02-25 05:44:47 +03:00
|
|
|
tags.push("im.vector.fake.invite");
|
2019-02-27 09:40:07 +03:00
|
|
|
} else if (dmRoomMap.getUserIdForRoomId(room.roomId)) {
|
|
|
|
tags.push("im.vector.fake.direct");
|
2019-02-25 05:44:47 +03:00
|
|
|
} else if (tags.length === 0) {
|
|
|
|
tags.push("im.vector.fake.recent");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
tags.push("im.vector.fake.archived");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return tags;
|
|
|
|
}
|
|
|
|
|
2019-02-14 04:19:18 +03:00
|
|
|
_setRoomCategory(room, category) {
|
2019-02-14 06:16:47 +03:00
|
|
|
if (!room) return; // This should only happen in tests
|
|
|
|
|
2019-02-13 23:00:35 +03:00
|
|
|
const listsClone = {};
|
2019-02-14 05:49:28 +03:00
|
|
|
const targetCategoryIndex = CATEGORY_ORDER.indexOf(category);
|
2019-02-20 00:56:56 +03:00
|
|
|
|
|
|
|
// Micro optimization: Support lazily loading the last timestamp in a room
|
|
|
|
let _targetTimestamp = null;
|
|
|
|
const targetTimestamp = () => {
|
|
|
|
if (_targetTimestamp === null) {
|
|
|
|
_targetTimestamp = this._tsOfNewestEvent(room);
|
|
|
|
}
|
|
|
|
return _targetTimestamp;
|
|
|
|
};
|
2019-02-13 23:00:35 +03:00
|
|
|
|
2019-02-25 05:44:47 +03:00
|
|
|
const targetTags = this._getRecommendedTagsForRoom(room);
|
|
|
|
const insertedIntoTags = [];
|
2019-02-14 00:08:19 +03:00
|
|
|
|
2019-02-25 05:44:47 +03:00
|
|
|
// We need to make sure all the tags (lists) are updated with the room's new position. We
|
|
|
|
// generally only get called here when there's a new room to insert or a room has potentially
|
|
|
|
// changed positions within the list.
|
|
|
|
//
|
|
|
|
// We do all our checks by iterating over the rooms in the existing lists, trying to insert
|
|
|
|
// our room where we can. As a guiding principle, we should be removing the room from all
|
|
|
|
// tags, and insert the room into targetTags. We should perform the deletion before the addition
|
|
|
|
// where possible to keep a consistent state. By the end of this, targetTags should be the
|
|
|
|
// same as insertedIntoTags.
|
2019-02-13 23:00:35 +03:00
|
|
|
|
|
|
|
for (const key of Object.keys(this._state.lists)) {
|
2019-02-25 05:44:47 +03:00
|
|
|
const shouldHaveRoom = targetTags.includes(key);
|
|
|
|
|
|
|
|
// Speed optimization: Don't do complicated math if we don't have to.
|
|
|
|
if (!shouldHaveRoom) {
|
|
|
|
listsClone[key] = this._state.lists[key].filter((e) => e.room.roomId !== room.roomId);
|
2019-02-25 23:39:08 +03:00
|
|
|
} else if (LIST_ORDERS[key] !== 'recent') {
|
|
|
|
// Manually ordered tags are sorted later, so for now we'll just clone the tag
|
|
|
|
// and add our room if needed
|
|
|
|
listsClone[key] = this._state.lists[key].filter((e) => e.room.roomId !== room.roomId);
|
|
|
|
listsClone[key].push({room, category});
|
|
|
|
insertedIntoTags.push(key);
|
2019-02-20 01:15:39 +03:00
|
|
|
} else {
|
|
|
|
listsClone[key] = [];
|
|
|
|
|
2019-02-25 23:39:08 +03:00
|
|
|
// We track where the boundary within listsClone[key] is just in case our timestamp
|
|
|
|
// ordering fails. If we can't stick the room in at the correct place in the category
|
|
|
|
// grouping based on timestamp, we'll stick it at the top of the group which will be
|
|
|
|
// the index we track here.
|
|
|
|
let desiredCategoryBoundaryIndex = 0;
|
|
|
|
let foundBoundary = false;
|
|
|
|
let pushedEntry = false;
|
|
|
|
|
|
|
|
for (const entry of this._state.lists[key]) {
|
|
|
|
// We insert our own record as needed, so don't let the old one through.
|
|
|
|
if (entry.room.roomId === room.roomId) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-02-14 05:35:01 +03:00
|
|
|
|
2019-02-25 23:39:08 +03:00
|
|
|
// if the list is a recent list, and the room appears in this list, and we're
|
|
|
|
// not looking at a sticky room (sticky rooms have unreliable categories), try
|
|
|
|
// to slot the new room in
|
|
|
|
if (entry.room.roomId !== this._state.stickyRoomId) {
|
|
|
|
if (!pushedEntry && shouldHaveRoom) {
|
|
|
|
// Micro optimization: Support lazily loading the last timestamp in a room
|
|
|
|
let _entryTimestamp = null;
|
|
|
|
const entryTimestamp = () => {
|
|
|
|
if (_entryTimestamp === null) {
|
|
|
|
_entryTimestamp = this._tsOfNewestEvent(entry.room);
|
2019-02-25 05:44:47 +03:00
|
|
|
}
|
2019-02-25 23:39:08 +03:00
|
|
|
return _entryTimestamp;
|
|
|
|
};
|
|
|
|
|
|
|
|
const entryCategoryIndex = CATEGORY_ORDER.indexOf(entry.category);
|
2019-02-25 05:44:47 +03:00
|
|
|
|
2019-02-25 23:39:08 +03:00
|
|
|
// As per above, check if we're meeting that boundary we wanted to locate.
|
|
|
|
if (entryCategoryIndex >= targetCategoryIndex && !foundBoundary) {
|
|
|
|
desiredCategoryBoundaryIndex = listsClone[key].length - 1;
|
|
|
|
foundBoundary = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we've hit the top of a boundary beyond our target category, insert at the top of
|
|
|
|
// the grouping to ensure the room isn't slotted incorrectly. Otherwise, try to insert
|
|
|
|
// based on most recent timestamp.
|
|
|
|
const changedBoundary = entryCategoryIndex > targetCategoryIndex;
|
|
|
|
const currentCategory = entryCategoryIndex === targetCategoryIndex;
|
|
|
|
if (changedBoundary || (currentCategory && targetTimestamp() >= entryTimestamp())) {
|
|
|
|
if (changedBoundary) {
|
|
|
|
// If we changed a boundary, then we've gone too far - go to the top of the last
|
|
|
|
// section instead.
|
|
|
|
listsClone[key].splice(desiredCategoryBoundaryIndex, 0, {room, category});
|
|
|
|
} else {
|
|
|
|
// If we're ordering by timestamp, just insert normally
|
|
|
|
listsClone[key].push({room, category});
|
2019-02-25 05:44:47 +03:00
|
|
|
}
|
2019-02-25 23:39:08 +03:00
|
|
|
pushedEntry = true;
|
|
|
|
insertedIntoTags.push(key);
|
2019-02-14 04:19:18 +03:00
|
|
|
}
|
2019-02-13 23:00:35 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-25 23:39:08 +03:00
|
|
|
// Fall through and clone the list.
|
|
|
|
listsClone[key].push(entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!pushedEntry) {
|
|
|
|
if (listsClone[key].length === 0) {
|
|
|
|
listsClone[key].push({room, category});
|
|
|
|
insertedIntoTags.push(key);
|
|
|
|
} else {
|
|
|
|
// In theory, this should never happen
|
|
|
|
console.warn(`!! Room ${room.roomId} lost: No position available`);
|
2019-02-13 23:00:35 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-25 05:44:47 +03:00
|
|
|
// Double check that we inserted the room in the right places
|
|
|
|
for (const targetTag of targetTags) {
|
|
|
|
let count = 0;
|
|
|
|
for (const insertedTag of insertedIntoTags) {
|
|
|
|
if (insertedTag === targetTag) count++;
|
2019-02-14 00:08:19 +03:00
|
|
|
}
|
2019-02-25 05:44:47 +03:00
|
|
|
|
|
|
|
if (count !== 1) {
|
|
|
|
console.warn(`!! Room ${room.roomId} inserted ${count} times`);
|
2019-02-14 00:08:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-25 23:39:08 +03:00
|
|
|
// Sort the favourites before we set the clone
|
|
|
|
for (const tag of Object.keys(listsClone)) {
|
|
|
|
if (LIST_ORDERS[tag] === 'recent') continue; // skip recents (pre-sorted)
|
|
|
|
listsClone[tag].sort(this._getManualComparator(tag));
|
|
|
|
}
|
|
|
|
|
2019-02-13 23:00:35 +03:00
|
|
|
this._setState({lists: listsClone});
|
|
|
|
}
|
|
|
|
|
|
|
|
_generateInitialRoomLists() {
|
2019-02-26 22:43:10 +03:00
|
|
|
// Log something to show that we're throwing away the old results. This is for the inevitable
|
|
|
|
// question of "why is 100% of my CPU going towards Riot?" - a quick look at the logs would reveal
|
|
|
|
// that something is wrong with the RoomListStore.
|
|
|
|
console.log("Generating initial room lists");
|
|
|
|
|
2018-01-26 00:16:03 +03:00
|
|
|
const lists = {
|
2018-08-06 20:00:40 +03:00
|
|
|
"m.server_notice": [],
|
2018-01-26 00:16:03 +03:00
|
|
|
"im.vector.fake.invite": [],
|
|
|
|
"m.favourite": [],
|
|
|
|
"im.vector.fake.recent": [],
|
|
|
|
"im.vector.fake.direct": [],
|
|
|
|
"m.lowpriority": [],
|
|
|
|
"im.vector.fake.archived": [],
|
|
|
|
};
|
|
|
|
|
|
|
|
const dmRoomMap = DMRoomMap.shared();
|
2019-02-20 01:15:39 +03:00
|
|
|
|
|
|
|
// Speed optimization: Hitting the SettingsStore is expensive, so avoid that at all costs.
|
|
|
|
let _isCustomTagsEnabled = null;
|
|
|
|
const isCustomTagsEnabled = () => {
|
|
|
|
if (_isCustomTagsEnabled === null) {
|
|
|
|
_isCustomTagsEnabled = SettingsStore.isFeatureEnabled("feature_custom_tags");
|
|
|
|
}
|
|
|
|
return _isCustomTagsEnabled;
|
|
|
|
};
|
2019-02-07 21:04:30 +03:00
|
|
|
|
2019-02-13 23:00:35 +03:00
|
|
|
this._matrixClient.getRooms().forEach((room) => {
|
2018-07-25 17:08:44 +03:00
|
|
|
const myUserId = this._matrixClient.getUserId();
|
2018-08-14 12:47:05 +03:00
|
|
|
const membership = room.getMyMembership();
|
2018-07-25 17:08:44 +03:00
|
|
|
const me = room.getMember(myUserId);
|
2018-01-26 00:16:03 +03:00
|
|
|
|
2019-02-13 23:00:35 +03:00
|
|
|
if (membership === "invite") {
|
|
|
|
lists["im.vector.fake.invite"].push({room, category: CATEGORY_RED});
|
|
|
|
} else if (membership === "join" || membership === "ban" || (me && me.isKicked())) {
|
2018-01-26 00:16:03 +03:00
|
|
|
// Used to split rooms via tags
|
2018-02-06 18:15:47 +03:00
|
|
|
let tagNames = Object.keys(room.tags);
|
|
|
|
|
2019-02-05 20:35:05 +03:00
|
|
|
// ignore any m. tag names we don't know about
|
2018-08-06 18:58:54 +03:00
|
|
|
tagNames = tagNames.filter((t) => {
|
2019-02-20 01:15:39 +03:00
|
|
|
// Speed optimization: Avoid hitting the SettingsStore at all costs by making it the
|
|
|
|
// last condition possible.
|
|
|
|
return lists[t] !== undefined || (!t.startsWith('m.') && isCustomTagsEnabled());
|
2018-08-06 18:58:54 +03:00
|
|
|
});
|
|
|
|
|
2018-01-26 00:16:03 +03:00
|
|
|
if (tagNames.length) {
|
|
|
|
for (let i = 0; i < tagNames.length; i++) {
|
|
|
|
const tagName = tagNames[i];
|
|
|
|
lists[tagName] = lists[tagName] || [];
|
2019-02-13 23:00:35 +03:00
|
|
|
|
2019-02-20 01:15:39 +03:00
|
|
|
// Default to an arbitrary category for tags which aren't ordered by recents
|
|
|
|
let category = CATEGORY_IDLE;
|
|
|
|
if (LIST_ORDERS[tagName] === 'recent') category = this._calculateCategory(room);
|
|
|
|
lists[tagName].push({room, category: category});
|
2018-01-26 00:16:03 +03:00
|
|
|
}
|
|
|
|
} else if (dmRoomMap.getUserIdForRoomId(room.roomId)) {
|
|
|
|
// "Direct Message" rooms (that we're still in and that aren't otherwise tagged)
|
2019-02-13 23:00:35 +03:00
|
|
|
lists["im.vector.fake.direct"].push({room, category: this._calculateCategory(room)});
|
2018-01-26 00:16:03 +03:00
|
|
|
} else {
|
2019-02-13 23:00:35 +03:00
|
|
|
lists["im.vector.fake.recent"].push({room, category: this._calculateCategory(room)});
|
2018-01-26 00:16:03 +03:00
|
|
|
}
|
2018-07-25 15:14:36 +03:00
|
|
|
} else if (membership === "leave") {
|
2019-02-20 01:15:39 +03:00
|
|
|
// The category of these rooms is not super important, so deprioritize it to the lowest
|
|
|
|
// possible value.
|
|
|
|
lists["im.vector.fake.archived"].push({room, category: CATEGORY_IDLE});
|
2018-01-26 00:16:03 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-02-14 05:49:28 +03:00
|
|
|
// We use this cache in the recents comparator because _tsOfNewestEvent can take a while. This
|
|
|
|
// cache only needs to survive the sort operation below and should not be implemented outside
|
|
|
|
// of this function, otherwise the room lists will almost certainly be out of date and wrong.
|
2019-02-14 05:16:11 +03:00
|
|
|
const latestEventTsCache = {}; // roomId => timestamp
|
|
|
|
|
2018-02-14 14:23:29 +03:00
|
|
|
Object.keys(lists).forEach((listKey) => {
|
|
|
|
let comparator;
|
2019-02-13 23:00:35 +03:00
|
|
|
switch (LIST_ORDERS[listKey]) {
|
2018-02-14 14:23:29 +03:00
|
|
|
case "recent":
|
2019-02-14 05:16:11 +03:00
|
|
|
comparator = (entryA, entryB) => {
|
2019-02-14 05:35:01 +03:00
|
|
|
return this._recentsComparator(entryA, entryB, (room) => {
|
2019-02-14 06:16:47 +03:00
|
|
|
if (!room) return Number.MAX_SAFE_INTEGER; // Should only happen in tests
|
|
|
|
|
2019-02-14 05:16:11 +03:00
|
|
|
if (latestEventTsCache[room.roomId]) {
|
|
|
|
return latestEventTsCache[room.roomId];
|
|
|
|
}
|
2019-02-20 01:15:39 +03:00
|
|
|
|
2019-02-14 05:16:11 +03:00
|
|
|
const ts = this._tsOfNewestEvent(room);
|
|
|
|
latestEventTsCache[room.roomId] = ts;
|
|
|
|
return ts;
|
|
|
|
});
|
|
|
|
};
|
2018-02-14 14:23:29 +03:00
|
|
|
break;
|
|
|
|
case "manual":
|
|
|
|
default:
|
2019-02-13 23:00:35 +03:00
|
|
|
comparator = this._getManualComparator(listKey);
|
2018-02-14 14:23:29 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
lists[listKey].sort(comparator);
|
2018-02-06 18:15:47 +03:00
|
|
|
});
|
|
|
|
|
2018-01-26 00:16:03 +03:00
|
|
|
this._setState({
|
|
|
|
lists,
|
2019-02-13 23:00:35 +03:00
|
|
|
ready: true, // Ready to receive updates to ordering
|
2018-01-26 00:16:03 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-02-16 17:16:50 +03:00
|
|
|
_eventTriggersRecentReorder(ev) {
|
|
|
|
return ev.getTs() && (
|
|
|
|
Unread.eventTriggersUnreadCount(ev) ||
|
|
|
|
ev.getSender() === this._matrixClient.credentials.userId
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-02-06 18:15:47 +03:00
|
|
|
_tsOfNewestEvent(room) {
|
2019-02-14 06:02:18 +03:00
|
|
|
// Apparently we can have rooms without timelines, at least under testing
|
|
|
|
// environments. Just return MAX_INT when this happens.
|
2019-02-14 06:16:47 +03:00
|
|
|
if (!room || !room.timeline) return Number.MAX_SAFE_INTEGER;
|
2019-02-14 06:02:18 +03:00
|
|
|
|
2018-02-06 18:15:47 +03:00
|
|
|
for (let i = room.timeline.length - 1; i >= 0; --i) {
|
|
|
|
const ev = room.timeline[i];
|
2018-02-16 17:16:50 +03:00
|
|
|
if (this._eventTriggersRecentReorder(ev)) {
|
2018-02-06 18:15:47 +03:00
|
|
|
return ev.getTs();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// we might only have events that don't trigger the unread indicator,
|
|
|
|
// in which case use the oldest event even if normally it wouldn't count.
|
|
|
|
// This is better than just assuming the last event was forever ago.
|
|
|
|
if (room.timeline.length && room.timeline[0].getTs()) {
|
|
|
|
return room.timeline[0].getTs();
|
|
|
|
} else {
|
|
|
|
return Number.MAX_SAFE_INTEGER;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-13 23:00:35 +03:00
|
|
|
_calculateCategory(room) {
|
2019-02-23 02:57:41 +03:00
|
|
|
if (!this._state.orderRoomsByImportance) {
|
|
|
|
// Effectively disable the categorization of rooms if we're supposed to
|
|
|
|
// be sorting by more recent messages first. This triggers the timestamp
|
|
|
|
// comparison bit of _setRoomCategory and _recentsComparator instead of
|
|
|
|
// the category ordering.
|
|
|
|
return CATEGORY_IDLE;
|
|
|
|
}
|
|
|
|
|
2019-02-13 23:00:35 +03:00
|
|
|
const mentions = room.getUnreadNotificationCount("highlight") > 0;
|
|
|
|
if (mentions) return CATEGORY_RED;
|
|
|
|
|
|
|
|
let unread = room.getUnreadNotificationCount() > 0;
|
|
|
|
if (unread) return CATEGORY_GREY;
|
|
|
|
|
|
|
|
unread = Unread.doesRoomHaveUnreadMessages(room);
|
|
|
|
if (unread) return CATEGORY_BOLD;
|
2018-10-12 23:35:54 +03:00
|
|
|
|
2019-02-13 23:00:35 +03:00
|
|
|
return CATEGORY_IDLE;
|
|
|
|
}
|
|
|
|
|
2019-02-14 05:16:11 +03:00
|
|
|
_recentsComparator(entryA, entryB, tsOfNewestEventFn) {
|
2019-02-13 23:00:35 +03:00
|
|
|
const roomA = entryA.room;
|
|
|
|
const roomB = entryB.room;
|
|
|
|
const categoryA = entryA.category;
|
|
|
|
const categoryB = entryB.category;
|
|
|
|
|
|
|
|
if (categoryA !== categoryB) {
|
|
|
|
const idxA = CATEGORY_ORDER.indexOf(categoryA);
|
|
|
|
const idxB = CATEGORY_ORDER.indexOf(categoryB);
|
|
|
|
if (idxA > idxB) return 1;
|
|
|
|
if (idxA < idxB) return -1;
|
2019-02-20 01:15:39 +03:00
|
|
|
return 0; // Technically not possible
|
2018-10-12 23:35:54 +03:00
|
|
|
}
|
|
|
|
|
2019-02-14 05:16:11 +03:00
|
|
|
const timestampA = tsOfNewestEventFn(roomA);
|
|
|
|
const timestampB = tsOfNewestEventFn(roomB);
|
2019-02-13 23:00:35 +03:00
|
|
|
return timestampB - timestampA;
|
2018-02-06 18:15:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
_lexicographicalComparator(roomA, roomB) {
|
|
|
|
return roomA.name > roomB.name ? 1 : -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
_getManualComparator(tagName, optimisticRequest) {
|
2019-02-13 23:00:35 +03:00
|
|
|
return (entryA, entryB) => {
|
|
|
|
const roomA = entryA.room;
|
|
|
|
const roomB = entryB.room;
|
|
|
|
|
2018-02-06 18:15:47 +03:00
|
|
|
let metaA = roomA.tags[tagName];
|
|
|
|
let metaB = roomB.tags[tagName];
|
|
|
|
|
|
|
|
if (optimisticRequest && roomA === optimisticRequest.room) metaA = optimisticRequest.metaData;
|
|
|
|
if (optimisticRequest && roomB === optimisticRequest.room) metaB = optimisticRequest.metaData;
|
|
|
|
|
|
|
|
// Make sure the room tag has an order element, if not set it to be the bottom
|
2019-02-13 23:00:35 +03:00
|
|
|
const a = metaA ? Number(metaA.order) : undefined;
|
|
|
|
const b = metaB ? Number(metaB.order) : undefined;
|
2018-02-06 18:15:47 +03:00
|
|
|
|
|
|
|
// Order undefined room tag orders to the bottom
|
|
|
|
if (a === undefined && b !== undefined) {
|
|
|
|
return 1;
|
|
|
|
} else if (a !== undefined && b === undefined) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-02-25 05:44:47 +03:00
|
|
|
return a === b ? this._lexicographicalComparator(roomA, roomB) : (a > b ? 1 : -1);
|
2018-02-06 18:15:47 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-01-26 00:16:03 +03:00
|
|
|
getRoomLists() {
|
2019-02-13 23:00:35 +03:00
|
|
|
return this._state.presentationLists;
|
2018-01-26 00:16:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (global.singletonRoomListStore === undefined) {
|
|
|
|
global.singletonRoomListStore = new RoomListStore();
|
|
|
|
}
|
|
|
|
export default global.singletonRoomListStore;
|