Hook into more of event-handler-service and mimic its functions for now.

This commit is contained in:
Kegan Dougal 2014-10-31 15:16:43 +00:00
parent 394f77c3ff
commit ea80b9208d
2 changed files with 130 additions and 68 deletions

View File

@ -96,7 +96,7 @@ function(matrixService, $rootScope, $q, $timeout, mPresence, notificationService
__room.old_room_state.storeStateEvents(room.state);
__room.old_room_state.pagination_token = room.messages.start;
__room.addMessages(room.messages.chunk);
__room.addMessageEvents(room.messages.chunk);
}
};
@ -108,6 +108,26 @@ function(matrixService, $rootScope, $q, $timeout, mPresence, notificationService
// Generic method to handle events data
var handleRoomDateEvent = function(event, isLiveEvent, addToRoomMessages) {
var __room = modelService.getRoom(event.room_id);
if (addToRoomMessages) {
__room.addMessageEvent(event, !isLiveEvent);
}
if (isLiveEvent) {
__room.current_room_state.storeStateEvent(event);
}
else {
var eventTs = event.origin_server_ts;
var storedEvent = __room.current_room_state.getStateEvent(event.type, event.state_key);
if (storedEvent) {
if (storedEvent.origin_server_ts < eventTs) {
// the incoming event is newer, use it.
__room.current_room_state.storeStateEvent(event);
}
}
}
// =====================================
// Add topic changes as if they were a room message
if (addToRoomMessages) {
if (isLiveEvent) {
@ -145,35 +165,7 @@ function(matrixService, $rootScope, $q, $timeout, mPresence, notificationService
matrixService.createRoomIdToAliasMapping(event.room_id, event.content.aliases[0]);
};
var handleMessage = function(event, isLiveEvent) {
// Check for empty event content
var hasContent = false;
for (var prop in event.content) {
hasContent = true;
break;
}
if (!hasContent) {
// empty json object is a redacted event, so ignore.
return;
}
if (isLiveEvent) {
if (event.user_id === matrixService.config().user_id &&
(event.content.msgtype === "m.text" || event.content.msgtype === "m.emote") ) {
// Assume we've already echoed it. So, there is a fake event in the messages list of the room
// Replace this fake event by the true one
var index = getRoomEventIndex(event.room_id, event.event_id);
if (index) {
$rootScope.events.rooms[event.room_id].messages[index] = event;
}
else {
$rootScope.events.rooms[event.room_id].messages.push(event);
}
}
else {
$rootScope.events.rooms[event.room_id].messages.push(event);
}
var displayNotification = function(event) {
if (window.Notification && event.user_id != matrixService.config().user_id) {
var shouldBing = notificationService.containsBingWord(
matrixService.config().user_id,
@ -235,6 +227,54 @@ function(matrixService, $rootScope, $q, $timeout, mPresence, notificationService
);
}
}
};
var handleMessage = function(event, isLiveEvent) {
// Check for empty event content
var hasContent = false;
for (var prop in event.content) {
hasContent = true;
break;
}
if (!hasContent) {
// empty json object is a redacted event, so ignore.
return;
}
// =======================
var __room = modelService.getRoom(event.room_id);
if (event.user_id !== matrixService.config().user_id) {
__room.addMessageEvent(event, !isLiveEvent);
}
else {
// we may have locally echoed this, so we should replace the event
// instead of just adding.
__room.addOrReplaceMessageEvent(event, !isLiveEvent);
}
// =======================
if (isLiveEvent) {
if (event.user_id === matrixService.config().user_id &&
(event.content.msgtype === "m.text" || event.content.msgtype === "m.emote") ) {
// Assume we've already echoed it. So, there is a fake event in the messages list of the room
// Replace this fake event by the true one
var index = getRoomEventIndex(event.room_id, event.event_id);
if (index) {
$rootScope.events.rooms[event.room_id].messages[index] = event;
}
else {
$rootScope.events.rooms[event.room_id].messages.push(event);
}
}
else {
$rootScope.events.rooms[event.room_id].messages.push(event);
displayNotification(event);
}
}
else {
$rootScope.events.rooms[event.room_id].messages.unshift(event);

View File

@ -36,15 +36,33 @@ angular.module('modelService', [])
this.messages = []; // events which can be displayed on the UI. TODO move?
};
Room.prototype = {
addMessages: function addMessages(events, toFront) {
addMessageEvents: function addMessageEvents(events, toFront) {
for (var i=0; i<events.length; i++) {
this.addMessageEvent(events[i], toFront);
}
},
addMessageEvent: function addMessageEvent(event, toFront) {
if (toFront) {
this.messages.unshift(events[i]);
this.messages.unshift(event);
}
else {
this.messages.push(events[i]);
this.messages.push(event);
}
},
addOrReplaceMessageEvent: function addOrReplaceMessageEvent(event, toFront) {
// Start looking from the tail since the first goal of this function
// is to find a message among the latest ones
for (var i = this.messages.length - 1; i >= 0; i--) {
var storedEvent = this.messages[i];
if (storedEvent.event_id === event.event_id) {
// It's clobbering time!
this.messages[i] = event;
return;
}
}
this.addMessageEvent(event, toFront);
},
leave: function leave() {
@ -81,6 +99,10 @@ angular.module('modelService', [])
for (var i=0; i<events.length; i++) {
this.storeStateEvent(events[i]);
}
},
getStateEvent: function getStateEvent(event_type, state_key) {
return this.state_events[event_type + state_key];
}
};