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),