49 lines
992 B
JavaScript
49 lines
992 B
JavaScript
import { CryptoUtil } from './crypto.js';
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
export class PostContent {
|
|
constructor(content) {
|
|
this.id = CryptoUtil.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 PostContent(content);
|
|
post.id = id;
|
|
post.citations = citations.map((citation) => Citation.fromJSON(citation));
|
|
return post;
|
|
}
|
|
}
|