dao-governance-framework/forum-network/public/classes/post.js

47 lines
931 B
JavaScript

export class Post {
constructor(content) {
this.id = crypto.randomUUID();
this.content = content;
this.citations = [];
}
addCitation(postId, weight) {
const citation = new Citation(postId, weight);
this.citations.push(citation);
return this;
}
toJSON() {
return {
id: this.id,
content: this.content,
citations: this.citations.map((citation) => citation.toJSON()),
};
}
static fromJSON({ id, content, citations }) {
const post = new Post(content);
post.id = id;
post.citations = citations.map((citation) => Citation.fromJSON(citation));
return post;
}
}
export class Citation {
constructor(postId, weight) {
this.postId = postId;
this.weight = weight;
}
toJSON() {
return {
postId: this.postId,
weight: this.weight,
};
}
static fromJSON({ postId, weight }) {
return new Citation(postId, weight);
}
}