2014-10-31 10:26:51 -04:00
|
|
|
/*
|
|
|
|
Copyright 2014 OpenMarket Ltd
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/*
|
|
|
|
This service serves as the entry point for all models in the app. If access to
|
|
|
|
underlying data in a room is required, then this service should be used as the
|
|
|
|
dependency.
|
|
|
|
*/
|
|
|
|
// NB: This is more explicit than linking top-level models to $rootScope
|
|
|
|
// in that by adding this service as a dep you are clearly saying "this X
|
|
|
|
// needs access to the underlying data store", rather than polluting the
|
|
|
|
// $rootScope.
|
|
|
|
angular.module('modelService', [])
|
|
|
|
.factory('modelService', ['matrixService', function(matrixService) {
|
|
|
|
|
|
|
|
/***** Room Object *****/
|
|
|
|
var Room = function Room(room_id) {
|
|
|
|
this.room_id = room_id;
|
2014-10-31 10:50:31 -04:00
|
|
|
this.old_room_state = new RoomState();
|
|
|
|
this.current_room_state = new RoomState();
|
2014-10-31 12:22:15 -04:00
|
|
|
this.events = []; // events which can be displayed on the UI. TODO move?
|
2014-10-31 10:26:51 -04:00
|
|
|
};
|
|
|
|
Room.prototype = {
|
2014-10-31 11:16:43 -04:00
|
|
|
addMessageEvents: function addMessageEvents(events, toFront) {
|
2014-10-31 10:50:31 -04:00
|
|
|
for (var i=0; i<events.length; i++) {
|
2014-10-31 11:16:43 -04:00
|
|
|
this.addMessageEvent(events[i], toFront);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
addMessageEvent: function addMessageEvent(event, toFront) {
|
2014-10-31 12:22:15 -04:00
|
|
|
// every message must reference the RoomMember which made it *at
|
|
|
|
// that time* so things like display names display correctly.
|
2014-10-31 11:16:43 -04:00
|
|
|
if (toFront) {
|
2014-10-31 12:22:15 -04:00
|
|
|
event.room_member = this.old_room_state.getStateEvent("m.room.member", event.user_id);
|
|
|
|
this.events.unshift(event);
|
2014-10-31 11:16:43 -04:00
|
|
|
}
|
|
|
|
else {
|
2014-10-31 12:22:15 -04:00
|
|
|
event.room_member = this.current_room_state.getStateEvent("m.room.member", event.user_id);
|
|
|
|
this.events.push(event);
|
2014-10-31 11:16:43 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
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
|
2014-10-31 12:22:15 -04:00
|
|
|
for (var i = this.events.length - 1; i >= 0; i--) {
|
|
|
|
var storedEvent = this.events[i];
|
2014-10-31 11:16:43 -04:00
|
|
|
if (storedEvent.event_id === event.event_id) {
|
|
|
|
// It's clobbering time!
|
2014-10-31 12:22:15 -04:00
|
|
|
this.events[i] = event;
|
2014-10-31 11:16:43 -04:00
|
|
|
return;
|
2014-10-31 10:50:31 -04:00
|
|
|
}
|
|
|
|
}
|
2014-10-31 11:16:43 -04:00
|
|
|
this.addMessageEvent(event, toFront);
|
2014-10-31 10:50:31 -04:00
|
|
|
},
|
|
|
|
|
2014-10-31 10:26:51 -04:00
|
|
|
leave: function leave() {
|
|
|
|
return matrixService.leave(this.room_id);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/***** Room State Object *****/
|
|
|
|
var RoomState = function RoomState() {
|
|
|
|
// list of RoomMember
|
2014-10-31 13:13:27 -04:00
|
|
|
this.members = {};
|
2014-10-31 10:26:51 -04:00
|
|
|
// state events, the key is a compound of event type + state_key
|
|
|
|
this.state_events = {};
|
|
|
|
this.pagination_token = "";
|
|
|
|
};
|
|
|
|
RoomState.prototype = {
|
|
|
|
// get a state event for this room from this.state_events. State events
|
|
|
|
// are unique per type+state_key tuple, with a lot of events using 0-len
|
|
|
|
// state keys. To make it not Really Annoying to access, this method is
|
|
|
|
// provided which can just be given the type and it will return the
|
|
|
|
// 0-len event by default.
|
|
|
|
state: function state(type, state_key) {
|
2014-11-03 05:23:14 -05:00
|
|
|
if (!type) {
|
|
|
|
return undefined; // event type MUST be specified
|
|
|
|
}
|
2014-10-31 10:26:51 -04:00
|
|
|
if (!state_key) {
|
2014-11-03 05:23:14 -05:00
|
|
|
return this.state_events[type]; // treat as 0-len state key
|
2014-10-31 10:26:51 -04:00
|
|
|
}
|
|
|
|
return this.state_events[type + state_key];
|
|
|
|
},
|
|
|
|
|
2014-10-31 10:50:31 -04:00
|
|
|
storeStateEvent: function storeState(event) {
|
2014-10-31 10:26:51 -04:00
|
|
|
this.state_events[event.type + event.state_key] = event;
|
2014-11-03 06:44:39 -05:00
|
|
|
if (event.type === "m.room.member") {
|
|
|
|
this.members[event.state_key] = event;
|
|
|
|
}
|
2014-10-31 10:50:31 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
storeStateEvents: function storeState(events) {
|
2014-11-03 06:44:39 -05:00
|
|
|
if (!events) {
|
|
|
|
return;
|
|
|
|
}
|
2014-10-31 10:50:31 -04:00
|
|
|
for (var i=0; i<events.length; i++) {
|
|
|
|
this.storeStateEvent(events[i]);
|
|
|
|
}
|
2014-10-31 11:16:43 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
getStateEvent: function getStateEvent(event_type, state_key) {
|
|
|
|
return this.state_events[event_type + state_key];
|
2014-10-31 10:26:51 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/***** Room Member Object *****/
|
|
|
|
var RoomMember = function RoomMember() {
|
|
|
|
this.event = {}; // the m.room.member event representing the RoomMember.
|
|
|
|
this.user = undefined; // the User
|
|
|
|
};
|
|
|
|
|
|
|
|
/***** User Object *****/
|
|
|
|
var User = function User() {
|
|
|
|
this.event = {}; // the m.presence event representing the User.
|
|
|
|
};
|
|
|
|
|
|
|
|
// rooms are stored here when they come in.
|
|
|
|
var rooms = {
|
|
|
|
// roomid: <Room>
|
|
|
|
};
|
|
|
|
|
|
|
|
console.log("Models inited.");
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
|
|
getRoom: function(roomId) {
|
|
|
|
if(!rooms[roomId]) {
|
|
|
|
rooms[roomId] = new Room(roomId);
|
|
|
|
}
|
|
|
|
return rooms[roomId];
|
2014-11-03 06:44:39 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
getRooms: function() {
|
|
|
|
return rooms;
|
2014-10-31 10:26:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
}]);
|