Keep matrixService stateless and make matrixFilter use modelService.

This commit is contained in:
Kegan Dougal 2014-11-03 10:23:14 +00:00
parent 5ab9929cbb
commit 395bb64b26
5 changed files with 54 additions and 54 deletions

View File

@ -705,6 +705,27 @@ function(matrixService, $rootScope, $q, $timeout, mPresence, notificationService
return getMember(room_id, user_id); 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 * Return the display name of an user acccording to data already downloaded
* @param {String} room_id the room id * @param {String} room_id the room id

View File

@ -19,23 +19,24 @@
angular.module('matrixFilter', []) angular.module('matrixFilter', [])
// Compute the room name according to information we have // 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) { return function(room_id) {
var roomName; var roomName;
// If there is an alias, use it // If there is an alias, use it
// TODO: only one alias is managed for now // TODO: only one alias is managed for now
var alias = matrixService.getRoomIdToAliasMapping(room_id); var alias = matrixService.getRoomIdToAliasMapping(room_id);
var room = modelService.getRoom(room_id).current_room_state;
var room = $rootScope.events.rooms[room_id];
if (room) { if (room) {
// Get name from room state date // 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 // Determine if it is a public room
var isPublicRoom = false; var isPublicRoom = false;
if (room["m.room.join_rules"] && room["m.room.join_rules"].content) { if (room.state("m.room.join_rules") && room.state("m.room.join_rules").content) {
isPublicRoom = ("public" === room["m.room.join_rules"].content.join_rule); isPublicRoom = ("public" === room.state("m.room.join_rules").content.join_rule);
} }
if (room_name_event) { if (room_name_event) {
@ -44,7 +45,7 @@ angular.module('matrixFilter', [])
else if (alias) { else if (alias) {
roomName = 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; var user_id = matrixService.config().user_id;
// Else, build the name from its users // Else, build the name from its users

View File

@ -725,56 +725,29 @@ angular.module('matrixService', [])
return 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 * Change or reset the power level of a user
* @param {String} room_id the room id * @param {String} room_id the room id
* @param {String} user_id the user 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 * 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 * @returns {promise} an $http promise
*/ */
setUserPowerLevel: function(room_id, user_id, powerLevel) { setUserPowerLevel: function(room_id, user_id, powerLevel, event) {
var content = {};
// Hack: currently, there is no home server API so do it by hand by updating if (event) {
// the current m.room.power_levels of the room and send it to the server // if there is an existing event, copy the content as it contains
var room = $rootScope.events.rooms[room_id]; // the power level values for other members which we do not want
if (room && room["m.room.power_levels"]) { // to modify.
var content = angular.copy(room["m.room.power_levels"].content); content = angular.copy(event.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);
} }
content[user_id] = powerLevel;
// The room does not exist or does not contain power_levels data var path = "/rooms/$room_id/state/m.room.power_levels";
var deferred = $q.defer(); path = path.replace("$room_id", encodeURIComponent(room_id));
deferred.reject({data:{error: "Invalid room: " + room_id}});
return deferred.promise; return doRequest("PUT", path, undefined, content);
}, },
getTurnServer: function() { getTurnServer: function() {

View File

@ -89,8 +89,11 @@ angular.module('modelService', [])
// provided which can just be given the type and it will return the // provided which can just be given the type and it will return the
// 0-len event by default. // 0-len event by default.
state: function state(type, state_key) { state: function state(type, state_key) {
if (!type) {
return undefined; // event type MUST be specified
}
if (!state_key) { 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]; return this.state_events[type + state_key];
}, },

View File

@ -404,7 +404,7 @@ angular.module('RoomController', ['ngSanitize', 'matrixFilter', 'mFileInput'])
var updateUserPowerLevel = function(user_id) { var updateUserPowerLevel = function(user_id) {
var member = $scope.members[user_id]; var member = $scope.members[user_id];
if (member) { if (member) {
member.powerLevel = matrixService.getUserPowerLevel($scope.room_id, user_id); member.powerLevel = eventHandlerService.getUserPowerLevel($scope.room_id, user_id);
normaliseMembersPowerLevels(); normaliseMembersPowerLevels();
} }
@ -576,7 +576,8 @@ angular.module('RoomController', ['ngSanitize', 'matrixFilter', 'mFileInput'])
powerLevel = parseInt(matches[3]); powerLevel = parseInt(matches[3]);
} }
if (powerLevel !== NaN) { 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) { if (args) {
var matches = args.match(/^(\S+)$/); var matches = args.match(/^(\S+)$/);
if (matches) { 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.event_selected = content;
// scope this so the template can check power levels and enable/disable // scope this so the template can check power levels and enable/disable
// buttons // buttons
$scope.pow = matrixService.getUserPowerLevel; $scope.pow = eventHandlerService.getUserPowerLevel;
var modalInstance = $modal.open({ var modalInstance = $modal.open({
templateUrl: 'eventInfoTemplate.html', templateUrl: 'eventInfoTemplate.html',