summaryrefslogtreecommitdiffstats
path: root/server/notifications/outgoing.js
blob: c227366e15524c4f1cb7902a4fd3c4290c4173f9 (plain)
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
const postCatchError = Meteor.wrapAsync((url, options, resolve) => {
  HTTP.post(url, options, (err, res) => {
    if (err) {
      resolve(null, err.response);
    } else {
      resolve(null, res);
    }
  });
});

Meteor.methods({
  outgoingWebhooks(integration, description, params) {
    check(integration, Object);
    check(description, String);
    check(params, Object);

    const quoteParams = _.clone(params);
    ['card', 'list', 'oldList', 'board', 'comment'].forEach((key) => {
      if (quoteParams[key]) quoteParams[key] = `"${params[key]}"`;
    });

    const user = Users.findOne(integration.userId);
    const text = `${params.user} ${TAPi18n.__(description, quoteParams, user.getLanguage())}\n${params.url}`;

    if (text.length === 0) return;

    const value = {
      text: `${text}`,
    };

    ['cardId', 'listId', 'oldListId', 'boardId'].forEach((key) => {
      if (params[key]) value[key] = params[key];
    });
    value.$description = description;

    const options = {
      headers: {
        // 'Content-Type': 'application/json',
        // 'X-Wekan-Activities-Token': 'Random.Id()',
      },
      data: value,
    };

    const response = postCatchError(integration.url, options);

    if (response && response.statusCode && response.statusCode === 200) {
      return true; // eslint-disable-line consistent-return
    } else {
      throw new Meteor.Error('error-invalid-webhook-response');
    }
  },
});