class Post { constructor(info){ if(!info || !info.userid) throw "Can not construct empty posts" this.userid = info.userid; this.content = info.content; this.createdAt = info.createdAt || new Date(); this.reactions = info.reactions || {}; this.comments = info.comments || []; //This should record edits this.contentHistory = info.contentHistory || []; // Any reaction or comment updates this ts, // this will be used as index to query new posts this.lastUpdated = info.lastUpdated || this.createdAt; } addComment(comment){ this.comments.push(comment); this.lastUpdated = new Date(); } addReaction(reaction){ if(this.reactions[reaction.userid] && this.reactions[reaction.userid].type === reaction.type){ delete this.reactions[reaction.userid] } else{ this.reactions[reaction.userid] = reaction; } this.lastUpdated = new Date(); } toObj=function(){ let r = {} r.userid = this.userid; r.content = this.content; r.createdAt = this.createdAt; r.reactions = this.reactions; r.comments = this.comments; r.contentHistory = this.contentHistory; r.lastUpdated = this.lastUpdated; return r; } } module.exports = Post;