Commit 3194d9e5 authored by Gaston Dombiak's avatar Gaston Dombiak Committed by gaston

Room history is now rebuilt from the conversation log. JM-40


git-svn-id: http://svn.igniterealtime.org/svn/repos/messenger/trunk@562 b35dd754-fafc-0310-a699-88a17e54d16e
parent 68a93783
......@@ -19,6 +19,8 @@ import java.util.TimeZone;
import org.jivesoftware.messenger.Message;
import org.jivesoftware.messenger.MetaDataFragment;
import org.jivesoftware.messenger.XMPPAddress;
import org.jivesoftware.messenger.spi.MessageImpl;
import org.jivesoftware.messenger.user.UserNotFoundException;
/**
......@@ -125,4 +127,59 @@ public final class MUCRoomHistory {
public ListIterator getReverseMessageHistory() {
return historyStrategy.getReverseMessageHistory();
}
/**
* Creates a new message and adds it to the history. The new message will be created based on
* the provided information. This information will likely come from the database when loading
* the room history from the database.
*
* @param senderJID the sender's JID of the message to add to the history.
* @param nickname the sender's nickname of the message to add to the history.
* @param sentDate the date when the message was sent to the room.
* @param subject the subject included in the message.
* @param body the body of the message.
*/
public void addOldMessage(String senderJID, String nickname, Date sentDate, String subject,
String body) {
Message packetToAdd = new MessageImpl();
packetToAdd.setType(Message.GROUP_CHAT);
packetToAdd.setSubject(subject);
packetToAdd.setBody(body);
// Set the sender of the message
if (nickname != null && nickname.trim().length() > 0) {
XMPPAddress roomJID = room.getRole().getRoleAddress();
// Recreate the sender address based on the nickname and room's JID
packetToAdd.setSender(new XMPPAddress(roomJID.getNamePrep(), roomJID.getHostPrep(),
nickname));
}
else {
// Set the room as the sender of the message
packetToAdd.setSender(room.getRole().getRoleAddress());
}
// Add the delay information to the message
MetaDataFragment delayInformation = new MetaDataFragment("jabber:x:delay", "x");
delayInformation.setProperty("x:stamp", UTC_FORMAT.format(sentDate));
if (room.canAnyoneDiscoverJID()) {
// Set the Full JID as the "from" attribute
delayInformation.setProperty("x:from", senderJID);
}
else {
// Set the Room JID as the "from" attribute
delayInformation.setProperty("x:from", room.getRole().getRoleAddress().toStringPrep());
}
packetToAdd.addFragment(delayInformation);
historyStrategy.addMessage(packetToAdd);
}
/**
* Returns true if there is a message within the history of the room that has changed the
* room's subject.
*
* @return true if there is a message within the history of the room that has changed the
* room's subject.
*/
public boolean hasChangedSubject() {
return historyStrategy.hasChangedSubject();
}
}
\ No newline at end of file
......@@ -46,9 +46,12 @@ public class MUCPersistenceManager {
"password, canDiscoverJID, logEnabled, subject, rolesToBroadcast " +
"FROM mucRoom WHERE name=?";
private static final String LOAD_AFFILIATIONS =
"SELECT jid,affiliation FROM mucAffiliation WHERE roomID=?";
"SELECT jid, affiliation FROM mucAffiliation WHERE roomID=?";
private static final String LOAD_MEMBERS =
"SELECT jid, nickname FROM mucMember WHERE roomID=?";
private static final String LOAD_HISTORY =
"SELECT sender, nickname, time, subject, body FROM mucConversationLog " +
"WHERE time>? AND roomID=? AND (nickname != \"\" OR subject IS NOT NULL) ORDER BY time";
private static final String LOAD_ALL_ROOMS =
"SELECT roomID, creationDate, modificationDate, name, naturalName, description, " +
"canChangeSubject, maxUsers, publicRoom, moderated, invitationRequired, canInvite, " +
......@@ -58,10 +61,13 @@ public class MUCPersistenceManager {
"SELECT roomID,jid,affiliation FROM mucAffiliation";
private static final String LOAD_ALL_MEMBERS =
"SELECT roomID,jid, nickname FROM mucMember";
private static final String LOAD_ALL_HISTORY =
"SELECT roomID, sender, nickname, time, subject, body FROM mucConversationLog " +
"WHERE time>? AND (nickname != \"\" OR subject IS NOT NULL) ORDER BY time";
private static final String UPDATE_ROOM =
"UPDATE mucRoom SET modificationDate=?, naturalName=?, description=?, " +
"canChangeSubject=?, maxUsers=?, publicRoom=?, moderated=?, invitationRequired=?, " +
"canInvite=?, password=?, canDiscoverJID=?, logEnabled=?, rolesToBroadcast=?, " +
"canInvite=?, password=?, canDiscoverJID=?, logEnabled=?, rolesToBroadcast=? " +
"WHERE roomID=?";
private static final String ADD_ROOM =
"INSERT INTO mucRoom (roomID, creationDate, modificationDate, name, naturalName, " +
......@@ -175,6 +181,32 @@ public class MUCPersistenceManager {
rs.close();
pstmt.close();
pstmt = con.prepareStatement(LOAD_HISTORY);
// Recreate the history until two days ago
long from = System.currentTimeMillis() - (86400000 * 2);
pstmt.setString(1, StringUtils.dateToMillis(new Date(from)));
pstmt.setLong(2, room.getID());
rs = pstmt.executeQuery();
while (rs.next()) {
Date sentDate = new Date(Long.parseLong(rs.getString(3).trim()));
// Recreate the history only for the rooms that have the conversation logging
// enabled
if (room.isLogEnabled()) {
room.getRoomHistory().addOldMessage(rs.getString(1), rs.getString(2), sentDate,
rs.getString(4), rs.getString(5));
}
}
rs.close();
pstmt.close();
// If the room does not include the last subject in the history then recreate one if
// possible
if (!room.getRoomHistory().hasChangedSubject() && room.getSubject() != null &&
room.getSubject().length() > 0) {
room.getRoomHistory().addOldMessage(room.getRole().getRoleAddress().toStringPrep(),
null, room.getModificationDate(), room.getSubject(), null);
}
pstmt = con.prepareStatement(LOAD_AFFILIATIONS);
pstmt.setLong(1, room.getID());
rs = pstmt.executeQuery();
......@@ -387,6 +419,42 @@ public class MUCPersistenceManager {
rs.close();
pstmt.close();
pstmt = con.prepareStatement(LOAD_ALL_HISTORY);
// Recreate the history until two days ago
long from = System.currentTimeMillis() - (86400000 * 2);
pstmt.setString(1, StringUtils.dateToMillis(new Date(from)));
// Load the rooms conversations from the last two days
rs = pstmt.executeQuery();
while (rs.next()) {
room = rooms.get(rs.getLong(1));
Date sentDate = new Date(Long.parseLong(rs.getString(4).trim()));
try {
// Recreate the history only for the rooms that have the conversation logging
// enabled
if (room.isLogEnabled()) {
room.getRoomHistory().addOldMessage(rs.getString(2), rs.getString(3),
sentDate, rs.getString(5), rs.getString(6));
}
}
catch (Exception e) {
Log.error(e);
}
}
rs.close();
pstmt.close();
// Add the last known room subject to the room history only for those rooms that still
// don't have in their histories the last room subject
for (MUCRoom loadedRoom : rooms.values()) {
if (!loadedRoom.getRoomHistory().hasChangedSubject() &&
loadedRoom.getSubject() != null &&
loadedRoom.getSubject().length() > 0) {
loadedRoom.getRoomHistory().addOldMessage(loadedRoom.getRole().getRoleAddress()
.toStringPrep(), null,
loadedRoom.getModificationDate(), loadedRoom.getSubject(), null);
}
}
pstmt = con.prepareStatement(LOAD_ALL_AFFILIATIONS);
rs = pstmt.executeQuery();
while (rs.next()) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment