use of com.xabber.android.data.database.messagerealm.Attachment in project xabber-android by redsolution.
the class MessageManager method updateFileMessage.
public void updateFileMessage(AccountJid account, UserJid user, final String messageId, final HashMap<String, String> urls, final List<String> notUploadedFilesUrls) {
final AbstractChat chat = getChat(account, user);
if (chat == null) {
return;
}
Realm realm = MessageDatabaseManager.getInstance().getNewBackgroundRealm();
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
MessageItem messageItem = realm.where(MessageItem.class).equalTo(MessageItem.Fields.UNIQUE_ID, messageId).findFirst();
if (messageItem != null) {
RealmList<Attachment> attachments = messageItem.getAttachments();
// remove attachments that not uploaded
for (String file : notUploadedFilesUrls) {
for (Attachment attachment : attachments) {
if (file.equals(attachment.getFilePath())) {
attachments.remove(attachment);
break;
}
}
}
for (Attachment attachment : attachments) {
attachment.setFileUrl(urls.get(attachment.getFilePath()));
}
messageItem.setText("");
messageItem.setSent(false);
messageItem.setInProgress(false);
messageItem.setError(false);
messageItem.setErrorDescription("");
}
}
});
realm.close();
chat.sendMessages();
}
use of com.xabber.android.data.database.messagerealm.Attachment in project xabber-android by redsolution.
the class MessageManager method processCarbonsMessage.
public void processCarbonsMessage(AccountJid account, final Message message, CarbonExtension.Direction direction) {
if (direction == CarbonExtension.Direction.sent) {
UserJid companion;
try {
companion = UserJid.from(message.getTo()).getBareUserJid();
} catch (UserJid.UserJidCreateException e) {
return;
}
AbstractChat chat = getChat(account, companion);
if (chat == null) {
chat = createChat(account, companion);
}
final String body = message.getBody();
if (body == null) {
return;
}
final AbstractChat finalChat = chat;
String text = body;
String uid = UUID.randomUUID().toString();
RealmList<ForwardId> forwardIds = finalChat.parseForwardedMessage(true, message, uid);
String originalStanza = message.toXML().toString();
String originalFrom = message.getFrom().toString();
// forward comment (to support previous forwarded xep)
String forwardComment = ForwardManager.parseForwardComment(message);
if (forwardComment != null)
text = forwardComment;
// modify body with references
Pair<String, String> bodies = ReferencesManager.modifyBodyWithReferences(message, text);
text = bodies.first;
String markupText = bodies.second;
MessageItem newMessageItem = finalChat.createNewMessageItem(text);
newMessageItem.setStanzaId(AbstractChat.getStanzaId(message));
newMessageItem.setSent(true);
newMessageItem.setForwarded(true);
if (markupText != null)
newMessageItem.setMarkupText(markupText);
// forwarding
if (forwardIds != null)
newMessageItem.setForwardedIds(forwardIds);
newMessageItem.setOriginalStanza(originalStanza);
newMessageItem.setOriginalFrom(originalFrom);
// attachments
RealmList<Attachment> attachments = HttpFileUploadManager.parseFileMessage(message);
if (attachments.size() > 0)
newMessageItem.setAttachments(attachments);
// groupchat
RefUser groupchatUser = ReferencesManager.getGroupchatUserFromReferences(message);
if (groupchatUser != null) {
GroupchatUserManager.getInstance().saveGroupchatUser(groupchatUser);
newMessageItem.setGroupchatUserId(groupchatUser.getId());
}
BackpressureMessageSaver.getInstance().saveMessageItem(newMessageItem);
// mark incoming messages as read
finalChat.markAsReadAll(false);
// start grace period
AccountManager.getInstance().startGracePeriod(account);
return;
}
UserJid companion = null;
try {
companion = UserJid.from(message.getFrom()).getBareUserJid();
} catch (UserJid.UserJidCreateException e) {
return;
}
// check for spam
if (SettingsManager.spamFilterMode() != SettingsManager.SpamFilterMode.disabled && RosterManager.getInstance().getRosterContact(account, companion) == null) {
// just ignore carbons from not-authorized user
return;
}
boolean processed = false;
for (AbstractChat chat : chats.getNested(account.toString()).values()) {
if (chat.onPacket(companion, message, true)) {
processed = true;
break;
}
}
if (getChat(account, companion) != null) {
return;
}
if (processed) {
return;
}
final String body = message.getBody();
if (body == null) {
return;
}
createChat(account, companion).onPacket(companion, message, true);
}
use of com.xabber.android.data.database.messagerealm.Attachment in project xabber-android by redsolution.
the class RegularChat method parseInnerMessage.
@Override
protected String parseInnerMessage(boolean ui, Message message, Date timestamp, String parentMessageId) {
if (message.getType() == Message.Type.error)
return null;
MUCUser mucUser = MUCUser.from(message);
if (mucUser != null && mucUser.getInvite() != null)
return null;
final Jid fromJid = message.getFrom();
Resourcepart resource = null;
if (fromJid != null)
resource = fromJid.getResourceOrNull();
String text = message.getBody();
if (text == null)
return null;
boolean encrypted = OTRManager.getInstance().isEncrypted(text);
RealmList<Attachment> attachments = HttpFileUploadManager.parseFileMessage(message);
String uid = UUID.randomUUID().toString();
RealmList<ForwardId> forwardIds = parseForwardedMessage(ui, message, uid);
String originalStanza = message.toXML().toString();
String originalFrom = "";
if (fromJid != null)
originalFrom = fromJid.toString();
boolean fromMuc = message.getType().equals(Type.groupchat);
// groupchat
String gropchatUserId = null;
RefUser groupchatUser = ReferencesManager.getGroupchatUserFromReferences(message);
if (groupchatUser != null) {
gropchatUserId = groupchatUser.getId();
GroupchatUserManager.getInstance().saveGroupchatUser(groupchatUser, timestamp.getTime());
}
// forward comment (to support previous forwarded xep)
String forwardComment = ForwardManager.parseForwardComment(message);
if (forwardComment != null && !forwardComment.isEmpty())
text = forwardComment;
// modify body with references
Pair<String, String> bodies = ReferencesManager.modifyBodyWithReferences(message, text);
text = bodies.first;
String markupText = bodies.second;
// create message with file-attachments
if (attachments.size() > 0)
createAndSaveFileMessage(ui, uid, resource, text, markupText, null, timestamp, getDelayStamp(message), true, false, encrypted, false, getStanzaId(message), attachments, originalStanza, parentMessageId, originalFrom, fromMuc, true, gropchatUserId);
else
// create message without attachments
createAndSaveNewMessage(ui, uid, resource, text, markupText, null, timestamp, getDelayStamp(message), true, false, encrypted, false, getStanzaId(message), originalStanza, parentMessageId, originalFrom, forwardIds, fromMuc, true, gropchatUserId);
return uid;
}
use of com.xabber.android.data.database.messagerealm.Attachment in project xabber-android by redsolution.
the class AbstractChat method attachmentsFromUris.
public RealmList<Attachment> attachmentsFromUris(List<Uri> uris) {
RealmList<Attachment> attachments = new RealmList<>();
for (Uri uri : uris) {
Attachment attachment = new Attachment();
attachment.setTitle(UriUtils.getFullFileName(uri));
attachment.setIsImage(UriUtils.uriIsImage(uri));
attachment.setMimeType(UriUtils.getMimeType(uri));
attachment.setDuration((long) 0);
attachments.add(attachment);
}
return attachments;
}
use of com.xabber.android.data.database.messagerealm.Attachment in project xabber-android by redsolution.
the class AbstractChat method createFileMessagePacket.
/**
* Send stanza with data-references.
*/
public Message createFileMessagePacket(String stanzaId, RealmList<Attachment> attachments, String body) {
Message message = new Message();
message.setTo(getTo());
message.setType(getType());
message.setThread(threadId);
if (stanzaId != null)
message.setStanzaId(stanzaId);
StringBuilder builder = new StringBuilder(body);
for (Attachment attachment : attachments) {
StringBuilder rowBuilder = new StringBuilder();
if (builder.length() > 0)
rowBuilder.append("\n");
rowBuilder.append(attachment.getFileUrl());
int begin = getSizeOfEncodedChars(builder.toString());
builder.append(rowBuilder);
ReferenceElement reference = ReferencesManager.createMediaReferences(attachment, begin, getSizeOfEncodedChars(builder.toString()) - 1);
message.addExtension(reference);
}
message.setBody(builder);
return message;
}
Aggregations