use of org.jivesoftware.smack.packet.Stanza in project xabber-android by redsolution.
the class NextMamManager method parseAndSaveMessageFromMamResult.
/**
* PARSING
*/
private void parseAndSaveMessageFromMamResult(Realm realm, AccountJid account, Forwarded forwarded) {
Stanza stanza = forwarded.getForwardedStanza();
AccountItem accountItem = AccountManager.getInstance().getAccount(account);
Jid user = stanza.getFrom().asBareJid();
if (user.equals(account.getFullJid().asBareJid()))
user = stanza.getTo().asBareJid();
try {
AbstractChat chat = MessageManager.getInstance().getOrCreateChat(account, UserJid.from(user));
MessageItem messageItem = parseMessage(accountItem, account, chat.getUser(), forwarded, null);
if (messageItem != null) {
saveOrUpdateMessages(realm, Collections.singletonList(messageItem), true);
updateLastMessageId(chat, realm);
}
} catch (UserJid.UserJidCreateException e) {
LogManager.d(LOG_TAG, e.toString());
}
}
use of org.jivesoftware.smack.packet.Stanza in project xabber-android by redsolution.
the class NextMamManager method loadAllNewMessages.
private boolean loadAllNewMessages(Realm realm, AccountItem accountItem, String lastArchivedId) {
if (accountItem.getLoadHistorySettings() != LoadHistorySettings.all || !isSupported(accountItem.getAccount()))
return true;
LogManager.d(LOG_TAG, "load new messages");
List<Forwarded> messages = new ArrayList<>();
boolean complete = false;
String id = lastArchivedId;
int pageLoaded = 0;
// Request all new messages after last archived id
while (!complete && id != null && pageLoaded < 2) {
MamManager.MamQueryResult queryResult = requestMessagesFromId(accountItem, null, id);
if (queryResult != null) {
messages.addAll(queryResult.forwardedMessages);
complete = queryResult.mamFin.isComplete();
id = getNextId(queryResult);
pageLoaded++;
} else
complete = true;
}
if (!messages.isEmpty()) {
HashMap<String, ArrayList<Forwarded>> messagesByChat = new HashMap<>();
List<MessageItem> parsedMessages = new ArrayList<>();
List<AbstractChat> chatsNeedUpdateLastMessageId = new ArrayList<>();
// Sort messages by chat to separate lists
for (Forwarded forwarded : messages) {
Stanza stanza = forwarded.getForwardedStanza();
Jid user = stanza.getFrom().asBareJid();
if (user.equals(accountItem.getAccount().getFullJid().asBareJid()))
user = stanza.getTo().asBareJid();
if (!messagesByChat.containsKey(user.toString())) {
messagesByChat.put(user.toString(), new ArrayList<Forwarded>());
}
ArrayList<Forwarded> list = messagesByChat.get(user.toString());
if (list != null)
list.add(forwarded);
}
// parse message lists
for (Map.Entry<String, ArrayList<Forwarded>> entry : messagesByChat.entrySet()) {
ArrayList<Forwarded> list = entry.getValue();
if (list != null) {
try {
AbstractChat chat = MessageManager.getInstance().getOrCreateChat(accountItem.getAccount(), UserJid.from(entry.getKey()));
// sort messages in list by timestamp
Collections.sort(list, new Comparator<Forwarded>() {
@Override
public int compare(Forwarded o1, Forwarded o2) {
DelayInformation delayInformation1 = o1.getDelayInformation();
long time1 = delayInformation1.getStamp().getTime();
DelayInformation delayInformation2 = o2.getDelayInformation();
long time2 = delayInformation2.getStamp().getTime();
return Long.valueOf(time1).compareTo(time2);
}
});
// parse messages and set previous id
parsedMessages.addAll(parseMessage(accountItem, accountItem.getAccount(), chat.getUser(), list, chat.getLastMessageId()));
chatsNeedUpdateLastMessageId.add(chat);
} catch (UserJid.UserJidCreateException e) {
LogManager.d(LOG_TAG, e.toString());
continue;
}
}
}
// save messages to Realm
saveOrUpdateMessages(realm, parsedMessages);
for (AbstractChat chat : chatsNeedUpdateLastMessageId) {
updateLastMessageId(chat, realm);
}
}
return complete;
}
use of org.jivesoftware.smack.packet.Stanza in project xabber-android by redsolution.
the class UploadService method startWork.
private void startWork(AccountJid account, UserJid user, List<String> filePaths, CharSequence uploadServerUrl, String existMessageId) {
// get account item
AccountItem accountItem = AccountManager.getInstance().getAccount(account);
if (accountItem == null) {
publishError(null, "Account not found");
return;
}
// get upload jid
Jid uploadJid;
try {
uploadJid = JidCreate.bareFrom(uploadServerUrl);
} catch (XmppStringprepException e) {
publishError(null, "Wrong upload jid");
return;
}
final String fileMessageId;
if (existMessageId == null) {
// create fileMessage with files
List<File> files = new ArrayList<>();
for (String filePath : filePaths) {
files.add(new File(filePath));
}
fileMessageId = MessageManager.getInstance().createFileMessage(account, user, files);
} else
// use existing fileMessage
fileMessageId = existMessageId;
HashMap<String, String> uploadedFilesUrls = new HashMap<>();
List<String> notUploadedFilesPaths = new ArrayList<>();
List<File> notUploadedFiles = new ArrayList<>();
List<String> errors = new ArrayList<>();
for (String filePath : filePaths) {
if (needStop) {
stopWork(fileMessageId);
return;
}
try {
File uncompressedFile = new File(filePath);
final File file;
// compress file if image
if (FileManager.fileIsImage(uncompressedFile) && SettingsManager.connectionCompressImage()) {
file = ImageCompressor.compressImage(uncompressedFile, getCompressedDirPath());
if (file == null)
throw new Exception("Compress image failed");
} else
file = uncompressedFile;
// request slot
Stanza slot = requestSlot(accountItem, file, uploadJid);
if (!(slot instanceof Slot))
throw new Exception("Could not request upload slot");
// upload file
Response response = uploadFileToSlot(account, (Slot) slot, file);
if (response.isSuccessful())
uploadedFilesUrls.put(filePath, ((Slot) slot).getGetUrl());
else
throw new Exception("Upload failed: " + response.message());
} catch (Exception e) {
notUploadedFilesPaths.add(filePath);
notUploadedFiles.add(new File(filePath));
errors.add(e.toString());
}
publishProgress(fileMessageId, uploadedFilesUrls.size(), filePaths.size());
}
removeTempDirectory();
// check that files are uploaded
if (uploadedFilesUrls.size() == 0) {
setErrorForMessage(fileMessageId, generateErrorDescriptionForFiles(notUploadedFilesPaths, errors));
publishError(fileMessageId, "Could not upload any files");
return;
}
// save results to Realm and send message
MessageManager.getInstance().updateFileMessage(account, user, fileMessageId, uploadedFilesUrls, notUploadedFilesPaths);
publishCompleted(fileMessageId);
// if some files have errors move its to separate message
if (notUploadedFilesPaths.size() > 0) {
String messageId = MessageManager.getInstance().createFileMessage(account, user, notUploadedFiles);
setErrorForMessage(messageId, generateErrorDescriptionForFiles(notUploadedFilesPaths, errors));
}
}
use of org.jivesoftware.smack.packet.Stanza in project xabber-android by redsolution.
the class AbstractChat method parseForwardedMessage.
public RealmList<ForwardId> parseForwardedMessage(boolean ui, Stanza packet, String parentMessageId) {
List<Forwarded> forwarded = ReferencesManager.getForwardedFromReferences(packet);
if (forwarded.isEmpty())
forwarded = ForwardManager.getForwardedFromStanza(packet);
if (forwarded.isEmpty())
return null;
RealmList<ForwardId> forwardedIds = new RealmList<>();
for (Forwarded forward : forwarded) {
Stanza stanza = forward.getForwardedStanza();
DelayInformation delayInformation = forward.getDelayInformation();
Date timestamp = delayInformation.getStamp();
if (stanza instanceof Message) {
forwardedIds.add(new ForwardId(parseInnerMessage(ui, (Message) stanza, timestamp, parentMessageId)));
}
}
return forwardedIds;
}
use of org.jivesoftware.smack.packet.Stanza in project Smack by igniterealtime.
the class AbstractXMPPConnection method firePacketInterceptors.
/**
* Process interceptors. Interceptors may modify the stanza that is about to be sent.
* Since the thread that requested to send the stanza will invoke all interceptors, it
* is important that interceptors perform their work as soon as possible so that the
* thread does not remain blocked for a long period.
*
* @param packet the stanza that is going to be sent to the server.
* @return the, potentially modified stanza, after the interceptors are run.
*/
private Stanza firePacketInterceptors(Stanza packet) {
List<StanzaListener> interceptorsToInvoke = new LinkedList<>();
synchronized (interceptors) {
for (InterceptorWrapper interceptorWrapper : interceptors.values()) {
if (interceptorWrapper.filterMatches(packet)) {
interceptorsToInvoke.add(interceptorWrapper.getInterceptor());
}
}
}
for (StanzaListener interceptor : interceptorsToInvoke) {
try {
interceptor.processStanza(packet);
} catch (Exception e) {
LOGGER.log(Level.SEVERE, "Packet interceptor threw exception", e);
}
}
final Stanza stanzaAfterInterceptors;
if (packet instanceof Message) {
Message message = (Message) packet;
stanzaAfterInterceptors = fireMessageOrPresenceInterceptors(message, messageInterceptors);
} else if (packet instanceof Presence) {
Presence presence = (Presence) packet;
stanzaAfterInterceptors = fireMessageOrPresenceInterceptors(presence, presenceInterceptors);
} else {
// We do not (yet) support interceptors for IQ stanzas.
assert packet instanceof IQ;
stanzaAfterInterceptors = packet;
}
return stanzaAfterInterceptors;
}
Aggregations