Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Issue OF-1933 Step 1 #1538

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1510,7 +1510,7 @@ public void addFirstOwner(JID bareJID) {

@Override
public List<Presence> addOwner(JID jid, MUCRole sendRole) throws ForbiddenException {

final JID bareJID = jid.asBareJID();
lock.writeLock().lock();
try {
Expand Down Expand Up @@ -1600,7 +1600,7 @@ else if (removeOutcast(bareJID)) {
}
// Update other cluster nodes with new affiliation
CacheFactory.doClusterTask(new AddAffiliation(this, jid.toBareJID(), MUCRole.Affiliation.admin));

// apply the affiliation change, assigning a new affiliation
// based on the group(s) of the affected user(s)
return applyAffiliationChange(getRole(), bareJID, null);
Expand Down Expand Up @@ -1675,7 +1675,7 @@ else if (removeOutcast(bareJID)) {
}
// Update other cluster nodes with new member
CacheFactory.doClusterTask(new AddMember(this, jid.toBareJID(), (nickname == null ? "" : nickname)));

// apply the affiliation change, assigning a new affiliation
// based on the group(s) of the affected user(s)
return applyAffiliationChange(getRole(), bareJID, null);
Expand Down Expand Up @@ -1731,7 +1731,7 @@ else if (removeMember(bareJID)) {
}
// Update other cluster nodes with new affiliation
CacheFactory.doClusterTask(new AddAffiliation(this, jid.toBareJID(), MUCRole.Affiliation.outcast));

// apply the affiliation change, assigning a new affiliation
// based on the group(s) of the affected user(s)
return applyAffiliationChange(senderRole, bareJID, reason);
Expand All @@ -1743,7 +1743,7 @@ private boolean removeOutcast(JID bareJID) {

@Override
public List<Presence> addNone(JID jid, MUCRole senderRole) throws ForbiddenException, ConflictException {

final JID bareJID = jid.asBareJID();
MUCRole.Affiliation oldAffiliation = MUCRole.Affiliation.none;
boolean jidWasAffiliated = false;
Expand Down Expand Up @@ -1781,7 +1781,7 @@ else if (removeOutcast(bareJID)) {
}
// Update other cluster nodes with new affiliation
CacheFactory.doClusterTask(new AddAffiliation(this, jid.toBareJID(), MUCRole.Affiliation.none));

if (jidWasAffiliated) {
// apply the affiliation change, assigning a new affiliation
// based on the group(s) of the affected user(s)
Expand All @@ -1800,17 +1800,17 @@ else if (removeOutcast(bareJID)) {
* applied to each presence corresponding to that user. If the given JID is a group,
* each user in the group is evaluated to determine what their new affiliations will
* be. The returned presence updates will be broadcast to the occupants of the room.
*
*
* @param senderRole Typically the room itself, or an owner/admin
* @param affiliationJID The JID for the user or group that has been changed
* @param reason An optional reason to explain why a user was kicked from the room
* @return List of presence updates to be delivered to the room's occupants
*/
private List<Presence> applyAffiliationChange(MUCRole senderRole, final JID affiliationJID, String reason) {

// Update the presence(s) for the new affiliation and inform all occupants
List<JID> affectedOccupants = new ArrayList<>();

// first, determine which actual (user) JIDs are affected by the affiliation change
if (GroupJID.isGroup(affiliationJID)) {
try {
Expand All @@ -1830,7 +1830,7 @@ private List<Presence> applyAffiliationChange(MUCRole senderRole, final JID affi
affectedOccupants.add(affiliationJID);
}
}

// now update each of the affected occupants with a new role/affiliation
MUCRole.Role newRole;
MUCRole.Affiliation newAffiliation;
Expand Down Expand Up @@ -2682,7 +2682,7 @@ public void saveToDB() {
// Set that the room is now in the DB
savedToDB = true;
// Notify other cluster nodes that the room is now in DB
CacheFactory.doClusterTask(new RoomUpdatedEvent(this));
CacheFactory.doClusterTask(new RoomUpdatedEvent(this));
// Save the existing room owners to the DB
for (JID owner : owners) {
MUCPersistenceManager.saveAffiliationToDB(
Expand Down Expand Up @@ -2894,7 +2894,7 @@ public boolean equals(Object obj) {
return false;
return roomID==other.roomID;
}

// overrides for important Group events

@Override
Expand Down Expand Up @@ -2933,35 +2933,56 @@ public void groupModified(Group group, Map params) {

@Override
public void memberAdded(Group group, Map params) {
applyAffiliationChangeAndSendPresence(new JID((String)params.get("member")));
applyAffiliationChangeAndSendPresence(new JID((String)params.get("member")), "member");
}

@Override
public void memberRemoved(Group group, Map params) {
applyAffiliationChangeAndSendPresence(new JID((String)params.get("member")));
applyAffiliationChangeAndSendPresence(new JID((String)params.get("member")), "none");
}

@Override
public void adminAdded(Group group, Map params) {
applyAffiliationChangeAndSendPresence(new JID((String)params.get("admin")));
applyAffiliationChangeAndSendPresence(new JID((String)params.get("admin")), "member");
}

@Override
public void adminRemoved(Group group, Map params) {
applyAffiliationChangeAndSendPresence(new JID((String)params.get("admin")));
applyAffiliationChangeAndSendPresence(new JID((String)params.get("admin")), "none");
}
private void applyAffiliationChangeAndSendPresence(JID groupMember) {

private void applyAffiliationChangeAndSendPresence(JID groupMember, String affiliation) {
List<Presence> presences = applyAffiliationChange(getRole(), groupMember, null);
for (Presence presence : presences) {
send(presence);

if (presences.size() == 0 && isMembersOnly()) {
sendOutOfRoomAffiliationChangeNotification(groupMember, affiliation);
}
else { // member is in MUC, send presence stanzas
for (Presence presence : presences) {
send(presence);
}
}
}

private void sendOutOfRoomAffiliationChangeNotification(JID jid, String affiliation) {
// Announce affiliation change for a user that is NOT currently in the room,
// XEP-0045 (v1.31.2) example 195

Message message = new Message();
Element fragment = message.addChildElement("x", "http://jabber.org/protocol/muc#user");
Element item = fragment.addElement("item");
item.addAttribute("affiliation", affiliation);
item.addAttribute("role", "none");
deleolajide marked this conversation as resolved.
Show resolved Hide resolved
item.addAttribute("jid", jid.toBareJID());
message.setFrom(getRole().getRoleAddress());

broadcast(message);
}

@Override
public void groupCreated(Group group, Map params) {
// ignore
}


}