/
author.js
107 lines (95 loc) · 3.17 KB
/
author.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
/**
* Represents the author of a comment.
*/
export class Author {
/**
* The author's mail address. If you set it to `"akismet-guaranteed-spam@example.com"`, Akismet will always return `true`.
* @type {string}
*/
email;
/**
* The author's IP address.
* @type {string}
*/
ipAddress;
/**
* The author's name. If you set it to `"viagra-test-123"`, Akismet will always return `true`.
* @type {string}
*/
name;
/**
* The author's role. If you set it to `"administrator"`, Akismet will always return `false`.
* @type {AuthorRole|string}
*/
role;
/**
* The URL of the author's website.
* @type {URL|null}
*/
url;
/**
* The author's user agent, that is the string identifying the Web browser used to submit comments.
* @type {string}
*/
userAgent;
/**
* Creates a new author.
* @param {Partial<AuthorOptions>} options An object providing values to initialize this instance.
*/
constructor(options = {}) {
this.email = options.email ?? "";
this.ipAddress = options.ipAddress ?? "";
this.name = options.name ?? "";
this.role = options.role ?? "";
this.url = options.url ? new URL(options.url) : null;
this.userAgent = options.userAgent ?? "";
}
/**
* Creates a new author from the specified JSON object.
* @param {Record<string, any>} json A JSON object representing an author.
* @returns {Author} The instance corresponding to the specified JSON object.
*/
static fromJson(json) {
return new this({
email: typeof json.comment_author_email == "string" ? json.comment_author_email : "",
ipAddress: typeof json.user_ip == "string" ? json.user_ip : "",
name: typeof json.comment_author == "string" ? json.comment_author : "",
role: typeof json.user_role == "string" ? json.user_role : "",
url: typeof json.comment_author_url == "string" ? json.comment_author_url : "",
userAgent: typeof json.user_agent == "string" ? json.user_agent : ""
});
}
/**
* Converts this object to a map in JSON format.
* @returns {Record<string, any>} The map in JSON format corresponding to this object.
*/
toJSON() {
/** @type {Record<string, any>} */ const map = {user_ip: this.ipAddress};
if (this.email) map.comment_author_email = this.email;
if (this.name) map.comment_author = this.name;
if (this.role) map.user_role = this.role;
if (this.url) map.comment_author_url = this.url.href;
if (this.userAgent) map.user_agent = this.userAgent;
return map;
}
}
/**
* Defines the options of an {@link Author} instance.
* @typedef {object} AuthorOptions
* @property {string} email The author's mail address. If you set it to `"akismet-guaranteed-spam@example.com"`, Akismet will always return `true`.
* @property {string} ipAddress The author's IP address.
* @property {string} name The author's name.
* @property {AuthorRole|string} role The author's role.
* @property {URL|string} url The URL of the author's website.
* @property {string} userAgent The author's user agent, that is the string identifying the Web browser used to submit comments.
*/
/**
* Specifies the role of an author.
* @enum {string}
*/
export const AuthorRole = Object.freeze({
/**
* The author is an administrator.
*/
administrator: "administrator"
});