1package eu.siacs.conversations.services;
2
3import android.util.Log;
4import android.util.Pair;
5
6import java.math.BigInteger;
7import java.util.ArrayList;
8import java.util.HashSet;
9import java.util.Iterator;
10import java.util.List;
11
12import eu.siacs.conversations.Config;
13import eu.siacs.conversations.R;
14import eu.siacs.conversations.entities.Account;
15import eu.siacs.conversations.entities.Conversation;
16import eu.siacs.conversations.generator.AbstractGenerator;
17import eu.siacs.conversations.xml.Element;
18import eu.siacs.conversations.xmpp.OnAdvancedStreamFeaturesLoaded;
19import eu.siacs.conversations.xmpp.OnIqPacketReceived;
20import eu.siacs.conversations.xmpp.jid.Jid;
21import eu.siacs.conversations.xmpp.stanzas.IqPacket;
22
23public class MessageArchiveService implements OnAdvancedStreamFeaturesLoaded {
24
25 private final XmppConnectionService mXmppConnectionService;
26
27 private final HashSet<Query> queries = new HashSet<>();
28 private final ArrayList<Query> pendingQueries = new ArrayList<>();
29
30 public enum PagingOrder {
31 NORMAL,
32 REVERSE
33 }
34
35 public MessageArchiveService(final XmppConnectionService service) {
36 this.mXmppConnectionService = service;
37 }
38
39 private void catchup(final Account account) {
40 synchronized (this.queries) {
41 for(Iterator<Query> iterator = this.queries.iterator(); iterator.hasNext();) {
42 Query query = iterator.next();
43 if (query.getAccount() == account) {
44 iterator.remove();
45 }
46 }
47 }
48 final Pair<Long,String> lastMessageReceived = mXmppConnectionService.databaseBackend.getLastMessageReceived(account);
49 final Pair<Long,String> lastClearDate = mXmppConnectionService.databaseBackend.getLastClearDate(account);
50 long startCatchup;
51 final String reference;
52 if (lastMessageReceived != null && lastMessageReceived.first >= lastClearDate.first) {
53 startCatchup = lastMessageReceived.first;
54 reference = lastMessageReceived.second;
55 } else {
56 startCatchup = lastClearDate.first;
57 reference = null;
58 }
59 long endCatchup = account.getXmppConnection().getLastSessionEstablished();
60 final Query query;
61 if (startCatchup == 0) {
62 return;
63 } else if (endCatchup - startCatchup >= Config.MAM_MAX_CATCHUP) {
64 startCatchup = endCatchup - Config.MAM_MAX_CATCHUP;
65 List<Conversation> conversations = mXmppConnectionService.getConversations();
66 for (Conversation conversation : conversations) {
67 if (conversation.getMode() == Conversation.MODE_SINGLE && conversation.getAccount() == account && startCatchup > conversation.getLastMessageTransmitted()) {
68 this.query(conversation,startCatchup);
69 }
70 }
71 query = new Query(account, startCatchup, endCatchup);
72 } else {
73 query = new Query(account, startCatchup, endCatchup);
74 query.reference = reference;
75 }
76 this.queries.add(query);
77 this.execute(query);
78 }
79
80 public void catchupMUC(final Conversation conversation) {
81 if (conversation.getLastMessageTransmitted() < 0 && conversation.countMessages() == 0) {
82 query(conversation,
83 0,
84 System.currentTimeMillis());
85 } else {
86 query(conversation,
87 conversation.getLastMessageTransmitted(),
88 System.currentTimeMillis());
89 }
90 }
91
92 public Query query(final Conversation conversation) {
93 if (conversation.getLastMessageTransmitted() < 0 && conversation.countMessages() == 0) {
94 return query(conversation,
95 0,
96 System.currentTimeMillis());
97 } else {
98 return query(conversation,
99 conversation.getLastMessageTransmitted(),
100 conversation.getAccount().getXmppConnection().getLastSessionEstablished());
101 }
102 }
103
104 public Query query(final Conversation conversation, long end) {
105 return this.query(conversation,conversation.getLastMessageTransmitted(),end);
106 }
107
108 public Query query(Conversation conversation, long start, long end) {
109 synchronized (this.queries) {
110 if (start > end) {
111 return null;
112 }
113 final Query query = new Query(conversation, start, end,PagingOrder.REVERSE);
114 query.reference = conversation.getFirstMamReference();
115 this.queries.add(query);
116 this.execute(query);
117 return query;
118 }
119 }
120
121 public void executePendingQueries(final Account account) {
122 List<Query> pending = new ArrayList<>();
123 synchronized(this.pendingQueries) {
124 for(Iterator<Query> iterator = this.pendingQueries.iterator(); iterator.hasNext();) {
125 Query query = iterator.next();
126 if (query.getAccount() == account) {
127 pending.add(query);
128 iterator.remove();
129 }
130 }
131 }
132 for(Query query : pending) {
133 this.execute(query);
134 }
135 }
136
137 private void execute(final Query query) {
138 final Account account= query.getAccount();
139 if (account.getStatus() == Account.State.ONLINE) {
140 Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": running mam query " + query.toString());
141 IqPacket packet = this.mXmppConnectionService.getIqGenerator().queryMessageArchiveManagement(query);
142 this.mXmppConnectionService.sendIqPacket(account, packet, new OnIqPacketReceived() {
143 @Override
144 public void onIqPacketReceived(Account account, IqPacket packet) {
145 if (packet.getType() == IqPacket.TYPE.TIMEOUT) {
146 synchronized (MessageArchiveService.this.queries) {
147 MessageArchiveService.this.queries.remove(query);
148 if (query.hasCallback()) {
149 query.callback(false);
150 }
151 }
152 } else if (packet.getType() != IqPacket.TYPE.RESULT) {
153 Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": error executing mam: " + packet.toString());
154 finalizeQuery(query, true);
155 }
156 }
157 });
158 } else {
159 synchronized (this.pendingQueries) {
160 this.pendingQueries.add(query);
161 }
162 }
163 }
164
165 private void finalizeQuery(Query query, boolean done) {
166 synchronized (this.queries) {
167 this.queries.remove(query);
168 }
169 final Conversation conversation = query.getConversation();
170 if (conversation != null) {
171 conversation.sort();
172 conversation.setHasMessagesLeftOnServer(!done);
173 } else {
174 for(Conversation tmp : this.mXmppConnectionService.getConversations()) {
175 if (tmp.getAccount() == query.getAccount()) {
176 tmp.sort();
177 }
178 }
179 }
180 if (query.hasCallback()) {
181 query.callback(done);
182 } else {
183 this.mXmppConnectionService.updateConversationUi();
184 }
185 }
186
187 public boolean queryInProgress(Conversation conversation, XmppConnectionService.OnMoreMessagesLoaded callback) {
188 synchronized (this.queries) {
189 for(Query query : queries) {
190 if (query.conversation == conversation) {
191 if (!query.hasCallback() && callback != null) {
192 query.setCallback(callback);
193 }
194 return true;
195 }
196 }
197 return false;
198 }
199 }
200
201 public boolean queryInProgress(Conversation conversation) {
202 return queryInProgress(conversation, null);
203 }
204
205 public void processFin(Element fin, Jid from) {
206 if (fin == null) {
207 return;
208 }
209 Query query = findQuery(fin.getAttribute("queryid"));
210 if (query == null || !query.validFrom(from)) {
211 return;
212 }
213 boolean complete = fin.getAttributeAsBoolean("complete");
214 Element set = fin.findChild("set","http://jabber.org/protocol/rsm");
215 Element last = set == null ? null : set.findChild("last");
216 Element first = set == null ? null : set.findChild("first");
217 Element relevant = query.getPagingOrder() == PagingOrder.NORMAL ? last : first;
218 boolean abort = (query.getStart() == 0 && query.getTotalCount() >= Config.PAGE_SIZE) || query.getTotalCount() >= Config.MAM_MAX_MESSAGES;
219 if (query.getConversation() != null) {
220 query.getConversation().setFirstMamReference(first == null ? null : first.getContent());
221 }
222 if (complete || relevant == null || abort) {
223 final boolean done = (complete || query.getMessageCount() == 0) && query.getStart() == 0;
224 this.finalizeQuery(query, done);
225 Log.d(Config.LOGTAG,query.getAccount().getJid().toBareJid()+": finished mam after "+query.getTotalCount()+" messages. messages left="+Boolean.toString(!done));
226 if (query.getWith() == null && query.getMessageCount() > 0) {
227 mXmppConnectionService.getNotificationService().finishBacklog(true);
228 }
229 } else {
230 final Query nextQuery;
231 if (query.getPagingOrder() == PagingOrder.NORMAL) {
232 nextQuery = query.next(last == null ? null : last.getContent());
233 } else {
234 nextQuery = query.prev(first == null ? null : first.getContent());
235 }
236 this.execute(nextQuery);
237 this.finalizeQuery(query, false);
238 synchronized (this.queries) {
239 this.queries.add(nextQuery);
240 }
241 }
242 }
243
244 public Query findQuery(String id) {
245 if (id == null) {
246 return null;
247 }
248 synchronized (this.queries) {
249 for(Query query : this.queries) {
250 if (query.getQueryId().equals(id)) {
251 return query;
252 }
253 }
254 return null;
255 }
256 }
257
258 @Override
259 public void onAdvancedStreamFeaturesAvailable(Account account) {
260 if (account.getXmppConnection() != null && account.getXmppConnection().getFeatures().mam()) {
261 this.catchup(account);
262 }
263 }
264
265 public class Query {
266 private int totalCount = 0;
267 private int messageCount = 0;
268 private long start;
269 private long end;
270 private String queryId;
271 private String reference = null;
272 private Account account;
273 private Conversation conversation;
274 private PagingOrder pagingOrder = PagingOrder.NORMAL;
275 private XmppConnectionService.OnMoreMessagesLoaded callback = null;
276
277
278 public Query(Conversation conversation, long start, long end) {
279 this(conversation.getAccount(), start, end);
280 this.conversation = conversation;
281 }
282
283 public Query(Conversation conversation, long start, long end, PagingOrder order) {
284 this(conversation,start,end);
285 this.pagingOrder = order;
286 }
287
288 public Query(Account account, long start, long end) {
289 this.account = account;
290 this.start = start;
291 this.end = end;
292 this.queryId = new BigInteger(50, mXmppConnectionService.getRNG()).toString(32);
293 }
294
295 private Query page(String reference) {
296 Query query = new Query(this.account,this.start,this.end);
297 query.reference = reference;
298 query.conversation = conversation;
299 query.totalCount = totalCount;
300 query.callback = callback;
301 return query;
302 }
303
304 public Query next(String reference) {
305 Query query = page(reference);
306 query.pagingOrder = PagingOrder.NORMAL;
307 return query;
308 }
309
310 public Query prev(String reference) {
311 Query query = page(reference);
312 query.pagingOrder = PagingOrder.REVERSE;
313 return query;
314 }
315
316 public String getReference() {
317 return reference;
318 }
319
320 public PagingOrder getPagingOrder() {
321 return this.pagingOrder;
322 }
323
324 public String getQueryId() {
325 return queryId;
326 }
327
328 public Jid getWith() {
329 return conversation == null ? null : conversation.getJid().toBareJid();
330 }
331
332 public boolean muc() {
333 return conversation != null && conversation.getMode() == Conversation.MODE_MULTI;
334 }
335
336 public long getStart() {
337 return start;
338 }
339
340 public void setCallback(XmppConnectionService.OnMoreMessagesLoaded callback) {
341 this.callback = callback;
342 }
343
344 public void callback(boolean done) {
345 if (this.callback != null) {
346 this.callback.onMoreMessagesLoaded(messageCount,conversation);
347 if (done) {
348 this.callback.informUser(R.string.no_more_history_on_server);
349 }
350 }
351 }
352
353 public long getEnd() {
354 return end;
355 }
356
357 public Conversation getConversation() {
358 return conversation;
359 }
360
361 public Account getAccount() {
362 return this.account;
363 }
364
365 public void incrementMessageCount() {
366 this.messageCount++;
367 this.totalCount++;
368 }
369
370 public int getTotalCount() {
371 return this.totalCount;
372 }
373
374 public int getMessageCount() {
375 return this.messageCount;
376 }
377
378 public boolean validFrom(Jid from) {
379 if (muc()) {
380 return getWith().equals(from);
381 } else {
382 return (from == null) || account.getJid().toBareJid().equals(from.toBareJid());
383 }
384 }
385
386 @Override
387 public String toString() {
388 StringBuilder builder = new StringBuilder();
389 if (this.muc()) {
390 builder.append("to=");
391 builder.append(this.getWith().toString());
392 } else {
393 builder.append("with=");
394 if (this.getWith() == null) {
395 builder.append("*");
396 } else {
397 builder.append(getWith().toString());
398 }
399 }
400 builder.append(", start=");
401 builder.append(AbstractGenerator.getTimestamp(this.start));
402 builder.append(", end=");
403 builder.append(AbstractGenerator.getTimestamp(this.end));
404 if (this.reference!=null) {
405 if (this.pagingOrder == PagingOrder.NORMAL) {
406 builder.append(", after=");
407 } else {
408 builder.append(", before=");
409 }
410 builder.append(this.reference);
411 }
412 return builder.toString();
413 }
414
415 public boolean hasCallback() {
416 return this.callback != null;
417 }
418 }
419}