refactored code to always contain full jid for counterpart

This commit is contained in:
iNPUTmice 2014-11-09 16:21:13 +01:00
parent 2b99b694eb
commit 377bd49ee8
8 changed files with 84 additions and 86 deletions

View file

@ -55,7 +55,7 @@ public class Conversation extends AbstractEntity {
private JSONObject attributes = new JSONObject(); private JSONObject attributes = new JSONObject();
private String nextPresence; private Jid nextCounterpart;
protected ArrayList<Message> messages = new ArrayList<>(); protected ArrayList<Message> messages = new ArrayList<>();
protected Account account = null; protected Account account = null;
@ -345,12 +345,12 @@ public class Conversation extends AbstractEntity {
this.contactJid = jid; this.contactJid = jid;
} }
public void setNextPresence(String presence) { public void setNextCounterpart(Jid jid) {
this.nextPresence = presence; this.nextCounterpart = jid;
} }
public String getNextPresence() { public Jid getNextCounterpart() {
return this.nextPresence; return this.nextCounterpart;
} }
public int getLatestEncryption() { public int getLatestEncryption() {

View file

@ -232,27 +232,10 @@ public class Message extends AbstractEntity {
return this.type; return this.type;
} }
public void setPresence(String presence) {
if (presence == null) {
this.counterpart = this.counterpart.toBareJid();
} else {
try {
this.counterpart = Jid.fromString(this.counterpart.toBareJid() + "/" + presence);
} catch (final InvalidJidException ignored) {
// TODO: Handle this?
}
}
}
public void setTrueCounterpart(String trueCounterpart) { public void setTrueCounterpart(String trueCounterpart) {
this.trueCounterpart = trueCounterpart; this.trueCounterpart = trueCounterpart;
} }
public Jid getPresence() {
// TODO: This is now the same as getCounterpart()... find usages in code and remove one?
return counterpart;
}
public void setDownloadable(Downloadable downloadable) { public void setDownloadable(Downloadable downloadable) {
this.downloadable = downloadable; this.downloadable = downloadable;
} }

View file

@ -42,7 +42,7 @@ public class MessageParser extends AbstractParser implements
if (conversation.getMode() == Conversation.MODE_MULTI if (conversation.getMode() == Conversation.MODE_MULTI
&& !jid.getResourcepart().isEmpty()) { && !jid.getResourcepart().isEmpty()) {
finishedMessage.setType(Message.TYPE_PRIVATE); finishedMessage.setType(Message.TYPE_PRIVATE);
finishedMessage.setPresence(jid.getResourcepart()); finishedMessage.setCounterpart(packet.getFrom());
finishedMessage.setTrueCounterpart(conversation.getMucOptions() finishedMessage.setTrueCounterpart(conversation.getMucOptions()
.getTrueCounterpart(jid.getResourcepart())); .getTrueCounterpart(jid.getResourcepart()));
if (conversation.hasDuplicateMessage(finishedMessage)) { if (conversation.hasDuplicateMessage(finishedMessage)) {
@ -249,7 +249,7 @@ public class MessageParser extends AbstractParser implements
if (conversation.getMode() == Conversation.MODE_MULTI if (conversation.getMode() == Conversation.MODE_MULTI
&& !fullJid.isBareJid()) { && !fullJid.isBareJid()) {
finishedMessage.setType(Message.TYPE_PRIVATE); finishedMessage.setType(Message.TYPE_PRIVATE);
finishedMessage.setPresence(fullJid.getResourcepart()); finishedMessage.setCounterpart(fullJid);
finishedMessage.setTrueCounterpart(conversation.getMucOptions() finishedMessage.setTrueCounterpart(conversation.getMucOptions()
.getTrueCounterpart(fullJid.getResourcepart())); .getTrueCounterpart(fullJid.getResourcepart()));
if (conversation.hasDuplicateMessage(finishedMessage)) { if (conversation.hasDuplicateMessage(finishedMessage)) {
@ -478,7 +478,7 @@ public class MessageParser extends AbstractParser implements
if (message.getStatus() == Message.STATUS_RECEIVED if (message.getStatus() == Message.STATUS_RECEIVED
&& conversation.getOtrSession() != null && conversation.getOtrSession() != null
&& !conversation.getOtrSession().getSessionID().getUserID() && !conversation.getOtrSession().getSessionID().getUserID()
.equals(message.getPresence())) { .equals(message.getCounterpart().getResourcepart())) {
conversation.endOtrIfNeeded(); conversation.endOtrIfNeeded();
} }

View file

@ -19,6 +19,7 @@ import de.duenndns.ssl.MemorizingTrustManager;
import net.java.otr4j.OtrException; import net.java.otr4j.OtrException;
import net.java.otr4j.session.Session; import net.java.otr4j.session.Session;
import net.java.otr4j.session.SessionID;
import net.java.otr4j.session.SessionStatus; import net.java.otr4j.session.SessionStatus;
import eu.siacs.conversations.Config; import eu.siacs.conversations.Config;
import eu.siacs.conversations.R; import eu.siacs.conversations.R;
@ -308,7 +309,7 @@ public class XmppConnectionService extends Service {
message = new Message(conversation, "", message = new Message(conversation, "",
conversation.getNextEncryption(forceEncryption())); conversation.getNextEncryption(forceEncryption()));
} }
message.setPresence(conversation.getNextPresence()); message.setCounterpart(conversation.getNextCounterpart());
message.setType(Message.TYPE_IMAGE); message.setType(Message.TYPE_IMAGE);
message.setStatus(Message.STATUS_OFFERED); message.setStatus(Message.STATUS_OFFERED);
new Thread(new Runnable() { new Thread(new Runnable() {
@ -559,11 +560,10 @@ public class XmppConnectionService extends Service {
if (account.getStatus() == Account.STATUS_ONLINE if (account.getStatus() == Account.STATUS_ONLINE
&& account.getXmppConnection() != null) { && account.getXmppConnection() != null) {
if (message.getType() == Message.TYPE_IMAGE) { if (message.getType() == Message.TYPE_IMAGE) {
if (message.getPresence() != null) { if (message.getCounterpart() != null) {
if (message.getEncryption() == Message.ENCRYPTION_OTR) { if (message.getEncryption() == Message.ENCRYPTION_OTR) {
if (!conv.hasValidOtrSession() if (!conv.hasValidOtrSession()) {
&& (message.getPresence() != null)) { conv.startOtrSession(this, message.getCounterpart().getResourcepart(),
conv.startOtrSession(this, message.getPresence().toString(),
true); true);
message.setStatus(Message.STATUS_WAITING); message.setStatus(Message.STATUS_WAITING);
} else if (conv.hasValidOtrSession() } else if (conv.hasValidOtrSession()
@ -582,18 +582,21 @@ public class XmppConnectionService extends Service {
} }
} else { } else {
if (message.getEncryption() == Message.ENCRYPTION_OTR) { if (message.getEncryption() == Message.ENCRYPTION_OTR) {
if (!conv.hasValidOtrSession() if (!conv.hasValidOtrSession()&& (message.getCounterpart() != null)) {
&& (message.getPresence() != null)) { conv.startOtrSession(this, message.getCounterpart().getResourcepart(), true);
conv.startOtrSession(this, message.getPresence().toString(), true);
message.setStatus(Message.STATUS_WAITING); message.setStatus(Message.STATUS_WAITING);
} else if (conv.hasValidOtrSession() } else if (conv.hasValidOtrSession()
&& conv.getOtrSession().getSessionStatus() == SessionStatus.ENCRYPTED) { && conv.getOtrSession().getSessionStatus() == SessionStatus.ENCRYPTED) {
message.setPresence(conv.getOtrSession().getSessionID() SessionID id = conv.getOtrSession().getSessionID();
.getUserID()); try {
message.setCounterpart(Jid.fromString(id.getAccountID() + "/" + id.getUserID()));
} catch (final InvalidJidException e) {
message.setCounterpart(null);
}
packet = mMessageGenerator.generateOtrChat(message); packet = mMessageGenerator.generateOtrChat(message);
send = true; send = true;
} else if (message.getPresence() == null) { } else if (message.getCounterpart() == null) {
conv.startOtrIfNeeded(); conv.startOtrIfNeeded();
message.setStatus(Message.STATUS_WAITING); message.setStatus(Message.STATUS_WAITING);
} }
@ -627,11 +630,15 @@ public class XmppConnectionService extends Service {
message.setEncryption(Message.ENCRYPTION_DECRYPTED); message.setEncryption(Message.ENCRYPTION_DECRYPTED);
} else if (message.getEncryption() == Message.ENCRYPTION_OTR) { } else if (message.getEncryption() == Message.ENCRYPTION_OTR) {
if (conv.hasValidOtrSession()) { if (conv.hasValidOtrSession()) {
message.setPresence(conv.getOtrSession().getSessionID() SessionID id = conv.getOtrSession().getSessionID();
.getUserID()); try {
message.setCounterpart(Jid.fromString(id.getAccountID() + "/" + id.getUserID()));
} catch (final InvalidJidException e) {
message.setCounterpart(null);
}
} else if (!conv.hasValidOtrSession() } else if (!conv.hasValidOtrSession()
&& message.getPresence() != null) { && message.getCounterpart() != null) {
conv.startOtrSession(this, message.getPresence().toString(), false); conv.startOtrSession(this, message.getCounterpart().getResourcepart(), false);
} }
} }
} }
@ -666,10 +673,10 @@ public class XmppConnectionService extends Service {
Presences presences = message.getConversation().getContact() Presences presences = message.getConversation().getContact()
.getPresences(); .getPresences();
if (!message.getConversation().hasValidOtrSession()) { if (!message.getConversation().hasValidOtrSession()) {
if ((message.getPresence() != null) if ((message.getCounterpart() != null)
&& (presences.has(message.getPresence().toString()))) { && (presences.has(message.getCounterpart().getResourcepart()))) {
message.getConversation().startOtrSession(this, message.getConversation().startOtrSession(this,
message.getPresence().toString(), true); message.getCounterpart().getResourcepart(), true);
} else { } else {
if (presences.size() == 1) { if (presences.size() == 1) {
String presence = presences.asStringArray()[0]; String presence = presences.asStringArray()[0];
@ -696,16 +703,20 @@ public class XmppConnectionService extends Service {
packet = mMessageGenerator.generatePgpChat(message, true); packet = mMessageGenerator.generatePgpChat(message, true);
} }
} else if (message.getType() == Message.TYPE_IMAGE) { } else if (message.getType() == Message.TYPE_IMAGE) {
Presences presences = message.getConversation().getContact() Contact contact = message.getConversation().getContact();
.getPresences(); Presences presences = contact.getPresences();
if ((message.getPresence() != null) if ((message.getCounterpart() != null)
&& (presences.has(message.getPresence().toString()))) { && (presences.has(message.getCounterpart().getResourcepart()))) {
markMessage(message, Message.STATUS_OFFERED); markMessage(message, Message.STATUS_OFFERED);
mJingleConnectionManager.createNewConnection(message); mJingleConnectionManager.createNewConnection(message);
} else { } else {
if (presences.size() == 1) { if (presences.size() == 1) {
String presence = presences.asStringArray()[0]; String presence = presences.asStringArray()[0];
message.setPresence(presence); try {
message.setCounterpart(Jid.fromParts(contact.getJid().getLocalpart(), contact.getJid().getDomainpart(), presence));
} catch (InvalidJidException e) {
return;
}
markMessage(message, Message.STATUS_OFFERED); markMessage(message, Message.STATUS_OFFERED);
mJingleConnectionManager.createNewConnection(message); mJingleConnectionManager.createNewConnection(message);
} }
@ -1371,7 +1382,12 @@ public class XmppConnectionService extends Service {
for (Message msg : messages) { for (Message msg : messages) {
if ((msg.getStatus() == Message.STATUS_UNSEND || msg.getStatus() == Message.STATUS_WAITING) if ((msg.getStatus() == Message.STATUS_UNSEND || msg.getStatus() == Message.STATUS_WAITING)
&& (msg.getEncryption() == Message.ENCRYPTION_OTR)) { && (msg.getEncryption() == Message.ENCRYPTION_OTR)) {
msg.setPresence(otrSession.getSessionID().getUserID()); SessionID id = otrSession.getSessionID();
try {
msg.setCounterpart(Jid.fromString(id.getAccountID()+"/"+id.getUserID()));
} catch (InvalidJidException e) {
break;
}
if (msg.getType() == Message.TYPE_TEXT) { if (msg.getType() == Message.TYPE_TEXT) {
MessagePacket outPacket = mMessageGenerator MessagePacket outPacket = mMessageGenerator
.generateOtrChat(msg, true); .generateOtrChat(msg, true);
@ -1400,7 +1416,7 @@ public class XmppConnectionService extends Service {
packet.addChild("private", "urn:xmpp:carbons:2"); packet.addChild("private", "urn:xmpp:carbons:2");
packet.addChild("no-copy", "urn:xmpp:hints"); packet.addChild("no-copy", "urn:xmpp:hints");
packet.setAttribute("to", otrSession.getSessionID().getAccountID() + "/" packet.setAttribute("to", otrSession.getSessionID().getAccountID() + "/"
+ otrSession.getSessionID().getUserID()); + otrSession.getSessionID().getUserID());
try { try {
packet.setBody(otrSession packet.setBody(otrSession
.transformSending(CryptoHelper.FILETRANSFER .transformSending(CryptoHelper.FILETRANSFER

View file

@ -190,7 +190,7 @@ public class ConversationFragment extends Fragment {
} }
if (mEditMessage.getText().length() < 1) { if (mEditMessage.getText().length() < 1) {
if (this.conversation.getMode() == Conversation.MODE_MULTI) { if (this.conversation.getMode() == Conversation.MODE_MULTI) {
conversation.setNextPresence(null); conversation.setNextCounterpart(null);
updateChatMsgHint(); updateChatMsgHint();
} }
return; return;
@ -199,10 +199,10 @@ public class ConversationFragment extends Fragment {
.toString(), conversation.getNextEncryption(activity .toString(), conversation.getNextEncryption(activity
.forceEncryption())); .forceEncryption()));
if (conversation.getMode() == Conversation.MODE_MULTI) { if (conversation.getMode() == Conversation.MODE_MULTI) {
if (conversation.getNextPresence() != null) { if (conversation.getNextCounterpart() != null) {
message.setPresence(conversation.getNextPresence()); message.setCounterpart(conversation.getNextCounterpart());
message.setType(Message.TYPE_PRIVATE); message.setType(Message.TYPE_PRIVATE);
conversation.setNextPresence(null); conversation.setNextCounterpart(null);
} }
} }
if (conversation.getNextEncryption(activity.forceEncryption()) == Message.ENCRYPTION_OTR) { if (conversation.getNextEncryption(activity.forceEncryption()) == Message.ENCRYPTION_OTR) {
@ -216,10 +216,10 @@ public class ConversationFragment extends Fragment {
public void updateChatMsgHint() { public void updateChatMsgHint() {
if (conversation.getMode() == Conversation.MODE_MULTI if (conversation.getMode() == Conversation.MODE_MULTI
&& conversation.getNextPresence() != null) { && conversation.getNextCounterpart() != null) {
this.mEditMessage.setHint(getString( this.mEditMessage.setHint(getString(
R.string.send_private_message_to, R.string.send_private_message_to,
conversation.getNextPresence())); conversation.getNextCounterpart().getResourcepart()));
} else { } else {
switch (conversation.getNextEncryption(activity.forceEncryption())) { switch (conversation.getNextEncryption(activity.forceEncryption())) {
case Message.ENCRYPTION_NONE: case Message.ENCRYPTION_NONE:
@ -279,8 +279,8 @@ public class ConversationFragment extends Fragment {
public void onContactPictureClicked(Message message) { public void onContactPictureClicked(Message message) {
if (message.getStatus() <= Message.STATUS_RECEIVED) { if (message.getStatus() <= Message.STATUS_RECEIVED) {
if (message.getConversation().getMode() == Conversation.MODE_MULTI) { if (message.getConversation().getMode() == Conversation.MODE_MULTI) {
if (message.getPresence() != null) { if (message.getCounterpart() != null) {
highlightInConference(message.getPresence().getResourcepart()); highlightInConference(message.getCounterpart().getResourcepart());
} else { } else {
highlightInConference(message highlightInConference(message
.getContact().getDisplayName()); .getContact().getDisplayName());
@ -310,9 +310,7 @@ public class ConversationFragment extends Fragment {
public void onContactPictureLongClicked(Message message) { public void onContactPictureLongClicked(Message message) {
if (message.getStatus() <= Message.STATUS_RECEIVED) { if (message.getStatus() <= Message.STATUS_RECEIVED) {
if (message.getConversation().getMode() == Conversation.MODE_MULTI) { if (message.getConversation().getMode() == Conversation.MODE_MULTI) {
if (message.getPresence() != null) { if (message.getCounterpart() != null) {
privateMessageWith(message.getPresence());
} else {
privateMessageWith(message.getCounterpart()); privateMessageWith(message.getCounterpart());
} }
} }
@ -431,7 +429,7 @@ public class ConversationFragment extends Fragment {
protected void privateMessageWith(final Jid counterpart) { protected void privateMessageWith(final Jid counterpart) {
this.mEditMessage.setText(""); this.mEditMessage.setText("");
this.conversation.setNextPresence(counterpart.toString()); this.conversation.setNextCounterpart(counterpart);
updateChatMsgHint(); updateChatMsgHint();
} }
@ -465,7 +463,7 @@ public class ConversationFragment extends Fragment {
this.activity = (ConversationActivity) getActivity(); this.activity = (ConversationActivity) getActivity();
this.conversation = conversation; this.conversation = conversation;
if (this.conversation.getMode() == Conversation.MODE_MULTI) { if (this.conversation.getMode() == Conversation.MODE_MULTI) {
this.conversation.setNextPresence(null); this.conversation.setNextCounterpart(null);
} }
this.mEditMessage.setText(""); this.mEditMessage.setText("");
this.mEditMessage.append(this.conversation.getNextMessage()); this.mEditMessage.append(this.conversation.getNextMessage());
@ -835,7 +833,7 @@ public class ConversationFragment extends Fragment {
@Override @Override
public void onPresenceSelected() { public void onPresenceSelected() {
message.setPresence(conversation.getNextPresence()); message.setCounterpart(conversation.getNextCounterpart());
xmppService.sendMessage(message); xmppService.sendMessage(message);
messageSent(); messageSent();
} }

View file

@ -65,6 +65,7 @@ import eu.siacs.conversations.services.AvatarService;
import eu.siacs.conversations.services.XmppConnectionService; import eu.siacs.conversations.services.XmppConnectionService;
import eu.siacs.conversations.services.XmppConnectionService.XmppConnectionBinder; import eu.siacs.conversations.services.XmppConnectionService.XmppConnectionBinder;
import eu.siacs.conversations.utils.ExceptionHelper; import eu.siacs.conversations.utils.ExceptionHelper;
import eu.siacs.conversations.xmpp.jid.InvalidJidException;
import eu.siacs.conversations.xmpp.jid.Jid; import eu.siacs.conversations.xmpp.jid.Jid;
public abstract class XmppActivity extends Activity { public abstract class XmppActivity extends Activity {
@ -399,7 +400,7 @@ public abstract class XmppActivity extends Activity {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
conversation.setNextPresence(null); conversation.setNextCounterpart(null);
if (listener != null) { if (listener != null) {
listener.onPresenceSelected(); listener.onPresenceSelected();
} }
@ -450,7 +451,7 @@ public abstract class XmppActivity extends Activity {
public void selectPresence(final Conversation conversation, public void selectPresence(final Conversation conversation,
final OnPresenceSelected listener) { final OnPresenceSelected listener) {
Contact contact = conversation.getContact(); final Contact contact = conversation.getContact();
if (!contact.showInRoster()) { if (!contact.showInRoster()) {
showAddToRosterDialog(conversation); showAddToRosterDialog(conversation);
} else { } else {
@ -464,12 +465,16 @@ public abstract class XmppActivity extends Activity {
|| !contact.getOption(Contact.Options.FROM)) { || !contact.getOption(Contact.Options.FROM)) {
warnMutalPresenceSubscription(conversation, listener); warnMutalPresenceSubscription(conversation, listener);
} else { } else {
conversation.setNextPresence(null); conversation.setNextCounterpart(null);
listener.onPresenceSelected(); listener.onPresenceSelected();
} }
} else if (presences.size() == 1) { } else if (presences.size() == 1) {
String presence = presences.asStringArray()[0]; String presence = presences.asStringArray()[0];
conversation.setNextPresence(presence); try {
conversation.setNextCounterpart(Jid.fromParts(contact.getJid().getLocalpart(),contact.getJid().getDomainpart(),presence));
} catch (InvalidJidException e) {
conversation.setNextCounterpart(null);
}
listener.onPresenceSelected(); listener.onPresenceSelected();
} else { } else {
final StringBuilder presence = new StringBuilder(); final StringBuilder presence = new StringBuilder();
@ -500,7 +505,11 @@ public abstract class XmppActivity extends Activity {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
conversation.setNextPresence(presence.toString()); try {
conversation.setNextCounterpart(Jid.fromParts(contact.getJid().getLocalpart(),contact.getJid().getDomainpart(),presence.toString()));
} catch (InvalidJidException e) {
conversation.setNextCounterpart(null);
}
listener.onPresenceSelected(); listener.onPresenceSelected();
} }
}); });

View file

@ -135,16 +135,10 @@ public class MessageAdapter extends ArrayAdapter<Message> {
Contact contact = message.getContact(); Contact contact = message.getContact();
if (contact != null) { if (contact != null) {
info = contact.getDisplayName(); info = contact.getDisplayName();
} else if (message.getCounterpart() != null) {
info = message.getCounterpart().getResourcepart();
} else { } else {
if (message.getPresence() != null) { info = "";
if (message.getPresence().isBareJid()) {
info = message.getPresence().toString();
} else {
info = message.getPresence().getResourcepart();
}
} else {
info = message.getCounterpart().toString();
}
} }
} }
break; break;
@ -232,11 +226,11 @@ public class MessageAdapter extends ArrayAdapter<Message> {
privateMarker = activity privateMarker = activity
.getString(R.string.private_message); .getString(R.string.private_message);
} else { } else {
final Jid to; final String to;
if (message.getPresence() != null) { if (message.getCounterpart() != null) {
to = message.getPresence(); to = message.getCounterpart().getResourcepart();
} else { } else {
to = message.getCounterpart(); to = "";
} }
privateMarker = activity.getString(R.string.private_message_to, to); privateMarker = activity.getString(R.string.private_message_to, to);
} }
@ -417,9 +411,7 @@ public class MessageAdapter extends ArrayAdapter<Message> {
if (contact != null) { if (contact != null) {
viewHolder.contact_picture.setImageBitmap(activity.avatarService().get(contact, activity.getPixel(48))); viewHolder.contact_picture.setImageBitmap(activity.avatarService().get(contact, activity.getPixel(48)));
} else if (item.getConversation().getMode() == Conversation.MODE_MULTI) { } else if (item.getConversation().getMode() == Conversation.MODE_MULTI) {
final Jid name = item.getPresence() != null ? item.getPresence() : item.getCounterpart(); viewHolder.contact_picture.setImageBitmap(activity.avatarService().get(item.getCounterpart().getResourcepart(),
viewHolder.contact_picture.setImageBitmap(activity.avatarService().get(
name.isBareJid() ? name.toString() : name.getResourcepart(),
activity.getPixel(48))); activity.getPixel(48)));
} }
} else if (type == SENT) { } else if (type == SENT) {

View file

@ -262,7 +262,7 @@ public class JingleConnection implements Downloadable {
this.mStatus = Downloadable.STATUS_OFFER; this.mStatus = Downloadable.STATUS_OFFER;
this.message.setDownloadable(this); this.message.setDownloadable(this);
final Jid from = packet.getFrom(); final Jid from = packet.getFrom();
this.message.setPresence(from.isBareJid() ? "" : from.getResourcepart()); this.message.setCounterpart(from);
this.account = account; this.account = account;
this.initiator = packet.getFrom(); this.initiator = packet.getFrom();
this.responder = this.account.getFullJid(); this.responder = this.account.getFullJid();