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 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

View file

@ -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 = modelService.getRoom(room_id).current_room_state;
var room = $rootScope.events.rooms[room_id];
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

View file

@ -725,56 +725,29 @@ angular.module('matrixService', [])
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);
}
content[user_id] = powerLevel;
// 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;
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() {

View file

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

View file

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