-
Notifications
You must be signed in to change notification settings - Fork 18
/
gossip-chat-list-provider.js
274 lines (259 loc) · 8.95 KB
/
gossip-chat-list-provider.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/**
* The MIT License (MIT)
* Copyright (c) 2016 Krypto Fin ry and the FIMK Developers
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
* */
(function () {
'use strict';
var module = angular.module('fim.base');
module.factory('GossipChatListProvider', function (nxt, $q, Gossip, db, $rootScope) {
function emptyToNull(val) {
if (typeof val == 'string') {
return val.length == 0 ? null : val;
}
if (typeof val == 'number') {
return val > 0 ? val : null;
}
return null;
}
/* Asynchronous (lazy) Iterator of remote Blockchain chat messages data */
function ChatListProvider(parent) {
this.parent = parent;
this.entities = [];
this.hasMore = true;
this.index = 0;
this.pageSize = 3;
}
ChatListProvider.prototype = {
/* @returns Promise(transaction) */
peek: function (advance) {
var deferred = $q.defer();
if (this.index < this.entities.length) {
var result = this.entities[this.index];
if (advance) {
this.index++;
}
deferred.resolve(result);
}
else {
var self = this;
this.getMore().then(
function () {
if (self.index < self.entities.length) {
var result = self.entities[self.index];
if (advance) {
self.index++;
}
deferred.resolve(result);
}
else {
deferred.resolve(null);
}
},
function () {
deferred.resolve(null);
}
);
}
return deferred.promise;
},
/* @returns Promise(transaction) */
next: function () {
return this.peek(true);
},
/* @returns Promise */
getMore: function () {
var deferred = $q.defer();
var args = {
account: this.parent.account,
firstIndex: this.index,
lastIndex: this.index + this.pageSize,
requestType: 'getChatList'
};
var self = this;
this.parent.api.engine.socket().callAPIFunction(args).then(
function (data) {
var chats = data.chats||[];
self.hasMore = chats.length != 0;
for (var i=0; i<chats.length; i++) {
self.entities.push(chats[i]);
}
deferred.resolve();
},
deferred.reject
);
return deferred.promise;
}
};
function GossipChatListProvider(api, $scope, account) {
this.api = api;
this.$scope = $scope;
this.account = account;
this.entities = []; // { accountRS: String }
this.provider = new ChatListProvider(this);
db.chats.addObserver($scope, this.createObserver());
}
GossipChatListProvider.prototype = {
createObserver: function () {
var self = this;
/* note that when having multiple tabs open you could potentially get
duplicate reports. */
var must_update = false;
return {
create: function (models) {
models.forEach(function (chat) {
if (chat.accountRS == self.account) {
self.translate(chat);
self.entities.push(chat);
must_update = true;
}
});
},
remove: function (models) {
models.forEach(function (chat) {
for (var i=0; i<self.entities.length; i++) {
if (self.entities[i].id === chat.id) {
must_update = true;
self.entities.splice(i, 1);
return;
}
}
});
},
update: function (models) {
models.forEach(function (chat) {
for (var i=0; i<self.entities.length; i++) {
if (self.entities[i].id === chat.id) {
angular.extend(self.entities[i], chat);
self.entities[i].label = emptyToNull(chat.name) || chat.otherRS;
self.translate(self.entities[i]);
must_update = true;
break;
}
}
});
},
finally: function () {
if (must_update) {
must_update = false;
self.$scope.$evalAsync(function () {
/* nothing to do here */
var breakpoint_helper = 1;
});
}
}
}
},
reload: function () {
var deferred = $q.defer();
Gossip.getChatService().list().then(
function (entities) {
this.$scope.$evalAsync(function () {
this.entities.length = 0;
entities.forEach(function (chat) {
this.translate(chat);
this.entities.push(chat);
}.bind(this));
this.sort();
deferred.resolve();
/**
* Lazily inspect the blockchain chats to see if there has been any activity
* there. For existing chats update the timestamp, for non-existing chats create
* a db entry. The db observer should be taking care of propagating it to the UI.
*/
var repeat = function () {
this.advance().then(repeat);
}.bind(this);
repeat();
}.bind(this));
}.bind(this)
);
return deferred.promise;
},
sort: function () {
this.entities.sort(this.sortFunction);
},
sortFunction: function (a,b) {
return b.timestamp - a.timestamp;
},
translate: function (chat) {
chat.label = emptyToNull(chat.name) || chat.otherRS;
chat.date = emptyToNull(chat.timestamp) ? nxt.util.formatTimestamp(chat.timestamp, false, true) : 'never';
if (!chat.provider) {
chat.provider = Gossip.getChatStatusProvider(this.$scope, chat.otherRS);
}
},
advance: function () {
var deferred = $q.defer();
var self = this;
this.provider.peek().then(function (remote_chat) {
if (!remote_chat) return;
db.transaction("rw", db.chats, function() {
Gossip.getChatService().get(remote_chat.accountRS).then(
function (local_chat) {
/* we have a remote and a local chat */
if (local_chat) {
/* first determine if we already know of this blockchain message */
if (local_chat.timestamp2 == remote_chat.timestamp) {
/* aparantly we already know of this remote chat
it is safe to stop looking further
we dont need to process any other remote chats since they returned in order */
deferred.reject();
}
else {
/* update the remote timestamp */
local_chat.timestamp2 = remote_chat.timestamp;
/* update the local timestamp - but only if it's newer */
if (local_chat.timestamp < remote_chat.timestamp) {
local_chat.timestamp = remote_chat.timestamp;
}
/* save the changes - if there are any */
local_chat.save().then(
function () {
self.provider.next().then(deferred.resolve);
}
);
}
}
/* there is no local chat object */
else if (remote_chat.accountRS != $rootScope.currentAccount.id_rs) {
/* create local copy and move on */
Gossip.getChatService().add(remote_chat.accountRS).then(
function (local_chat) {
local_chat.timestamp2 = remote_chat.timestamp;
local_chat.timestamp = remote_chat.timestamp;
local_chat.save().then(
function () {
self.provider.next().then(deferred.resolve);
}
);
}
);
}
}
);
});
});
return deferred.promise;
}
};
return GossipChatListProvider;
});
})();