use of im.actor.core.entity.MessageState in project actor-platform by actorapp.
the class ConversationHistoryActor method applyHistory.
private Promise<Void> applyHistory(Peer peer, List<ApiMessageContainer> history) {
ArrayList<Message> messages = new ArrayList<>();
long maxLoadedDate = Long.MAX_VALUE;
long maxReadDate = 0;
long maxReceiveDate = 0;
for (ApiMessageContainer historyMessage : history) {
AbsContent content = AbsContent.fromMessage(historyMessage.getMessage());
MessageState state = EntityConverter.convert(historyMessage.getState());
ArrayList<Reaction> reactions = new ArrayList<>();
for (ApiMessageReaction r : historyMessage.getReactions()) {
reactions.add(new Reaction(r.getCode(), r.getUsers()));
}
messages.add(new Message(historyMessage.getRid(), historyMessage.getDate(), historyMessage.getDate(), historyMessage.getSenderUid(), state, content, reactions, 0));
maxLoadedDate = Math.min(historyMessage.getDate(), maxLoadedDate);
if (historyMessage.getState() == ApiMessageState.RECEIVED) {
maxReceiveDate = Math.max(historyMessage.getDate(), maxReceiveDate);
} else if (historyMessage.getState() == ApiMessageState.READ) {
maxReceiveDate = Math.max(historyMessage.getDate(), maxReceiveDate);
maxReadDate = Math.max(historyMessage.getDate(), maxReadDate);
}
}
boolean isEnded = history.size() < LIMIT;
// Sending updates to conversation actor
final long finalMaxLoadedDate = maxLoadedDate;
return context().getMessagesModule().getRouter().onChatHistoryLoaded(peer, messages, maxReceiveDate, maxReadDate, isEnded).map(r -> {
// Saving Internal State
if (isEnded) {
historyLoaded = true;
} else {
historyLoaded = false;
historyMaxDate = finalMaxLoadedDate;
}
preferences().putLong(KEY_LOADED_DATE, finalMaxLoadedDate);
preferences().putBool(KEY_LOADED, historyLoaded);
preferences().putBool(KEY_LOADED_INIT, true);
return r;
});
}
Aggregations