proper error parsing. some clean up

This commit is contained in:
Daniel Gultsch 2015-05-19 08:23:12 +02:00
parent 9658146575
commit 201bc158bd
1 changed files with 49 additions and 41 deletions

View File

@ -101,6 +101,20 @@ public class MessageParser extends AbstractParser implements
this.jid = jid; this.jid = jid;
this.password = password; this.password = password;
} }
public boolean execute(Account account) {
if (jid != null) {
Conversation conversation = mXmppConnectionService.findOrCreateConversation(account, jid, true);
if (!conversation.getMucOptions().online()) {
conversation.getMucOptions().setPassword(password);
mXmppConnectionService.databaseBackend.updateConversation(conversation);
mXmppConnectionService.joinMuc(conversation);
mXmppConnectionService.updateConversationUi();
}
return true;
}
return false;
}
} }
private Invite extractInvite(Element message) { private Invite extractInvite(Element message) {
@ -122,34 +136,23 @@ public class MessageParser extends AbstractParser implements
private void parseEvent(final Element event, final Jid from, final Account account) { private void parseEvent(final Element event, final Jid from, final Account account) {
Element items = event.findChild("items"); Element items = event.findChild("items");
if (items == null) { String node = items == null ? null : items.getAttribute("node");
return; if ("urn:xmpp:avatar:metadata".equals(node)) {
}
String node = items.getAttribute("node");
if (node == null) {
return;
}
if (node.equals("urn:xmpp:avatar:metadata")) {
Avatar avatar = Avatar.parseMetadata(items); Avatar avatar = Avatar.parseMetadata(items);
if (avatar != null) { if (avatar != null) {
avatar.owner = from; avatar.owner = from;
if (mXmppConnectionService.getFileBackend().isAvatarCached( if (mXmppConnectionService.getFileBackend().isAvatarCached(avatar)) {
avatar)) {
if (account.getJid().toBareJid().equals(from)) { if (account.getJid().toBareJid().equals(from)) {
if (account.setAvatar(avatar.getFilename())) { if (account.setAvatar(avatar.getFilename())) {
mXmppConnectionService.databaseBackend mXmppConnectionService.databaseBackend.updateAccount(account);
.updateAccount(account);
} }
mXmppConnectionService.getAvatarService().clear( mXmppConnectionService.getAvatarService().clear(account);
account);
mXmppConnectionService.updateConversationUi(); mXmppConnectionService.updateConversationUi();
mXmppConnectionService.updateAccountUi(); mXmppConnectionService.updateAccountUi();
} else { } else {
Contact contact = account.getRoster().getContact( Contact contact = account.getRoster().getContact(from);
from);
contact.setAvatar(avatar); contact.setAvatar(avatar);
mXmppConnectionService.getAvatarService().clear( mXmppConnectionService.getAvatarService().clear(contact);
contact);
mXmppConnectionService.updateConversationUi(); mXmppConnectionService.updateConversationUi();
mXmppConnectionService.updateRosterUi(); mXmppConnectionService.updateRosterUi();
} }
@ -157,15 +160,11 @@ public class MessageParser extends AbstractParser implements
mXmppConnectionService.fetchAvatar(account, avatar); mXmppConnectionService.fetchAvatar(account, avatar);
} }
} }
} else if (node.equals("http://jabber.org/protocol/nick")) { } else if ("http://jabber.org/protocol/nick".equals(node)) {
Element item = items.findChild("item"); Element i = items.findChild("item");
if (item != null) { Element nick = i == null ? null : i.findChild("nick", "http://jabber.org/protocol/nick");
Element nick = item.findChild("nick",
"http://jabber.org/protocol/nick");
if (nick != null) { if (nick != null) {
if (from != null) { Contact contact = account.getRoster().getContact(from);
Contact contact = account.getRoster().getContact(
from);
contact.setPresenceName(nick.getContent()); contact.setPresenceName(nick.getContent());
mXmppConnectionService.getAvatarService().clear(account); mXmppConnectionService.getAvatarService().clear(account);
mXmppConnectionService.updateConversationUi(); mXmppConnectionService.updateConversationUi();
@ -173,11 +172,23 @@ public class MessageParser extends AbstractParser implements
} }
} }
} }
private boolean handleErrorMessage(Account account, MessagePacket packet) {
if (packet.getType() == MessagePacket.TYPE_ERROR) {
Jid from = packet.getFrom();
if (from != null) {
mXmppConnectionService.markMessage(account, from.toBareJid(), packet.getId(), Message.STATUS_SEND_FAILED);
} }
return true;
}
return false;
} }
@Override @Override
public void onMessagePacketReceived(Account account, MessagePacket original) { public void onMessagePacketReceived(Account account, MessagePacket original) {
if (handleErrorMessage(account, original)) {
return;
}
final MessagePacket packet; final MessagePacket packet;
Long timestamp = null; Long timestamp = null;
final boolean isForwarded; final boolean isForwarded;
@ -207,12 +218,16 @@ public class MessageParser extends AbstractParser implements
f = original.getForwardedMessagePacket("received", "urn:xmpp:carbons:2"); f = original.getForwardedMessagePacket("received", "urn:xmpp:carbons:2");
f = f == null ? original.getForwardedMessagePacket("sent", "urn:xmpp:carbons:2") : f; f = f == null ? original.getForwardedMessagePacket("sent", "urn:xmpp:carbons:2") : f;
packet = f != null ? f.first : original; packet = f != null ? f.first : original;
if (handleErrorMessage(account, packet)) {
return;
}
timestamp = f != null ? f.second : null; timestamp = f != null ? f.second : null;
isForwarded = f != null; isForwarded = f != null;
} else { } else {
packet = original; packet = original;
isForwarded = false; isForwarded = false;
} }
if (timestamp == null) { if (timestamp == null) {
timestamp = AbstractParser.getTimestamp(packet, System.currentTimeMillis()); timestamp = AbstractParser.getTimestamp(packet, System.currentTimeMillis());
} }
@ -239,14 +254,7 @@ public class MessageParser extends AbstractParser implements
} }
Invite invite = extractInvite(packet); Invite invite = extractInvite(packet);
if (invite != null && invite.jid != null) { if (invite != null && invite.execute(account)) {
Conversation conversation = mXmppConnectionService.findOrCreateConversation(account, invite.jid, true,query);
if (!conversation.getMucOptions().online()) {
conversation.getMucOptions().setPassword(invite.password);
mXmppConnectionService.databaseBackend.updateConversation(conversation);
mXmppConnectionService.joinMuc(conversation);
mXmppConnectionService.updateConversationUi();
}
return; return;
} }