Skip to content

Commit

Permalink
igniterealtime#166: Refactor to reduce code duplication
Browse files Browse the repository at this point in the history
guusdk committed Mar 15, 2023

Verified

This commit was signed with the committer’s verified signature.
lucasteles Lucas Teles
1 parent 329ba93 commit ef6eaa0
Showing 4 changed files with 60 additions and 42 deletions.
20 changes: 6 additions & 14 deletions src/java/org/jivesoftware/openfire/archive/ArchiveInterceptor.java
Original file line number Diff line number Diff line change
@@ -89,28 +89,20 @@ public void interceptPacket(Packet packet, Session session, boolean incoming, bo
JID sender = message.getFrom();
JID receiver = message.getTo();
ConversationEventsQueue eventsQueue = conversationManager.getConversationEventsQueue();
if (message.getBody()!=null)
if (message.getBody() != null && !message.getBody().isEmpty())
{
eventsQueue.addChatEvent(conversationManager.getConversationKey(sender, receiver),
ConversationEvent.chatMessageReceived(sender, receiver,
conversationManager.isMessageArchivingEnabled() ? message.getBody() : null,
conversationManager.isMessageArchivingEnabled() ? message.toXML() : null,
new Date()));
}
else
else if (conversationManager.isEmptyMessageArchivingEnabledFor(message))
{
EmptyMessageType emptyMessageType = EmptyMessageType.getMessageType(message.getElement());

long bitmask = conversationManager.getSpecificEmptyMessageArchivingEnabled();

if (emptyMessageType!=EmptyMessageType.IGNORE && (bitmask & emptyMessageType.getValue()) == emptyMessageType.getValue())
{
eventsQueue.addChatEvent(conversationManager.getConversationKey(sender, receiver),
ConversationEvent.getEmptyMessageReceivedEvent(sender, receiver,
emptyMessageType,
conversationManager.isMessageArchivingEnabled() ? message.toXML() : null,
new Date()));
}
eventsQueue.addChatEvent(conversationManager.getConversationKey(sender, receiver),
ConversationEvent.getEmptyMessageReceivedEvent(sender, receiver,
message.toXML(),
new Date()));
}
}
}
Original file line number Diff line number Diff line change
@@ -60,8 +60,6 @@ public class ConversationEvent {

private String nickname;

private EmptyMessageType emptyMessageType;

/**
* Do not use this constructor. It only exists for serialization purposes.
*/
@@ -198,12 +196,11 @@ public int hashCode() {
return Objects.hash(type, date, body, stanza, sender, receiver, roomJID, user, nickname);
}

public static ConversationEvent getEmptyMessageReceivedEvent(JID sender, JID receiver, EmptyMessageType emptyMessageType, String stanza, Date date) {
public static ConversationEvent getEmptyMessageReceivedEvent(JID sender, JID receiver, String stanza, Date date) {
ConversationEvent event = new ConversationEvent();
event.type = Type.chatMessageReceived;
event.sender = sender;
event.receiver = receiver;
event.emptyMessageType = emptyMessageType;
event.date = date;
event.stanza=stanza;
return event;
61 changes: 49 additions & 12 deletions src/java/org/jivesoftware/openfire/archive/ConversationManager.java
Original file line number Diff line number Diff line change
@@ -462,6 +462,54 @@ public boolean isMessageArchivingEnabled() {
return messageArchivingEnabled;
}

/**
* Checks if with the current configuration, message archiving should be enabled for the provided message stanza,
* assuming that it has no body. It does so by evaluating child elements of the stanza for occurrence of elements
* identified in {@link EmptyMessageType}.
*
* This method does not verify if the stanza indeed has no body.
*
* @param message the stanza to evaluate
* @return true if the stanza is eligible for archiving, otherwise false.
*/
public boolean isEmptyMessageArchivingEnabledFor(final Message message)
{
if (!isEmptyMessageArchivingEnabled()) {
return false;
}

final EmptyMessageType emptyMessageType = EmptyMessageType.getMessageType(message.getElement());
if (emptyMessageType!=EmptyMessageType.IGNORE) {
return false;
}
final long bitmask = getSpecificEmptyMessageArchivingEnabled();
return (bitmask & emptyMessageType.getValue()) == emptyMessageType.getValue();
}

/**
* Checks if with the current configuration, message archiving should be enabled for the provided message stanza,
* assuming that it has no body. It does so by evaluating child elements of the stanza for occurrence of elements
* identified in {@link EmptyMessageType}.
*
* This method does not verify if the stanza indeed has no body.
*
* @param message the stanza to evaluate
* @return true if the stanza is eligible for archiving, otherwise false.
*/
public boolean isEmptyMessageArchivingEnabledFor(final String message)
{
if (!isEmptyMessageArchivingEnabled()) {
return false;
}

final EmptyMessageType emptyMessageType = EmptyMessageType.getMessageType(message);
if (emptyMessageType!=EmptyMessageType.IGNORE) {
return false;
}
final long bitmask = getSpecificEmptyMessageArchivingEnabled();
return (bitmask & emptyMessageType.getValue()) == emptyMessageType.getValue();
}

/**
* Sets whether message archiving is enabled. When enabled, all messages in conversations are stored in the database. Note: it's not possible for
* meta-data archiving to be disabled when message archiving is enabled; enabling message archiving automatically enables meta-data archiving.
@@ -864,20 +912,9 @@ else if ((date.getTime() - conversation.getLastActivity().getTime() > idleTime.t
conversationArchiver.archive(conversation);
}
if (messageArchivingEnabled) {
if (body != null) {
if ((body != null && !body.isEmpty()) || isEmptyMessageArchivingEnabledFor(stanza)) {
messageArchiver.archive(new ArchivedMessage(conversation.getConversationID(), sender, receiver, date, body, stanza, false, null) );
}
else
if (isEmptyMessageArchivingEnabled()) {
EmptyMessageType emptyMessageType = EmptyMessageType.getMessageType(stanza);

long bitmask = getSpecificEmptyMessageArchivingEnabled();

if (emptyMessageType!=EmptyMessageType.IGNORE && (bitmask & emptyMessageType.getValue()) == emptyMessageType.getValue())
{
messageArchiver.archive(new ArchivedMessage(conversation.getConversationID(), sender, receiver, date, body, stanza, false, null) );
}
}
}
// Notify listeners of the conversation update.
for (ConversationListener listener : conversationListeners) {
Original file line number Diff line number Diff line change
@@ -154,20 +154,12 @@ public void messageReceived(JID roomJID, JID user, String nickname, Message mess
eventsQueue.addGroupChatEvent(conversationManager.getRoomConversationKey(roomJID),
ConversationEvent.roomMessageReceived(roomJID, user, null, nickname, withBody ? message.getBody() : null, message.toXML(), now));
}
else
else if (conversationManager.isEmptyMessageArchivingEnabledFor(message))
{
EmptyMessageType emptyMessageType = EmptyMessageType.getMessageType(message.getElement());

long bitmask = conversationManager.getSpecificEmptyMessageArchivingForMUCEnabled();

if (emptyMessageType!=EmptyMessageType.IGNORE && (bitmask & emptyMessageType.getValue()) == emptyMessageType.getValue())
{
ConversationEventsQueue eventsQueue = conversationManager.getConversationEventsQueue();
ConversationEventsQueue eventsQueue = conversationManager.getConversationEventsQueue();

eventsQueue.addGroupChatEvent(conversationManager.getRoomConversationKey(roomJID),
ConversationEvent.getEmptyMessageReceivedEvent(roomJID, user, emptyMessageType,
conversationManager.isMessageArchivingEnabled() ? message.toXML() : null, new Date()));
}
eventsQueue.addGroupChatEvent(conversationManager.getRoomConversationKey(roomJID),
ConversationEvent.getEmptyMessageReceivedEvent(roomJID, user, message.toXML(), new Date()));
}
}
}

0 comments on commit ef6eaa0

Please sign in to comment.