dao-governance-framework/forum-network/src/classes/supporting/post-content.js

82 lines
1.6 KiB
JavaScript

class Author {
constructor(publicKey, weight) {
this.publicKey = publicKey;
this.weight = weight;
}
toJSON() {
return {
publicKey: this.publicKey,
weight: this.weight,
};
}
static fromJSON({ publicKey, weight }) {
return new Author(publicKey, weight);
}
}
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);
}
}
export class PostContent {
constructor(content = {}) {
this.content = content;
this.authors = [];
this.citations = [];
}
addAuthor(authorPublicKey, weight) {
const author = new Author(authorPublicKey, weight);
this.authors.push(author);
return this;
}
addCitation(postId, weight) {
const citation = new Citation(postId, weight);
this.citations.push(citation);
return this;
}
setTitle(title) {
this.title = title;
return this;
}
toJSON() {
return {
content: this.content,
authors: this.authors.map((author) => author.toJSON()),
citations: this.citations.map((citation) => citation.toJSON()),
...(this.id ? { id: this.id } : {}),
title: this.title,
};
}
static fromJSON({
id, content, authors, citations, title,
}) {
const post = new PostContent(content);
post.authors = authors.map((author) => Author.fromJSON(author));
post.citations = citations.map((citation) => Citation.fromJSON(citation));
post.id = id;
post.title = title;
return post;
}
}