Search in sources :

Example 21 with Message

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

the class SenderActor method doSendAnimation.

public void doSendAnimation(Peer peer, String fileName, int w, int h, FastThumb fastThumb, String descriptor, int fileSize) {
    long rid = RandomUtils.nextRid();
    long date = createPendingDate();
    long sortDate = date + 365 * 24 * 60 * 60 * 1000L;
    AnimationContent animationContent = AnimationContent.createLocalAnimation(descriptor, fileName, fileSize, w, h, fastThumb);
    Message message = new Message(rid, sortDate, date, myUid(), MessageState.PENDING, animationContent);
    context().getMessagesModule().getRouter().onOutgoingMessage(peer, message);
    pendingMessages.getPendingMessages().add(new PendingMessage(peer, rid, animationContent));
    savePending();
    performUploadFile(rid, descriptor, fileName);
}
Also used : PendingMessage(im.actor.core.modules.messaging.actions.entity.PendingMessage) ApiTextMessage(im.actor.core.api.ApiTextMessage) Message(im.actor.core.entity.Message) ApiDocumentMessage(im.actor.core.api.ApiDocumentMessage) RequestSendMessage(im.actor.core.api.rpc.RequestSendMessage) ApiMessage(im.actor.core.api.ApiMessage) ApiJsonMessage(im.actor.core.api.ApiJsonMessage) AnimationContent(im.actor.core.entity.content.AnimationContent) PendingMessage(im.actor.core.modules.messaging.actions.entity.PendingMessage)

Example 22 with Message

use of im.actor.core.entity.Message 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 23 with Message

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

Example 24 with Message

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

the class JsBindingModule method onFileLoaded.

@Override
public void onFileLoaded(HashSet<Long> fileId) {
    if (dialogsList != null) {
        for (JsDisplayListBind<JsDialog, Dialog> b : dialogsList.getActiveBinds()) {
            b.startReconverting();
            for (Dialog dialog : b.getRawItems()) {
                if (checkAvatar(dialog.getDialogAvatar(), fileId)) {
                    b.forceReconvert(dialog.getEngineId());
                }
            }
            b.stopReconverting();
        }
    }
    if (dialogsGroupedList != null) {
        ArrayList<DialogGroup> groups = context().getMessagesModule().getDialogGroupsVM().getGroupsValueModel().get();
        if (groups != null) {
            outer: for (DialogGroup g : groups) {
                for (DialogSmall ds : g.getDialogs()) {
                    if (checkAvatar(ds.getAvatar(), fileId)) {
                        context().getMessagesModule().getDialogGroupsVM().getGroupsValueModel().forceNotify();
                        break outer;
                    }
                }
            }
        }
    }
    if (contactsList != null) {
        for (JsDisplayListBind<JsContact, Contact> b : contactsList.getActiveBinds()) {
            b.startReconverting();
            for (Contact contact : b.getRawItems()) {
                if (checkAvatar(contact.getAvatar(), fileId)) {
                    b.forceReconvert(contact.getEngineId());
                }
            }
            b.stopReconverting();
        }
    }
    for (JsDisplayList<JsMessage, Message> messageList : messageLists.values()) {
        for (JsDisplayListBind<JsMessage, Message> b : messageList.getActiveBinds()) {
            b.startReconverting();
            for (Message message : b.getRawItems()) {
                UserVM user = context().getUsersModule().getUsers().get(message.getSenderId());
                if (checkAvatar(user.getAvatar().get(), fileId)) {
                    b.forceReconvert(message.getEngineId());
                    continue;
                }
                if (message.getContent() instanceof DocumentContent) {
                    DocumentContent doc = (DocumentContent) message.getContent();
                    if (doc.getSource() instanceof FileRemoteSource) {
                        if (fileId.contains(((FileRemoteSource) doc.getSource()).getFileReference().getFileId())) {
                            b.forceReconvert(message.getEngineId());
                        }
                    }
                }
                if (message.getContent() instanceof StickerContent) {
                    StickerContent content = (StickerContent) message.getContent();
                    if (content.getImage512() != null) {
                        long stickerFileId = content.getImage512().getReference().getFileId();
                        if (fileId.contains(stickerFileId)) {
                            b.forceReconvert(message.getEngineId());
                        }
                    } else if (content.getImage256() != null) {
                        long stickerFileId = content.getImage256().getReference().getFileId();
                        if (fileId.contains(stickerFileId)) {
                            b.forceReconvert(message.getEngineId());
                        }
                    }
                }
            }
            b.stopReconverting();
        }
    }
    for (JsBindedValue<JsUser> u : users.values()) {
        int uid = u.get().getUid();
        UserVM userVM = context().getUsersModule().getUsers().get(uid);
        if (checkAvatar(userVM.getAvatar().get(), fileId)) {
            u.changeValue(JsUser.fromUserVM(userVM, messenger));
        }
    }
    for (JsBindedValue<JsGroup> g : groups.values()) {
        int gid = g.get().getGid();
        GroupVM groupVM = context().getGroupsModule().getGroupsCollection().get(gid);
        if (checkAvatar(groupVM.getAvatar().get(), fileId)) {
            g.changeValue(JsGroup.fromGroupVM(groupVM, messenger));
        }
    }
    // 
    if (stickers != null) {
        outer: for (StickerPack stickerPack : messenger.getAvailableStickersVM().getOwnStickerPacks().get()) {
            for (Sticker s : stickerPack.getStickers()) {
                if (s.getImage256() != null && fileId.contains(s.getImage256().getFileId())) {
                    messenger.getAvailableStickersVM().getOwnStickerPacks().forceNotify();
                    break outer;
                }
            }
        }
    }
}
Also used : JsMessage(im.actor.core.js.entity.JsMessage) Message(im.actor.core.entity.Message) StickerPack(im.actor.core.entity.StickerPack) JsMessage(im.actor.core.js.entity.JsMessage) Sticker(im.actor.core.entity.Sticker) JsSticker(im.actor.core.js.entity.JsSticker) JsDialog(im.actor.core.js.entity.JsDialog) JsDialog(im.actor.core.js.entity.JsDialog) Dialog(im.actor.core.entity.Dialog) JsUser(im.actor.core.js.entity.JsUser) DialogSmall(im.actor.core.viewmodel.DialogSmall) StickerContent(im.actor.core.entity.content.StickerContent) GroupVM(im.actor.core.viewmodel.GroupVM) JsContact(im.actor.core.js.entity.JsContact) JsContact(im.actor.core.js.entity.JsContact) Contact(im.actor.core.entity.Contact) UserVM(im.actor.core.viewmodel.UserVM) FileRemoteSource(im.actor.core.entity.content.FileRemoteSource) DocumentContent(im.actor.core.entity.content.DocumentContent) JsDialogGroup(im.actor.core.js.entity.JsDialogGroup) DialogGroup(im.actor.core.viewmodel.DialogGroup) JsGroup(im.actor.core.js.entity.JsGroup)

Example 25 with Message

use of im.actor.core.entity.Message 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(), "\u2764").start(new CommandCallback<Void>() {

                            @Override
                            public void onResult(Void res) {
                            }

                            @Override
                            public void onError(Exception e) {
                            }
                        });
                    } else {
                        ActorSDK.sharedActor().getMessenger().addReaction(getPeer(), currentMessage.getRid(), "\u2764").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)

Aggregations

Message (im.actor.core.entity.Message)28 ApiMessage (im.actor.core.api.ApiMessage)13 ApiTextMessage (im.actor.core.api.ApiTextMessage)12 ApiDocumentMessage (im.actor.core.api.ApiDocumentMessage)11 ApiJsonMessage (im.actor.core.api.ApiJsonMessage)11 RequestSendMessage (im.actor.core.api.rpc.RequestSendMessage)11 PendingMessage (im.actor.core.modules.messaging.actions.entity.PendingMessage)11 UpdateMessage (im.actor.core.api.updates.UpdateMessage)7 TextContent (im.actor.core.entity.content.TextContent)6 RouterOutgoingMessage (im.actor.core.modules.messaging.router.entity.RouterOutgoingMessage)6 ArrayList (java.util.ArrayList)6 ConversationState (im.actor.core.entity.ConversationState)5 Peer (im.actor.core.entity.Peer)5 AbsContent (im.actor.core.entity.content.AbsContent)5 Reaction (im.actor.core.entity.Reaction)3 UserVM (im.actor.core.viewmodel.UserVM)3 Void (im.actor.runtime.actors.messages.Void)3 ApiPeer (im.actor.core.api.ApiPeer)2 Dialog (im.actor.core.entity.Dialog)2 Sticker (im.actor.core.entity.Sticker)2