Move getUserPowerLevel to modelService.

This commit is contained in:
Kegan Dougal 2014-11-14 16:15:32 +00:00
parent 78bf5648e7
commit b93804529d
5 changed files with 58 additions and 71 deletions

View file

@ -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;
}
};
}]);

View file

@ -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;
}
};

View file

@ -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',

View file

@ -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);
}));
});

View file

@ -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);
}));
});