handle m.room.aliases for id<->alias mapping; remove local_storage map; stop local echo flickering by removing opacity transition for now; implement /join

This commit is contained in:
Matthew Hodgson 2014-09-06 00:31:57 -07:00
parent aa90e53312
commit a1bf28b7f0
4 changed files with 85 additions and 10 deletions

View file

@ -66,11 +66,21 @@ angular.module('eventHandlerService', [])
$rootScope.$broadcast(ROOM_CREATE_EVENT, event, isLiveEvent); $rootScope.$broadcast(ROOM_CREATE_EVENT, event, isLiveEvent);
}; };
var handleRoomAliases = function(event, isLiveEvent) {
matrixService.createRoomIdToAliasMapping(event.room_id, event.content.aliases[0]);
};
var handleMessage = function(event, isLiveEvent) { var handleMessage = function(event, isLiveEvent) {
initRoom(event.room_id); initRoom(event.room_id);
if (isLiveEvent) { if (isLiveEvent) {
$rootScope.events.rooms[event.room_id].messages.push(event); if (event.user_id === matrixService.config().user_id) {
// assume we've already echoed it
// FIXME: track events by ID and ungrey the right message to show it's been delivered
}
else {
$rootScope.events.rooms[event.room_id].messages.push(event);
}
} }
else { else {
$rootScope.events.rooms[event.room_id].messages.unshift(event); $rootScope.events.rooms[event.room_id].messages.unshift(event);
@ -87,6 +97,14 @@ angular.module('eventHandlerService', [])
var handleRoomMember = function(event, isLiveEvent) { var handleRoomMember = function(event, isLiveEvent) {
initRoom(event.room_id); initRoom(event.room_id);
// if the server is stupidly re-relaying a no-op join, discard it.
if (event.prev_content &&
event.content.membership === "join" &&
event.content.membership === event.prev_content.membership)
{
return;
}
// add membership changes as if they were a room message if something interesting changed // add membership changes as if they were a room message if something interesting changed
if (event.content.prev !== event.content.membership) { if (event.content.prev !== event.content.membership) {
if (isLiveEvent) { if (isLiveEvent) {
@ -144,6 +162,9 @@ angular.module('eventHandlerService', [])
case "m.room.create": case "m.room.create":
handleRoomCreate(event, isLiveEvent); handleRoomCreate(event, isLiveEvent);
break; break;
case "m.room.aliases":
handleRoomAliases(event, isLiveEvent);
break;
case "m.room.message": case "m.room.message":
handleMessage(event, isLiveEvent); handleMessage(event, isLiveEvent);
break; break;

View file

@ -110,6 +110,7 @@ angular.module('eventStreamService', [])
var rooms = response.data.rooms; var rooms = response.data.rooms;
for (var i = 0; i < rooms.length; ++i) { for (var i = 0; i < rooms.length; ++i) {
var room = rooms[i]; var room = rooms[i];
// console.log("got room: " + room.room_id);
if ("state" in room) { if ("state" in room) {
eventHandlerService.handleEvents(room.state, false); eventHandlerService.handleEvents(room.state, false);
} }

View file

@ -36,6 +36,9 @@ angular.module('matrixService', [])
*/ */
var config; var config;
var roomIdToAlias = {};
var aliasToRoomId = {};
// Current version of permanent storage // Current version of permanent storage
var configVersion = 0; var configVersion = 0;
var prefixPath = "/_matrix/client/api/v1"; var prefixPath = "/_matrix/client/api/v1";
@ -529,6 +532,8 @@ angular.module('matrixService', [])
result.room_alias = alias; result.room_alias = alias;
result.room_display_name = alias; result.room_display_name = alias;
} }
// XXX: this only lets us learn aliases from our local HS - we should
// make the client stop returning this if we can trust m.room.aliases state events
else if (room.aliases && room.aliases[0]) { else if (room.aliases && room.aliases[0]) {
// save the mapping // save the mapping
// TODO: select the smarter alias from the array // TODO: select the smarter alias from the array
@ -546,13 +551,23 @@ angular.module('matrixService', [])
}, },
createRoomIdToAliasMapping: function(roomId, alias) { createRoomIdToAliasMapping: function(roomId, alias) {
localStorage.setItem(MAPPING_PREFIX+roomId, alias); //console.log("creating mapping between " + roomId + " and " + alias);
roomIdToAlias[roomId] = alias;
aliasToRoomId[alias] = roomId;
// localStorage.setItem(MAPPING_PREFIX+roomId, alias);
}, },
getRoomIdToAliasMapping: function(roomId) { getRoomIdToAliasMapping: function(roomId) {
return localStorage.getItem(MAPPING_PREFIX+roomId); var alias = roomIdToAlias[roomId]; // was localStorage.getItem(MAPPING_PREFIX+roomId)
//console.log("looking for alias for " + roomId + "; found: " + alias);
return alias;
}, },
getAliasToRoomIdMapping: function(alias) {
var roomId = aliasToRoomId[alias];
//console.log("looking for roomId for " + alias + "; found: " + roomId);
return roomId;
},
/****** Power levels management ******/ /****** Power levels management ******/

View file

@ -334,6 +334,40 @@ angular.module('RoomController', ['ngSanitize', 'matrixFilter', 'mFileInput'])
} }
break; break;
case "/join":
// Join a room
if (args) {
var matches = args.match(/^(\S+)$/);
if (matches) {
var room_alias = matches[1];
if (room_alias.indexOf(':') == -1) {
// FIXME: actually track the :domain style name of our homeserver
// with or without port as is appropriate and append it at this point
}
var room_id = matrixService.getAliasToRoomIdMapping(room_alias);
console.log("joining " + room_alias + " id=" + room_id);
if ($rootScope.events.rooms[room_id]) {
// don't send a join event for a room you're already in.
$location.url("room/" + room_alias);
}
else {
promise = matrixService.joinAlias(room_alias).then(
function(response) {
$location.url("room/" + room_alias);
},
function(error) {
$scope.feedback = "Can't join room: " + JSON.stringify(error.data);
}
);
}
}
}
else {
$scope.feedback = "Usage: /join <room_alias>";
}
break;
case "/kick": case "/kick":
// Kick a user from the room with an optional reason // Kick a user from the room with an optional reason
if (args) { if (args) {
@ -428,13 +462,14 @@ angular.module('RoomController', ['ngSanitize', 'matrixFilter', 'mFileInput'])
var echoMessage = { var echoMessage = {
content: { content: {
body: message, body: message,
hsob_ts: "Sending...", // Hack timestamp to display this text in place of the message time hsob_ts: new Date().getTime(), // fake a timestamp
msgtype: "m.text" msgtype: "m.text"
}, },
room_id: $scope.room_id, room_id: $scope.room_id,
type: "m.room.message", type: "m.room.message",
user_id: $scope.state.user_id, user_id: $scope.state.user_id,
echo_msg_state: "messagePending" // Add custom field to indicate the state of this fake message to HTML // FIXME: re-enable echo_msg_state when we have a nice way to turn the field off again
// echo_msg_state: "messagePending" // Add custom field to indicate the state of this fake message to HTML
}; };
$rootScope.events.rooms[$scope.room_id].messages.push(echoMessage); $rootScope.events.rooms[$scope.room_id].messages.push(echoMessage);
@ -448,18 +483,21 @@ angular.module('RoomController', ['ngSanitize', 'matrixFilter', 'mFileInput'])
promise.then( promise.then(
function() { function() {
console.log("Request successfully sent"); console.log("Request successfully sent");
$scope.textInput = "";
/*
if (echoMessage) { if (echoMessage) {
// Remove the fake echo message from the room messages // Remove the fake echo message from the room messages
// It will be replaced by the one acknowledged by the server // It will be replaced by the one acknowledged by the server
var index = $rootScope.events.rooms[$scope.room_id].messages.indexOf(echoMessage); // ...except this causes a nasty flicker. So don't swap messages for now. --matthew
if (index > -1) { // var index = $rootScope.events.rooms[$scope.room_id].messages.indexOf(echoMessage);
$rootScope.events.rooms[$scope.room_id].messages.splice(index, 1); // if (index > -1) {
} // $rootScope.events.rooms[$scope.room_id].messages.splice(index, 1);
// }
} }
else { else {
$scope.textInput = ""; $scope.textInput = "";
} }
*/
}, },
function(error) { function(error) {
$scope.feedback = "Request failed: " + error.data.error; $scope.feedback = "Request failed: " + error.data.error;