From b93804529db45ab8bc5e320bdb9b34e9580e111a Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Fri, 14 Nov 2014 16:15:32 +0000 Subject: [PATCH] Move getUserPowerLevel to modelService. --- .../matrix/event-handler-service.js | 21 --------- .../components/matrix/model-service.js | 24 +++++++++- syweb/webclient/room/room-controller.js | 3 +- .../test/unit/event-handler-service.spec.js | 47 ------------------- .../webclient/test/unit/model-service.spec.js | 34 ++++++++++++++ 5 files changed, 58 insertions(+), 71 deletions(-) diff --git a/syweb/webclient/components/matrix/event-handler-service.js b/syweb/webclient/components/matrix/event-handler-service.js index e9540bd5c0..8a32d06696 100644 --- a/syweb/webclient/components/matrix/event-handler-service.js +++ b/syweb/webclient/components/matrix/event-handler-service.js @@ -546,27 +546,6 @@ function(matrixService, $rootScope, $q, $timeout, $filter, mPresence, notificati } return memberCount; - }, - - /** - * 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 = modelService.getRoom(room_id).current_room_state; - if (room.state("m.room.power_levels")) { - if (user_id in room.state("m.room.power_levels").content) { - powerLevel = room.state("m.room.power_levels").content[user_id]; - } - else { - // Use the room default user power - powerLevel = room.state("m.room.power_levels").content["default"]; - } - } - return powerLevel; } }; }]); diff --git a/syweb/webclient/components/matrix/model-service.js b/syweb/webclient/components/matrix/model-service.js index e1fed65152..b4d59e7324 100644 --- a/syweb/webclient/components/matrix/model-service.js +++ b/syweb/webclient/components/matrix/model-service.js @@ -118,7 +118,8 @@ angular.module('modelService', []) }, storeStateEvent: function storeState(event) { - this.state_events[event.type + event.state_key] = event; + var keyIndex = event.state_key === undefined ? event.type : event.type + event.state_key; + this.state_events[keyIndex] = event; if (event.type === "m.room.member") { var userId = event.state_key; var rm = new RoomMember(); @@ -262,6 +263,27 @@ angular.module('modelService', []) rm.user = usr; } } + }, + + /** + * 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} + */ + getUserPowerLevel: function(room_id, user_id) { + var powerLevel = 0; + var room = this.getRoom(room_id).current_room_state; + if (room.state("m.room.power_levels")) { + if (user_id in room.state("m.room.power_levels").content) { + powerLevel = room.state("m.room.power_levels").content[user_id]; + } + else { + // Use the room default user power + powerLevel = room.state("m.room.power_levels").content["default"]; + } + } + return powerLevel; } }; diff --git a/syweb/webclient/room/room-controller.js b/syweb/webclient/room/room-controller.js index afa029117b..cf9bc1fcdb 100644 --- a/syweb/webclient/room/room-controller.js +++ b/syweb/webclient/room/room-controller.js @@ -405,7 +405,6 @@ angular.module('RoomController', ['ngSanitize', 'matrixFilter', 'mFileInput', 'a // The room members is available in the data fetched by initialSync if ($scope.room) { - var messages = $scope.room.events; if (0 === messages.length @@ -578,7 +577,7 @@ angular.module('RoomController', ['ngSanitize', 'matrixFilter', 'mFileInput', 'a // scope this so the template can check power levels and enable/disable // buttons - $scope.pow = eventHandlerService.getUserPowerLevel; + $scope.pow = modelService.getUserPowerLevel; var modalInstance = $modal.open({ templateUrl: 'eventInfoTemplate.html', diff --git a/syweb/webclient/test/unit/event-handler-service.spec.js b/syweb/webclient/test/unit/event-handler-service.spec.js index 2a4dc3b5a5..c17cdb8715 100644 --- a/syweb/webclient/test/unit/event-handler-service.spec.js +++ b/syweb/webclient/test/unit/event-handler-service.spec.js @@ -67,51 +67,4 @@ describe('EventHandlerService', function() { var num = eventHandlerService.getUsersCountInRoom(roomId); expect(num).toEqual(2); })); - - it('should be able to get a users power level', inject( - function(eventHandlerService) { - var roomId = "!foo:matrix.org"; - // set mocked data - modelService.getRoom = function(roomId) { - return { - room_id: roomId, - current_room_state: { - members: { - "@adam:matrix.org": { - event: { - content: { membership: "join" }, - user_id: "@adam:matrix.org" - } - }, - "@beth:matrix.org": { - event: { - content: { membership: "join" }, - user_id: "@beth:matrix.org" - } - } - }, - s: { - "m.room.power_levels": { - content: { - "@adam:matrix.org": 90, - "default": 50 - } - } - }, - state: function(type, key) { - return key ? this.s[type+key] : this.s[type] - } - } - }; - }; - - var num = eventHandlerService.getUserPowerLevel(roomId, "@beth:matrix.org"); - expect(num).toEqual(50); - - num = eventHandlerService.getUserPowerLevel(roomId, "@adam:matrix.org"); - expect(num).toEqual(90); - - num = eventHandlerService.getUserPowerLevel(roomId, "@unknown:matrix.org"); - expect(num).toEqual(50); - })); }); diff --git a/syweb/webclient/test/unit/model-service.spec.js b/syweb/webclient/test/unit/model-service.spec.js index e2fa8ceba3..b99a76310b 100644 --- a/syweb/webclient/test/unit/model-service.spec.js +++ b/syweb/webclient/test/unit/model-service.spec.js @@ -27,4 +27,38 @@ describe('ModelService', function() { var user = modelService.getMember(roomId, userId); expect(user.event.state_key).toEqual(userId); })); + + it('should be able to get a users power level', inject( + function(modelService) { + var roomId = "!foo:matrix.org"; + + var room = modelService.getRoom(roomId); + room.current_room_state.storeStateEvent({ + content: { membership: "join" }, + user_id: "@adam:matrix.org", + type: "m.room.member" + }); + room.current_room_state.storeStateEvent({ + content: { membership: "join" }, + user_id: "@beth:matrix.org", + type: "m.room.member" + }); + room.current_room_state.storeStateEvent({ + content: { + "@adam:matrix.org": 90, + "default": 50 + }, + user_id: "@adam:matrix.org", + type: "m.room.power_levels" + }); + + var num = modelService.getUserPowerLevel(roomId, "@beth:matrix.org"); + expect(num).toEqual(50); + + num = modelService.getUserPowerLevel(roomId, "@adam:matrix.org"); + expect(num).toEqual(90); + + num = modelService.getUserPowerLevel(roomId, "@unknown:matrix.org"); + expect(num).toEqual(50); + })); });