From 395bb64b2677465cdb6bd2dc2d659dd59f583e8a Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Mon, 3 Nov 2014 10:23:14 +0000 Subject: [PATCH] Keep matrixService stateless and make matrixFilter use modelService. --- .../matrix/event-handler-service.js | 21 +++++++ webclient/components/matrix/matrix-filter.js | 15 ++--- webclient/components/matrix/matrix-service.js | 57 +++++-------------- webclient/components/matrix/model-service.js | 5 +- webclient/room/room-controller.js | 10 ++-- 5 files changed, 54 insertions(+), 54 deletions(-) diff --git a/webclient/components/matrix/event-handler-service.js b/webclient/components/matrix/event-handler-service.js index c11391a6da..dc00d83f33 100644 --- a/webclient/components/matrix/event-handler-service.js +++ b/webclient/components/matrix/event-handler-service.js @@ -705,6 +705,27 @@ function(matrixService, $rootScope, $q, $timeout, mPresence, notificationService return getMember(room_id, user_id); }, + /** + * Return the power level of an user in a particular room + * @param {String} room_id the room id + * @param {String} user_id the user id + * @returns {Number} a value between 0 and 10 + */ + getUserPowerLevel: function(room_id, user_id) { + var powerLevel = 0; + var room = $rootScope.events.rooms[room_id]; + if (room && room["m.room.power_levels"]) { + if (user_id in room["m.room.power_levels"].content) { + powerLevel = room["m.room.power_levels"].content[user_id]; + } + else { + // Use the room default user power + powerLevel = room["m.room.power_levels"].content["default"]; + } + } + return powerLevel; + }, + /** * Return the display name of an user acccording to data already downloaded * @param {String} room_id the room id diff --git a/webclient/components/matrix/matrix-filter.js b/webclient/components/matrix/matrix-filter.js index 3d64a569a1..e4b42a2edb 100644 --- a/webclient/components/matrix/matrix-filter.js +++ b/webclient/components/matrix/matrix-filter.js @@ -19,23 +19,24 @@ angular.module('matrixFilter', []) // Compute the room name according to information we have -.filter('mRoomName', ['$rootScope', 'matrixService', 'eventHandlerService', function($rootScope, matrixService, eventHandlerService) { +.filter('mRoomName', ['$rootScope', 'matrixService', 'eventHandlerService', 'modelService', +function($rootScope, matrixService, eventHandlerService, modelService) { return function(room_id) { var roomName; // If there is an alias, use it // TODO: only one alias is managed for now var alias = matrixService.getRoomIdToAliasMapping(room_id); - - var room = $rootScope.events.rooms[room_id]; + var room = modelService.getRoom(room_id).current_room_state; + if (room) { // Get name from room state date - var room_name_event = room["m.room.name"]; + var room_name_event = room.state("m.room.name"); // Determine if it is a public room var isPublicRoom = false; - if (room["m.room.join_rules"] && room["m.room.join_rules"].content) { - isPublicRoom = ("public" === room["m.room.join_rules"].content.join_rule); + if (room.state("m.room.join_rules") && room.state("m.room.join_rules").content) { + isPublicRoom = ("public" === room.state("m.room.join_rules").content.join_rule); } if (room_name_event) { @@ -44,7 +45,7 @@ angular.module('matrixFilter', []) else if (alias) { roomName = alias; } - else if (room.members && !isPublicRoom) { // Do not rename public room + else if (room.members.length > 0 && !isPublicRoom) { // Do not rename public room var user_id = matrixService.config().user_id; // Else, build the name from its users diff --git a/webclient/components/matrix/matrix-service.js b/webclient/components/matrix/matrix-service.js index 1840cf46c0..d089bf601c 100644 --- a/webclient/components/matrix/matrix-service.js +++ b/webclient/components/matrix/matrix-service.js @@ -724,57 +724,30 @@ angular.module('matrixService', []) //console.log("looking for roomId for " + alias + "; found: " + roomId); return roomId; }, - - /****** Power levels management ******/ - - /** - * Return the power level of an user in a particular room - * @param {String} room_id the room id - * @param {String} user_id the user id - * @returns {Number} a value between 0 and 10 - */ - getUserPowerLevel: function(room_id, user_id) { - var powerLevel = 0; - var room = $rootScope.events.rooms[room_id]; - if (room && room["m.room.power_levels"]) { - if (user_id in room["m.room.power_levels"].content) { - powerLevel = room["m.room.power_levels"].content[user_id]; - } - else { - // Use the room default user power - powerLevel = room["m.room.power_levels"].content["default"]; - } - } - return powerLevel; - }, /** * Change or reset the power level of a user * @param {String} room_id the room id * @param {String} user_id the user id - * @param {Number} powerLevel a value between 0 and 10 + * @param {Number} powerLevel The desired power level. * If undefined, the user power level will be reset, ie he will use the default room user power level + * @param event The existing m.room.power_levels event if one exists. * @returns {promise} an $http promise */ - setUserPowerLevel: function(room_id, user_id, powerLevel) { - - // Hack: currently, there is no home server API so do it by hand by updating - // the current m.room.power_levels of the room and send it to the server - var room = $rootScope.events.rooms[room_id]; - if (room && room["m.room.power_levels"]) { - var content = angular.copy(room["m.room.power_levels"].content); - content[user_id] = powerLevel; - - var path = "/rooms/$room_id/state/m.room.power_levels"; - path = path.replace("$room_id", encodeURIComponent(room_id)); - - return doRequest("PUT", path, undefined, content); + setUserPowerLevel: function(room_id, user_id, powerLevel, event) { + var content = {}; + if (event) { + // if there is an existing event, copy the content as it contains + // the power level values for other members which we do not want + // to modify. + content = angular.copy(event.content); } - - // The room does not exist or does not contain power_levels data - var deferred = $q.defer(); - deferred.reject({data:{error: "Invalid room: " + room_id}}); - return deferred.promise; + content[user_id] = powerLevel; + + var path = "/rooms/$room_id/state/m.room.power_levels"; + path = path.replace("$room_id", encodeURIComponent(room_id)); + + return doRequest("PUT", path, undefined, content); }, getTurnServer: function() { diff --git a/webclient/components/matrix/model-service.js b/webclient/components/matrix/model-service.js index 6167a84c62..26349f1461 100644 --- a/webclient/components/matrix/model-service.js +++ b/webclient/components/matrix/model-service.js @@ -89,8 +89,11 @@ angular.module('modelService', []) // provided which can just be given the type and it will return the // 0-len event by default. state: function state(type, state_key) { + if (!type) { + return undefined; // event type MUST be specified + } if (!state_key) { - return this.state_events[type]; + return this.state_events[type]; // treat as 0-len state key } return this.state_events[type + state_key]; }, diff --git a/webclient/room/room-controller.js b/webclient/room/room-controller.js index b5f2b60419..38158ffc74 100644 --- a/webclient/room/room-controller.js +++ b/webclient/room/room-controller.js @@ -404,7 +404,7 @@ angular.module('RoomController', ['ngSanitize', 'matrixFilter', 'mFileInput']) var updateUserPowerLevel = function(user_id) { var member = $scope.members[user_id]; if (member) { - member.powerLevel = matrixService.getUserPowerLevel($scope.room_id, user_id); + member.powerLevel = eventHandlerService.getUserPowerLevel($scope.room_id, user_id); normaliseMembersPowerLevels(); } @@ -576,7 +576,8 @@ angular.module('RoomController', ['ngSanitize', 'matrixFilter', 'mFileInput']) powerLevel = parseInt(matches[3]); } if (powerLevel !== NaN) { - promise = matrixService.setUserPowerLevel($scope.room_id, user_id, powerLevel); + var powerLevelEvent = $scope.room.current_room_state.state("m.room.power_levels"); + promise = matrixService.setUserPowerLevel($scope.room_id, user_id, powerLevel, powerLevelEvent); } } } @@ -591,7 +592,8 @@ angular.module('RoomController', ['ngSanitize', 'matrixFilter', 'mFileInput']) if (args) { var matches = args.match(/^(\S+)$/); if (matches) { - promise = matrixService.setUserPowerLevel($scope.room_id, args, undefined); + var powerLevelEvent = $scope.room.current_room_state.state("m.room.power_levels"); + promise = matrixService.setUserPowerLevel($scope.room_id, args, undefined, powerLevelEvent); } } @@ -1005,7 +1007,7 @@ angular.module('RoomController', ['ngSanitize', 'matrixFilter', 'mFileInput']) $scope.event_selected = content; // scope this so the template can check power levels and enable/disable // buttons - $scope.pow = matrixService.getUserPowerLevel; + $scope.pow = eventHandlerService.getUserPowerLevel; var modalInstance = $modal.open({ templateUrl: 'eventInfoTemplate.html',