Files
EMI-Backend/def/post.js
2021-08-18 07:57:19 -07:00

47 lines
1.5 KiB
JavaScript

class Post {
constructor(info){
if(!info || !info.profileid) throw "Can not construct empty posts";
this.profileid = info.profileid;
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.toProfile = info.toProfile || '';
// 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.profileid] &&
this.reactions[reaction.profileid].type === reaction.type){
delete this.reactions[reaction.profileid]
}
else{
this.reactions[reaction.profileid] = reaction;
}
this.lastUpdated = new Date();
}
toObj=function(){
let r = {}
r.profileid = this.profileid;
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.toProfile = this.toProfile;
return r;
}
}
module.exports = Post;