Search in sources :

Example 11 with ConversationState

use of im.actor.core.entity.ConversationState in project actor-platform by actorapp.

the class RouterActor method updateChatState.

private void updateChatState(Peer peer) {
    boolean isEmpty = conversation(peer).isEmpty();
    ConversationState state = conversationStates.getValue(peer.getUnuqueId());
    if (state.isEmpty() != isEmpty) {
        state = state.changeIsEmpty(isEmpty);
    }
    conversationStates.addOrUpdateItem(state);
}
Also used : ConversationState(im.actor.core.entity.ConversationState)

Example 12 with ConversationState

use of im.actor.core.entity.ConversationState in project actor-platform by actorapp.

the class RouterActor method onMessageReadByMe.

private Promise<Void> onMessageReadByMe(Peer peer, long date, int counter) {
    ConversationState state = conversationStates.getValue(peer.getUnuqueId());
    if (state.getInReadDate() >= date) {
        return Promise.success(null);
    }
    state = state.changeCounter(counter).changeInReadDate(date);
    conversationStates.addOrUpdateItem(state);
    Promise<Void> res = getDialogsRouter().onCounterChanged(peer, counter);
    notifyActiveDialogsVM();
    context().getNotificationsModule().onOwnRead(peer, date);
    return res;
}
Also used : ConversationState(im.actor.core.entity.ConversationState) Void(im.actor.runtime.actors.messages.Void)

Example 13 with ConversationState

use of im.actor.core.entity.ConversationState in project actor-platform by actorapp.

the class RouterActor method onMessageReceived.

private Promise<Void> onMessageReceived(Peer peer, long date) {
    ConversationState state = conversationStates.getValue(peer.getUnuqueId());
    if (date > state.getOutReceiveDate()) {
        state = state.changeOutReceiveDate(date);
        conversationStates.addOrUpdateItem(state);
        return getDialogsRouter().onPeerReceiveChanged(peer, date);
    } else {
        return Promise.success(null);
    }
}
Also used : ConversationState(im.actor.core.entity.ConversationState)

Example 14 with ConversationState

use of im.actor.core.entity.ConversationState in project actor-platform by actorapp.

the class RouterActor method onNewMessages.

//
// Incoming Messages
//
private Promise<Void> onNewMessages(Peer peer, List<Message> messages) {
    assertTrue(messages.size() != 0);
    boolean isConversationVisible = isConversationVisible(peer);
    //
    // Collecting Information
    //
    ConversationState state = conversationStates.getValue(peer.getUnuqueId());
    Message topMessage = null;
    int unreadCount = 0;
    long maxInReadDate = 0;
    long maxInDate = 0;
    for (Message m : messages) {
        if (topMessage == null || topMessage.getSortDate() < m.getSortDate()) {
            topMessage = m;
        }
        if (m.getSenderId() != myUid()) {
            if (m.getSortDate() > state.getInReadDate()) {
                unreadCount++;
                maxInReadDate = Math.max(maxInReadDate, m.getSortDate());
            }
            if (m.getSortDate() > state.getInMaxMessageDate()) {
                maxInDate = Math.max(maxInDate, m.getSortDate());
            }
        }
    }
    //
    // Writing to Conversation
    //
    conversation(peer).addOrUpdateItems(messages);
    //
    // Update Chat State
    //
    updateChatState(peer);
    //
    // Updating Counter
    //
    boolean isRead = false;
    if (unreadCount != 0) {
        if (isConversationVisible) {
            // Auto Reading message
            boolean needUpdateState = false;
            if (maxInReadDate > 0) {
                if (state.getInReadDate() < maxInReadDate) {
                    state = state.changeInReadDate(maxInReadDate);
                }
                state = state.changeCounter(0);
                context().getMessagesModule().getPlainReadActor().send(new CursorReaderActor.MarkRead(peer, maxInReadDate));
                context().getNotificationsModule().onOwnRead(peer, maxInReadDate);
                isRead = true;
                needUpdateState = true;
            }
            if (state.getInMaxMessageDate() < maxInDate) {
                state.changeInMaxDate(maxInDate);
                needUpdateState = true;
            }
            if (needUpdateState) {
                conversationStates.addOrUpdateItem(state);
            }
        } else {
            // Updating counter
            state = state.changeCounter(state.getUnreadCount() + unreadCount);
            if (state.getInMaxMessageDate() < maxInDate) {
                state = state.changeInMaxDate(maxInDate);
            }
            conversationStates.addOrUpdateItem(state);
            notifyActiveDialogsVM();
        }
    }
    //
    if (maxInReadDate > 0 && !isRead) {
        context().getMessagesModule().getPlainReceiverActor().send(new CursorReceiverActor.MarkReceived(peer, maxInReadDate));
    }
    //
    // Updating Dialog List
    //
    Promise<Void> res = getDialogsRouter().onMessage(peer, topMessage, state.getUnreadCount());
    //
    if (!isConversationVisible) {
        for (Message m : messages) {
            if (m.getSenderId() != myUid()) {
                boolean hasCurrentMention = false;
                if (m.getContent() instanceof TextContent) {
                    if (((TextContent) m.getContent()).getMentions().contains(myUid())) {
                        hasCurrentMention = true;
                    }
                }
                int messagesCount = 0;
                int dialogsCount = 0;
                for (Peer activePeer : activeDialogStorage.getAllPeers()) {
                    int activeDialogueUnreadCount = conversationStates.getValue(activePeer.getUnuqueId()).getUnreadCount();
                    if (activeDialogueUnreadCount > 0) {
                        dialogsCount++;
                        messagesCount += activeDialogueUnreadCount;
                    }
                }
                context().getNotificationsModule().onInMessage(peer, m.getSenderId(), m.getSortDate(), ContentDescription.fromContent(m.getContent()), hasCurrentMention, messagesCount, dialogsCount);
            }
        }
    }
    return res;
}
Also used : UpdateMessage(im.actor.core.api.updates.UpdateMessage) Message(im.actor.core.entity.Message) RouterOutgoingMessage(im.actor.core.modules.messaging.router.entity.RouterOutgoingMessage) Peer(im.actor.core.entity.Peer) ConversationState(im.actor.core.entity.ConversationState) CursorReaderActor(im.actor.core.modules.messaging.actions.CursorReaderActor) CursorReceiverActor(im.actor.core.modules.messaging.actions.CursorReceiverActor) Void(im.actor.runtime.actors.messages.Void) TextContent(im.actor.core.entity.content.TextContent)

Aggregations

ConversationState (im.actor.core.entity.ConversationState)14 UpdateMessage (im.actor.core.api.updates.UpdateMessage)4 Message (im.actor.core.entity.Message)4 RouterOutgoingMessage (im.actor.core.modules.messaging.router.entity.RouterOutgoingMessage)4 Void (im.actor.runtime.actors.messages.Void)3 Peer (im.actor.core.entity.Peer)2 CursorReaderActor (im.actor.core.modules.messaging.actions.CursorReaderActor)2 ArrayList (java.util.ArrayList)2 ApiDialogGroup (im.actor.core.api.ApiDialogGroup)1 ApiDialogShort (im.actor.core.api.ApiDialogShort)1 TextContent (im.actor.core.entity.content.TextContent)1 CursorReceiverActor (im.actor.core.modules.messaging.actions.CursorReceiverActor)1 DialogHistory (im.actor.core.modules.messaging.history.entity.DialogHistory)1 ActiveDialogGroup (im.actor.core.modules.messaging.router.entity.ActiveDialogGroup)1