From ec4086c5fc7fe73c5b018de783a95a7e447cfcf0 Mon Sep 17 00:00:00 2001 From: David Baker Date: Wed, 7 Sep 2016 17:46:45 +0100 Subject: [PATCH 1/2] API for (un)marking rooms as DM rooms --- src/Rooms.js | 57 ++++++++++++++++++++++++++ src/components/views/rooms/RoomList.js | 8 ++++ 2 files changed, 65 insertions(+) diff --git a/src/Rooms.js b/src/Rooms.js index 7f4564b439..08c09b7ff8 100644 --- a/src/Rooms.js +++ b/src/Rooms.js @@ -14,6 +14,9 @@ See the License for the specific language governing permissions and limitations under the License. */ +import MatrixClientPeg from './MatrixClientPeg'; +import DMRoomMap from './utils/DMRoomMap'; + /** * Given a room object, return the alias we should use for it, @@ -75,3 +78,57 @@ export function looksLikeDirectMessageRoom(room, me) { } return false; } + +/** + * Marks or unmarks the given room as being as a DM room. + * @param {string} roomId The ID of the room to modify + * @param {string} userId The user ID of the desired DM + room target user or null to un-mark + this room as a DM room + * @returns {object} A promise + */ +export function setDMRoom(roomId, userId) { + const mDirectEvent = MatrixClientPeg.get().getAccountData('m.direct'); + let dmRoomMap = {}; + + if (mDirectEvent !== undefined) dmRoomMap = mDirectEvent.getContent(); + + for (const thisUserId of Object.keys(dmRoomMap)) { + const roomList = dmRoomMap[thisUserId]; + + if (thisUserId == userId) { + if (roomList.indexOf(roomId) == -1) { + roomList.push(roomId); + } + } else { + const indexOfRoom = roomList.indexOf(roomId); + if (indexOfRoom > -1) { + roomList.splice(indexOfRoom, 1); + } + } + } + + return MatrixClientPeg.get().setAccountData('m.direct', dmRoomMap); +} + +/** + * Given a room, estimate which of its members is likley to + * be the target if the room were a DM room and return that user. + */ +export function guessDMRoomTarget(room, me) { + let oldestTs; + let oldestUser; + + // Pick the user who's been here longest (and isn't us) + for (const user of room.currentState.getMembers()) { + if (user.userId == me.userId) continue; + + if (oldestTs === undefined || user.events.member.getTs() < oldestTs) { + oldestUser = user; + oldestTs = user.events.member.getTs(); + } + } + + if (oldestUser === undefined) return me; + return oldestUser; +} diff --git a/src/components/views/rooms/RoomList.js b/src/components/views/rooms/RoomList.js index d679a1f4c6..1b1f7f6bd2 100644 --- a/src/components/views/rooms/RoomList.js +++ b/src/components/views/rooms/RoomList.js @@ -58,6 +58,7 @@ module.exports = React.createClass({ cli.on("Room.receipt", this.onRoomReceipt); cli.on("RoomState.events", this.onRoomStateEvents); cli.on("RoomMember.name", this.onRoomMemberName); + cli.on("accountData", this.onAccountData); var s = this.getRoomLists(); this.setState(s); @@ -109,6 +110,7 @@ module.exports = React.createClass({ MatrixClientPeg.get().removeListener("Room.receipt", this.onRoomReceipt); MatrixClientPeg.get().removeListener("RoomState.events", this.onRoomStateEvents); MatrixClientPeg.get().removeListener("RoomMember.name", this.onRoomMemberName); + MatrixClientPeg.get().removeListener("accountData", this.onAccountData); } // cancel any pending calls to the rate_limited_funcs this._delayedRefreshRoomList.cancelPendingCall(); @@ -180,6 +182,12 @@ module.exports = React.createClass({ this._delayedRefreshRoomList(); }, + onAccountData: function(ev) { + if (ev.getType() == 'm.direct') { + this._delayedRefreshRoomList(); + } + }, + _delayedRefreshRoomList: new rate_limited_func(function() { this.refreshRoomList(); }, 500), From ee7c06469018ab3ebb806cfa0f615283782bcaa1 Mon Sep 17 00:00:00 2001 From: David Baker Date: Thu, 8 Sep 2016 13:56:45 +0100 Subject: [PATCH 2/2] I can't spell 'likely' --- src/Rooms.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Rooms.js b/src/Rooms.js index 08c09b7ff8..436580371e 100644 --- a/src/Rooms.js +++ b/src/Rooms.js @@ -112,7 +112,7 @@ export function setDMRoom(roomId, userId) { } /** - * Given a room, estimate which of its members is likley to + * Given a room, estimate which of its members is likely to * be the target if the room were a DM room and return that user. */ export function guessDMRoomTarget(room, me) {