-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmutations.js
117 lines (108 loc) · 3.1 KB
/
mutations.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
import {
addGraphQLSchema,
addGraphQLMutation,
addGraphQLResolvers,
updateMutator,
runGraphQL,
Connectors,
} from "meteor/vulcan:core";
import VulcanEmail from "meteor/vulcan:email";
import Users from "meteor/vulcan:users";
import Posts from "../../../modules/posts/collection";
import { postStatus, userStatus } from "../../../modules/data";
import get from "lodash/get";
import { getMetadata } from "../../helpers/metadata.js";
// import metascraper from 'metascraper';
const urlMetadataType = `
type UrlMetadata {
author: String
description: String
image: String
publisher: String
title: String
url: String
twitter: String
}`;
const getUrlMetadata = async (root, { url }, context) => {
return await getMetadata(url);
};
addGraphQLSchema(urlMetadataType);
addGraphQLMutation(`getUrlMetadata(url: String): UrlMetadata`);
addGraphQLResolvers({ Mutation: { getUrlMetadata } });
const postQuery = `
query PostsSingleQuery($input: SinglePostInput!){
post(input: $input){
result{
user{
_id
email
}
}
}
}
`;
const approvePost = async (root, { documentId }, context) => {
const variables = { input: { id: documentId } };
const post = await runGraphQL(postQuery, variables);
const { currentUser } = context;
if (Users.isAdmin(currentUser)) {
const result = await updateMutator({
collection: Posts,
documentId,
data: { status: postStatus.published, postedAt: new Date() },
validate: true,
context,
});
const to = get(post, "user.email");
if (to) {
const email = {
to,
emailName: "postApproved",
variables,
};
await VulcanEmail.buildAndSend(email);
}
return { ...post, ...result.data };
} else {
throw new Error("Sorry, you are not allowed to perform this action");
}
};
addGraphQLMutation(`approvePost(documentId: String): Post`);
addGraphQLResolvers({ Mutation: { approvePost } });
const spamPost = async (root, { documentId }, context) => {
const variables = { input: { id: documentId } };
const result = await runGraphQL(postQuery, variables);
const post = get(result, 'data.post.result', {});
const user = post.user;
const { currentUser } = context;
if (Users.isAdmin(currentUser)) {
// mark user as spammer
await updateMutator({
collection: Users,
documentId: user._id,
data: { isSpammer: true },
validate: true,
context,
});
// mark post as spam
// const result = await updateMutator({
// collection: Posts,
// documentId,
// data: { status: postStatus.spam },
// validate: true,
// context,
// });
// mark all of that user's pending posts as spam
await Connectors.update(
Posts,
{ userId: user._id, status: postStatus.pending },
{ $set: { status: postStatus.spam } },
{ multi: true }
);
return { ...post, status: postStatus.spam };
} else {
throw new Error("Sorry, you are not allowed to perform this action");
}
};
addGraphQLMutation(`spamPost(documentId: String): Post`);
addGraphQLResolvers({ Mutation: { spamPost } });