2015-09-30 17:52:45 +02:00
|
|
|
/*
|
2016-01-07 05:17:56 +01:00
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
2015-09-30 17:52:45 +02:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2015-09-11 16:49:47 +02:00
|
|
|
"use strict";
|
2015-09-30 17:52:45 +02:00
|
|
|
|
2017-07-13 01:48:31 +02:00
|
|
|
import Promise from 'bluebird';
|
2015-09-11 16:49:47 +02:00
|
|
|
var Matrix = require("matrix-js-sdk");
|
|
|
|
var Room = Matrix.Room;
|
2015-09-30 19:22:15 +02:00
|
|
|
var CallHandler = require('matrix-react-sdk/lib/CallHandler');
|
2015-09-11 16:49:47 +02:00
|
|
|
|
2015-09-17 12:47:42 +02:00
|
|
|
// FIXME: This currently forces Vector to try to hit the matrix.org AS for conferencing.
|
|
|
|
// This is bad because it prevents people running their own ASes from being used.
|
|
|
|
// This isn't permanent and will be customisable in the future: see the proposal
|
|
|
|
// at docs/conferencing.md for more info.
|
2015-09-11 16:49:47 +02:00
|
|
|
var USER_PREFIX = "fs_";
|
|
|
|
var DOMAIN = "matrix.org";
|
|
|
|
|
2015-09-11 17:55:48 +02:00
|
|
|
function ConferenceCall(matrixClient, groupChatRoomId) {
|
2015-09-11 16:49:47 +02:00
|
|
|
this.client = matrixClient;
|
|
|
|
this.groupRoomId = groupChatRoomId;
|
2015-09-15 14:04:09 +02:00
|
|
|
this.confUserId = module.exports.getConferenceUserIdForRoom(this.groupRoomId);
|
2015-09-11 16:49:47 +02:00
|
|
|
}
|
|
|
|
|
2015-09-11 17:55:48 +02:00
|
|
|
ConferenceCall.prototype.setup = function() {
|
2015-09-11 16:49:47 +02:00
|
|
|
var self = this;
|
|
|
|
return this._joinConferenceUser().then(function() {
|
|
|
|
return self._getConferenceUserRoom();
|
|
|
|
}).then(function(room) {
|
2015-09-15 14:04:09 +02:00
|
|
|
// return a call for *this* room to be placed. We also tack on
|
|
|
|
// confUserId to speed up lookups (else we'd need to loop every room
|
|
|
|
// looking for a 1:1 room with this conf user ID!)
|
|
|
|
var call = Matrix.createNewMatrixCall(self.client, room.roomId);
|
|
|
|
call.confUserId = self.confUserId;
|
2016-04-12 03:27:35 +02:00
|
|
|
call.groupRoomId = self.groupRoomId;
|
2015-09-15 14:04:09 +02:00
|
|
|
return call;
|
2015-09-11 16:49:47 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-09-11 17:55:48 +02:00
|
|
|
ConferenceCall.prototype._joinConferenceUser = function() {
|
2015-09-11 16:49:47 +02:00
|
|
|
// Make sure the conference user is in the group chat room
|
|
|
|
var groupRoom = this.client.getRoom(this.groupRoomId);
|
|
|
|
if (!groupRoom) {
|
|
|
|
return q.reject("Bad group room ID");
|
|
|
|
}
|
2015-09-11 17:14:30 +02:00
|
|
|
var member = groupRoom.getMember(this.confUserId);
|
|
|
|
if (member && member.membership === "join") {
|
2015-09-11 16:49:47 +02:00
|
|
|
return q();
|
|
|
|
}
|
|
|
|
return this.client.invite(this.groupRoomId, this.confUserId);
|
|
|
|
};
|
|
|
|
|
2015-09-11 17:55:48 +02:00
|
|
|
ConferenceCall.prototype._getConferenceUserRoom = function() {
|
2015-09-11 16:49:47 +02:00
|
|
|
// Use an existing 1:1 with the conference user; else make one
|
|
|
|
var rooms = this.client.getRooms();
|
|
|
|
var confRoom = null;
|
|
|
|
for (var i = 0; i < rooms.length; i++) {
|
2015-09-11 17:14:30 +02:00
|
|
|
var confUser = rooms[i].getMember(this.confUserId);
|
|
|
|
if (confUser && confUser.membership === "join" &&
|
2015-09-11 16:49:47 +02:00
|
|
|
rooms[i].getJoinedMembers().length === 2) {
|
|
|
|
confRoom = rooms[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (confRoom) {
|
|
|
|
return q(confRoom);
|
|
|
|
}
|
|
|
|
return this.client.createRoom({
|
|
|
|
preset: "private_chat",
|
|
|
|
invite: [this.confUserId]
|
|
|
|
}).then(function(res) {
|
|
|
|
return new Room(res.room_id);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-09-11 17:55:48 +02:00
|
|
|
/**
|
2015-12-16 12:55:13 +01:00
|
|
|
* Check if this user ID is in fact a conference bot.
|
|
|
|
* @param {string} userId The user ID to check.
|
2015-09-11 17:55:48 +02:00
|
|
|
* @return {boolean} True if it is a conference bot.
|
|
|
|
*/
|
2015-12-16 12:55:13 +01:00
|
|
|
module.exports.isConferenceUser = function(userId) {
|
|
|
|
if (userId.indexOf("@" + USER_PREFIX) !== 0) {
|
2015-09-11 17:55:48 +02:00
|
|
|
return false;
|
|
|
|
}
|
2015-12-16 12:55:13 +01:00
|
|
|
var base64part = userId.split(":")[0].substring(1 + USER_PREFIX.length);
|
2015-09-11 17:55:48 +02:00
|
|
|
if (base64part) {
|
|
|
|
var decoded = new Buffer(base64part, "base64").toString();
|
|
|
|
// ! $STUFF : $STUFF
|
|
|
|
return /^!.+:.+/.test(decoded);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2015-09-15 14:04:09 +02:00
|
|
|
module.exports.getConferenceUserIdForRoom = function(roomId) {
|
|
|
|
// abuse browserify's core node Buffer support (strip padding ='s)
|
|
|
|
var base64RoomId = new Buffer(roomId).toString("base64").replace(/=/g, "");
|
|
|
|
return "@" + USER_PREFIX + base64RoomId + ":" + DOMAIN;
|
|
|
|
};
|
|
|
|
|
2015-09-30 17:52:45 +02:00
|
|
|
module.exports.createNewMatrixCall = function(client, roomId) {
|
2015-09-30 19:22:15 +02:00
|
|
|
var confCall = new ConferenceCall(
|
2015-09-30 17:52:45 +02:00
|
|
|
client, roomId
|
|
|
|
);
|
2015-09-30 19:22:15 +02:00
|
|
|
return confCall.setup();
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports.getConferenceCallForRoom = function(roomId) {
|
|
|
|
// search for a conference 1:1 call for this group chat room ID
|
|
|
|
var activeCall = CallHandler.getAnyActiveCall();
|
|
|
|
if (activeCall && activeCall.confUserId) {
|
|
|
|
var thisRoomConfUserId = module.exports.getConferenceUserIdForRoom(
|
|
|
|
roomId
|
|
|
|
);
|
|
|
|
if (thisRoomConfUserId === activeCall.confUserId) {
|
|
|
|
return activeCall;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
2015-09-30 17:52:45 +02:00
|
|
|
};
|
|
|
|
|
2015-09-11 17:55:48 +02:00
|
|
|
module.exports.ConferenceCall = ConferenceCall;
|
2015-09-11 16:49:47 +02:00
|
|
|
|
2015-09-30 17:52:45 +02:00
|
|
|
module.exports.slot = 'conference';
|