2015-09-11 16:49:47 +02:00
|
|
|
"use strict";
|
|
|
|
var q = require("q");
|
|
|
|
var Matrix = require("matrix-js-sdk");
|
|
|
|
var Room = Matrix.Room;
|
|
|
|
|
|
|
|
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;
|
|
|
|
// abuse browserify's core node Buffer support (strip padding ='s)
|
2015-09-11 17:55:48 +02:00
|
|
|
var base64RoomId = new Buffer(groupChatRoomId).toString("base64").replace(/=/g, "");
|
|
|
|
this.confUserId = "@" + USER_PREFIX + base64RoomId + ":" + DOMAIN;
|
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) {
|
|
|
|
// return a call for *this* room to be placed.
|
|
|
|
return Matrix.createNewMatrixCall(self.client, room.roomId);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
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
|
|
|
/**
|
|
|
|
* Check if this room member is in fact a conference bot.
|
|
|
|
* @param {RoomMember} The room member to check
|
|
|
|
* @return {boolean} True if it is a conference bot.
|
|
|
|
*/
|
|
|
|
module.exports.isConferenceUser = function(roomMember) {
|
|
|
|
if (roomMember.userId.indexOf("@" + USER_PREFIX) !== 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
var base64part = roomMember.userId.split(":")[0].substring(1 + USER_PREFIX.length);
|
|
|
|
if (base64part) {
|
|
|
|
var decoded = new Buffer(base64part, "base64").toString();
|
|
|
|
// ! $STUFF : $STUFF
|
|
|
|
return /^!.+:.+/.test(decoded);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports.ConferenceCall = ConferenceCall;
|
2015-09-11 16:49:47 +02:00
|
|
|
|