-
-
Notifications
You must be signed in to change notification settings - Fork 293
Expand file tree
/
Copy pathWebhookMutations.js
More file actions
120 lines (99 loc) · 3.51 KB
/
WebhookMutations.js
File metadata and controls
120 lines (99 loc) · 3.51 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
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
import { GraphQLNonNull } from 'graphql';
import twoFactorAuthLib from '../../../lib/two-factor-authentication';
import models from '../../../models';
import { checkRemoteUserCanUseWebhooks } from '../../common/scope-check';
import { Forbidden, NotFound } from '../../errors';
import { fetchAccountWithReference } from '../input/AccountReferenceInput';
import { GraphQLWebhookCreateInput } from '../input/WebhookCreateInput';
import { fetchWebhookWithReference, GraphQLWebhookReferenceInput } from '../input/WebhookReferenceInput';
import { GraphQLWebhookUpdateInput } from '../input/WebhookUpdateInput';
import { GraphQLWebhook } from '../object/Webhook';
const createWebhook = {
type: GraphQLWebhook,
description: 'Create webhook. Scope: "webhooks".',
args: {
webhook: {
type: new GraphQLNonNull(GraphQLWebhookCreateInput),
},
},
async resolve(_, args, req) {
checkRemoteUserCanUseWebhooks(req);
const account = await fetchAccountWithReference(args.webhook.account);
if (!account) {
throw new NotFound('Account not found');
}
if (!req.remoteUser.isAdminOfCollective(account)) {
throw new Forbidden("You don't have sufficient permissions to create a webhook on this account.");
}
// Check 2FA
await twoFactorAuthLib.enforceForAccount(req, account);
const createParams = {
channel: 'webhook',
active: true,
type: args.webhook.activityType,
webhookUrl: args.webhook.webhookUrl,
UserId: req.remoteUser.id,
CollectiveId: account.id,
};
return models.ActivitySubscription.create(createParams);
},
};
const updateWebhook = {
type: GraphQLWebhook,
description: 'Update webhook. Scope: "webhooks".',
args: {
webhook: {
type: new GraphQLNonNull(GraphQLWebhookUpdateInput),
},
},
async resolve(_, args, req) {
checkRemoteUserCanUseWebhooks(req);
const notification = await fetchWebhookWithReference(args.webhook);
if (!notification) {
throw new NotFound(`Webhook not found`);
}
const account = await req.loaders.Collective.byId.load(notification.CollectiveId);
if (!account || !req.remoteUser.isAdminOfCollective(account)) {
throw new Forbidden("You don't have sufficient permissions to update this webhook");
}
// Check 2FA
await twoFactorAuthLib.enforceForAccount(req, account);
const updateParams = {};
if (args.webhook.activityType) {
updateParams.type = args.webhook.activityType;
}
if (args.webhook.webhookUrl) {
updateParams.webhookUrl = args.webhook.webhookUrl;
}
return notification.update(updateParams);
},
};
const deleteWebhook = {
type: GraphQLWebhook,
description: 'Delete webhook. Scope: "webhooks".',
args: {
webhook: {
type: new GraphQLNonNull(GraphQLWebhookReferenceInput),
},
},
async resolve(_, args, req) {
checkRemoteUserCanUseWebhooks(req);
const notification = await fetchWebhookWithReference(args.webhook);
if (!notification) {
throw new NotFound(`Webhook not found`);
}
const account = await req.loaders.Collective.byId.load(notification.CollectiveId);
if (!account || !req.remoteUser.isAdminOfCollective(account)) {
throw new Forbidden("You don't have sufficient permissions to delete this webhook");
}
// Check 2FA
await twoFactorAuthLib.enforceForAccount(req, account, { onlyAskOnLogin: true });
return notification.destroy();
},
};
const webhookMutations = {
createWebhook,
updateWebhook,
deleteWebhook,
};
export default webhookMutations;