SYWEB-152: Remove room join logic from RoomController and put it in eventHandlerService.joinRoom.
This commit is contained in:
parent
7799e14121
commit
da6df07a9d
|
@ -50,11 +50,6 @@ function(matrixService, $rootScope, $q, $timeout, $filter, mPresence, notificati
|
||||||
eventMap = {};
|
eventMap = {};
|
||||||
};
|
};
|
||||||
reset();
|
reset();
|
||||||
|
|
||||||
var resetRoomMessages = function(room_id) {
|
|
||||||
var room = modelService.getRoom(room_id);
|
|
||||||
room.events = [];
|
|
||||||
};
|
|
||||||
|
|
||||||
// Generic method to handle events data
|
// Generic method to handle events data
|
||||||
var handleRoomStateEvent = function(event, isLiveEvent, addToRoomMessages) {
|
var handleRoomStateEvent = function(event, isLiveEvent, addToRoomMessages) {
|
||||||
|
@ -318,7 +313,30 @@ function(matrixService, $rootScope, $q, $timeout, $filter, mPresence, notificati
|
||||||
|
|
||||||
console.log("Redacted an event.");
|
console.log("Redacted an event.");
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
|
// resolves a room ID or alias, returning a deferred.
|
||||||
|
var resolveRoomIdentifier = function(roomIdOrAlias) {
|
||||||
|
var defer = $q.defer();
|
||||||
|
if ('#' === roomIdOrAlias[0]) {
|
||||||
|
matrixService.resolveRoomAlias(roomIdOrAlias).then(function(response) {
|
||||||
|
defer.resolve(response.data.room_id);
|
||||||
|
console.log("resolveRoomIdentifier: "+roomIdOrAlias+" -> " + response.data.room_id);
|
||||||
|
},
|
||||||
|
function(err) {
|
||||||
|
console.error("resolveRoomIdentifier: lookup failed. "+JSON.stringify(err.data));
|
||||||
|
defer.reject(err.data);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else if ('!' === roomIdOrAlias[0]) {
|
||||||
|
defer.resolve(roomIdOrAlias);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
console.error("resolveRoomIdentifier: Unknown roomIdOrAlias => "+roomIdOrAlias);
|
||||||
|
defer.reject("Bad room identifier: "+roomIdOrAlias);
|
||||||
|
}
|
||||||
|
return defer.promise;
|
||||||
|
};
|
||||||
|
|
||||||
return {
|
return {
|
||||||
ROOM_CREATE_EVENT: ROOM_CREATE_EVENT,
|
ROOM_CREATE_EVENT: ROOM_CREATE_EVENT,
|
||||||
|
@ -337,7 +355,6 @@ function(matrixService, $rootScope, $q, $timeout, $filter, mPresence, notificati
|
||||||
},
|
},
|
||||||
|
|
||||||
handleEvent: function(event, isLiveEvent, isStateEvent) {
|
handleEvent: function(event, isLiveEvent, isStateEvent) {
|
||||||
|
|
||||||
// Avoid duplicated events
|
// Avoid duplicated events
|
||||||
// Needed for rooms where initialSync has not been done.
|
// Needed for rooms where initialSync has not been done.
|
||||||
// In this case, we do not know where to start pagination. So, it starts from the END
|
// In this case, we do not know where to start pagination. So, it starts from the END
|
||||||
|
@ -488,15 +505,49 @@ function(matrixService, $rootScope, $q, $timeout, $filter, mPresence, notificati
|
||||||
|
|
||||||
initialSyncDeferred.resolve(response);
|
initialSyncDeferred.resolve(response);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// joins a room and handles the requests which need to be done e.g. getting room state
|
||||||
|
joinRoom: function(roomIdOrAlias) {
|
||||||
|
var defer = $q.defer();
|
||||||
|
var eventHandlerService = this;
|
||||||
|
|
||||||
|
var errorFunc = function(error) {
|
||||||
|
console.error("joinRoom: " + JSON.stringify(error));
|
||||||
|
defer.reject(error);
|
||||||
|
};
|
||||||
|
|
||||||
|
resolveRoomIdentifier(roomIdOrAlias).then(function(roomId) {
|
||||||
|
// check if you are joined already
|
||||||
|
eventHandlerService.waitForInitialSyncCompletion().then(function() {
|
||||||
|
var members = modelService.getRoom(roomId).current_room_state.members;
|
||||||
|
var me = matrixService.config().user_id;
|
||||||
|
if (me in members) {
|
||||||
|
if ("join" === members[me].event.content.membership) {
|
||||||
|
console.log("joinRoom: Already joined room "+roomId);
|
||||||
|
defer.resolve(roomId);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// join the room and get current room state
|
||||||
|
matrixService.join(roomId).then(function() {
|
||||||
|
matrixService.roomState(roomId).then(function(response) {
|
||||||
|
var room = modelService.getRoom(roomId);
|
||||||
|
room.current_room_state.storeStateEvents(response.data);
|
||||||
|
room.old_room_state.storeStateEvents(response.data);
|
||||||
|
console.log("joinRoom: Joined room "+roomId);
|
||||||
|
defer.resolve(roomId);
|
||||||
|
}, errorFunc);
|
||||||
|
}, errorFunc);
|
||||||
|
}, errorFunc);
|
||||||
|
}, errorFunc);
|
||||||
|
|
||||||
|
return defer.promise;
|
||||||
|
},
|
||||||
|
|
||||||
// Returns a promise that resolves when the initialSync request has been processed
|
// Returns a promise that resolves when the initialSync request has been processed
|
||||||
waitForInitialSyncCompletion: function() {
|
waitForInitialSyncCompletion: function() {
|
||||||
return initialSyncDeferred.promise;
|
return initialSyncDeferred.promise;
|
||||||
},
|
},
|
||||||
|
|
||||||
resetRoomMessages: function(room_id) {
|
|
||||||
resetRoomMessages(room_id);
|
|
||||||
},
|
|
||||||
|
|
||||||
eventContainsBingWord: function(event) {
|
eventContainsBingWord: function(event) {
|
||||||
return containsBingWord(event);
|
return containsBingWord(event);
|
||||||
|
|
|
@ -26,7 +26,6 @@ angular.module('RoomController', ['ngSanitize', 'matrixFilter', 'mFileInput', 'a
|
||||||
|
|
||||||
// Room ids. Computed and resolved in onInit
|
// Room ids. Computed and resolved in onInit
|
||||||
$scope.room_id = undefined;
|
$scope.room_id = undefined;
|
||||||
$scope.room_alias = undefined;
|
|
||||||
|
|
||||||
$scope.state = {
|
$scope.state = {
|
||||||
user_id: matrixService.config().user_id,
|
user_id: matrixService.config().user_id,
|
||||||
|
@ -157,13 +156,8 @@ angular.module('RoomController', ['ngSanitize', 'matrixFilter', 'mFileInput', 'a
|
||||||
$scope.$on(eventHandlerService.MEMBER_EVENT, function(ngEvent, event, isLive) {
|
$scope.$on(eventHandlerService.MEMBER_EVENT, function(ngEvent, event, isLive) {
|
||||||
// if there is a live event affecting us
|
// if there is a live event affecting us
|
||||||
if (isLive && event.room_id === $scope.room_id && event.state_key === $scope.state.user_id) {
|
if (isLive && event.room_id === $scope.room_id && event.state_key === $scope.state.user_id) {
|
||||||
if ($scope.state.waiting_for_joined_event) {
|
|
||||||
// The user has successfully joined the room, we can getting data for this room
|
|
||||||
$scope.state.waiting_for_joined_event = false;
|
|
||||||
onInit3();
|
|
||||||
}
|
|
||||||
// if someone else changed our state..
|
// if someone else changed our state..
|
||||||
else if (event.user_id !== $scope.state.user_id && "invite" !== event.content.membership && "join" !== event.content.membership) {
|
if (event.user_id !== $scope.state.user_id && "invite" !== event.content.membership && "join" !== event.content.membership) {
|
||||||
if ("ban" === event.content.membership) {
|
if ("ban" === event.content.membership) {
|
||||||
$scope.state.permission_denied = "You have been banned by " + mUserDisplayNameFilter(event.user_id);
|
$scope.state.permission_denied = "You have been banned by " + mUserDisplayNameFilter(event.user_id);
|
||||||
}
|
}
|
||||||
|
@ -343,145 +337,56 @@ angular.module('RoomController', ['ngSanitize', 'matrixFilter', 'mFileInput', 'a
|
||||||
$scope.onInit = function() {
|
$scope.onInit = function() {
|
||||||
console.log("onInit");
|
console.log("onInit");
|
||||||
|
|
||||||
// Try to find out the room ID to load.
|
// Extract the room identifier being loaded
|
||||||
var room_id_or_alias;
|
var room_id_or_alias;
|
||||||
if ($routeParams.room_id_or_alias) { // provided in the url
|
if ($routeParams.room_id_or_alias) { // provided in the url
|
||||||
room_id_or_alias = decodeURIComponent($routeParams.room_id_or_alias);
|
room_id_or_alias = decodeURIComponent($routeParams.room_id_or_alias);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
eventHandlerService.joinRoom(room_id_or_alias).then(function(roomId) {
|
||||||
|
$scope.room_id = roomId;
|
||||||
|
$scope.room = modelService.getRoom($scope.room_id);
|
||||||
|
|
||||||
|
var messages = $scope.room.events;
|
||||||
|
|
||||||
if (room_id_or_alias && '!' === room_id_or_alias[0]) {
|
if (0 === messages.length
|
||||||
// it's a room ID since they start with !
|
|| (1 === messages.length && "m.room.member" === messages[0].type && "invite" === messages[0].content.membership && $scope.state.user_id === messages[0].state_key)) {
|
||||||
$scope.room_id = room_id_or_alias;
|
// If we just joined a room, we won't have this history from initial sync, so we should try to paginate it anyway
|
||||||
$scope.room_alias = modelService.getRoomIdToAliasMapping($scope.room_id);
|
$scope.state.first_pagination = true;
|
||||||
onInit2();
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
if (room_id_or_alias && '#' === room_id_or_alias[0]) {
|
|
||||||
$scope.room_alias = room_id_or_alias;
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// Else get the room alias by hand from the URL
|
// There is no need to do a 1st pagination (initialSync provided enough to fill a page)
|
||||||
// ie: extract #public:localhost:8080 from http://127.0.0.1:8000/#/room/#public:localhost:8080
|
$scope.state.first_pagination = false;
|
||||||
if (3 === location.hash.split("#").length) {
|
|
||||||
$scope.room_alias = "#" + location.hash.split("#")[2];
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// In case of issue, go to the default page
|
|
||||||
console.log("Error: cannot extract room alias");
|
|
||||||
$location.url("/");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Need a room ID required in Matrix API requests
|
// Make recents highlight the current room
|
||||||
console.log("Resolving alias: " + $scope.room_alias);
|
recentsService.setSelectedRoomId($scope.room_id);
|
||||||
matrixService.resolveRoomAlias($scope.room_alias).then(function(response) {
|
|
||||||
$scope.room_id = response.data.room_id;
|
updatePresenceTimes();
|
||||||
console.log(" -> Room ID: " + $scope.room_id);
|
|
||||||
|
|
||||||
// Now, we can go on
|
// Allow pagination
|
||||||
onInit2();
|
$scope.state.can_paginate = true;
|
||||||
},
|
|
||||||
function () {
|
|
||||||
// In case of issue, go to the default page
|
|
||||||
console.log("Error: cannot resolve room alias");
|
|
||||||
$location.url("/");
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
var onInit2 = function() {
|
|
||||||
$scope.room = modelService.getRoom($scope.room_id);
|
|
||||||
|
|
||||||
// Scroll down as soon as possible so that we point to the last message
|
|
||||||
// if it already exists in memory
|
|
||||||
scrollToBottom(true);
|
|
||||||
|
|
||||||
// Make sure the initialSync has been before going further
|
// Do a first pagination only if it is required (e.g. we've JUST joined a room and have no messages to display.)
|
||||||
eventHandlerService.waitForInitialSyncCompletion().then(
|
// FIXME: Should be no more require when initialSync/{room_id} will be available
|
||||||
function() {
|
if ($scope.state.first_pagination) {
|
||||||
console.log("initialSync is complete.");
|
paginate(MESSAGES_PER_PAGINATION);
|
||||||
var needsToJoin = true;
|
|
||||||
|
|
||||||
// The room members is available in the data fetched by initialSync
|
|
||||||
if ($scope.room) {
|
|
||||||
var messages = $scope.room.events;
|
|
||||||
|
|
||||||
if (0 === messages.length
|
|
||||||
|| (1 === messages.length && "m.room.member" === messages[0].type && "invite" === messages[0].content.membership && $scope.state.user_id === messages[0].state_key)) {
|
|
||||||
// If we just joined a room, we won't have this history from initial sync, so we should try to paginate it anyway
|
|
||||||
$scope.state.first_pagination = true;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// There is no need to do a 1st pagination (initialSync provided enough to fill a page)
|
|
||||||
$scope.state.first_pagination = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
var members = $scope.room.current_room_state.members;
|
|
||||||
|
|
||||||
// Check if the user has already join the room
|
|
||||||
if ($scope.state.user_id in members) {
|
|
||||||
if ("join" === members[$scope.state.user_id].event.content.membership) {
|
|
||||||
needsToJoin = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Do we to join the room before starting?
|
|
||||||
if (needsToJoin) {
|
|
||||||
$scope.state.waiting_for_joined_event = true;
|
|
||||||
matrixService.join($scope.room_id).then(
|
|
||||||
function() {
|
|
||||||
// TODO: factor out the common housekeeping whenever we try to join a room or alias
|
|
||||||
matrixService.roomState($scope.room_id).then(
|
|
||||||
function(response) {
|
|
||||||
eventHandlerService.handleEvents(response.data, false, true);
|
|
||||||
},
|
|
||||||
function(error) {
|
|
||||||
console.error("Failed to get room state for: " + $scope.room_id);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
// onInit3 will be called once the joined m.room.member event is received from the events stream
|
|
||||||
// This avoids to get the joined information twice in parallel:
|
|
||||||
// - one from the events stream
|
|
||||||
// - one from the pagination because the pagination window covers this event ts
|
|
||||||
console.log("Joined room "+$scope.room_id);
|
|
||||||
},
|
|
||||||
function(reason) {
|
|
||||||
console.log("Can't join room: " + JSON.stringify(reason));
|
|
||||||
// FIXME: what if it wasn't a perms problem?
|
|
||||||
$scope.state.permission_denied = "You do not have permission to join this room";
|
|
||||||
});
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
onInit3();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
);
|
|
||||||
};
|
// Scroll down as soon as possible so that we point to the last message
|
||||||
|
// if it already exists in memory
|
||||||
var onInit3 = function() {
|
|
||||||
console.log("onInit3");
|
|
||||||
|
|
||||||
// Make recents highlight the current room
|
|
||||||
recentsService.setSelectedRoomId($scope.room_id);
|
|
||||||
|
|
||||||
updatePresenceTimes();
|
|
||||||
|
|
||||||
// Allow pagination
|
|
||||||
$scope.state.can_paginate = true;
|
|
||||||
|
|
||||||
// Do a first pagination only if it is required
|
|
||||||
// FIXME: Should be no more require when initialSync/{room_id} will be available
|
|
||||||
if ($scope.state.first_pagination) {
|
|
||||||
paginate(MESSAGES_PER_PAGINATION);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// There are already messages, go to the last message
|
|
||||||
scrollToBottom(true);
|
scrollToBottom(true);
|
||||||
}
|
},
|
||||||
};
|
function(err) {
|
||||||
|
if (err.data.errcode === "M_FORBIDDEN") {
|
||||||
|
$scope.state.permission_denied = "You do not have permission to join this room";
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
console.log("Error: cannot join room: "+JSON.stringify(err));
|
||||||
|
$location.url("/");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
$scope.leaveRoom = function() {
|
$scope.leaveRoom = function() {
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue