From 36665d3c6993e18566d41d5d008ee90eb3733eda Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Mon, 27 Aug 2018 17:40:31 +0200 Subject: [PATCH 1/3] Fix: dont show 1:1 avatar for room with only 2 members loaded --- src/components/views/avatars/RoomAvatar.js | 62 +++++----------------- 1 file changed, 14 insertions(+), 48 deletions(-) diff --git a/src/components/views/avatars/RoomAvatar.js b/src/components/views/avatars/RoomAvatar.js index 821448207f..c3336ce36a 100644 --- a/src/components/views/avatars/RoomAvatar.js +++ b/src/components/views/avatars/RoomAvatar.js @@ -19,6 +19,7 @@ import {ContentRepo} from "matrix-js-sdk"; import MatrixClientPeg from "../../../MatrixClientPeg"; import Modal from '../../../Modal'; import sdk from "../../../index"; +import DMRoomMap from '../../../utils/DMRoomMap'; module.exports = React.createClass({ displayName: 'RoomAvatar', @@ -109,56 +110,21 @@ module.exports = React.createClass({ getOneToOneAvatar: function(props) { if (!props.room) return null; - const mlist = props.room.currentState.members; - const userIds = []; - const leftUserIds = []; - // for .. in optimisation to return early if there are >2 keys - for (const uid in mlist) { - if (mlist.hasOwnProperty(uid)) { - if (["join", "invite"].includes(mlist[uid].membership)) { - userIds.push(uid); - } else { - leftUserIds.push(uid); - } - } - if (userIds.length > 2) { - return null; - } + const otherUserId = DMRoomMap.shared().getUserIdForRoomId(props.room.roomId); + if (!otherUserId) { + return null; } - - if (userIds.length == 2) { - let theOtherGuy = null; - if (mlist[userIds[0]].userId == MatrixClientPeg.get().credentials.userId) { - theOtherGuy = mlist[userIds[1]]; - } else { - theOtherGuy = mlist[userIds[0]]; - } - return theOtherGuy.getAvatarUrl( - MatrixClientPeg.get().getHomeserverUrl(), - Math.floor(props.width * window.devicePixelRatio), - Math.floor(props.height * window.devicePixelRatio), - props.resizeMethod, - false, - ); - } else if (userIds.length == 1) { - // The other 1-1 user left, leaving just the current user, so show the left user's avatar - if (leftUserIds.length === 1) { - return mlist[leftUserIds[0]].getAvatarUrl( - MatrixClientPeg.get().getHomeserverUrl(), - props.width, props.height, props.resizeMethod, - false, - ); - } - return mlist[userIds[0]].getAvatarUrl( - MatrixClientPeg.get().getHomeserverUrl(), - Math.floor(props.width * window.devicePixelRatio), - Math.floor(props.height * window.devicePixelRatio), - props.resizeMethod, - false, - ); - } else { - return null; + const otherMember = props.room.getMember(otherUserId); + if (!otherMember) { + return null; } + return otherMember.getAvatarUrl( + MatrixClientPeg.get().getHomeserverUrl(), + Math.floor(props.width * window.devicePixelRatio), + Math.floor(props.height * window.devicePixelRatio), + props.resizeMethod, + false, + ); }, onRoomAvatarClick: function() { From 6a077655e9f60f86b8776e6ca2113c043f7f5226 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Mon, 27 Aug 2018 18:48:21 +0200 Subject: [PATCH 2/3] bring back old behaviour to also show member avatars if not marked as 1:1 room --- src/components/views/avatars/RoomAvatar.js | 43 +++++++++++++++------- 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/src/components/views/avatars/RoomAvatar.js b/src/components/views/avatars/RoomAvatar.js index c3336ce36a..48c3d2a0d2 100644 --- a/src/components/views/avatars/RoomAvatar.js +++ b/src/components/views/avatars/RoomAvatar.js @@ -108,23 +108,38 @@ module.exports = React.createClass({ }, getOneToOneAvatar: function(props) { - if (!props.room) return null; + const room = props.room; + if (!room) { + return null; + } + let otherMember = null; + const otherUserId = DMRoomMap.shared().getUserIdForRoomId(room.roomId); + if (otherUserId) { + otherMember = room.getMember(otherUserId); + } else { + // if the room is not marked as a 1:1, but only has max 2 members + // then still try to show any avatar (pref. other member) + const totalMemberCount = room.getJoinedMemberCount() + + room.getInvitedMemberCount(); + const members = room.currentState.getMembers(); + if (totalMemberCount == 2) { + const myUserId = MatrixClientPeg.get().getUserId(); + otherMember = members.find(m => m.userId !== myUserId); + } else if(totalMemberCount == 1) { + otherMember = members[0]; + } - const otherUserId = DMRoomMap.shared().getUserIdForRoomId(props.room.roomId); - if (!otherUserId) { - return null; } - const otherMember = props.room.getMember(otherUserId); - if (!otherMember) { - return null; + if (otherMember) { + return otherMember.getAvatarUrl( + MatrixClientPeg.get().getHomeserverUrl(), + Math.floor(props.width * window.devicePixelRatio), + Math.floor(props.height * window.devicePixelRatio), + props.resizeMethod, + false, + ); } - return otherMember.getAvatarUrl( - MatrixClientPeg.get().getHomeserverUrl(), - Math.floor(props.width * window.devicePixelRatio), - Math.floor(props.height * window.devicePixelRatio), - props.resizeMethod, - false, - ); + return null; }, onRoomAvatarClick: function() { From 06160f5fae35672f3608e4a9435e707e8c964f0f Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Mon, 27 Aug 2018 18:52:33 +0200 Subject: [PATCH 3/3] fix lint --- src/components/views/avatars/RoomAvatar.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/components/views/avatars/RoomAvatar.js b/src/components/views/avatars/RoomAvatar.js index 48c3d2a0d2..e0105159fb 100644 --- a/src/components/views/avatars/RoomAvatar.js +++ b/src/components/views/avatars/RoomAvatar.js @@ -125,10 +125,9 @@ module.exports = React.createClass({ if (totalMemberCount == 2) { const myUserId = MatrixClientPeg.get().getUserId(); otherMember = members.find(m => m.userId !== myUserId); - } else if(totalMemberCount == 1) { + } else if (totalMemberCount == 1) { otherMember = members[0]; } - } if (otherMember) { return otherMember.getAvatarUrl(