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

OF-2918 Clear MUC Chat History #2627

Open
wants to merge 24 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
e8ac57a
OF-2918: Add option to clear history for a given MUC
Nov 29, 2024
2df54ce
OF-2918: Add option to clear history for a given MUC
Nov 29, 2024
4afca51
OF-2918: Add option to clear history for a given MUC
Nov 29, 2024
7bd86d3
OF-2918: Add option to clear history for a given MUC
Nov 29, 2024
196e6ee
OF-2918: Add option to clear history for a given MUC
Nov 29, 2024
f8343f6
OF-2918: Add option to clear history for a given MUC
Nov 29, 2024
5f3573a
OF-2918: Add option to clear history for a given MUC
Dec 3, 2024
156f9ce
OF-2918: Add option to clear history for a given MUC
Dec 3, 2024
01af2dd
OF-2918: Add option to clear history for a given MUC
Dec 3, 2024
d5a9754
OF-2918: Add option to clear history for a given MUC
Dec 6, 2024
7be30bd
OF-2918: Add option to clear history for a given MUC
Dec 6, 2024
94f083d
OF-2918: Add option to clear history for a given MUC
Dec 6, 2024
4e83456
OF-2918: Add option to clear history for a given MUC
Dec 6, 2024
4ed359c
OF-2918: Add option to clear history for a given MUC
Dec 6, 2024
5d673d2
#369 and #370 Clear Chat Room History
Dec 11, 2024
4ae3b94
#369 and #370 Clear Chat Room History
Dec 11, 2024
82fc000
#369 and #370 Clear Chat Room History
Dec 11, 2024
b7f21ae
#369 and #370 Clear Chat Room History
Dec 11, 2024
c03e7e0
#369 and #370 Clear Chat Room History
Dec 11, 2024
db1bc7d
OF-2918: Add option to clear history for a given MUC
Dec 12, 2024
2e8dabf
OF-2918: Add option to clear history for a given MUC
Dec 16, 2024
9de6408
OF-2918: Add option to clear history for a given MUC
Dec 16, 2024
79f01f0
OF-2918: Add option to clear history for a given MUC
Dec 16, 2024
2c0f292
OF-2918: Add option to clear history for a given MUC
Dec 16, 2024
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 @@ -1308,6 +1308,48 @@ public void removeOccupant(@Nonnull final MUCOccupant occupant) {
MUCEventDispatcher.occupantLeft(occupant.getOccupantJID(), occupant.getUserAddress(), occupant.getNickname());
}

/**
* Clears the chat history of a room. Sends out message retraction stanzas to all occupants, for all the message in the chat history.
guusdk marked this conversation as resolved.
Show resolved Hide resolved
*/
public void clearChatHistory() {
Iterator<Message> history = roomHistory.getMessageHistory();
guusdk marked this conversation as resolved.
Show resolved Hide resolved
while (history.hasNext()) {
Message originalMsg = history.next();
Message retractionMsg = new Message();

// Sets a unique ID for the retraction message by prefixing the original message ID with "retract-"
retractionMsg.setID("retract-" + originalMsg.getID());
// Sets the sender of the retraction message to be the chat room itself
retractionMsg.setFrom(selfOccupantData.getOccupantJID());
// Sets the recipient of the retraction message to be the chat room itself to send to all occupants
retractionMsg.setTo(new JID(getName(), getMUCService().getServiceDomain(), null).toBareJID());
retractionMsg.setType(Message.Type.groupchat);
// An XML element is added to the message to indicate that it is a retraction, with an attribute specifying
// the ID of the message being retracted
retractionMsg.addChildElement("retract", "urn:xmpp:message-retract:1").addAttribute(
"id",
new JID(getName(), getMUCService().getServiceDomain(), null).toBareJID()
);
// A fallback element is added to provide a fallback message for clients that do not support message retraction
retractionMsg.addChildElement("fallback", "urn:xmpp:fallback:0").addAttribute(
"for",
"urn:xmpp:message-retract:1"
);
retractionMsg.setBody("A request was received to retract a previous message as part of clearing the chat room history, but this is not supported by your client.");
guusdk marked this conversation as resolved.
Show resolved Hide resolved
// Finally, a hint is added to the message to indicate that it should be stored by the client.
// This ensures that the retraction event is recorded and can be referenced later.
retractionMsg.addChildElement("store", "urn:xmpp:hints");

// Broadcast the retraction message but don't store it in the history
broadcast(retractionMsg, false);
}

// Clear the history of the room from the DB if the room was persistent
MUCPersistenceManager.clearRoomChatFromDB(this);
// Remove the history of the room from memory (preventing it to pop up in a new room by the same name).
roomHistory.purge();
}

/**
* Destroys the room. Each occupant will be removed and will receive a presence stanza of type
* "unavailable" whose "from" attribute will be the occupant's nickname that the user knows he
Expand Down Expand Up @@ -1756,6 +1798,11 @@ private void broadcast(@Nonnull final Message message, @Nonnull final MUCOccupan
* @param message The message stanza
*/
public void broadcast(@Nonnull final Message message)
{
broadcast(message, true);
}

private void broadcast(@Nonnull final Message message, final boolean storeMsgInRoomHistory)
{
Log.debug("Broadcasting message in room {} for occupant {}", this.getName(), message.getFrom() );

Expand All @@ -1768,7 +1815,7 @@ public void broadcast(@Nonnull final Message message)
}

// Add message to the room history
roomHistory.addMessage(message);
if (storeMsgInRoomHistory) roomHistory.addMessage(message);
// Send message to occupants connected to this JVM

// Create a defensive copy of the message that will be broadcast, as the broadcast will modify it ('to' addresses
Expand All @@ -1789,7 +1836,7 @@ public void broadcast(@Nonnull final Message message)
Log.warn("An unexpected exception prevented a message from {} to be broadcast to {}.", message.getFrom(), occupant.getUserAddress(), e);
}
}
if (isLogEnabled()) {
if (isLogEnabled() && storeMsgInRoomHistory) {
JID senderAddress = getSelfRepresentation().getOccupantJID(); // default to the room being the sender of the message.

// convert the MUC nickname/role JID back into a real user JID
Expand Down