2018-01-25 21:16:03 +00:00
|
|
|
/*
|
|
|
|
Copyright 2018 New Vector Ltd
|
|
|
|
|
|
|
|
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';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A class for storing application state for categorising rooms in
|
|
|
|
* the RoomList.
|
|
|
|
*/
|
|
|
|
class RoomListStore extends Store {
|
|
|
|
constructor() {
|
|
|
|
super(dis);
|
|
|
|
|
|
|
|
this._init();
|
|
|
|
}
|
|
|
|
|
|
|
|
_init() {
|
|
|
|
// Initialise state
|
|
|
|
this._state = {
|
|
|
|
lists: {
|
|
|
|
"im.vector.fake.invite": [],
|
|
|
|
"m.favourite": [],
|
|
|
|
"im.vector.fake.recent": [],
|
|
|
|
"im.vector.fake.direct": [],
|
|
|
|
"m.lowpriority": [],
|
|
|
|
"im.vector.fake.archived": [],
|
|
|
|
},
|
|
|
|
ready: false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
_setState(newState) {
|
|
|
|
this._state = Object.assign(this._state, newState);
|
|
|
|
this.__emitChange();
|
|
|
|
}
|
|
|
|
|
|
|
|
__onDispatch(payload) {
|
|
|
|
switch (payload.action) {
|
|
|
|
// Initialise state after initial sync
|
|
|
|
case 'MatrixActions.sync': {
|
|
|
|
if (!(payload.prevState !== 'PREPARED' && payload.state === 'PREPARED')) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-02-05 17:29:22 +00:00
|
|
|
this._matrixClient = payload.matrixClient;
|
|
|
|
this._generateRoomLists();
|
2018-01-25 21:16:03 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'MatrixActions.Room.tags': {
|
|
|
|
if (!this._state.ready) break;
|
|
|
|
this._updateRoomLists(payload.room);
|
|
|
|
}
|
|
|
|
break;
|
2018-02-05 18:06:29 +00:00
|
|
|
case 'MatrixActions.accountData': {
|
|
|
|
if (payload.event_type !== 'm.direct') break;
|
|
|
|
this._generateRoomLists();
|
|
|
|
}
|
|
|
|
break;
|
2018-02-05 18:27:50 +00:00
|
|
|
case 'MatrixActions.RoomMember.membership': {
|
|
|
|
if (!this._matrixClient || payload.member.userId !== this._matrixClient.credentials.userId) break;
|
|
|
|
this._generateRoomLists();
|
|
|
|
}
|
|
|
|
break;
|
2018-01-25 21:16:03 +00:00
|
|
|
case 'RoomListActions.tagRoom.pending': {
|
|
|
|
this._updateRoomListsOptimistic(
|
|
|
|
payload.request.room,
|
|
|
|
payload.request.oldTag,
|
|
|
|
payload.request.newTag,
|
|
|
|
payload.request.metaData,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'RoomListActions.tagRoom.failure': {
|
2018-02-05 17:29:22 +00:00
|
|
|
// Reset state according to js-sdk
|
|
|
|
this._generateRoomLists();
|
2018-01-25 21:16:03 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_updateRoomListsOptimistic(updatedRoom, oldTag, newTag, metaData) {
|
|
|
|
const newLists = {};
|
|
|
|
|
2018-02-06 12:00:06 +00:00
|
|
|
// Adding a tag to an untagged room - need to remove it from recents
|
|
|
|
if (newTag && Object.keys(updatedRoom.tags).length === 0) {
|
|
|
|
oldTag = 'im.vector.fake.recent';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Removing a tag from a room with one tag left - need to add it to recents
|
|
|
|
if (oldTag && Object.keys(updatedRoom.tags).length === 1) {
|
|
|
|
newTag = 'im.vector.fake.recent';
|
|
|
|
}
|
|
|
|
|
2018-01-25 21:16:03 +00:00
|
|
|
// Remove room from oldTag
|
|
|
|
Object.keys(this._state.lists).forEach((tagName) => {
|
|
|
|
if (tagName === oldTag) {
|
|
|
|
newLists[tagName] = this._state.lists[tagName].filter((room) => {
|
|
|
|
return room.roomId !== updatedRoom.roomId;
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
newLists[tagName] = this._state.lists[tagName];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
/// XXX: RoomSubList sorts by data on the room object. We
|
|
|
|
/// should sort in advance and incrementally insert new rooms
|
|
|
|
/// instead of resorting every time.
|
|
|
|
if (metaData) {
|
|
|
|
updatedRoom.tags[newTag] = metaData;
|
|
|
|
}
|
|
|
|
|
|
|
|
newLists[newTag].push(updatedRoom);
|
|
|
|
|
|
|
|
this._setState({
|
|
|
|
lists: newLists,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
_updateRoomLists(updatedRoom) {
|
|
|
|
const roomTags = Object.keys(updatedRoom.tags);
|
|
|
|
|
|
|
|
const newLists = {};
|
|
|
|
|
|
|
|
// Removal of the updatedRoom from tags it no longer has
|
|
|
|
Object.keys(this._state.lists).forEach((tagName) => {
|
|
|
|
newLists[tagName] = this._state.lists[tagName].filter((room) => {
|
|
|
|
return room.roomId !== updatedRoom.roomId || roomTags.includes(tagName);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
roomTags.forEach((tagName) => {
|
|
|
|
if (newLists[tagName].includes(updatedRoom)) return;
|
|
|
|
newLists[tagName].push(updatedRoom);
|
|
|
|
});
|
|
|
|
|
2018-02-06 12:00:23 +00:00
|
|
|
if (roomTags.length === 0) {
|
|
|
|
newLists['im.vector.fake.recent'].unshift(updatedRoom);
|
|
|
|
}
|
|
|
|
|
2018-01-25 21:16:03 +00:00
|
|
|
this._setState({
|
|
|
|
lists: newLists,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-02-05 17:29:22 +00:00
|
|
|
_generateRoomLists() {
|
2018-01-25 21:16:03 +00:00
|
|
|
const lists = {
|
|
|
|
"im.vector.fake.invite": [],
|
|
|
|
"m.favourite": [],
|
|
|
|
"im.vector.fake.recent": [],
|
|
|
|
"im.vector.fake.direct": [],
|
|
|
|
"m.lowpriority": [],
|
|
|
|
"im.vector.fake.archived": [],
|
|
|
|
};
|
|
|
|
|
2018-02-05 17:29:22 +00:00
|
|
|
|
2018-01-25 21:16:03 +00:00
|
|
|
const dmRoomMap = DMRoomMap.shared();
|
|
|
|
|
2018-02-05 17:29:22 +00:00
|
|
|
// If somehow we dispatched a RoomListActions.tagRoom.failure before a MatrixActions.sync
|
|
|
|
if (!this._matrixClient) return;
|
|
|
|
|
|
|
|
this._matrixClient.getRooms().forEach((room, index) => {
|
|
|
|
const me = room.getMember(this._matrixClient.credentials.userId);
|
2018-01-25 21:16:03 +00:00
|
|
|
if (!me) return;
|
|
|
|
|
|
|
|
if (me.membership == "invite") {
|
|
|
|
lists["im.vector.fake.invite"].push(room);
|
|
|
|
} else if (me.membership == "join" || me.membership === "ban" ||
|
|
|
|
(me.membership === "leave" && me.events.member.getSender() !== me.events.member.getStateKey())) {
|
|
|
|
// Used to split rooms via tags
|
|
|
|
const tagNames = Object.keys(room.tags);
|
|
|
|
if (tagNames.length) {
|
|
|
|
for (let i = 0; i < tagNames.length; i++) {
|
|
|
|
const tagName = tagNames[i];
|
|
|
|
lists[tagName] = lists[tagName] || [];
|
|
|
|
lists[tagName].push(room);
|
|
|
|
}
|
|
|
|
} else if (dmRoomMap.getUserIdForRoomId(room.roomId)) {
|
|
|
|
// "Direct Message" rooms (that we're still in and that aren't otherwise tagged)
|
|
|
|
lists["im.vector.fake.direct"].push(room);
|
|
|
|
} else {
|
|
|
|
lists["im.vector.fake.recent"].push(room);
|
|
|
|
}
|
|
|
|
} else if (me.membership === "leave") {
|
|
|
|
lists["im.vector.fake.archived"].push(room);
|
|
|
|
} else {
|
|
|
|
console.error("unrecognised membership: " + me.membership + " - this should never happen");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this._setState({
|
|
|
|
lists,
|
|
|
|
ready: true, // Ready to receive updates via Room.tags events
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getRoomLists() {
|
|
|
|
return this._state.lists;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (global.singletonRoomListStore === undefined) {
|
|
|
|
global.singletonRoomListStore = new RoomListStore();
|
|
|
|
}
|
|
|
|
export default global.singletonRoomListStore;
|