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);
}
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();
}
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;
}
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);
}
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;
});
}
Aggregations