Files
EMI-Backend/def/post.js
2021-08-17 11:28:45 -07:00

47 lines
1.4 KiB
JavaScript

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 || [];
this.toUser = info.toUser || '';
// 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;
r.toUser = this.toUser;
return r;
}
}
module.exports = Post;