Introduce myRoomJid EntityFullJid in MultiUserChat

This commit is contained in:
Florian Schmaus 2018-06-12 11:14:38 +02:00
parent 9161ba9e7d
commit 6ba9218c77
1 changed files with 30 additions and 13 deletions

View File

@ -57,7 +57,7 @@ import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.Message; import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Presence; import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.Stanza; import org.jivesoftware.smack.packet.Stanza;
import org.jivesoftware.smack.util.StringUtils; import org.jivesoftware.smack.util.Objects;
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager; import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
import org.jivesoftware.smackx.disco.packet.DiscoverInfo; import org.jivesoftware.smackx.disco.packet.DiscoverInfo;
@ -149,7 +149,7 @@ public class MultiUserChat {
private final StanzaListener declinesListener; private final StanzaListener declinesListener;
private String subject; private String subject;
private Resourcepart nickname; private EntityFullJid myRoomJid;
private boolean joined = false; private boolean joined = false;
private StanzaCollector messageCollector; private StanzaCollector messageCollector;
@ -208,7 +208,7 @@ public class MultiUserChat {
LOGGER.warning("Presence not from a full JID: " + presence.getFrom()); LOGGER.warning("Presence not from a full JID: " + presence.getFrom());
return; return;
} }
final String myRoomJID = MultiUserChat.this.room + "/" + nickname; final EntityFullJid myRoomJID = myRoomJid;
final boolean isUserStatusModification = presence.getFrom().equals(myRoomJID); final boolean isUserStatusModification = presence.getFrom().equals(myRoomJID);
asyncButOrdered.performAsyncButOrdered(MultiUserChat.this, new Runnable() { asyncButOrdered.performAsyncButOrdered(MultiUserChat.this, new Runnable() {
@ -383,7 +383,9 @@ public class MultiUserChat {
// This presence must be send from a full JID. We use the resourcepart of this JID as nick, since the room may // This presence must be send from a full JID. We use the resourcepart of this JID as nick, since the room may
// performed roomnick rewriting // performed roomnick rewriting
this.nickname = presence.getFrom().asEntityFullJidIfPossible().getResourcepart(); Resourcepart receivedNickname = presence.getFrom().getResourceOrThrow();
setNickname(receivedNickname);
joined = true; joined = true;
// Update the list of joined rooms // Update the list of joined rooms
@ -391,6 +393,10 @@ public class MultiUserChat {
return presence; return presence;
} }
private void setNickname(Resourcepart nickname) {
this.myRoomJid = JidCreate.entityFullFrom(room, nickname);
}
/** /**
* Get a new MUC enter configuration builder. * Get a new MUC enter configuration builder.
* *
@ -486,6 +492,7 @@ public class MultiUserChat {
* @deprecated use {@link #createOrJoin(MucEnterConfiguration)} instead. * @deprecated use {@link #createOrJoin(MucEnterConfiguration)} instead.
*/ */
@Deprecated @Deprecated
// TODO Remove in Smack 4.4
public MucCreateConfigFormHandle createOrJoin(Resourcepart nickname, String password, @SuppressWarnings("deprecation") DiscussionHistory history, long timeout) public MucCreateConfigFormHandle createOrJoin(Resourcepart nickname, String password, @SuppressWarnings("deprecation") DiscussionHistory history, long timeout)
throws NoResponseException, XMPPErrorException, InterruptedException, MucAlreadyJoinedException, NotConnectedException, NotAMucServiceException { throws NoResponseException, XMPPErrorException, InterruptedException, MucAlreadyJoinedException, NotConnectedException, NotAMucServiceException {
MucEnterConfiguration.Builder builder = getEnterConfigurationBuilder(nickname).withPassword( MucEnterConfiguration.Builder builder = getEnterConfigurationBuilder(nickname).withPassword(
@ -684,6 +691,7 @@ public class MultiUserChat {
* @deprecated use {@link #join(MucEnterConfiguration)} instead. * @deprecated use {@link #join(MucEnterConfiguration)} instead.
*/ */
@Deprecated @Deprecated
// TODO Remove in Smack 4.4
public void join( public void join(
Resourcepart nickname, Resourcepart nickname,
String password, String password,
@ -759,7 +767,7 @@ public class MultiUserChat {
// We leave a room by sending a presence packet where the "to" // We leave a room by sending a presence packet where the "to"
// field is in the form "roomName@service/nickname" // field is in the form "roomName@service/nickname"
Presence leavePresence = new Presence(Presence.Type.unavailable); Presence leavePresence = new Presence(Presence.Type.unavailable);
leavePresence.setTo(JidCreate.fullFrom(room, nickname)); leavePresence.setTo(myRoomJid);
connection.sendStanza(leavePresence); connection.sendStanza(leavePresence);
} }
@ -1096,7 +1104,11 @@ public class MultiUserChat {
* @return the nickname currently being used. * @return the nickname currently being used.
*/ */
public Resourcepart getNickname() { public Resourcepart getNickname() {
return nickname; final EntityFullJid myRoomJid = this.myRoomJid;
if (myRoomJid == null) {
return null;
}
return myRoomJid.getResourcepart();
} }
/** /**
@ -1114,7 +1126,7 @@ public class MultiUserChat {
* @throws MucNotJoinedException * @throws MucNotJoinedException
*/ */
public synchronized void changeNickname(Resourcepart nickname) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException, MucNotJoinedException { public synchronized void changeNickname(Resourcepart nickname) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException, MucNotJoinedException {
StringUtils.requireNotNullOrEmpty(nickname, "Nickname must not be null or blank."); Objects.requireNonNull(nickname, "Nickname must not be null or blank.");
// Check that we already have joined the room before attempting to change the // Check that we already have joined the room before attempting to change the
// nickname. // nickname.
if (!joined) { if (!joined) {
@ -1137,7 +1149,8 @@ public class MultiUserChat {
// exception will be thrown // exception will be thrown
response.nextResultOrThrow(); response.nextResultOrThrow();
this.nickname = nickname; // TODO: Shouldn't this handle nickname rewriting by the MUC service?
setNickname(nickname);
} }
/** /**
@ -1152,7 +1165,11 @@ public class MultiUserChat {
* @throws MucNotJoinedException * @throws MucNotJoinedException
*/ */
public void changeAvailabilityStatus(String status, Presence.Mode mode) throws NotConnectedException, InterruptedException, MucNotJoinedException { public void changeAvailabilityStatus(String status, Presence.Mode mode) throws NotConnectedException, InterruptedException, MucNotJoinedException {
StringUtils.requireNotNullOrEmpty(nickname, "Nickname must not be null or blank."); final EntityFullJid myRoomJid = this.myRoomJid;
if (myRoomJid == null) {
throw new MucNotJoinedException(this);
}
// Check that we already have joined the room before attempting to change the // Check that we already have joined the room before attempting to change the
// availability status. // availability status.
if (!joined) { if (!joined) {
@ -1163,7 +1180,7 @@ public class MultiUserChat {
Presence joinPresence = new Presence(Presence.Type.available); Presence joinPresence = new Presence(Presence.Type.available);
joinPresence.setStatus(status); joinPresence.setStatus(status);
joinPresence.setMode(mode); joinPresence.setMode(mode);
joinPresence.setTo(JidCreate.fullFrom(room, nickname)); joinPresence.setTo(myRoomJid);
// Send join packet. // Send join packet.
connection.sendStanza(joinPresence); connection.sendStanza(joinPresence);
@ -2398,7 +2415,7 @@ public class MultiUserChat {
// Reset occupant information. // Reset occupant information.
occupantsMap.clear(); occupantsMap.clear();
nickname = null; myRoomJid = null;
userHasLeft(); userHasLeft();
} }
else { else {
@ -2418,7 +2435,7 @@ public class MultiUserChat {
// Reset occupant information. // Reset occupant information.
occupantsMap.clear(); occupantsMap.clear();
nickname = null; myRoomJid = null;
userHasLeft(); userHasLeft();
} }
} }
@ -2437,7 +2454,7 @@ public class MultiUserChat {
// Reset occupant information. // Reset occupant information.
occupantsMap.clear(); occupantsMap.clear();
nickname = null; myRoomJid = null;
userHasLeft(); userHasLeft();
} }
} }