Skip to content

Commit

Permalink
SPARK-2334: Announce avatar when joining a room
Browse files Browse the repository at this point in the history
By announcing an avatar in the presence stanza that is used to join a MUC room, other occupants can see the avatar of the user that's joining (if their client support showing avatars).
  • Loading branch information
guusdk committed Dec 27, 2023
1 parent 6a2b421 commit 95e3fa2
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -170,47 +170,6 @@ public static void joinConferenceRoom(final String roomName, EntityBareJid roomJ
changePresenceToAvailableIfInvisible();
}


/**
* Joins a chat room without using the UI.
*
* @param groupChat the <code>MultiUserChat</code>
* @param nickname the nickname of the user.
* @param password the password to join the room with.
* @return a List of errors, if any.
*/
public static List<String> joinRoom(MultiUserChat groupChat, Resourcepart nickname, String password) {
final List<String> errors = new ArrayList<>();
if ( !groupChat.isJoined() )
{
try
{
if ( ModelUtil.hasLength( password ) )
{
groupChat.join( nickname, password );
}
else
{
groupChat.join( nickname );
}
changePresenceToAvailableIfInvisible();
}
catch ( XMPPException | SmackException | InterruptedException ex )
{
StanzaError error = null;
if ( ex instanceof XMPPException.XMPPErrorException )
{
error = ( (XMPPException.XMPPErrorException) ex ).getStanzaError();
}

final String errorText = ConferenceUtils.getReason( error );
errors.add( errorText );
}
}

return errors;
}

/**
* Invites users to an existing room.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.PresenceBuilder;
import org.jivesoftware.smack.packet.StandardExtensionElement;
import org.jivesoftware.smack.packet.StanzaError;
import org.jivesoftware.smack.util.Consumer;
import org.jivesoftware.smackx.muc.MucEnterConfiguration;
import org.jivesoftware.smackx.muc.MultiUserChat;
import org.jivesoftware.smackx.muc.MultiUserChatManager;
import org.jivesoftware.spark.SparkManager;
Expand All @@ -17,6 +22,7 @@
import org.jivesoftware.spark.util.SwingWorker;
import org.jivesoftware.spark.util.UIComponentRegistry;
import org.jivesoftware.spark.util.log.Log;
import org.jivesoftware.sparkimpl.profile.VCardManager;
import org.jivesoftware.sparkimpl.settings.local.SettingsManager;
import org.jxmpp.jid.EntityBareJid;
import org.jxmpp.jid.parts.Resourcepart;
Expand Down Expand Up @@ -153,14 +159,7 @@ public Object construct()

if ( !groupChat.isJoined() ) {
Log.debug("Start server-sided join of chat room " + roomJID);
if ( ModelUtil.hasLength( password ) )
{
groupChat.join( nickname, password );
}
else
{
groupChat.join( nickname );
}
groupChat.join(getMucEnterConfiguration(groupChat, nickname, password));
Log.debug("Joined chat room " + roomJID + " on the server.");
}

Expand Down Expand Up @@ -212,6 +211,36 @@ public Object construct()
}
}

public static MucEnterConfiguration getMucEnterConfiguration(final MultiUserChat groupChat, final Resourcepart nickname) {
return getMucEnterConfiguration(groupChat, nickname, null);
}

public static MucEnterConfiguration getMucEnterConfiguration(final MultiUserChat groupChat, final Resourcepart nickname, final String password)
{
final MucEnterConfiguration.Builder builder = groupChat.getEnterConfigurationBuilder(nickname);
if ( ModelUtil.hasLength( password ) )
{
builder.withPassword(password);
}

if (SparkManager.getVCardManager().getVCard() != null && SparkManager.getVCardManager().getVCard().getAvatarHash() != null) {
final String hash = SparkManager.getVCardManager().getVCard().getAvatarHash();
builder.withPresence( presenceBuilder -> presenceBuilder
.addExtension(
StandardExtensionElement.builder("x", "vcard-temp:x:update")
.addElement("photo", hash)
.build()
)
.addExtension(
StandardExtensionElement.builder("x", "jabber:x:avatar")
.addElement("hash", hash)
.build()
)
);
}
return builder.build();
}

@Override
public void finished()
{
Expand Down

0 comments on commit 95e3fa2

Please sign in to comment.