Add a source item in the chat history. This allows us to determine from which speak object this chat originated from.

This commit is contained in:
Sylvain Royer
2014-03-21 13:01:46 -07:00
parent 43d7e5b726
commit b9a3efc0b7
7 changed files with 47 additions and 20 deletions
@@ -21,6 +21,8 @@
package com.threerings.crowd.chat.data;
import com.threerings.presents.dobj.DObject;
import com.threerings.util.Name;
/**
@@ -33,12 +35,17 @@ public interface SpeakObject
public static interface ListenerOp
{
/** Call this method if you only have access to body oids. */
void apply (int bodyOid);
void apply (SpeakObject sender, int bodyOid);
/** Call this method if you can provide usernames directly. */
void apply (Name username);
void apply (SpeakObject sender, Name username);
}
/**
* Returns an identifier for what type of chat this speak object represents.
*/
String getChatIdentifier ();
/**
* The speak service will call this every time a chat message is delivered on this speak object
* to note the listeners that received the message.
@@ -334,7 +334,7 @@ public abstract class ChatChannelManager
for (int bodyId : bodyIds) {
BodyObject bobj = getBodyObject(bodyId);
if (bobj != null && shouldDeliverSpeak(channel, message, bobj)) {
_chatHistory.record(channel, message, bobj.getVisibleName());
_chatHistory.record(channel, bobj.getChatIdentifier(), message, bobj.getVisibleName());
bobj.postMessage(ChatCodes.CHAT_CHANNEL_NOTIFICATION, channel, message);
}
}
@@ -67,9 +67,13 @@ public class ChatHistory
/** The message sent. */
public final ChatMessage message;
public Entry (ChatChannel channel, ChatMessage message) {
/** The source of the sent message. */
public final String source;
public Entry (ChatChannel channel, String source, ChatMessage message) {
this.channel = channel;
this.message = message;
this.source = source;
}
public void writeObject (ObjectOutputStream out) throws IOException {
@@ -91,8 +95,8 @@ public class ChatHistory
public ChatHistory ()
{
SpeakUtil.registerMessageObserver(new SpeakUtil.MessageObserver() {
public void messageDelivered (Name hearer, UserMessage message) {
record(null, message, hearer);
public void messageDelivered (String source, Name hearer, UserMessage message) {
record(null, source, message, hearer);
}
});
}
@@ -136,20 +140,21 @@ public class ChatHistory
* Records the specified channel and message to the specified users' chat histories. If {@link
* ChatMessage#timestamp} is not already filled in, it will be.
*/
public void record (ChatChannel channel, UserMessage msg, Name ...usernames)
public void record (ChatChannel channel, String source, UserMessage msg, Name ...usernames)
{
// fill in the message's time stamp if necessary
if (msg.timestamp == 0L) {
msg.timestamp = System.currentTimeMillis();
}
Entry entry = new Entry(channel, msg);
Entry entry = new Entry(channel, source, msg);
for (Name username : usernames) {
// add the message to this user's chat history
List<Entry> history = getList(username);
if (history == null) {
continue;
}
history.add(entry);
// if the history is big enough, potentially prune it (we always prune when asked for
@@ -263,7 +263,7 @@ public class ChatProvider
SpeakUtil.sendMessage(target, message);
// note that the teller "heard" what they said
SpeakUtil.noteMessage(message.speaker, message);
SpeakUtil.noteMessage(target, message.speaker, message);
}
/**
@@ -51,7 +51,7 @@ public class SpeakUtil
/**
* Called for each player that hears a particular chat message.
*/
void messageDelivered (Name hearer, UserMessage message);
void messageDelivered (String source, Name hearer, UserMessage message);
}
/**
@@ -196,7 +196,7 @@ public class SpeakUtil
* Notes that the specified user was privy to the specified message. If {@link
* ChatMessage#timestamp} is not already filled in, it will be.
*/
protected static void noteMessage (Name username, UserMessage msg)
protected static void noteMessage (SpeakObject sender, Name username, UserMessage msg)
{
// fill in the message's time stamp if necessary
if (msg.timestamp == 0L) {
@@ -206,7 +206,7 @@ public class SpeakUtil
// Log.info("Noted that " + username + " heard " + msg + ".");
// notify any message observers
_messageOp.init(username, msg);
_messageOp.init(sender, username, msg);
_messageObs.apply(_messageOp);
}
@@ -224,15 +224,15 @@ public class SpeakUtil
public RootDObjectManager omgr;
public UserMessage message;
public void apply (int bodyOid) {
public void apply (SpeakObject sender, int bodyOid) {
DObject dobj = omgr.getObject(bodyOid);
if (dobj != null && dobj instanceof BodyObject) {
noteMessage(((BodyObject)dobj).getVisibleName(), message);
noteMessage(sender, ((BodyObject)dobj).getVisibleName(), message);
}
}
public void apply (Name username) {
noteMessage(username, message);
public void apply (SpeakObject sender, Name username) {
noteMessage(sender, username, message);
}
}
@@ -240,16 +240,18 @@ public class SpeakUtil
protected static class MessageObserverOp
implements ObserverList.ObserverOp<MessageObserver>
{
public void init (Name hearer, UserMessage message) {
public void init (SpeakObject sender, Name hearer, UserMessage message) {
_hearer = hearer;
_message = message;
_sender = sender;
}
public boolean apply (MessageObserver observer) {
observer.messageDelivered(_hearer, _message);
observer.messageDelivered(_sender.getChatIdentifier(), _hearer, _message);
return true;
}
protected SpeakObject _sender;
protected Name _hearer;
protected UserMessage _message;
}
@@ -127,7 +127,13 @@ public class BodyObject extends ClientObject
// from interface SpeakObject
public void applyToListeners (ListenerOp op)
{
op.apply(getVisibleName());
op.apply(this, getVisibleName());
}
// from interface SpeakObject
public String getChatIdentifier ()
{
return "default";
}
/**
@@ -30,6 +30,7 @@ import com.threerings.presents.dobj.DSet;
import com.threerings.presents.dobj.OidList;
import com.threerings.presents.dobj.ServerMessageEvent;
import com.threerings.crowd.chat.data.ChatCodes;
import com.threerings.crowd.chat.data.SpeakMarshaller;
import com.threerings.crowd.chat.data.SpeakObject;
@@ -149,10 +150,16 @@ public class PlaceObject extends DObject
public void applyToListeners (ListenerOp op)
{
for (int ii = 0, ll = occupants.size(); ii < ll; ii++) {
op.apply(occupants.get(ii));
op.apply(this, occupants.get(ii));
}
}
// documentation inherited
public String getChatIdentifier ()
{
return "default";
}
// AUTO-GENERATED: METHODS START
/**
* Requests that <code>oid</code> be added to the <code>occupants</code>