From db42d629aa3e6820fb61d14d9c53174b637f7f3e Mon Sep 17 00:00:00 2001 From: David Baker Date: Wed, 7 Sep 2016 11:30:09 +0100 Subject: [PATCH 1/4] Rename MatrixTools to Rooms Since all the functions therein are to do with rooms, so this name is probably more helpful --- src/{MatrixTools.js => Rooms.js} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/{MatrixTools.js => Rooms.js} (100%) diff --git a/src/MatrixTools.js b/src/Rooms.js similarity index 100% rename from src/MatrixTools.js rename to src/Rooms.js From df1cc8748f151f80a0e60b903c0b866756b6dfba Mon Sep 17 00:00:00 2001 From: David Baker Date: Wed, 7 Sep 2016 11:33:58 +0100 Subject: [PATCH 2/4] Change references to MatrixTools to Rooms or remove where they were unused --- src/SlashCommands.js | 1 - src/autocomplete/RoomProvider.js | 2 +- src/components/structures/MatrixChat.js | 6 +++--- src/components/structures/RoomView.js | 1 - src/components/views/rooms/RoomList.js | 8 ++++---- 5 files changed, 8 insertions(+), 10 deletions(-) diff --git a/src/SlashCommands.js b/src/SlashCommands.js index 759a95c8ff..be007496dd 100644 --- a/src/SlashCommands.js +++ b/src/SlashCommands.js @@ -15,7 +15,6 @@ limitations under the License. */ var MatrixClientPeg = require("./MatrixClientPeg"); -var MatrixTools = require("./MatrixTools"); var dis = require("./dispatcher"); var Tinter = require("./Tinter"); diff --git a/src/autocomplete/RoomProvider.js b/src/autocomplete/RoomProvider.js index 39cf1179d7..ac7f1b418a 100644 --- a/src/autocomplete/RoomProvider.js +++ b/src/autocomplete/RoomProvider.js @@ -4,7 +4,7 @@ import Q from 'q'; import MatrixClientPeg from '../MatrixClientPeg'; import Fuse from 'fuse.js'; import {PillCompletion} from './Components'; -import {getDisplayAliasForRoom} from '../MatrixTools'; +import {getDisplayAliasForRoom} from '../Rooms'; import sdk from '../index'; const ROOM_REGEX = /(?=#)([^\s]*)/g; diff --git a/src/components/structures/MatrixChat.js b/src/components/structures/MatrixChat.js index e1e622af65..251f3f1dc8 100644 --- a/src/components/structures/MatrixChat.js +++ b/src/components/structures/MatrixChat.js @@ -36,7 +36,7 @@ var PostRegistration = require("./login/PostRegistration"); var Modal = require("../../Modal"); var Tinter = require("../../Tinter"); var sdk = require('../../index'); -var MatrixTools = require('../../MatrixTools'); +var Rooms = require('../../Rooms'); var linkifyMatrix = require("../../linkify-matrix"); var KeyCode = require('../../KeyCode'); var Lifecycle = require('../../Lifecycle'); @@ -479,7 +479,7 @@ module.exports = React.createClass({ var presentedId = room_info.room_alias || room_info.room_id; var room = MatrixClientPeg.get().getRoom(room_info.room_id); if (room) { - var theAlias = MatrixTools.getDisplayAliasForRoom(room); + var theAlias = Rooms.getDisplayAliasForRoom(room); if (theAlias) presentedId = theAlias; // No need to do this given RoomView triggers it itself... @@ -592,7 +592,7 @@ module.exports = React.createClass({ var presentedId = self.state.currentRoomId; var room = MatrixClientPeg.get().getRoom(self.state.currentRoomId); if (room) { - var theAlias = MatrixTools.getDisplayAliasForRoom(room); + var theAlias = Rooms.getDisplayAliasForRoom(room); if (theAlias) presentedId = theAlias; } diff --git a/src/components/structures/RoomView.js b/src/components/structures/RoomView.js index 3d0bcf7445..041493d420 100644 --- a/src/components/structures/RoomView.js +++ b/src/components/structures/RoomView.js @@ -36,7 +36,6 @@ var dis = require("../../dispatcher"); var Tinter = require("../../Tinter"); var rate_limited_func = require('../../ratelimitedfunc'); var ObjectUtils = require('../../ObjectUtils'); -var MatrixTools = require('../../MatrixTools'); import UserProvider from '../../autocomplete/UserProvider'; diff --git a/src/components/views/rooms/RoomList.js b/src/components/views/rooms/RoomList.js index bdb1ea3c8c..d679a1f4c6 100644 --- a/src/components/views/rooms/RoomList.js +++ b/src/components/views/rooms/RoomList.js @@ -25,7 +25,7 @@ var Unread = require('../../../Unread'); var dis = require("../../../dispatcher"); var sdk = require('../../../index'); var rate_limited_func = require('../../../ratelimitedfunc'); -var MatrixTools = require('../../../MatrixTools'); +var Rooms = require('../../../Rooms'); var DMRoomMap = require('../../../utils/DMRoomMap'); var HIDE_CONFERENCE_CHANS = true; @@ -224,7 +224,7 @@ module.exports = React.createClass({ if (me.membership == "invite") { s.lists["im.vector.fake.invite"].push(room); } - else if (HIDE_CONFERENCE_CHANS && MatrixTools.isConfCallRoom(room, me, self.props.ConferenceHandler)) { + else if (HIDE_CONFERENCE_CHANS && Rooms.isConfCallRoom(room, me, self.props.ConferenceHandler)) { // skip past this room & don't put it in any lists } else if (dmRoomMap.getUserIdForRoomId(room.roomId)) { @@ -265,7 +265,7 @@ module.exports = React.createClass({ for (const room of oldRecents) { const me = room.getMember(MatrixClientPeg.get().credentials.userId); - if (me && MatrixTools.looksLikeDirectMessageRoom(room, me)) { + if (me && Rooms.looksLikeDirectMessageRoom(room, me)) { s.lists["im.vector.fake.direct"].push(room); } else { s.lists["im.vector.fake.recent"].push(room); @@ -276,7 +276,7 @@ module.exports = React.createClass({ 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); + const otherPerson = Rooms.getOnlyOtherMember(room, me); if (!otherPerson) continue; const roomList = newMDirectEvent[otherPerson.userId] || []; From 8a4606cfbf81061c01b4f6afaf1624fb597d7549 Mon Sep 17 00:00:00 2001 From: David Baker Date: Wed, 7 Sep 2016 11:34:55 +0100 Subject: [PATCH 3/4] Remove unused import --- src/Rooms.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Rooms.js b/src/Rooms.js index f3c0cd6949..be3420b507 100644 --- a/src/Rooms.js +++ b/src/Rooms.js @@ -13,7 +13,6 @@ 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. */ -var CallHandler = require('./CallHandler'); module.exports = { /** From 66b2944011f090e0fdc622dce0c92f17dff95b1f Mon Sep 17 00:00:00 2001 From: David Baker Date: Wed, 7 Sep 2016 11:45:32 +0100 Subject: [PATCH 4/4] Convert Rooms.js to ES6 --- src/Rooms.js | 118 +++++++++++++++++++++++++-------------------------- 1 file changed, 58 insertions(+), 60 deletions(-) diff --git a/src/Rooms.js b/src/Rooms.js index be3420b507..7f4564b439 100644 --- a/src/Rooms.js +++ b/src/Rooms.js @@ -14,66 +14,64 @@ See the License for the specific language governing permissions and limitations under the License. */ -module.exports = { - /** - * Given a room object, return the alias we should use for it, - * if any. This could be the canonical alias if one exists, otherwise - * an alias selected arbitrarily but deterministically from the list - * of aliases. Otherwise return null; - */ - getDisplayAliasForRoom: function(room) { - return room.getCanonicalAlias() || room.getAliases()[0]; - }, - /** - * If the room contains only two members including the logged-in user, - * return the other one. Otherwise, return null. - */ - getOnlyOtherMember(room, me) { - const joinedMembers = room.getJoinedMembers(); - - if (joinedMembers.length === 2) { - return joinedMembers.filter(function(m) { - return m.userId !== me.userId - })[0]; - } - - return null; - }, - - isConfCallRoom: function(room, me, conferenceHandler) { - if (!conferenceHandler) return false; - - if (me.membership != "join") { - return false; - } - - const otherMember = this.getOnlyOtherMember(room, me); - if (otherMember === null) { - return false; - } - - if (conferenceHandler.isConferenceUser(otherMember.userId)) { - return true; - } - }, - - looksLikeDirectMessageRoom: function(room, me) { - 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); - // Used for 1:1 direct chats - const joinedMembers = room.getJoinedMembers(); - - // Show 1:1 chats in seperate "Direct Messages" section as long as they haven't - // been moved to a different tag section - if (joinedMembers.length === 2 && !tagNames.length) { - return true; - } - } - return false; - }, +/** + * Given a room object, return the alias we should use for it, + * if any. This could be the canonical alias if one exists, otherwise + * an alias selected arbitrarily but deterministically from the list + * of aliases. Otherwise return null; + */ +export function getDisplayAliasForRoom(room) { + return room.getCanonicalAlias() || room.getAliases()[0]; } +/** + * If the room contains only two members including the logged-in user, + * return the other one. Otherwise, return null. + */ +export function getOnlyOtherMember(room, me) { + const joinedMembers = room.getJoinedMembers(); + + if (joinedMembers.length === 2) { + return joinedMembers.filter(function(m) { + return m.userId !== me.userId + })[0]; + } + + return null; +} + +export function isConfCallRoom(room, me, conferenceHandler) { + if (!conferenceHandler) return false; + + if (me.membership != "join") { + return false; + } + + const otherMember = getOnlyOtherMember(room, me); + if (otherMember === null) { + return false; + } + + if (conferenceHandler.isConferenceUser(otherMember.userId)) { + return true; + } +} + +export function looksLikeDirectMessageRoom(room, me) { + 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); + // Used for 1:1 direct chats + const joinedMembers = room.getJoinedMembers(); + + // Show 1:1 chats in seperate "Direct Messages" section as long as they haven't + // been moved to a different tag section + if (joinedMembers.length === 2 && !tagNames.length) { + return true; + } + } + return false; +}