/
comment.js
170 lines (148 loc) · 4.77 KB
/
comment.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import {Author} from "./author.js";
/**
* Represents a comment submitted by an author.
*/
export class Comment {
/**
* The comment's author.
* @type {Author|null}
*/
author;
/**
* The comment's content.
* @type {string}
*/
content;
/**
* The context in which this comment was posted.
* @type {string[]}
*/
context;
/**
* The UTC timestamp of the creation of the comment.
* @type {Date|null}
*/
date;
/**
* The permanent location of the entry the comment is submitted to.
* @type {URL|null}
*/
permalink;
/**
* The UTC timestamp of the publication time for the post, page or thread on which the comment was posted.
* @type {Date|null}
*/
postModified;
/**
* A string describing why the content is being rechecked.
* @type {string}
*/
recheckReason;
/**
* The URL of the webpage that linked to the entry being requested.
* @type {URL|null}
*/
referrer;
/**
* The comment's type.
* @type {CommentType|string}
*/
type;
/**
* Creates a new comment.
* @param {Partial<CommentOptions>} options An object providing values to initialize this instance.
*/
constructor(options = {}) {
this.author = options.author ?? null;
this.content = options.content ?? "";
this.context = options.context ?? [];
this.date = options.date ?? null;
this.permalink = options.permalink ? new URL(options.permalink) : null;
this.postModified = options.postModified ?? null;
this.recheckReason = options.recheckReason ?? "";
this.referrer = options.referrer ? new URL(options.referrer) : null;
this.type = options.type ?? "";
}
/**
* Creates a new comment from the specified JSON object.
* @param {Record<string, any>} json A JSON object representing a comment.
* @returns {Comment} The instance corresponding to the specified JSON object.
*/
static fromJson(json) {
const hasAuthor = Object.keys(json).filter(key => key.startsWith("comment_author") || key.startsWith("user")).length > 0;
return new this({
author: hasAuthor ? Author.fromJson(json) : null,
content: typeof json.comment_content == "string" ? json.comment_content : "",
context: Array.isArray(json.comment_context) ? json.comment_context : [],
date: typeof json.comment_date_gmt == "string" ? new Date(json.comment_date_gmt) : null,
permalink: typeof json.permalink == "string" ? json.permalink : "",
postModified: typeof json.comment_post_modified_gmt == "string" ? new Date(json.comment_post_modified_gmt) : null,
recheckReason: typeof json.recheck_reason == "string" ? json.recheck_reason : "",
referrer: typeof json.referrer == "string" ? json.referrer : "",
type: typeof json.comment_type == "string" ? json.comment_type : ""
});
}
/**
* Converts this object to a map in JSON format.
* @returns {Record<string, any>} The map in JSON format corresponding to this object.
*/
toJSON() {
const map = this.author ? this.author.toJSON() : {};
if (this.content) map.comment_content = this.content;
if (this.context.length) map.comment_context = this.context;
if (this.date) map.comment_date_gmt = this.date.toJSON();
if (this.permalink) map.permalink = this.permalink.href;
if (this.postModified) map.comment_post_modified_gmt = this.postModified.toJSON();
if (this.recheckReason) map.recheck_reason = this.recheckReason;
if (this.referrer) map.referrer = this.referrer.href;
if (this.type) map.comment_type = this.type;
return map;
}
}
/**
* Defines the options of a {@link Comment} instance.
* @typedef {object} CommentOptions
* @property {Author|null} author The comment's author.
* @property {string} content The comment's content.
* @property {string[]} context The context in which this comment was posted.
* @property {Date|null} date The UTC timestamp of the creation of the comment.
* @property {URL|string} permalink The permanent location of the entry the comment is submitted to.
* @property {Date|null} postModified The UTC timestamp of the publication time for the post, page or thread on which the comment was posted.
* @property {string} recheckReason A string describing why the content is being rechecked.
* @property {URL|string} referrer The URL of the webpage that linked to the entry being requested.
* @property {CommentType|string} type The comment's type.
*/
/**
* Specifies the type of a comment.
* @enum {string}
*/
export const CommentType = Object.freeze({
/**
* A blog post.
*/
blogPost: "blog-post",
/**
* A blog comment.
*/
comment: "comment",
/**
* A contact form or feedback form submission.
*/
contactForm: "contact-form",
/**
* A top-level forum post.
*/
forumPost: "forum-post",
/**
* A message sent between just a few users.
*/
message: "message",
/**
* A reply to a top-level forum post.
*/
reply: "reply",
/**
* A new user account.
*/
signup: "signup"
});