use of im.actor.core.entity.content.TextContent in project actor-platform by actorapp.
the class MessagesModule method updateMessage.
public Promise<Void> updateMessage(final Peer peer, final String message, final long rid) {
context().getTypingModule().onMessageSent(peer);
ArrayList<Integer> mentions = new ArrayList<>();
TextContent content = TextContent.create(message, null, mentions);
if (peer.getPeerType() == PeerType.GROUP) {
Group group = groups().getValue(peer.getPeerId());
String lowText = message.toLowerCase();
for (GroupMember member : group.getMembers()) {
User user = users().getValue(member.getUid());
if (user.getNick() != null) {
String nick = "@" + user.getNick().toLowerCase();
// TODO: Better filtering
if (lowText.contains(nick + ":") || lowText.contains(nick + " ") || lowText.contains(" " + nick) || lowText.endsWith(nick) || lowText.equals(nick)) {
mentions.add(user.getUid());
}
}
}
}
ApiMessage editMessage = new ApiTextMessage(message, content.getMentions(), content.getTextMessageEx());
return buildOutPeer(peer).flatMap(apiOutPeer -> api(new RequestUpdateMessage(apiOutPeer, rid, editMessage))).flatMap(responseSeqDate -> updates().applyUpdate(responseSeqDate.getSeq(), responseSeqDate.getState(), new UpdateMessageContentChanged(new ApiPeer(peer.getPeerType().toApi(), peer.getPeerId()), rid, editMessage)));
}
use of im.actor.core.entity.content.TextContent in project actor-platform by actorapp.
the class SenderActor method preStart.
@Override
public void preStart() {
pendingMessages = new PendingMessagesStorage();
byte[] p = preferences().getBytes(PREFERENCES);
if (p != null) {
try {
pendingMessages = PendingMessagesStorage.fromBytes(p);
} catch (IOException e) {
e.printStackTrace();
}
}
boolean isChanged = false;
ArrayList<PendingMessage> messages = pendingMessages.getPendingMessages();
for (PendingMessage pending : messages.toArray(new PendingMessage[messages.size()])) {
if (pending.getContent() instanceof TextContent) {
performSendContent(pending.getPeer(), pending.getRid(), pending.getContent());
} else if (pending.getContent() instanceof DocumentContent) {
DocumentContent documentContent = (DocumentContent) pending.getContent();
if (documentContent.getSource() instanceof FileLocalSource) {
if (Storage.isFsPersistent()) {
performUploadFile(pending.getRid(), ((FileLocalSource) documentContent.getSource()).getFileDescriptor(), ((FileLocalSource) documentContent.getSource()).getFileName());
} else {
List<Long> rids = new ArrayList<>();
rids.add(pending.getRid());
context().getMessagesModule().getRouter().onMessagesDeleted(pending.getPeer(), rids);
pendingMessages.getPendingMessages().remove(pending);
isChanged = true;
}
} else {
performSendContent(pending.getPeer(), pending.getRid(), pending.getContent());
}
}
}
if (isChanged) {
savePending();
}
}
use of im.actor.core.entity.content.TextContent 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.content.TextContent 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;
}
use of im.actor.core.entity.content.TextContent 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()]));
}
Aggregations