Skip to content

Instantly share code, notes, and snippets.

@antonycourtney
Created October 15, 2015 01:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antonycourtney/35ee89986347cc8ef63b to your computer and use it in GitHub Desktop.
Save antonycourtney/35ee89986347cc8ef63b to your computer and use it in GitHub Desktop.
oneref-chat message Thread
/**
* Contents of a single conversation (message thread) sharing a common thread ID
*
* We use a SortedMap and add messages in chronological order
*/
export default class Thread extends Immutable.Record({
messageIdMap: Immutable.OrderedMap() // map from id to Message, ordered by date
}) {
/*
* add or update the message by its message id
*
* pre: (!this.lastMessage) ||
* (message.date > this.lastMessage.date &&
* message.threadID === this.lastMessage.threadID)
*/
addMessage(message) {
const nextIdMap = this.messageIdMap.set(message.id,message);
return this.set('messageIdMap',nextIdMap);
}
/**
* mark this thread as read (by marking last message as read)
*/
markRead() {
const lastMessage = this.lastMessage;
if (!lastMessage)
return this;
const updMessage = lastMessage.set('isRead',true);
return this.addMessage(updMessage);
}
getMessages() {
return this.messageIdMap.toIndexedSeq().toArray();
}
get lastMessage() {
return this.messageIdMap.last();
}
get firstMessage() {
return this.messageIdMap.first();
}
get name() {
return this.firstMessage.threadName;
}
get id() {
return this.firstMessage.threadID;
}
get isRead() {
return this.lastMessage.isRead;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment