-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpost.js
More file actions
157 lines (147 loc) · 4.24 KB
/
Copy pathpost.js
File metadata and controls
157 lines (147 loc) · 4.24 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import admZip from 'adm-zip';
import { Controller } from 'lib/routeGenerator';
import sequelize from 'lib/sequelize';
import { error } from 'lib/logger';
import FbHook from 'database/fbHook/FbHook';
import findAllFbHooks from 'database/fbHook/findAllFbHooks';
import createFbHook from 'database/fbHook/createFbHook';
import IntentManager from 'apiai/intents/IntentManager';
export default {
parameters: {
arch: {
required: true
}
},
settings: {
},
method: async (
arch: {
name: string,
data: Buffer,
encoding: string,
mimetype: string,
mv: (path: string, callback: (error: Error) => void) => void
}
): Promise<boolean> => {
const fbHooksZip = new admZip(arch.data);
const fbHooksEntries = fbHooksZip.getEntries();
const fbHooksInput = new Map();
const fbHooksArray = await findAllFbHooks();
const existingFbHooks = new Map();
for (const hook of fbHooksArray) {
existingFbHooks.set(hook.payload, hook);
}
const executives = [];
while (fbHooksEntries.length) {
const entry = fbHooksEntries.pop();
if (entry.isDirectory) continue;
const nameSearch = entry.name.match(/^([\w-]+)\.json$/);
if (!nameSearch) continue;
const payload = nameSearch[1];
const duplicatedPayload = fbHooksInput.get(payload);
if (duplicatedPayload) {
const err = new Error(`Duplication of payload name '${entry.name}': ` +
`fbHook ${entry.entryName} has the same name as ${duplicatedPayload.entryName}`);
error(err.message, err);
throw {
code: 400,
message: err.message
};
}
const responseString = fbHooksZip.readAsText(entry);
let response;
try {
response = JSON.parse(responseString);
} catch (e) {
e.message = `${entry.entryName}: ` + e.message;
error(e.message, e);
throw {
code: 400,
message: e.message
};
}
fbHooksInput.set(payload, {
payload,
responseString,
response,
entryName: entry.entryName
});
}
const intentManager = new IntentManager();
for (const hook of fbHooksInput.values()) {
const existingHook = existingFbHooks.get(hook.payload);
if (existingHook) {
if (existingHook.response !== hook.responseString) {
executives.push(importHookWithIntent(
intentManager,
hook,
existingHook
));
}
} else {
executives.push(importHookWithIntent(
intentManager,
hook,
));
}
}
for (const executive of executives) {
await executive.execute();
}
return true;
}
} as Controller;
function importHookWithIntent(
intentManager: IntentManager,
hookData: {
payload: string,
response: any,
responseString: string
},
hookObject?: FbHook,
) {
return {
execute: async () => {
const transaction = await sequelize.transaction();
if (hookObject) { // just update and save it
hookObject.response = hookData.responseString;
try {
await hookObject.save({ transaction });
await intentManager.createOrUpdate(
hookObject.payload + '', hookObject.response
);
await transaction.commit();
} catch (err) {
await transaction.rollback();
const message = `While updating fbHook id=${hookObject.id} payload='${hookObject.payload}'`;
err.message = `${message}: ${err.message}`;
error(err.message, err);
throw {
code: 400,
message
};
}
} else { // create new
try {
await createFbHook(
hookData.payload,
hookData.responseString,
transaction,
);
await intentManager.createOrUpdate(
hookData.payload + '', hookData.response
);
await transaction.commit();
} catch (err) {
await transaction.rollback();
const message = `While creating fbHook payload='${hookData.payload}'`;
err.message = `${message}: ${err.message}`;
throw {
code: 400,
message
};
}
}
}
};
}