Search in sources :

Example 1 with AbsContent

use of im.actor.core.entity.content.AbsContent 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 AbsContent

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

the class SenderActor method onFileUploaded.

private void onFileUploaded(long rid, FileReference fileReference) {
    PendingMessage msg = findPending(rid);
    if (msg == null) {
        return;
    }
    pendingMessages.getPendingMessages().remove(msg);
    AbsContent nContent;
    if (msg.getContent() instanceof PhotoContent) {
        PhotoContent basePhotoContent = (PhotoContent) msg.getContent();
        nContent = PhotoContent.createRemotePhoto(fileReference, basePhotoContent.getW(), basePhotoContent.getH(), basePhotoContent.getFastThumb());
    } else if (msg.getContent() instanceof VideoContent) {
        VideoContent baseVideoContent = (VideoContent) msg.getContent();
        nContent = VideoContent.createRemoteVideo(fileReference, baseVideoContent.getW(), baseVideoContent.getH(), baseVideoContent.getDuration(), baseVideoContent.getFastThumb());
    } else if (msg.getContent() instanceof VoiceContent) {
        VoiceContent baseVoiceContent = (VoiceContent) msg.getContent();
        nContent = VoiceContent.createRemoteAudio(fileReference, baseVoiceContent.getDuration());
    } else if (msg.getContent() instanceof AnimationContent) {
        AnimationContent baseAnimcationContent = (AnimationContent) msg.getContent();
        nContent = AnimationContent.createRemoteAnimation(fileReference, baseAnimcationContent.getW(), baseAnimcationContent.getH(), baseAnimcationContent.getFastThumb());
    } else if (msg.getContent() instanceof DocumentContent) {
        DocumentContent baseDocContent = (DocumentContent) msg.getContent();
        nContent = DocumentContent.createRemoteDocument(fileReference, baseDocContent.getFastThumb());
    } else {
        return;
    }
    pendingMessages.getPendingMessages().add(new PendingMessage(msg.getPeer(), msg.getRid(), nContent));
    context().getMessagesModule().getRouter().onContentChanged(msg.getPeer(), msg.getRid(), nContent);
    performSendContent(msg.getPeer(), rid, nContent);
    fileUplaodingWakeLocks.remove(rid).releaseLock();
}
Also used : VideoContent(im.actor.core.entity.content.VideoContent) VoiceContent(im.actor.core.entity.content.VoiceContent) DocumentContent(im.actor.core.entity.content.DocumentContent) AbsContent(im.actor.core.entity.content.AbsContent) AnimationContent(im.actor.core.entity.content.AnimationContent) PendingMessage(im.actor.core.modules.messaging.actions.entity.PendingMessage) PhotoContent(im.actor.core.entity.content.PhotoContent)

Example 3 with AbsContent

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

the class MessagesDefaultFragment method onLongClick.

@Override
public boolean onLongClick(final Message message, final boolean hasMyReaction) {
    if (actionMode == null) {
        messagesAdapter.clearSelection();
        messagesAdapter.setSelected(message, true);
        actionMode = ((AppCompatActivity) getActivity()).startSupportActionMode(new ActionMode.Callback() {

            @Override
            public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
                getActivity().getMenuInflater().inflate(R.menu.messages_context, menu);
                return true;
            }

            @Override
            public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
                Message[] selected = messagesAdapter.getSelected();
                if (selected.length > 0) {
                    actionMode.setTitle("" + selected.length);
                }
                boolean isAllText = true;
                for (Message k : selected) {
                    if (!(k.getContent() instanceof TextContent)) {
                        isAllText = false;
                        break;
                    }
                }
                menu.findItem(R.id.copy).setVisible(isAllText);
                menu.findItem(R.id.quote).setVisible(isAllText);
                menu.findItem(R.id.forward).setVisible(selected.length == 1 || isAllText);
                menu.findItem(R.id.like).setVisible(selected.length == 1);
                return false;
            }

            @Override
            public boolean onActionItemClicked(final ActionMode actionMode, MenuItem menuItem) {
                if (menuItem.getItemId() == R.id.delete) {
                    Message[] selected = messagesAdapter.getSelected();
                    final long[] rids = new long[selected.length];
                    for (int i = 0; i < rids.length; i++) {
                        rids[i] = selected[i].getRid();
                    }
                    new AlertDialog.Builder(getActivity()).setMessage(getString(R.string.alert_delete_messages_text).replace("{0}", "" + rids.length)).setPositiveButton(R.string.alert_delete_messages_yes, (dialog, which) -> {
                        messenger().deleteMessages(peer, rids);
                        actionMode.finish();
                    }).setNegativeButton(R.string.dialog_cancel, null).show().setCanceledOnTouchOutside(true);
                    return true;
                } else if (menuItem.getItemId() == R.id.copy) {
                    String text = messenger().getFormatter().formatMessagesExport(messagesAdapter.getSelected());
                    android.content.ClipboardManager clipboard = (android.content.ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
                    android.content.ClipData clip = android.content.ClipData.newPlainText("Messages", text);
                    clipboard.setPrimaryClip(clip);
                    Toast.makeText(getActivity(), R.string.toast_messages_copied, Toast.LENGTH_SHORT).show();
                    actionMode.finish();
                    return true;
                } else if (menuItem.getItemId() == R.id.like) {
                    Message currentMessage = messagesAdapter.getSelected()[0];
                    if (hasMyReaction) {
                        ActorSDK.sharedActor().getMessenger().removeReaction(getPeer(), currentMessage.getRid(), "❤").start(new CommandCallback<Void>() {

                            @Override
                            public void onResult(Void res) {
                            }

                            @Override
                            public void onError(Exception e) {
                            }
                        });
                    } else {
                        ActorSDK.sharedActor().getMessenger().addReaction(getPeer(), currentMessage.getRid(), "❤").start(new CommandCallback<Void>() {

                            @Override
                            public void onResult(Void res) {
                            }

                            @Override
                            public void onError(Exception e) {
                            }
                        });
                    }
                    actionMode.finish();
                    return true;
                } else if (menuItem.getItemId() == R.id.quote) {
                    String rawQuote = "";
                    int i = 0;
                    for (Message m : messagesAdapter.getSelected()) {
                        if (m.getContent() instanceof TextContent) {
                            UserVM user = users().get(m.getSenderId());
                            String nick = user.getNick().get();
                            String name = (nick != null && !nick.isEmpty()) ? "@" + nick : user.getName().get();
                            String text = ((TextContent) m.getContent()).getText();
                            rawQuote = rawQuote + name + ": " + text + "\n";
                        }
                    }
                    Fragment fragment = getParentFragment();
                    if (fragment instanceof MessagesFragmentCallback) {
                        ((MessagesFragmentCallback) fragment).onMessageQuote(rawQuote);
                    }
                    actionMode.finish();
                    return true;
                } else if (menuItem.getItemId() == R.id.forward) {
                    Intent i = new Intent(getActivity(), ShareActivity.class);
                    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    if (messagesAdapter.getSelected().length == 1) {
                        Message m = messagesAdapter.getSelected()[0];
                        if (m.getContent() instanceof TextContent) {
                            UserVM user = users().get(m.getSenderId());
                            String nick = user.getNick().get();
                            String name = (nick != null && !nick.isEmpty()) ? "@".concat(nick) : user.getName().get();
                            String text = ((TextContent) m.getContent()).getText();
                            String forward = name.concat(": ").concat(text).concat("\n");
                            i.putExtra(Intents.EXTRA_FORWARD_TEXT, forward);
                            i.putExtra(Intents.EXTRA_FORWARD_TEXT_RAW, forward);
                        } else if (!(m.getContent() instanceof UnsupportedContent)) {
                            AbsContent fileMessage = m.getContent();
                            try {
                                i.putExtra(Intents.EXTRA_FORWARD_CONTENT, AbsContent.serialize(fileMessage));
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    } else {
                        String quote = "";
                        String rawQuote = "";
                        int j = 0;
                        for (Message m : messagesAdapter.getSelected()) {
                            if (m.getContent() instanceof TextContent) {
                                UserVM user = users().get(m.getSenderId());
                                String nick = user.getNick().get();
                                String name = (nick != null && !nick.isEmpty()) ? "@".concat(nick) : user.getName().get();
                                String text = ((TextContent) m.getContent()).getText();
                                quote = quote.concat(name).concat(": ").concat(text);
                                rawQuote = rawQuote.concat(name).concat(": ").concat(text).concat("\n");
                                if (j++ != messagesAdapter.getSelectedCount() - 1) {
                                    quote += ";\n";
                                } else {
                                    quote += "\n";
                                }
                            }
                        }
                        i.putExtra(Intents.EXTRA_FORWARD_TEXT, quote);
                        i.putExtra(Intents.EXTRA_FORWARD_TEXT_RAW, rawQuote);
                    }
                    actionMode.finish();
                    startActivity(i);
                    getActivity().finish();
                    return true;
                }
                return false;
            }

            @Override
            public void onDestroyActionMode(ActionMode actionMode) {
                MessagesDefaultFragment.this.actionMode = null;
                messagesAdapter.clearSelection();
            }
        });
    } else {
        if (messagesAdapter.isSelected(message)) {
            messagesAdapter.setSelected(message, false);
            if (messagesAdapter.getSelectedCount() == 0) {
                actionMode.finish();
                actionMode = null;
            } else {
                actionMode.invalidate();
            }
        } else {
            messagesAdapter.setSelected(message, true);
            actionMode.invalidate();
        }
    }
    return true;
}
Also used : AlertDialog(android.app.AlertDialog) Context(android.content.Context) Bundle(android.os.Bundle) ActorSDKMessenger.myUid(im.actor.sdk.util.ActorSDKMessenger.myUid) ActorSDKMessenger.messenger(im.actor.sdk.util.ActorSDKMessenger.messenger) Intent(android.content.Intent) MenuItem(android.view.MenuItem) Toast(android.widget.Toast) Menu(android.view.Menu) CommandCallback(im.actor.core.viewmodel.CommandCallback) TextContent(im.actor.core.entity.content.TextContent) Intents(im.actor.sdk.controllers.Intents) ActorSDK(im.actor.sdk.ActorSDK) AbsContent(im.actor.core.entity.content.AbsContent) ActorSDKLauncher(im.actor.sdk.ActorSDKLauncher) ActionMode(android.support.v7.view.ActionMode) Void(im.actor.runtime.actors.messages.Void) Fragment(android.support.v4.app.Fragment) ActorSDKMessenger.users(im.actor.sdk.util.ActorSDKMessenger.users) IOException(java.io.IOException) UnsupportedContent(im.actor.core.entity.content.UnsupportedContent) AppCompatActivity(android.support.v7.app.AppCompatActivity) AlertDialog(android.app.AlertDialog) UserVM(im.actor.core.viewmodel.UserVM) R(im.actor.sdk.R) ChatActivity(im.actor.sdk.controllers.conversation.ChatActivity) Peer(im.actor.core.entity.Peer) ShareActivity(im.actor.sdk.controllers.share.ShareActivity) Message(im.actor.core.entity.Message) Message(im.actor.core.entity.Message) UnsupportedContent(im.actor.core.entity.content.UnsupportedContent) Fragment(android.support.v4.app.Fragment) Menu(android.view.Menu) Void(im.actor.runtime.actors.messages.Void) AbsContent(im.actor.core.entity.content.AbsContent) MenuItem(android.view.MenuItem) Intent(android.content.Intent) IOException(java.io.IOException) IOException(java.io.IOException) UserVM(im.actor.core.viewmodel.UserVM) CommandCallback(im.actor.core.viewmodel.CommandCallback) ActionMode(android.support.v7.view.ActionMode) TextContent(im.actor.core.entity.content.TextContent)

Example 4 with AbsContent

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

the class MessagesProcessor method onDifferenceMessages.

@Verified
public Promise<Void> onDifferenceMessages(ApiPeer _peer, List<UpdateMessage> messages) {
    Peer peer = convert(_peer);
    ArrayList<Message> nMessages = new ArrayList<>();
    for (UpdateMessage u : messages) {
        AbsContent msgContent = AbsContent.fromMessage(u.getMessage());
        nMessages.add(new Message(u.getRid(), u.getDate(), u.getDate(), u.getSenderUid(), myUid() == u.getSenderUid() ? MessageState.SENT : MessageState.UNKNOWN, msgContent));
    }
    return context().getMessagesModule().getRouter().onNewMessages(peer, nMessages);
}
Also used : UpdateMessage(im.actor.core.api.updates.UpdateMessage) UpdateMessage(im.actor.core.api.updates.UpdateMessage) ApiMessage(im.actor.core.api.ApiMessage) Message(im.actor.core.entity.Message) ApiPeer(im.actor.core.api.ApiPeer) Peer(im.actor.core.entity.Peer) ArrayList(java.util.ArrayList) AbsContent(im.actor.core.entity.content.AbsContent) Verified(im.actor.runtime.annotations.Verified)

Example 5 with AbsContent

use of im.actor.core.entity.content.AbsContent 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

AbsContent (im.actor.core.entity.content.AbsContent)5 Message (im.actor.core.entity.Message)4 Peer (im.actor.core.entity.Peer)3 ArrayList (java.util.ArrayList)3 ApiMessageReaction (im.actor.core.api.ApiMessageReaction)2 UpdateMessage (im.actor.core.api.updates.UpdateMessage)2 Reaction (im.actor.core.entity.Reaction)2 AlertDialog (android.app.AlertDialog)1 Context (android.content.Context)1 Intent (android.content.Intent)1 Bundle (android.os.Bundle)1 Fragment (android.support.v4.app.Fragment)1 AppCompatActivity (android.support.v7.app.AppCompatActivity)1 ActionMode (android.support.v7.view.ActionMode)1 Menu (android.view.Menu)1 MenuItem (android.view.MenuItem)1 Toast (android.widget.Toast)1 ApiMessage (im.actor.core.api.ApiMessage)1 ApiMessageContainer (im.actor.core.api.ApiMessageContainer)1 ApiMessageState (im.actor.core.api.ApiMessageState)1