Merge pull request #447 from matrix-org/dbkr/refactor_matrixtools

Refactor MatrixTools to Rooms
This commit is contained in:
Richard van der Hoff 2016-09-07 11:56:00 +01:00 committed by GitHub
commit 603bf8c194
7 changed files with 85 additions and 90 deletions

View file

@ -1,80 +0,0 @@
/*
Copyright 2015, 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.
*/
var CallHandler = require('./CallHandler');
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;
},
}

77
src/Rooms.js Normal file
View file

@ -0,0 +1,77 @@
/*
Copyright 2015, 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.
*/
/**
* 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;
}

View file

@ -15,7 +15,6 @@ limitations under the License.
*/ */
var MatrixClientPeg = require("./MatrixClientPeg"); var MatrixClientPeg = require("./MatrixClientPeg");
var MatrixTools = require("./MatrixTools");
var dis = require("./dispatcher"); var dis = require("./dispatcher");
var Tinter = require("./Tinter"); var Tinter = require("./Tinter");

View file

@ -4,7 +4,7 @@ import Q from 'q';
import MatrixClientPeg from '../MatrixClientPeg'; import MatrixClientPeg from '../MatrixClientPeg';
import Fuse from 'fuse.js'; import Fuse from 'fuse.js';
import {PillCompletion} from './Components'; import {PillCompletion} from './Components';
import {getDisplayAliasForRoom} from '../MatrixTools'; import {getDisplayAliasForRoom} from '../Rooms';
import sdk from '../index'; import sdk from '../index';
const ROOM_REGEX = /(?=#)([^\s]*)/g; const ROOM_REGEX = /(?=#)([^\s]*)/g;

View file

@ -36,7 +36,7 @@ var PostRegistration = require("./login/PostRegistration");
var Modal = require("../../Modal"); var Modal = require("../../Modal");
var Tinter = require("../../Tinter"); var Tinter = require("../../Tinter");
var sdk = require('../../index'); var sdk = require('../../index');
var MatrixTools = require('../../MatrixTools'); var Rooms = require('../../Rooms');
var linkifyMatrix = require("../../linkify-matrix"); var linkifyMatrix = require("../../linkify-matrix");
var KeyCode = require('../../KeyCode'); var KeyCode = require('../../KeyCode');
var Lifecycle = require('../../Lifecycle'); var Lifecycle = require('../../Lifecycle');
@ -479,7 +479,7 @@ module.exports = React.createClass({
var presentedId = room_info.room_alias || room_info.room_id; var presentedId = room_info.room_alias || room_info.room_id;
var room = MatrixClientPeg.get().getRoom(room_info.room_id); var room = MatrixClientPeg.get().getRoom(room_info.room_id);
if (room) { if (room) {
var theAlias = MatrixTools.getDisplayAliasForRoom(room); var theAlias = Rooms.getDisplayAliasForRoom(room);
if (theAlias) presentedId = theAlias; if (theAlias) presentedId = theAlias;
// No need to do this given RoomView triggers it itself... // No need to do this given RoomView triggers it itself...
@ -592,7 +592,7 @@ module.exports = React.createClass({
var presentedId = self.state.currentRoomId; var presentedId = self.state.currentRoomId;
var room = MatrixClientPeg.get().getRoom(self.state.currentRoomId); var room = MatrixClientPeg.get().getRoom(self.state.currentRoomId);
if (room) { if (room) {
var theAlias = MatrixTools.getDisplayAliasForRoom(room); var theAlias = Rooms.getDisplayAliasForRoom(room);
if (theAlias) presentedId = theAlias; if (theAlias) presentedId = theAlias;
} }

View file

@ -36,7 +36,6 @@ var dis = require("../../dispatcher");
var Tinter = require("../../Tinter"); var Tinter = require("../../Tinter");
var rate_limited_func = require('../../ratelimitedfunc'); var rate_limited_func = require('../../ratelimitedfunc');
var ObjectUtils = require('../../ObjectUtils'); var ObjectUtils = require('../../ObjectUtils');
var MatrixTools = require('../../MatrixTools');
import UserProvider from '../../autocomplete/UserProvider'; import UserProvider from '../../autocomplete/UserProvider';

View file

@ -25,7 +25,7 @@ var Unread = require('../../../Unread');
var dis = require("../../../dispatcher"); var dis = require("../../../dispatcher");
var sdk = require('../../../index'); var sdk = require('../../../index');
var rate_limited_func = require('../../../ratelimitedfunc'); var rate_limited_func = require('../../../ratelimitedfunc');
var MatrixTools = require('../../../MatrixTools'); var Rooms = require('../../../Rooms');
var DMRoomMap = require('../../../utils/DMRoomMap'); var DMRoomMap = require('../../../utils/DMRoomMap');
var HIDE_CONFERENCE_CHANS = true; var HIDE_CONFERENCE_CHANS = true;
@ -224,7 +224,7 @@ module.exports = React.createClass({
if (me.membership == "invite") { if (me.membership == "invite") {
s.lists["im.vector.fake.invite"].push(room); 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 // skip past this room & don't put it in any lists
} }
else if (dmRoomMap.getUserIdForRoomId(room.roomId)) { else if (dmRoomMap.getUserIdForRoomId(room.roomId)) {
@ -265,7 +265,7 @@ module.exports = React.createClass({
for (const room of oldRecents) { for (const room of oldRecents) {
const me = room.getMember(MatrixClientPeg.get().credentials.userId); 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); s.lists["im.vector.fake.direct"].push(room);
} else { } else {
s.lists["im.vector.fake.recent"].push(room); s.lists["im.vector.fake.recent"].push(room);
@ -276,7 +276,7 @@ module.exports = React.createClass({
const newMDirectEvent = {}; const newMDirectEvent = {};
for (const room of s.lists["im.vector.fake.direct"]) { for (const room of s.lists["im.vector.fake.direct"]) {
const me = room.getMember(MatrixClientPeg.get().credentials.userId); const me = room.getMember(MatrixClientPeg.get().credentials.userId);
const otherPerson = MatrixTools.getOnlyOtherMember(room, me); const otherPerson = Rooms.getOnlyOtherMember(room, me);
if (!otherPerson) continue; if (!otherPerson) continue;
const roomList = newMDirectEvent[otherPerson.userId] || []; const roomList = newMDirectEvent[otherPerson.userId] || [];