SYWEB-152: Remove room join logic from RoomController and put it in eventHandlerService.joinRoom.

pull/19/head
Kegan Dougal 2014-11-17 11:03:58 +00:00
parent 7799e14121
commit da6df07a9d
2 changed files with 101 additions and 145 deletions

View File

@ -51,11 +51,6 @@ function(matrixService, $rootScope, $q, $timeout, $filter, mPresence, notificati
}; };
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) {
var room = modelService.getRoom(event.room_id); var room = modelService.getRoom(event.room_id);
@ -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
@ -489,15 +506,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);
} }

View File

@ -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,68 +337,16 @@ 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);
} }
if (room_id_or_alias && '!' === room_id_or_alias[0]) { eventHandlerService.joinRoom(room_id_or_alias).then(function(roomId) {
// it's a room ID since they start with ! $scope.room_id = roomId;
$scope.room_id = room_id_or_alias;
$scope.room_alias = modelService.getRoomIdToAliasMapping($scope.room_id);
onInit2();
}
else {
if (room_id_or_alias && '#' === room_id_or_alias[0]) {
$scope.room_alias = room_id_or_alias;
}
else {
// Else get the room alias by hand from the URL
// ie: extract #public:localhost:8080 from http://127.0.0.1:8000/#/room/#public:localhost:8080
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
console.log("Resolving alias: " + $scope.room_alias);
matrixService.resolveRoomAlias($scope.room_alias).then(function(response) {
$scope.room_id = response.data.room_id;
console.log(" -> Room ID: " + $scope.room_id);
// Now, we can go on
onInit2();
},
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); $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
eventHandlerService.waitForInitialSyncCompletion().then(
function() {
console.log("initialSync is complete.");
var needsToJoin = true;
// The room members is available in the data fetched by initialSync
if ($scope.room) {
var messages = $scope.room.events; var messages = $scope.room.events;
if (0 === messages.length if (0 === messages.length
@ -417,53 +359,6 @@ angular.module('RoomController', ['ngSanitize', 'matrixFilter', 'mFileInput', 'a
$scope.state.first_pagination = false; $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();
}
}
);
};
var onInit3 = function() {
console.log("onInit3");
// Make recents highlight the current room // Make recents highlight the current room
recentsService.setSelectedRoomId($scope.room_id); recentsService.setSelectedRoomId($scope.room_id);
@ -472,15 +367,25 @@ angular.module('RoomController', ['ngSanitize', 'matrixFilter', 'mFileInput', 'a
// Allow pagination // Allow pagination
$scope.state.can_paginate = true; $scope.state.can_paginate = true;
// Do a first pagination only if it is required // Do a first pagination only if it is required (e.g. we've JUST joined a room and have no messages to display.)
// FIXME: Should be no more require when initialSync/{room_id} will be available // FIXME: Should be no more require when initialSync/{room_id} will be available
if ($scope.state.first_pagination) { if ($scope.state.first_pagination) {
paginate(MESSAGES_PER_PAGINATION); paginate(MESSAGES_PER_PAGINATION);
} }
else {
// There are already messages, go to the last message // Scroll down as soon as possible so that we point to the last message
// if it already exists in memory
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() {