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