This repository was archived by the owner on Nov 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcomment.js
More file actions
80 lines (78 loc) · 1.89 KB
/
comment.js
File metadata and controls
80 lines (78 loc) · 1.89 KB
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
const {
Text,
Checkbox,
Relationship,
DateTime
} = require("@keystonejs/fields");
const { Markdown } = require("@keystonejs/fields-markdown");
const { isUserAdmin } = require("../access-control");
const striptags = require("striptags");
const Comment = {
schemaDoc: "Fresh hot-takes from the internet",
fields: {
name: { type: Text, schemaDoc: "Display name for Users", isRequired: true },
email: {
type: Text,
schemaDoc: "Email - could be used for user validation",
isRequired: true,
access: {
read: isUserAdmin, // Protect emails
create: true,
update: isUserAdmin
}
},
body: {
type: Markdown,
schemaDoc: "The opinion",
isRequired: true
},
date: {
type: DateTime,
schemaDoc: "Time of the opinion",
format: "MM/DD/YYYY h:mm A",
access: {
read: true,
create: true,
update: isUserAdmin
}
},
approved: {
type: Checkbox,
schemaDoc: "Only approved comments are shown",
access: {
read: true,
create: isUserAdmin,
update: isUserAdmin
}
},
page: { type: Relationship, ref: "Page.comments" },
user: { type: Relationship, ref: "User" }
},
access: {
read: ({ authentication: { item } }) => {
if (item && item.isAdmin) {
return {}; // Don't filter items for admin users
}
// Return only approved comments for non-admin users
return {
approved: true
};
},
create: true,
update: isUserAdmin,
delete: isUserAdmin
},
hooks: {
resolveInput: ({ resolvedData }) => {
if (resolvedData.body) {
return {
...resolvedData,
date: new Date().toISOString(),
body: striptags(resolvedData.body) // Don't allow any HTML
};
}
return resolvedData;
}
}
};
module.exports = { Comment };