1 | var _ = require('lodash');
|
2 | var check = require('check-types');
|
3 | var verify = check.verify;
|
4 | var wh = require('./webhooks');
|
5 | var param = encodeURIComponent;
|
6 |
|
7 | function WebhookManager(parent, store, tenant, client, baseUrl, webhookPath) {
|
8 | if (!(this instanceof WebhookManager)) {
|
9 | return new WebhookManager(parent, store, tenant, client, baseUrl, webhookPath);
|
10 | }
|
11 | this._parent = parent;
|
12 | this._store = store;
|
13 | this._tenant = tenant;
|
14 | this._client = client;
|
15 | this._baseUrl = baseUrl;
|
16 | this._webhookPath = webhookPath;
|
17 | }
|
18 |
|
19 |
|
20 |
|
21 | WebhookManager.prototype.get = function (roomId, name) {
|
22 | var self = this;
|
23 | if (typeof roomId === 'string') {
|
24 | name = roomId;
|
25 | roomId = self._tenant.room;
|
26 | if (!roomId) {
|
27 | throw new Error('Room id not found in method arguments or tenant');
|
28 | }
|
29 | }
|
30 | verify.string(name);
|
31 | return self._store.get(name).then(function (definition) {
|
32 | if (definition) {
|
33 | return definition;
|
34 | }
|
35 |
|
36 | return self._parent.get(name);
|
37 | });
|
38 | };
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 | WebhookManager.prototype.add = function () {
|
47 | var self = this;
|
48 | var args = [].slice.call(arguments);
|
49 | var roomId, event, pattern;
|
50 | if (typeof args[0] === 'number') {
|
51 | roomId = args[0];
|
52 | args.shift();
|
53 | } else {
|
54 | roomId = self._tenant.room;
|
55 | }
|
56 | if (args[0] != null && typeof args[0] === 'object') {
|
57 | event = args[0].event;
|
58 | pattern = args[0].pattern;
|
59 | } else {
|
60 | event = args[0];
|
61 | pattern = args[1];
|
62 | }
|
63 | if (!roomId) {
|
64 | throw new Error('Room id not found in method arguments or tenant');
|
65 | }
|
66 | verify.string(event);
|
67 | var definition = {event: event};
|
68 | if (pattern) {
|
69 | definition.pattern = pattern;
|
70 | }
|
71 | definition = wh.normalize(definition, self._baseUrl, self._webhookPath, self._tenant.webhookToken);
|
72 | verify.string(definition.name);
|
73 | return self._client.createWebhook(roomId, definition).then(function (webhook) {
|
74 | definition.id = webhook.id;
|
75 | return self._store.set(definition.name, definition).then(function () {
|
76 | return definition;
|
77 | });
|
78 | });
|
79 | };
|
80 |
|
81 |
|
82 |
|
83 | WebhookManager.prototype.remove = function (roomId, name) {
|
84 | var self = this;
|
85 | if (typeof roomId === 'string') {
|
86 | name = roomId;
|
87 | roomId = self._tenant.room;
|
88 | if (!roomId) {
|
89 | throw new Error('Room id not found in method arguments or tenant');
|
90 | }
|
91 | }
|
92 | verify.string(name);
|
93 | return self._store.get(name).then(function (definition) {
|
94 | if (definition) {
|
95 | return self._client.deleteWebhook(roomId, definition.id).then(function () {
|
96 | return self._store.del(name);
|
97 | });
|
98 | }
|
99 | });
|
100 | };
|
101 |
|
102 | module.exports = WebhookManager;
|