Search in sources :

Example 1 with UpdateOccupantRequest

use of org.jivesoftware.openfire.muc.cluster.UpdateOccupantRequest in project Openfire by igniterealtime.

the class LocalMUCRoom method changeOccupantAffiliation.

/**
     * Updates all the presences of the given user with the new affiliation and role information. Do
     * nothing if the given jid is not present in the room. If the user has joined the room from
     * several client resources, all his/her occupants' presences will be updated.
     *
     * @param jid the bare jid of the user to update his/her role.
     * @param newAffiliation the new affiliation for the JID.
     * @param newRole the new role for the JID.
     * @return the list of updated presences of all the client resources that the client used to
     *         join the room.
     * @throws NotAllowedException If trying to change the moderator role to an owner or an admin or
     *         if trying to ban an owner or an administrator.
     */
private List<Presence> changeOccupantAffiliation(MUCRole senderRole, JID jid, MUCRole.Affiliation newAffiliation, MUCRole.Role newRole) throws NotAllowedException {
    List<Presence> presences = new ArrayList<>();
    // Get all the roles (i.e. occupants) of this user based on his/her bare JID
    JID bareJID = jid.asBareJID();
    List<MUCRole> roles = occupantsByBareJID.get(bareJID);
    if (roles == null) {
        return presences;
    }
    // Collect all the updated presences of these roles
    for (MUCRole role : roles) {
        // Update the presence with the new affiliation and role
        if (role.isLocal()) {
            role.setAffiliation(newAffiliation);
            role.setRole(newRole);
            // Notify the other cluster nodes to update the occupant
            CacheFactory.doClusterTask(new UpdateOccupant(this, role));
            // Prepare a new presence to be sent to all the room occupants
            presences.add(role.getPresence().createCopy());
        } else {
            // Ask the cluster node hosting the occupant to make the changes. Note that if the change
            // is not allowed a NotAllowedException will be thrown
            Element element = (Element) CacheFactory.doSynchronousClusterTask(new UpdateOccupantRequest(this, role.getNickname(), newAffiliation, newRole), role.getNodeID().toByteArray());
            if (element != null) {
                // Prepare a new presence to be sent to all the room occupants
                presences.add(new Presence(element, true));
            } else {
                throw new NotAllowedException();
            }
        }
    }
    // Answer all the updated presences
    return presences;
}
Also used : UpdateOccupant(org.jivesoftware.openfire.muc.cluster.UpdateOccupant) MUCRole(org.jivesoftware.openfire.muc.MUCRole) GroupJID(org.jivesoftware.openfire.group.GroupJID) JID(org.xmpp.packet.JID) NotAllowedException(org.jivesoftware.openfire.muc.NotAllowedException) Element(org.dom4j.Element) ArrayList(java.util.ArrayList) UpdateOccupantRequest(org.jivesoftware.openfire.muc.cluster.UpdateOccupantRequest) Presence(org.xmpp.packet.Presence) UpdatePresence(org.jivesoftware.openfire.muc.cluster.UpdatePresence)

Example 2 with UpdateOccupantRequest

use of org.jivesoftware.openfire.muc.cluster.UpdateOccupantRequest in project Openfire by igniterealtime.

the class LocalMUCRoom method changeOccupantRole.

/**
     * Updates the presence of the given user with the new role information. Do nothing if the given
     * jid is not present in the room.
     *
     * @param jid the full jid of the user to update his/her role.
     * @param newRole the new role for the JID.
     * @return the updated presence of the user or null if none.
     * @throws NotAllowedException If trying to change the moderator role to an owner or an admin.
     */
private Presence changeOccupantRole(JID jid, MUCRole.Role newRole) throws NotAllowedException {
    // Try looking the role in the bare JID list
    MUCRole role = occupantsByFullJID.get(jid);
    //            }
    if (role != null) {
        if (role.isLocal()) {
            // Update the presence with the new role
            role.setRole(newRole);
            // Notify the other cluster nodes to update the occupant
            CacheFactory.doClusterTask(new UpdateOccupant(this, role));
            // Prepare a new presence to be sent to all the room occupants
            return role.getPresence().createCopy();
        } else {
            // Ask the cluster node hosting the occupant to make the changes. Note that if the change
            // is not allowed a NotAllowedException will be thrown
            Element element = (Element) CacheFactory.doSynchronousClusterTask(new UpdateOccupantRequest(this, role.getNickname(), null, newRole), role.getNodeID().toByteArray());
            if (element != null) {
                // Prepare a new presence to be sent to all the room occupants
                return new Presence(element, true);
            } else {
                throw new NotAllowedException();
            }
        }
    }
    return null;
}
Also used : UpdateOccupant(org.jivesoftware.openfire.muc.cluster.UpdateOccupant) MUCRole(org.jivesoftware.openfire.muc.MUCRole) NotAllowedException(org.jivesoftware.openfire.muc.NotAllowedException) Element(org.dom4j.Element) UpdateOccupantRequest(org.jivesoftware.openfire.muc.cluster.UpdateOccupantRequest) Presence(org.xmpp.packet.Presence) UpdatePresence(org.jivesoftware.openfire.muc.cluster.UpdatePresence)

Aggregations

Element (org.dom4j.Element)2 MUCRole (org.jivesoftware.openfire.muc.MUCRole)2 NotAllowedException (org.jivesoftware.openfire.muc.NotAllowedException)2 UpdateOccupant (org.jivesoftware.openfire.muc.cluster.UpdateOccupant)2 UpdateOccupantRequest (org.jivesoftware.openfire.muc.cluster.UpdateOccupantRequest)2 UpdatePresence (org.jivesoftware.openfire.muc.cluster.UpdatePresence)2 Presence (org.xmpp.packet.Presence)2 ArrayList (java.util.ArrayList)1 GroupJID (org.jivesoftware.openfire.group.GroupJID)1 JID (org.xmpp.packet.JID)1