Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
Merge pull request #446 from matrix-org/dbkr/m_direct_dm_rooms
Browse files Browse the repository at this point in the history
Track DM rooms in account data
  • Loading branch information
dbkr authored Sep 7, 2016
2 parents 80dd927 + 0940806 commit bed5aa8
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/MatrixTools.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ module.exports = {
}
},

isDirectMessageRoom: function(room, me) {
looksLikeDirectMessageRoom: function(room, me) {
if (me.membership == "join" || me.membership === "ban" ||
(me.membership === "leave" && me.events.member.getSender() !== me.events.member.getStateKey()))
{
Expand Down
39 changes: 37 additions & 2 deletions src/components/views/rooms/RoomList.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ var dis = require("../../../dispatcher");
var sdk = require('../../../index');
var rate_limited_func = require('../../../ratelimitedfunc');
var MatrixTools = require('../../../MatrixTools');
var DMRoomMap = require('../../../utils/DMRoomMap');

var HIDE_CONFERENCE_CHANS = true;

Expand Down Expand Up @@ -209,8 +210,10 @@ module.exports = React.createClass({
s.lists["m.lowpriority"] = [];
s.lists["im.vector.fake.archived"] = [];

const dmRoomMap = new DMRoomMap(MatrixClientPeg.get());

MatrixClientPeg.get().getRooms().forEach(function(room) {
var me = room.getMember(MatrixClientPeg.get().credentials.userId);
const me = room.getMember(MatrixClientPeg.get().credentials.userId);
if (!me) return;

// console.log("room = " + room.name + ", me.membership = " + me.membership +
Expand All @@ -224,7 +227,7 @@ module.exports = React.createClass({
else if (HIDE_CONFERENCE_CHANS && MatrixTools.isConfCallRoom(room, me, self.props.ConferenceHandler)) {
// skip past this room & don't put it in any lists
}
else if (MatrixTools.isDirectMessageRoom(room, me)) {
else if (dmRoomMap.getUserIdForRoomId(room.roomId)) {
// "Direct Message" rooms
s.lists["im.vector.fake.direct"].push(room);
}
Expand Down Expand Up @@ -253,6 +256,38 @@ module.exports = React.createClass({
}
});

if (s.lists["im.vector.fake.direct"].length == 0 && MatrixClientPeg.get().getAccountData('m.direct') === undefined) {
// scan through the 'recents' list for any rooms which look like DM rooms
// and make them DM rooms
const oldRecents = s.lists["im.vector.fake.recent"];
s.lists["im.vector.fake.recent"] = [];

for (const room of oldRecents) {
const me = room.getMember(MatrixClientPeg.get().credentials.userId);

if (me && MatrixTools.looksLikeDirectMessageRoom(room, me)) {
s.lists["im.vector.fake.direct"].push(room);
} else {
s.lists["im.vector.fake.recent"].push(room);
}
}

// save these new guessed DM rooms into the account data
const newMDirectEvent = {};
for (const room of s.lists["im.vector.fake.direct"]) {
const me = room.getMember(MatrixClientPeg.get().credentials.userId);
const otherPerson = MatrixTools.getOnlyOtherMember(room, me);
if (!otherPerson) continue;

const roomList = newMDirectEvent[otherPerson.userId] || [];
roomList.push(room.roomId);
newMDirectEvent[otherPerson.userId] = roomList;
}

// if this fails, fine, we'll just do the same thing next time we get the room lists
MatrixClientPeg.get().setAccountData('m.direct', newMDirectEvent).done();
}

//console.log("calculated new roomLists; im.vector.fake.recent = " + s.lists["im.vector.fake.recent"]);

// we actually apply the sorting to this when receiving the prop in RoomSubLists.
Expand Down
46 changes: 46 additions & 0 deletions src/utils/DMRoomMap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
Copyright 2016 OpenMarket 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.
*/

/**
* Class that takes a Matrix Client and flips the m.direct map
* so the operation of mapping a room ID to which user it's a DM
* with can be performed efficiently.
*/
export default class DMRoomMap {
constructor(matrixClient) {
const mDirectEvent = matrixClient.getAccountData('m.direct');
if (!mDirectEvent) {
this.userToRooms = {};
this.roomToUser = {};
} else {
this.userToRooms = mDirectEvent.getContent();
this.roomToUser = {};
for (const user of Object.keys(this.userToRooms)) {
for (const roomId of this.userToRooms[user]) {
this.roomToUser[roomId] = user;
}
}
}
}

getDMRoomsForUserId(userId) {
return this.userToRooms[userId];
}

getUserIdForRoomId(roomId) {
return this.roomToUser[roomId];
}
}

0 comments on commit bed5aa8

Please sign in to comment.