dbm-graph-api 1.1.3 → 1.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -2
- package/src/dbm-graph-api/UrlRequest.js +92 -0
- package/src/dbm-graph-api/WebSocketConnection.js +69 -30
- package/src/dbm-graph-api/admin/edit/ClearCloudflareCache.js +41 -0
- package/src/dbm-graph-api/admin/edit/index.js +7 -0
- package/src/dbm-graph-api/data/AltText.js +3 -0
- package/src/dbm-graph-api/data/FreeUrl.js +3 -0
- package/src/dbm-graph-api/data/SeoSummary.js +3 -0
- package/src/dbm-graph-api/data/UploadS3.js +3 -0
- package/src/dbm-graph-api/index.js +33 -0
- package/src/dbm-graph-api/range/select/IncludePrivate.js +3 -0
- package/src/dbm-graph-api/admin/edit/PurgeCache.js +0 -13
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dbm-graph-api",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.4",
|
|
4
4
|
"main": "index.js",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"scripts": {
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
"dependencies": {
|
|
14
14
|
"@aws-sdk/client-s3": "^3.741.0",
|
|
15
15
|
"@aws-sdk/s3-request-presigner": "^3.741.0",
|
|
16
|
-
"dbm": "^1.1.
|
|
16
|
+
"dbm": "^1.1.3",
|
|
17
17
|
"ws": "^8.18.0"
|
|
18
18
|
},
|
|
19
19
|
"optionalDependencies": {
|
|
@@ -8,6 +8,19 @@ export default class UrlRequest extends Dbm.core.BaseObject {
|
|
|
8
8
|
this._logs = [];
|
|
9
9
|
this._encodedObjects = [];
|
|
10
10
|
this._responseData = null;
|
|
11
|
+
|
|
12
|
+
this._request = null;
|
|
13
|
+
this._reply = null;
|
|
14
|
+
|
|
15
|
+
this.item.requireProperty("hasLoadedUser", false);
|
|
16
|
+
this.item.requireProperty("user", null);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
setup(aRequest, aReply) {
|
|
20
|
+
this._request = aRequest;
|
|
21
|
+
this._reply = aReply;
|
|
22
|
+
|
|
23
|
+
return null;
|
|
11
24
|
}
|
|
12
25
|
|
|
13
26
|
async requestUrl(aUrl) {
|
|
@@ -98,6 +111,8 @@ export default class UrlRequest extends Dbm.core.BaseObject {
|
|
|
98
111
|
let encodeSession = new DbmGraphApi.range.EncodeSession();
|
|
99
112
|
encodeSession.outputController = this;
|
|
100
113
|
|
|
114
|
+
//METODO: check visibility
|
|
115
|
+
|
|
101
116
|
await encodeSession.encodeSingleWithTypes(aId, aEncodes);
|
|
102
117
|
|
|
103
118
|
encodeSession.destroy();
|
|
@@ -132,6 +147,37 @@ export default class UrlRequest extends Dbm.core.BaseObject {
|
|
|
132
147
|
|
|
133
148
|
this._responseData = returnData;
|
|
134
149
|
}
|
|
150
|
+
|
|
151
|
+
async incomingWebhook(aWebhookType, aData) {
|
|
152
|
+
let encodeSession = new DbmGraphApi.range.EncodeSession();
|
|
153
|
+
encodeSession.outputController = this;
|
|
154
|
+
|
|
155
|
+
let returnObject = {};
|
|
156
|
+
|
|
157
|
+
let type = aWebhookType;
|
|
158
|
+
let data = aData;
|
|
159
|
+
|
|
160
|
+
let database = Dbm.getInstance().repository.getItem("graphDatabase").controller;
|
|
161
|
+
|
|
162
|
+
let webhookType = await database.getTypeObject("type/webhookType", type);
|
|
163
|
+
|
|
164
|
+
let incomingWebhook = await database.createObject("private", ["incomingWebhook"]);
|
|
165
|
+
await incomingWebhook.updateField("data", data);
|
|
166
|
+
await incomingWebhook.addIncomingRelation(webhookType, "for");
|
|
167
|
+
|
|
168
|
+
let actionType = await database.getTypeObject("type/actionType", "incomingWebhook/" + type);
|
|
169
|
+
let actionStatus = await database.getTypeObject("status/actionStatus", "readyToProcess");
|
|
170
|
+
|
|
171
|
+
let action = await database.createObject("private", ["action"]);
|
|
172
|
+
await action.addIncomingRelation(actionType, "for");
|
|
173
|
+
await action.addIncomingRelation(incomingWebhook, "from");
|
|
174
|
+
await action.addIncomingRelation(actionStatus, "for");
|
|
175
|
+
|
|
176
|
+
returnObject["id"] = incomingWebhook.id;
|
|
177
|
+
returnObject["action"] = action.id;
|
|
178
|
+
|
|
179
|
+
this._responseData = returnObject;
|
|
180
|
+
}
|
|
135
181
|
|
|
136
182
|
outputEncodedData(aId, aData, aEncoding) {
|
|
137
183
|
//console.log("UrlRequest::outputEncodedData");
|
|
@@ -143,4 +189,50 @@ export default class UrlRequest extends Dbm.core.BaseObject {
|
|
|
143
189
|
getResponse() {
|
|
144
190
|
return {"objects": this._encodedObjects, "data": this._responseData, "logs": this._logs};
|
|
145
191
|
}
|
|
192
|
+
|
|
193
|
+
async _loadUser() {
|
|
194
|
+
|
|
195
|
+
if(this._request.headers.cookie) {
|
|
196
|
+
let cookies = this._request.headers.cookie.split(";");
|
|
197
|
+
let currentArray = cookies;
|
|
198
|
+
let currentArrayLength = currentArray.length;
|
|
199
|
+
for(let i = 0; i < currentArrayLength; i++) {
|
|
200
|
+
let [key, value] = currentArray[i].split("=");
|
|
201
|
+
if(key === "dbm_session" || key === " dbm_session") {
|
|
202
|
+
let userId = 1*value.split(":")[1];
|
|
203
|
+
let user = Dbm.getInstance().repository.getItem("graphDatabase").controller.getUser(userId);
|
|
204
|
+
|
|
205
|
+
let isValidSession = await user.verifySession(value);
|
|
206
|
+
|
|
207
|
+
if(isValidSession) {
|
|
208
|
+
let database = Dbm.getInstance().repository.getItem("graphDatabase").controller;
|
|
209
|
+
|
|
210
|
+
let user = database.getUser(userId);
|
|
211
|
+
this.item.setValue("user", user);
|
|
212
|
+
}
|
|
213
|
+
break;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
this.item.setValue("hasLoadedUser", true);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
async getUser() {
|
|
222
|
+
if(!this.item.hasLoadedUser) {
|
|
223
|
+
await this._loadUser();
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
return this.item.user;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
async requireRole(aRole) {
|
|
230
|
+
let user = await this.getUser();
|
|
231
|
+
|
|
232
|
+
if(!user) {
|
|
233
|
+
throw("Only signed in users can use this endpoint");
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
return true;
|
|
237
|
+
}
|
|
146
238
|
}
|
|
@@ -6,6 +6,8 @@ export default class WebSocketConnection extends Dbm.core.BaseObject {
|
|
|
6
6
|
super._construct();
|
|
7
7
|
this._webSocket = null;
|
|
8
8
|
|
|
9
|
+
this.item.requireProperty("user", null);
|
|
10
|
+
|
|
9
11
|
this._callback_errorBound = this._callback_error.bind(this);
|
|
10
12
|
this._callback_messageBound = this._callback_message.bind(this);
|
|
11
13
|
this._callback_closeBound = this._callback_close.bind(this);
|
|
@@ -181,28 +183,39 @@ export default class WebSocketConnection extends Dbm.core.BaseObject {
|
|
|
181
183
|
break;
|
|
182
184
|
case "admin/createObject":
|
|
183
185
|
{
|
|
184
|
-
|
|
185
|
-
let
|
|
186
|
-
let
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
186
|
+
//METODO: require role
|
|
187
|
+
let returnId = 0;
|
|
188
|
+
let user = await this.getUser();
|
|
189
|
+
if(user) {
|
|
190
|
+
let types = data['types'];
|
|
191
|
+
let database = Dbm.getInstance().repository.getItem("graphDatabase").controller;
|
|
192
|
+
let visibility = data['visibility'] ? data['visibility'] : 'draft';
|
|
193
|
+
|
|
194
|
+
let draftVisibility = await database.getVisibilityType(visibility);
|
|
195
|
+
|
|
196
|
+
let newObject = await database.createObject(draftVisibility, types);
|
|
197
|
+
|
|
198
|
+
if(data.changes) {
|
|
199
|
+
await this._applyChanges(newObject, data.changes, request);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
if(data.encode) {
|
|
203
|
+
|
|
204
|
+
let encodeSession = new DbmGraphApi.range.EncodeSession();
|
|
205
|
+
encodeSession.outputController = this;
|
|
206
|
+
|
|
207
|
+
await encodeSession.encodeSingleWithTypes(newObject.id, data.encode);
|
|
208
|
+
|
|
209
|
+
encodeSession.destroy();
|
|
210
|
+
}
|
|
191
211
|
|
|
192
|
-
|
|
193
|
-
await this._applyChanges(newObject, data.changes, request);
|
|
212
|
+
returnId = newObject.id;
|
|
194
213
|
}
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
let encodeSession = new DbmGraphApi.range.EncodeSession();
|
|
199
|
-
encodeSession.outputController = this;
|
|
200
|
-
|
|
201
|
-
await encodeSession.encodeSingleWithTypes(newObject.id, data.encode);
|
|
202
|
-
|
|
203
|
-
encodeSession.destroy();
|
|
214
|
+
else {
|
|
215
|
+
//METODO: add logs
|
|
204
216
|
}
|
|
205
217
|
|
|
218
|
+
|
|
206
219
|
this._webSocket.send(JSON.stringify({"type": "item/response", "id": newObject.id, "requestId": data["requestId"]}));
|
|
207
220
|
}
|
|
208
221
|
break;
|
|
@@ -212,20 +225,27 @@ export default class WebSocketConnection extends Dbm.core.BaseObject {
|
|
|
212
225
|
|
|
213
226
|
let theObject = database.getObject(data.id);
|
|
214
227
|
|
|
215
|
-
|
|
216
|
-
|
|
228
|
+
let user = await this.getUser();
|
|
229
|
+
if(user) {
|
|
230
|
+
if(data.changes) {
|
|
231
|
+
await this._applyChanges(theObject, data.changes, request);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
if(data.encode) {
|
|
235
|
+
|
|
236
|
+
let encodeSession = new DbmGraphApi.range.EncodeSession();
|
|
237
|
+
encodeSession.outputController = this;
|
|
238
|
+
|
|
239
|
+
await encodeSession.encodeSingleWithTypes(theObject.id, data.encode);
|
|
240
|
+
|
|
241
|
+
encodeSession.destroy();
|
|
242
|
+
}
|
|
217
243
|
}
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
let encodeSession = new DbmGraphApi.range.EncodeSession();
|
|
222
|
-
encodeSession.outputController = this;
|
|
223
|
-
|
|
224
|
-
await encodeSession.encodeSingleWithTypes(theObject.id, data.encode);
|
|
225
|
-
|
|
226
|
-
encodeSession.destroy();
|
|
244
|
+
else {
|
|
245
|
+
//METODO: add log
|
|
227
246
|
}
|
|
228
247
|
|
|
248
|
+
|
|
229
249
|
this._webSocket.send(JSON.stringify({"type": "item/response", "id": theObject.id, "requestId": data["requestId"]}));
|
|
230
250
|
}
|
|
231
251
|
break;
|
|
@@ -240,7 +260,9 @@ export default class WebSocketConnection extends Dbm.core.BaseObject {
|
|
|
240
260
|
let userId = 0;
|
|
241
261
|
if(isVerified) {
|
|
242
262
|
//METODO: set user for connection
|
|
263
|
+
|
|
243
264
|
userId = user.id;
|
|
265
|
+
this.item.setValue("user", user);
|
|
244
266
|
}
|
|
245
267
|
|
|
246
268
|
this._webSocket.send(JSON.stringify({"type": "currentUser/response", "id": userId, "requestId": data["requestId"]}));
|
|
@@ -298,8 +320,25 @@ export default class WebSocketConnection extends Dbm.core.BaseObject {
|
|
|
298
320
|
|
|
299
321
|
setInitialUser(aId) {
|
|
300
322
|
|
|
301
|
-
|
|
323
|
+
let database = Dbm.getInstance().repository.getItem("graphDatabase").controller;
|
|
324
|
+
|
|
325
|
+
let user = database.getUser(aId);
|
|
326
|
+
this.item.setValue("user", user);
|
|
302
327
|
|
|
303
328
|
this._webSocket.send(JSON.stringify({"type": "connectionReady", "user": aId}));
|
|
304
329
|
}
|
|
330
|
+
|
|
331
|
+
async getUser() {
|
|
332
|
+
return this.item.user;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
async requireRole(aRole) {
|
|
336
|
+
let user = await this.getUser();
|
|
337
|
+
|
|
338
|
+
if(!user) {
|
|
339
|
+
throw("Only signed in users can use this endpoint");
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
return true;
|
|
343
|
+
}
|
|
305
344
|
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import Dbm from "dbm";
|
|
2
|
+
import EditBaseObject from "./EditBaseObject.js";
|
|
3
|
+
|
|
4
|
+
export default class ClearCloudflareCache extends EditBaseObject {
|
|
5
|
+
_construct() {
|
|
6
|
+
super._construct();
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
async performChange(aObject, aData, aRequest) {
|
|
10
|
+
|
|
11
|
+
let cloudflare = Dbm.getInstance().repository.getItem("cloudflare");
|
|
12
|
+
let url = await aObject.getUrl();
|
|
13
|
+
|
|
14
|
+
if(cloudflare.domain && cloudflare.zone) {
|
|
15
|
+
let fullUrl = cloudflare.domain + url;
|
|
16
|
+
|
|
17
|
+
let requestUrl = "https://api.cloudflare.com/client/v4/zones/" + cloudflare.zone + "/purge_cache";
|
|
18
|
+
|
|
19
|
+
let body = {
|
|
20
|
+
"files": [fullUrl]
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
let headers = {
|
|
24
|
+
"Content-Type": "application/json",
|
|
25
|
+
'Authorization': 'Bearer ' + cloudflare.cacheToken
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
let response = await fetch(requestUrl, {
|
|
29
|
+
method: "DELETE",
|
|
30
|
+
headers: headers,
|
|
31
|
+
body: JSON.stringify(body),
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
let data = await response.json();
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
//METODO: report error log
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -6,6 +6,7 @@ export {default as SetIdentifier} from "./SetIdentifier.js";
|
|
|
6
6
|
export {default as SetUrl} from "./SetUrl.js";
|
|
7
7
|
export {default as AddIncomingRelation} from "./AddIncomingRelation.js";
|
|
8
8
|
export {default as AddOutgoingRelation} from "./AddOutgoingRelation.js";
|
|
9
|
+
export {default as ClearCloudflareCache} from "./ClearCloudflareCache.js";
|
|
9
10
|
|
|
10
11
|
export const fullSetup = function() {
|
|
11
12
|
let prefix = "graphApi/admin/edit/";
|
|
@@ -38,4 +39,10 @@ export const fullSetup = function() {
|
|
|
38
39
|
let currentSelect = new DbmGraphApi.admin.edit.AddOutgoingRelation();
|
|
39
40
|
currentSelect.item.register(prefix + name);
|
|
40
41
|
}
|
|
42
|
+
|
|
43
|
+
{
|
|
44
|
+
let name = "clearCache";
|
|
45
|
+
let currentSelect = new DbmGraphApi.admin.edit.ClearCloudflareCache();
|
|
46
|
+
currentSelect.item.register(prefix + name);
|
|
47
|
+
}
|
|
41
48
|
}
|
|
@@ -262,6 +262,7 @@ let setupEndpoints = function(aServer) {
|
|
|
262
262
|
console.log(url);
|
|
263
263
|
//METODO: check visibility in database
|
|
264
264
|
let request = new UrlRequest();
|
|
265
|
+
request.setup(aRequest, aReply);
|
|
265
266
|
|
|
266
267
|
await request.requestUrl(url);
|
|
267
268
|
|
|
@@ -284,6 +285,7 @@ let setupEndpoints = function(aServer) {
|
|
|
284
285
|
}
|
|
285
286
|
|
|
286
287
|
let request = new UrlRequest();
|
|
288
|
+
request.setup(aRequest, aReply);
|
|
287
289
|
|
|
288
290
|
await request.requestRange(selects, encodes, params);
|
|
289
291
|
|
|
@@ -296,6 +298,7 @@ let setupEndpoints = function(aServer) {
|
|
|
296
298
|
let encodes = aRequest.params.encodes.split(",");
|
|
297
299
|
|
|
298
300
|
let request = new UrlRequest();
|
|
301
|
+
request.setup(aRequest, aReply);
|
|
299
302
|
|
|
300
303
|
await request.requestItem(itemId, encodes);
|
|
301
304
|
|
|
@@ -305,6 +308,7 @@ let setupEndpoints = function(aServer) {
|
|
|
305
308
|
aServer.get('/api/data/*', async function handler (aRequest, aReply) {
|
|
306
309
|
let params = {...aRequest.query};
|
|
307
310
|
let request = new UrlRequest();
|
|
311
|
+
request.setup(aRequest, aReply);
|
|
308
312
|
|
|
309
313
|
let currentUrl = url.parse(aRequest.url);
|
|
310
314
|
let functionName = currentUrl.pathname.substring("/api/data/".length);
|
|
@@ -318,6 +322,7 @@ let setupEndpoints = function(aServer) {
|
|
|
318
322
|
|
|
319
323
|
let params = {...aRequest.query};
|
|
320
324
|
let request = new UrlRequest();
|
|
325
|
+
request.setup(aRequest, aReply);
|
|
321
326
|
|
|
322
327
|
let currentUrl = url.parse(aRequest.url);
|
|
323
328
|
let functionName = currentUrl.pathname.substring("/api/action/".length);
|
|
@@ -330,6 +335,7 @@ let setupEndpoints = function(aServer) {
|
|
|
330
335
|
aServer.post('/api/action/*', async function handler (aRequest, aReply) {
|
|
331
336
|
let params = {...aRequest.body};
|
|
332
337
|
let request = new UrlRequest();
|
|
338
|
+
request.setup(aRequest, aReply);
|
|
333
339
|
|
|
334
340
|
let currentUrl = url.parse(aRequest.url);
|
|
335
341
|
let functionName = currentUrl.pathname.substring("/api/action/".length);
|
|
@@ -341,6 +347,33 @@ let setupEndpoints = function(aServer) {
|
|
|
341
347
|
|
|
342
348
|
//METODO: setup raw data posts
|
|
343
349
|
|
|
350
|
+
aServer.get('/api/webhook/*', async function handler (aRequest, aReply) {
|
|
351
|
+
|
|
352
|
+
let params = {...aRequest.query};
|
|
353
|
+
let request = new UrlRequest();
|
|
354
|
+
request.setup(aRequest, aReply);
|
|
355
|
+
|
|
356
|
+
let currentUrl = url.parse(aRequest.url);
|
|
357
|
+
let webhookType = currentUrl.pathname.substring("/api/incomingWebhook/".length);
|
|
358
|
+
|
|
359
|
+
await request.incomingWebhook(webhookType, params);
|
|
360
|
+
|
|
361
|
+
return request.getResponse();
|
|
362
|
+
});
|
|
363
|
+
|
|
364
|
+
aServer.post('/api/webhook/*', async function handler (aRequest, aReply) {
|
|
365
|
+
let params = {...aRequest.body};
|
|
366
|
+
let request = new UrlRequest();
|
|
367
|
+
request.setup(aRequest, aReply);
|
|
368
|
+
|
|
369
|
+
let currentUrl = url.parse(aRequest.url);
|
|
370
|
+
let webhookType = currentUrl.pathname.substring("/api/incomingWebhook/".length);
|
|
371
|
+
|
|
372
|
+
await request.incomingWebhook(webhookType, params);
|
|
373
|
+
|
|
374
|
+
return request.getResponse();
|
|
375
|
+
});
|
|
376
|
+
|
|
344
377
|
//METODO: setup edit
|
|
345
378
|
|
|
346
379
|
aServer.get('/api/', async function handler (aRequest, aResponse) {
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import Dbm from "dbm";
|
|
2
|
-
import EditBaseObject from "./EditBaseObject.js";
|
|
3
|
-
|
|
4
|
-
export default class PurgeCache extends EditBaseObject {
|
|
5
|
-
_construct() {
|
|
6
|
-
super._construct();
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
async performChange(aObject, aData, aRequest) {
|
|
10
|
-
let url = await aObject.getUrl();
|
|
11
|
-
console.log(url);
|
|
12
|
-
}
|
|
13
|
-
}
|