new posts
This commit is contained in:
45
def/post.js
Normal file
45
def/post.js
Normal file
@@ -0,0 +1,45 @@
|
||||
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;
|
||||
Reference in New Issue
Block a user