Search in sources :

Example 1 with ApiMessageContainer

use of im.actor.core.api.ApiMessageContainer 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 -> {
        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;
    });
}
Also used : ApiMessageState(im.actor.core.api.ApiMessageState) MessageState(im.actor.core.entity.MessageState) AskMessage(im.actor.runtime.actors.ask.AskMessage) Message(im.actor.core.entity.Message) ApiMessageContainer(im.actor.core.api.ApiMessageContainer) ApiMessageReaction(im.actor.core.api.ApiMessageReaction) ArrayList(java.util.ArrayList) AbsContent(im.actor.core.entity.content.AbsContent) ApiMessageReaction(im.actor.core.api.ApiMessageReaction) Reaction(im.actor.core.entity.Reaction)

Aggregations

ApiMessageContainer (im.actor.core.api.ApiMessageContainer)1 ApiMessageReaction (im.actor.core.api.ApiMessageReaction)1 ApiMessageState (im.actor.core.api.ApiMessageState)1 Message (im.actor.core.entity.Message)1 MessageState (im.actor.core.entity.MessageState)1 Reaction (im.actor.core.entity.Reaction)1 AbsContent (im.actor.core.entity.content.AbsContent)1 AskMessage (im.actor.runtime.actors.ask.AskMessage)1 ArrayList (java.util.ArrayList)1