Search in sources :

Example 1 with Reaction

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

the class RouterActor method onUpdate.

// 
// Messages
// 
public Promise<Void> onUpdate(Update update) {
    if (update instanceof UpdateMessage) {
        UpdateMessage msg = (UpdateMessage) update;
        Peer peer = convert(msg.getPeer());
        AbsContent msgContent = AbsContent.fromMessage(msg.getMessage());
        Message message = new Message(msg.getRid(), msg.getDate(), msg.getDate(), msg.getSenderUid(), myUid() == msg.getSenderUid() ? MessageState.SENT : MessageState.UNKNOWN, msgContent);
        ArrayList<Message> messages = new ArrayList<>();
        messages.add(message);
        return onNewMessages(peer, messages);
    } else if (update instanceof UpdateMessageSent) {
        UpdateMessageSent messageSent = (UpdateMessageSent) update;
        Peer peer = convert(messageSent.getPeer());
        if (isValidPeer(peer)) {
            // Notify Sender
            context().getMessagesModule().getSendMessageActor().send(new SenderActor.MessageSent(peer, messageSent.getRid()));
            return onOutgoingSent(peer, messageSent.getRid(), messageSent.getDate());
        }
        return Promise.success(null);
    } else if (update instanceof UpdateMessageRead) {
        UpdateMessageRead read = (UpdateMessageRead) update;
        Peer peer = convert(read.getPeer());
        if (isValidPeer(peer)) {
            return onMessageRead(peer, read.getStartDate());
        }
        return Promise.success(null);
    } else if (update instanceof UpdateMessageReadByMe) {
        UpdateMessageReadByMe readByMe = (UpdateMessageReadByMe) update;
        Peer peer = convert(readByMe.getPeer());
        if (isValidPeer(peer)) {
            int counter = 0;
            if (readByMe.getUnreadCounter() != null) {
                counter = readByMe.getUnreadCounter();
            }
            return onMessageReadByMe(peer, readByMe.getStartDate(), counter);
        }
        return Promise.success(null);
    } else if (update instanceof UpdateMessageReceived) {
        UpdateMessageReceived received = (UpdateMessageReceived) update;
        Peer peer = convert(received.getPeer());
        if (isValidPeer(peer)) {
            return onMessageReceived(peer, received.getStartDate());
        }
        return Promise.success(null);
    } else if (update instanceof UpdateChatDelete) {
        UpdateChatDelete delete = (UpdateChatDelete) update;
        Peer peer = convert(delete.getPeer());
        if (isValidPeer(peer)) {
            return onChatDelete(peer);
        }
        return Promise.success(null);
    } else if (update instanceof UpdateChatClear) {
        UpdateChatClear clear = (UpdateChatClear) update;
        Peer peer = convert(clear.getPeer());
        if (isValidPeer(peer)) {
            return onChatClear(peer);
        }
        return Promise.success(null);
    } else if (update instanceof UpdateChatDropCache) {
        UpdateChatDropCache dropCache = (UpdateChatDropCache) update;
        Peer peer = convert(dropCache.getPeer());
        if (isValidPeer(peer)) {
            return onChatDropCache(peer);
        }
        return Promise.success(null);
    } else if (update instanceof UpdateChatGroupsChanged) {
        UpdateChatGroupsChanged chatGroupsChanged = (UpdateChatGroupsChanged) update;
        onActiveDialogsChanged(chatGroupsChanged.getDialogs(), true, true);
        return Promise.success(null);
    } else if (update instanceof UpdateMessageDelete) {
        UpdateMessageDelete delete = (UpdateMessageDelete) update;
        Peer peer = convert(delete.getPeer());
        if (isValidPeer(peer)) {
            return onMessageDeleted(peer, delete.getRids());
        }
        return Promise.success(null);
    } else if (update instanceof UpdateMessageContentChanged) {
        UpdateMessageContentChanged contentChanged = (UpdateMessageContentChanged) update;
        Peer peer = convert(contentChanged.getPeer());
        if (isValidPeer(peer)) {
            AbsContent content = AbsContent.fromMessage(contentChanged.getMessage());
            return onContentUpdate(peer, contentChanged.getRid(), content);
        }
        return Promise.success(null);
    } else if (update instanceof UpdateReactionsUpdate) {
        UpdateReactionsUpdate reactionsUpdate = (UpdateReactionsUpdate) update;
        Peer peer = convert(reactionsUpdate.getPeer());
        if (isValidPeer(peer)) {
            ArrayList<Reaction> reactions = new ArrayList<>();
            for (ApiMessageReaction r : reactionsUpdate.getReactions()) {
                reactions.add(new Reaction(r.getCode(), r.getUsers()));
            }
            return onReactionsUpdate(peer, reactionsUpdate.getRid(), reactions);
        }
        return Promise.success(null);
    }
    return Promise.success(null);
}
Also used : UpdateMessage(im.actor.core.api.updates.UpdateMessage) UpdateMessageSent(im.actor.core.api.updates.UpdateMessageSent) UpdateMessageContentChanged(im.actor.core.api.updates.UpdateMessageContentChanged) UpdateChatClear(im.actor.core.api.updates.UpdateChatClear) 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) ApiMessageReaction(im.actor.core.api.ApiMessageReaction) AbsContent(im.actor.core.entity.content.AbsContent) ArrayList(java.util.ArrayList) UpdateChatDelete(im.actor.core.api.updates.UpdateChatDelete) UpdateChatDropCache(im.actor.core.api.updates.UpdateChatDropCache) UpdateMessageReadByMe(im.actor.core.api.updates.UpdateMessageReadByMe) UpdateMessageReceived(im.actor.core.api.updates.UpdateMessageReceived) ApiMessageReaction(im.actor.core.api.ApiMessageReaction) Reaction(im.actor.core.entity.Reaction) UpdateChatGroupsChanged(im.actor.core.api.updates.UpdateChatGroupsChanged) UpdateMessageDelete(im.actor.core.api.updates.UpdateMessageDelete) UpdateReactionsUpdate(im.actor.core.api.updates.UpdateReactionsUpdate) UpdateMessageSent(im.actor.core.api.updates.UpdateMessageSent) UpdateMessageRead(im.actor.core.api.updates.UpdateMessageRead)

Example 2 with Reaction

use of im.actor.core.entity.Reaction 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;
    });
}
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)

Example 3 with Reaction

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

the class ChatListProcessor method process.

@Nullable
@Override
public Object process(@NotNull List<Message> items, @Nullable Object previous) {
    // Init tools
    if (mobileInvitePattern == null) {
        mobileInvitePattern = Pattern.compile("(actor:\\\\/\\\\/)(invite\\\\?token=)([0-9-a-z]{1,64})");
    }
    if (invitePattern == null) {
        invitePattern = Pattern.compile("(https:\\/\\/)(quit\\.email\\/join\\/)([0-9-a-z]{1,64})");
    }
    if (peoplePattern == null) {
        peoplePattern = Pattern.compile("(people:\\\\/\\\\/)([0-9]{1,20})");
    }
    if (mentionPattern == null) {
        mentionPattern = Pattern.compile("(@)([0-9a-zA-Z_]{5,32})");
    }
    ArrayList<PreprocessedData> preprocessedDatas = new ArrayList<PreprocessedData>();
    for (Message msg : items) {
        // Preprocess message
        // Assume user is cached
        messenger().getUser(msg.getSenderId());
        // Process reactions
        boolean isImage = msg.getContent() instanceof PhotoContent || msg.getContent() instanceof VideoContent || msg.getContent() instanceof LocationContent;
        boolean hasReactions = msg.getReactions() != null && msg.getReactions().size() > 0;
        Spannable reactions = null;
        if (hasReactions) {
            SpannableStringBuilder builder = new SpannableStringBuilder();
            SpannableString s;
            boolean hasMyReaction = false;
            for (Reaction r : msg.getReactions()) {
                s = new SpannableString(Integer.toString(r.getUids().size()).concat(r.getCode()).concat("  "));
                for (Integer uid : r.getUids()) {
                    if (uid == myUid()) {
                        hasMyReaction = true;
                        break;
                    }
                }
                s.setSpan(new ReactionSpan(r.getCode(), hasMyReaction, peer, msg.getRid(), isImage ? Color.WHITE : ActorSDK.sharedActor().style.getConvTimeColor()), 0, s.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                reactions = builder.append(s);
            }
        }
        // Process Content
        if (msg.getContent() instanceof TextContent) {
            int updatedCounter = msg.getContent().getUpdatedCounter();
            if (!preprocessedTexts.containsKey(msg.getRid()) || (!updatedTexts.containsKey(msg.getRid()) || updatedTexts.get(msg.getRid()) != updatedCounter)) {
                TextContent text = (TextContent) msg.getContent();
                Spannable spannableString = new SpannableString(text.getText());
                boolean hasSpannable = false;
                // Wait Emoji to load
                emoji().waitForEmoji();
                // Process markdown
                Spannable markdown = AndroidMarkdown.processText(text.getText());
                if (markdown != null) {
                    spannableString = markdown;
                    hasSpannable = true;
                }
                // Process links
                if (Linkify.addLinks(spannableString, Linkify.EMAIL_ADDRESSES | Linkify.PHONE_NUMBERS | Linkify.WEB_URLS)) {
                    hasSpannable = true;
                }
                if (fixLinkifyCustomLinks(spannableString, mobileInvitePattern, false)) {
                    hasSpannable = true;
                }
                if (fixLinkifyCustomLinks(spannableString, invitePattern, false)) {
                    hasSpannable = true;
                }
                if (fixLinkifyCustomLinks(spannableString, peoplePattern, true)) {
                    hasSpannable = true;
                }
                if (fixLinkifyCustomLinks(spannableString, mentionPattern, true)) {
                    hasSpannable = true;
                }
                // Append Sender name for groups
                if (isGroup && msg.getSenderId() != myUid()) {
                    String name;
                    UserVM userModel = users().get(msg.getSenderId());
                    if (userModel != null) {
                        String userName = userModel.getName().get();
                        if (userName.equals("Bot")) {
                            name = group.getName().get();
                        } else {
                            name = userName;
                        }
                    } else {
                        name = "???";
                    }
                    SpannableStringBuilder builder = new SpannableStringBuilder();
                    builder.append(name);
                    builder.setSpan(new ForegroundColorSpan(colors[Math.abs(msg.getSenderId()) % colors.length]), 0, name.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                    builder.append("\n");
                    spannableString = builder.append(spannableString);
                    hasSpannable = true;
                }
                // Process Emoji
                if (SmileProcessor.containsEmoji(spannableString)) {
                    spannableString = emoji().processEmojiCompatMutable(spannableString, SmileProcessor.CONFIGURATION_BUBBLES);
                    hasSpannable = true;
                }
                updatedTexts.put(msg.getRid(), updatedCounter);
                preprocessedTexts.put(msg.getRid(), new PreprocessedTextData(reactions, text.getText(), hasSpannable ? spannableString : null));
            } else {
                PreprocessedTextData text = preprocessedTexts.get(msg.getRid());
                preprocessedTexts.put(msg.getRid(), new PreprocessedTextData(reactions, text.getText(), text.getSpannableString()));
            }
            preprocessedDatas.add(preprocessedTexts.get(msg.getRid()));
        } else if (msg.getContent() instanceof ContactContent) {
            ContactContent contact = (ContactContent) msg.getContent();
            String text = "";
            for (String phone : contact.getPhones()) {
                text += "\n".concat(phone);
            }
            for (String email : contact.getEmails()) {
                text += "\n".concat(email);
            }
            Spannable spannableString = new SpannableString(text);
            SpannableStringBuilder builder = new SpannableStringBuilder();
            String name;
            name = contact.getName();
            builder.append(name);
            builder.setSpan(new ForegroundColorSpan(colors[Math.abs(msg.getSenderId()) % colors.length]), 0, name.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            // builder.append("\n");
            spannableString = builder.append(spannableString);
            preprocessedTexts.put(msg.getRid(), new PreprocessedTextData(reactions, text, spannableString));
            preprocessedDatas.add(preprocessedTexts.get(msg.getRid()));
        } else {
            // Nothing to do yet
            preprocessedDatas.add(new PreprocessedData(reactions));
        }
    }
    return new PreprocessedList(preprocessedDatas.toArray(new PreprocessedData[preprocessedDatas.size()]));
}
Also used : LocationContent(im.actor.core.entity.content.LocationContent) VideoContent(im.actor.core.entity.content.VideoContent) Message(im.actor.core.entity.Message) ForegroundColorSpan(android.text.style.ForegroundColorSpan) ArrayList(java.util.ArrayList) SpannableString(android.text.SpannableString) Reaction(im.actor.core.entity.Reaction) ContactContent(im.actor.core.entity.content.ContactContent) SpannableString(android.text.SpannableString) ReactionSpan(im.actor.sdk.controllers.conversation.view.ReactionSpan) UserVM(im.actor.core.viewmodel.UserVM) TextContent(im.actor.core.entity.content.TextContent) Spannable(android.text.Spannable) SpannableStringBuilder(android.text.SpannableStringBuilder) PhotoContent(im.actor.core.entity.content.PhotoContent) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

Message (im.actor.core.entity.Message)3 Reaction (im.actor.core.entity.Reaction)3 ArrayList (java.util.ArrayList)3 ApiMessageReaction (im.actor.core.api.ApiMessageReaction)2 AbsContent (im.actor.core.entity.content.AbsContent)2 Spannable (android.text.Spannable)1 SpannableString (android.text.SpannableString)1 SpannableStringBuilder (android.text.SpannableStringBuilder)1 ForegroundColorSpan (android.text.style.ForegroundColorSpan)1 ApiMessageContainer (im.actor.core.api.ApiMessageContainer)1 ApiMessageState (im.actor.core.api.ApiMessageState)1 UpdateChatClear (im.actor.core.api.updates.UpdateChatClear)1 UpdateChatDelete (im.actor.core.api.updates.UpdateChatDelete)1 UpdateChatDropCache (im.actor.core.api.updates.UpdateChatDropCache)1 UpdateChatGroupsChanged (im.actor.core.api.updates.UpdateChatGroupsChanged)1 UpdateMessage (im.actor.core.api.updates.UpdateMessage)1 UpdateMessageContentChanged (im.actor.core.api.updates.UpdateMessageContentChanged)1 UpdateMessageDelete (im.actor.core.api.updates.UpdateMessageDelete)1 UpdateMessageRead (im.actor.core.api.updates.UpdateMessageRead)1 UpdateMessageReadByMe (im.actor.core.api.updates.UpdateMessageReadByMe)1