@meltwater/conversations-api-services 1.0.5 → 1.0.7
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
CHANGED
|
@@ -107,7 +107,7 @@ export class FacebookApiClient {
|
|
|
107
107
|
);
|
|
108
108
|
logger.info(
|
|
109
109
|
`Native Facebook API Publish Comment Response for documentId ${documentId}`,
|
|
110
|
-
{
|
|
110
|
+
{ status: response.status, ok: response.ok }
|
|
111
111
|
);
|
|
112
112
|
return response.body;
|
|
113
113
|
}
|
|
@@ -147,7 +147,7 @@ export class FacebookApiClient {
|
|
|
147
147
|
);
|
|
148
148
|
logger.info(
|
|
149
149
|
`Native Facebook API Publish Private Message Response for documentId ${documentId}`,
|
|
150
|
-
{
|
|
150
|
+
{ status: response.status, ok: response.ok }
|
|
151
151
|
);
|
|
152
152
|
return response.body;
|
|
153
153
|
}
|
|
@@ -162,23 +162,21 @@ export class FacebookApiClient {
|
|
|
162
162
|
.query({ access_token: accessToken, ...queryParams })
|
|
163
163
|
.send(payload);
|
|
164
164
|
} catch (err) {
|
|
165
|
+
let errorText = '';
|
|
165
166
|
if (
|
|
166
167
|
err &&
|
|
167
168
|
err.response &&
|
|
168
169
|
err.response.body &&
|
|
169
170
|
err.response.body.error
|
|
170
171
|
) {
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
);
|
|
172
|
+
errorText = `Failed to call facebook api for documentId ${documentId}: ${err.response.body.error.message}`;
|
|
173
|
+
logger.error(errorText);
|
|
174
174
|
} else {
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
err
|
|
178
|
-
);
|
|
175
|
+
errorText = `Failed to call facebook api for documentId ${documentId}`;
|
|
176
|
+
logger.error(errorText, err);
|
|
179
177
|
}
|
|
180
178
|
|
|
181
|
-
throw
|
|
179
|
+
throw new Error(errorText);
|
|
182
180
|
}
|
|
183
181
|
|
|
184
182
|
if (response.status !== 200) {
|
|
@@ -206,23 +204,21 @@ export class FacebookApiClient {
|
|
|
206
204
|
.query({ access_token: accessToken })
|
|
207
205
|
.send(payload);
|
|
208
206
|
} catch (err) {
|
|
207
|
+
let errorText = '';
|
|
209
208
|
if (
|
|
210
209
|
err &&
|
|
211
210
|
err.response &&
|
|
212
211
|
err.response.body &&
|
|
213
212
|
err.response.body.error
|
|
214
213
|
) {
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
);
|
|
214
|
+
errorText = `Failed to call facebook api for documentId ${documentId}: ${err.response.body.error.message}`;
|
|
215
|
+
logger.error(errorText);
|
|
218
216
|
} else {
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
err
|
|
222
|
-
);
|
|
217
|
+
errorText = `Failed to call facebook api for documentId ${documentId}`;
|
|
218
|
+
logger.error(errorText, err);
|
|
223
219
|
}
|
|
224
220
|
|
|
225
|
-
throw
|
|
221
|
+
throw new Error(errorText);
|
|
226
222
|
}
|
|
227
223
|
|
|
228
224
|
if (response.status !== 200) {
|
|
@@ -250,23 +246,21 @@ export class FacebookApiClient {
|
|
|
250
246
|
.query({ access_token: accessToken })
|
|
251
247
|
.send(payload);
|
|
252
248
|
} catch (err) {
|
|
249
|
+
let errorText = '';
|
|
253
250
|
if (
|
|
254
251
|
err &&
|
|
255
252
|
err.response &&
|
|
256
253
|
err.response.body &&
|
|
257
254
|
err.response.body.error
|
|
258
255
|
) {
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
);
|
|
256
|
+
errorText = `Failed to call facebook api for documentId ${documentId}: ${err.response.body.error.message}`;
|
|
257
|
+
logger.error(errorText);
|
|
262
258
|
} else {
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
err
|
|
266
|
-
);
|
|
259
|
+
errorText = `Failed to call facebook api for documentId ${documentId}`;
|
|
260
|
+
logger.error(errorText, err);
|
|
267
261
|
}
|
|
268
262
|
|
|
269
|
-
throw
|
|
263
|
+
throw new Error(errorText);
|
|
270
264
|
}
|
|
271
265
|
|
|
272
266
|
if (response.status !== 200) {
|
|
@@ -380,13 +374,6 @@ export class FacebookApiClient {
|
|
|
380
374
|
);
|
|
381
375
|
}
|
|
382
376
|
|
|
383
|
-
//sendNotification -> needs to wait for temp document to reach user
|
|
384
|
-
setTimeout(() => {
|
|
385
|
-
PushRepository.publish(
|
|
386
|
-
`${companyId}-${documentId}`,
|
|
387
|
-
updatedDocument
|
|
388
|
-
);
|
|
389
|
-
}, 5000);
|
|
390
377
|
return apiResponse;
|
|
391
378
|
}
|
|
392
379
|
|
|
@@ -458,7 +445,6 @@ export class FacebookApiClient {
|
|
|
458
445
|
} = document;
|
|
459
446
|
|
|
460
447
|
let banResponse;
|
|
461
|
-
let notificationPayload = document;
|
|
462
448
|
let authorId = authors[0].authorInfo.externalId;
|
|
463
449
|
|
|
464
450
|
try {
|
|
@@ -491,30 +477,9 @@ export class FacebookApiClient {
|
|
|
491
477
|
`finished banning in facebook for ${authorId} on company ${companyId} `,
|
|
492
478
|
{ banResponse }
|
|
493
479
|
);
|
|
494
|
-
|
|
495
|
-
notificationPayload.banStatus = {
|
|
496
|
-
code: banResponse.success === true ? 200 : 500,
|
|
497
|
-
message:
|
|
498
|
-
banResponse.success === true ? 'Success' : 'Failed to Ban',
|
|
499
|
-
};
|
|
500
|
-
logger.debug(
|
|
501
|
-
`Notification payload for ${authorId} on company ${companyId} `,
|
|
502
|
-
notificationPayload
|
|
503
|
-
);
|
|
504
480
|
} catch (error) {
|
|
505
481
|
logger.error(`${authorId} - error recieved - ${error.code}`, error);
|
|
506
|
-
// message failed ot send, mark as such
|
|
507
|
-
notificationPayload.banStatus = {
|
|
508
|
-
code: 405,
|
|
509
|
-
message: 'Exception Occurred',
|
|
510
|
-
};
|
|
511
482
|
}
|
|
512
|
-
setTimeout(() => {
|
|
513
|
-
PushRepository.publish(
|
|
514
|
-
`${companyId}-${authorId}`,
|
|
515
|
-
notificationPayload
|
|
516
|
-
);
|
|
517
|
-
}, 5000);
|
|
518
483
|
}
|
|
519
484
|
|
|
520
485
|
async toggleLike(document) {
|
|
@@ -579,7 +544,7 @@ export class FacebookApiClient {
|
|
|
579
544
|
|
|
580
545
|
logger.info(
|
|
581
546
|
`Native Facebook API Like Response for documentId ${documentId}`,
|
|
582
|
-
{
|
|
547
|
+
{ status: response.status, ok: response.ok }
|
|
583
548
|
);
|
|
584
549
|
return response.body;
|
|
585
550
|
}
|
|
@@ -601,7 +566,7 @@ export class FacebookApiClient {
|
|
|
601
566
|
|
|
602
567
|
logger.info(
|
|
603
568
|
`Native Facebook API Unlike Response for documentId ${documentId}`,
|
|
604
|
-
{
|
|
569
|
+
{ status: response.status, ok: response.ok }
|
|
605
570
|
);
|
|
606
571
|
return response.body;
|
|
607
572
|
}
|
|
@@ -624,7 +589,7 @@ export class FacebookApiClient {
|
|
|
624
589
|
|
|
625
590
|
logger.info(
|
|
626
591
|
`Native Facebook API Hide Response for documentId ${documentId}`,
|
|
627
|
-
{
|
|
592
|
+
{ status: response.status, ok: response.ok }
|
|
628
593
|
);
|
|
629
594
|
return response.body;
|
|
630
595
|
}
|
|
@@ -647,7 +612,7 @@ export class FacebookApiClient {
|
|
|
647
612
|
|
|
648
613
|
logger.info(
|
|
649
614
|
`Native Facebook API Unhide Response for documentId ${documentId}`,
|
|
650
|
-
{
|
|
615
|
+
{ status: response.status, ok: response.ok }
|
|
651
616
|
);
|
|
652
617
|
return response.body;
|
|
653
618
|
}
|
|
@@ -683,7 +648,7 @@ export class FacebookApiClient {
|
|
|
683
648
|
}
|
|
684
649
|
logger.info(
|
|
685
650
|
`Native Facebook API is User Banned Response was invalid for userId ${userId} ${credentialId}`,
|
|
686
|
-
{
|
|
651
|
+
{ status: response.status, ok: response.ok }
|
|
687
652
|
);
|
|
688
653
|
return false;
|
|
689
654
|
}
|
|
@@ -706,7 +671,7 @@ export class FacebookApiClient {
|
|
|
706
671
|
|
|
707
672
|
logger.info(
|
|
708
673
|
`Native Facebook API Hide Response for externalAuthorId ${authorId}`,
|
|
709
|
-
{
|
|
674
|
+
{ status: response.status, ok: response.ok }
|
|
710
675
|
);
|
|
711
676
|
return response.body;
|
|
712
677
|
}
|
|
@@ -729,7 +694,7 @@ export class FacebookApiClient {
|
|
|
729
694
|
|
|
730
695
|
logger.info(
|
|
731
696
|
`Native Facebook API Unban Response for externalAuthorId ${authorId}`,
|
|
732
|
-
{
|
|
697
|
+
{ status: response.status, ok: response.ok }
|
|
733
698
|
);
|
|
734
699
|
return response.body;
|
|
735
700
|
}
|
|
@@ -3,9 +3,6 @@ import logger from '../../lib/logger.js';
|
|
|
3
3
|
import { removePrefix } from '../../lib/externalId.helpers.js';
|
|
4
4
|
import assert from 'assert';
|
|
5
5
|
import { CredentialsApiClient } from '../http/credentialsApi.client.js';
|
|
6
|
-
// import { messagesRepository } from '@data-access/mongo/repository/messages.js';
|
|
7
|
-
// import PushRepository from '@data-access/redis/repository/push.repository.js';
|
|
8
|
-
// import { DOCUMENT_STATUS } from '@data-access/mongo/query.helpers.js';
|
|
9
6
|
|
|
10
7
|
const INSTAGRAM_URL = 'https://graph.facebook.com';
|
|
11
8
|
export class InstagramApiClient {
|
|
@@ -47,22 +44,22 @@ export class InstagramApiClient {
|
|
|
47
44
|
.query(queryStringArgs)
|
|
48
45
|
.send(data);
|
|
49
46
|
} catch (err) {
|
|
47
|
+
let errorText = '';
|
|
50
48
|
if (
|
|
51
49
|
err &&
|
|
52
50
|
err.response &&
|
|
53
51
|
err.response.body &&
|
|
54
52
|
err.response.body.error
|
|
55
53
|
) {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
);
|
|
54
|
+
errorText =
|
|
55
|
+
'Failed to call instagram api for documentId ${documentId}: ${err.response.body.error.message}';
|
|
56
|
+
logger.error(errorText);
|
|
59
57
|
} else {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
);
|
|
58
|
+
errorText =
|
|
59
|
+
'Failed to call instagram api for documentId ${documentId}';
|
|
60
|
+
logger.error(errorText, err);
|
|
64
61
|
}
|
|
65
|
-
throw
|
|
62
|
+
throw new Error(errorText);
|
|
66
63
|
}
|
|
67
64
|
|
|
68
65
|
if (response.status !== 200) {
|
|
@@ -100,7 +97,7 @@ export class InstagramApiClient {
|
|
|
100
97
|
|
|
101
98
|
logger.info(
|
|
102
99
|
`Native Instagram API getPost Response for documentId ${documentId}`,
|
|
103
|
-
{
|
|
100
|
+
{ status: response.status, ok: response.ok }
|
|
104
101
|
);
|
|
105
102
|
return response.body;
|
|
106
103
|
} catch (err) {
|
|
@@ -127,22 +124,20 @@ export class InstagramApiClient {
|
|
|
127
124
|
.query({ message: text })
|
|
128
125
|
.send();
|
|
129
126
|
} catch (err) {
|
|
127
|
+
let errorText = '';
|
|
130
128
|
if (
|
|
131
129
|
err &&
|
|
132
130
|
err.response &&
|
|
133
131
|
err.response.body &&
|
|
134
132
|
err.response.body.error
|
|
135
133
|
) {
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
);
|
|
134
|
+
errorText = `Failed to call instagram api for documentId ${documentId}: ${err.response.body.error.message}`;
|
|
135
|
+
logger.error(errorText);
|
|
139
136
|
} else {
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
err
|
|
143
|
-
);
|
|
137
|
+
errorText = `Failed to call instagram api for documentId ${documentId}`;
|
|
138
|
+
logger.error(errorText, err);
|
|
144
139
|
}
|
|
145
|
-
throw
|
|
140
|
+
throw new Error(errorText);
|
|
146
141
|
}
|
|
147
142
|
|
|
148
143
|
if (response.status !== 200) {
|
|
@@ -173,18 +168,15 @@ export class InstagramApiClient {
|
|
|
173
168
|
.query({ hide: hideStatus })
|
|
174
169
|
.send();
|
|
175
170
|
} catch (err) {
|
|
171
|
+
let errorText = '';
|
|
176
172
|
if (err && err.response && err.response.body) {
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
err.response.body.error
|
|
180
|
-
);
|
|
173
|
+
errorText = `Failed to call instagram api for documentId ${documentId}: ${err.response.body.error.message}`;
|
|
174
|
+
logger.error(errorText, err.response.body.error);
|
|
181
175
|
} else {
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
err
|
|
185
|
-
);
|
|
176
|
+
errorText = `Failed to call instagram api for documentId ${documentId}`;
|
|
177
|
+
logger.error(errorText, err);
|
|
186
178
|
}
|
|
187
|
-
throw
|
|
179
|
+
throw new Error(errorText);
|
|
188
180
|
}
|
|
189
181
|
|
|
190
182
|
if (response.status !== 200) {
|
|
@@ -215,18 +207,15 @@ export class InstagramApiClient {
|
|
|
215
207
|
.query({ fields: fields })
|
|
216
208
|
.send();
|
|
217
209
|
} catch (err) {
|
|
210
|
+
let errorText = '';
|
|
218
211
|
if (err && err.response && err.response.body) {
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
err.response.body.error
|
|
222
|
-
);
|
|
212
|
+
errorText = `Failed to call instagram api for documentId ${documentId}: ${err.response.body.error.message}`;
|
|
213
|
+
logger.error(errorText, err.response.body.error);
|
|
223
214
|
} else {
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
err
|
|
227
|
-
);
|
|
215
|
+
errorText = `Failed to call instagram api for documentId ${documentId}`;
|
|
216
|
+
logger.error(errorText, err);
|
|
228
217
|
}
|
|
229
|
-
throw
|
|
218
|
+
throw new Error(errorText);
|
|
230
219
|
}
|
|
231
220
|
|
|
232
221
|
if (response.status !== 200) {
|
|
@@ -278,7 +267,7 @@ export class InstagramApiClient {
|
|
|
278
267
|
|
|
279
268
|
logger.info(
|
|
280
269
|
`Native Intagram API Publish Comment Response for documentId ${documentId}`,
|
|
281
|
-
{
|
|
270
|
+
{ status: response.status, ok: response.ok }
|
|
282
271
|
);
|
|
283
272
|
return response.body;
|
|
284
273
|
}
|
|
@@ -327,7 +316,7 @@ export class InstagramApiClient {
|
|
|
327
316
|
|
|
328
317
|
logger.info(
|
|
329
318
|
`Native Instagram API Publish Reply Response for documentId ${documentId}`,
|
|
330
|
-
{
|
|
319
|
+
{ status: response.status, ok: response.ok }
|
|
331
320
|
);
|
|
332
321
|
return response.body;
|
|
333
322
|
}
|
|
@@ -413,51 +402,10 @@ export class InstagramApiClient {
|
|
|
413
402
|
default:
|
|
414
403
|
throw new Error('Unsupported discussion type');
|
|
415
404
|
}
|
|
416
|
-
|
|
417
|
-
if (apiResponse) {
|
|
418
|
-
let findQuery = { documentId };
|
|
419
|
-
findQuery['systemData.policies.storage.privateTo'] = companyId;
|
|
420
|
-
let updateObj = {};
|
|
421
|
-
updateObj['systemData.status'] = DOCUMENT_STATUS.SUCCESS;
|
|
422
|
-
// update with the new id from instagram reply/comment or direct message
|
|
423
|
-
updateObj['metaData.externalId'] =
|
|
424
|
-
apiResponse.id || `${apiResponse.message_id}`;
|
|
425
|
-
updateObj.documentId = Buffer.from(
|
|
426
|
-
`${updateObj['metaData.externalId']}`
|
|
427
|
-
).toString('base64');
|
|
428
|
-
updatedDocument = await messagesRepository.updateOne(
|
|
429
|
-
findQuery,
|
|
430
|
-
updateObj
|
|
431
|
-
);
|
|
432
|
-
} else {
|
|
433
|
-
throw new Error(
|
|
434
|
-
'Invalid response from instagram publish endpoints'
|
|
435
|
-
);
|
|
436
|
-
}
|
|
437
405
|
} catch (err) {
|
|
438
406
|
logger.error(documentId + ' - exception details ' + err, err);
|
|
439
|
-
let findQuery = { documentId };
|
|
440
|
-
findQuery['systemData.policies.storage.privateTo'] = companyId;
|
|
441
|
-
|
|
442
|
-
let updateQuery = {};
|
|
443
|
-
updateQuery['systemData.status'] = DOCUMENT_STATUS.FAILED;
|
|
444
|
-
updateQuery['systemData.error'] =
|
|
445
|
-
err && err.response && err.response.error
|
|
446
|
-
? err.response.error
|
|
447
|
-
: err;
|
|
448
|
-
updatedDocument = await messagesRepository.updateOne(
|
|
449
|
-
findQuery,
|
|
450
|
-
updateQuery
|
|
451
|
-
);
|
|
452
407
|
}
|
|
453
408
|
|
|
454
|
-
//sendNotification -> needs to wait for temp document to reach user
|
|
455
|
-
setTimeout(() => {
|
|
456
|
-
PushRepository.publish(
|
|
457
|
-
`${companyId}-${documentId}`,
|
|
458
|
-
updatedDocument
|
|
459
|
-
);
|
|
460
|
-
}, 5000);
|
|
461
409
|
return apiResponse;
|
|
462
410
|
}
|
|
463
411
|
|
|
@@ -479,7 +427,7 @@ export class InstagramApiClient {
|
|
|
479
427
|
|
|
480
428
|
logger.info(
|
|
481
429
|
`Native Instagram API Hide Response for documentId ${documentId}`,
|
|
482
|
-
{
|
|
430
|
+
{ status: response.status, ok: response.ok }
|
|
483
431
|
);
|
|
484
432
|
return response.body;
|
|
485
433
|
}
|
|
@@ -502,7 +450,7 @@ export class InstagramApiClient {
|
|
|
502
450
|
|
|
503
451
|
logger.info(
|
|
504
452
|
`Native Instagram API Unhide Response for documentId ${documentId}`,
|
|
505
|
-
{
|
|
453
|
+
{ status: response.status, ok: response.ok }
|
|
506
454
|
);
|
|
507
455
|
return response.body;
|
|
508
456
|
}
|
|
@@ -541,7 +489,7 @@ export class InstagramApiClient {
|
|
|
541
489
|
|
|
542
490
|
logger.info(
|
|
543
491
|
`Native Instagram API Publish Private Message Response for documentId ${documentId}`,
|
|
544
|
-
{
|
|
492
|
+
{ status: response.status, ok: response.ok }
|
|
545
493
|
);
|
|
546
494
|
return response.body;
|
|
547
495
|
}
|
|
@@ -1,13 +1,11 @@
|
|
|
1
1
|
import superagent from 'superagent';
|
|
2
2
|
import configuration from '../../lib/configuration.js';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
// import PushRepositoryfrom from '@data-access/redis/repository/push.repository.js';
|
|
3
|
+
import { CredentialsApiClient } from '../http/credentialsApi.client.js';
|
|
4
|
+
import logger from '../../lib/logger.js';
|
|
6
5
|
|
|
7
6
|
export class LinkedInApiClient {
|
|
8
|
-
constructor(
|
|
9
|
-
this.
|
|
10
|
-
this.logger = services.logger;
|
|
7
|
+
constructor() {
|
|
8
|
+
this.credentialsAPI = new CredentialsApiClient();
|
|
11
9
|
}
|
|
12
10
|
|
|
13
11
|
async likeMessage(document) {
|
|
@@ -43,12 +41,12 @@ export class LinkedInApiClient {
|
|
|
43
41
|
)}?actor=${fixedEncodeURIComponent(payload.actor)}`;
|
|
44
42
|
|
|
45
43
|
try {
|
|
46
|
-
let credential = await this.
|
|
44
|
+
let credential = await this.credentialsAPI.getToken(
|
|
47
45
|
credentialId,
|
|
48
46
|
companyId
|
|
49
47
|
);
|
|
50
48
|
if (!likedByUser) {
|
|
51
|
-
|
|
49
|
+
logger.debug(`${documentId} - trying Delete `, {
|
|
52
50
|
query,
|
|
53
51
|
payload,
|
|
54
52
|
});
|
|
@@ -63,7 +61,7 @@ export class LinkedInApiClient {
|
|
|
63
61
|
'LinkedIn-Version': '202305',
|
|
64
62
|
});
|
|
65
63
|
} else {
|
|
66
|
-
|
|
64
|
+
logger.debug(`${documentId} - sending to linkedin `, {
|
|
67
65
|
query,
|
|
68
66
|
payload,
|
|
69
67
|
});
|
|
@@ -80,7 +78,7 @@ export class LinkedInApiClient {
|
|
|
80
78
|
.send(payload);
|
|
81
79
|
}
|
|
82
80
|
|
|
83
|
-
|
|
81
|
+
logger.info(
|
|
84
82
|
`Native Linkedin API Like Comment Response for documentId ${documentId}`,
|
|
85
83
|
{
|
|
86
84
|
ok: response.ok,
|
|
@@ -90,7 +88,7 @@ export class LinkedInApiClient {
|
|
|
90
88
|
}
|
|
91
89
|
);
|
|
92
90
|
} catch (err) {
|
|
93
|
-
|
|
91
|
+
logger.error(documentId + ' - exception details', {
|
|
94
92
|
err,
|
|
95
93
|
});
|
|
96
94
|
throw err;
|
|
@@ -125,12 +123,12 @@ export class LinkedInApiClient {
|
|
|
125
123
|
)}/comments/${commentId}?actor=${fixedEncodeURIComponent(actorURN)}`;
|
|
126
124
|
|
|
127
125
|
try {
|
|
128
|
-
let credential = await this.
|
|
126
|
+
let credential = await this.credentialsAPI.getToken(
|
|
129
127
|
credentialId,
|
|
130
128
|
companyId
|
|
131
129
|
);
|
|
132
130
|
|
|
133
|
-
|
|
131
|
+
logger.debug(`${documentId} - trying Delete `, {
|
|
134
132
|
query,
|
|
135
133
|
});
|
|
136
134
|
response = await superagent
|
|
@@ -144,23 +142,22 @@ export class LinkedInApiClient {
|
|
|
144
142
|
'LinkedIn-Version': '202305',
|
|
145
143
|
});
|
|
146
144
|
|
|
147
|
-
|
|
145
|
+
logger.info(
|
|
148
146
|
`Native Linkedin API Delete Comment Response for documentId ${documentId}`,
|
|
149
147
|
{ status: response.status, ok: response.ok }
|
|
150
148
|
);
|
|
151
149
|
} catch (err) {
|
|
150
|
+
let errorText = '';
|
|
152
151
|
if (err && err.response && err.response.text) {
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
);
|
|
152
|
+
errorText = `Call to linkedin api with documentId ${documentId} failed`;
|
|
153
|
+
logger.error(errorText, {
|
|
154
|
+
message: JSON.parse(err.response.text),
|
|
155
|
+
});
|
|
157
156
|
} else {
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
err
|
|
161
|
-
);
|
|
157
|
+
errorText = `Call to linkedin api with documentId ${documentId} failed`;
|
|
158
|
+
logger.error(errorText, err);
|
|
162
159
|
}
|
|
163
|
-
throw
|
|
160
|
+
throw new Error(errorText);
|
|
164
161
|
}
|
|
165
162
|
return response;
|
|
166
163
|
}
|
|
@@ -222,7 +219,7 @@ export class LinkedInApiClient {
|
|
|
222
219
|
// data-listener -> linkedin_api.client sendComment
|
|
223
220
|
let response, updatedDocument;
|
|
224
221
|
try {
|
|
225
|
-
let credential = await this.
|
|
222
|
+
let credential = await this.credentialsAPI.getToken(
|
|
226
223
|
credentialId,
|
|
227
224
|
companyId
|
|
228
225
|
);
|
|
@@ -244,52 +241,13 @@ export class LinkedInApiClient {
|
|
|
244
241
|
status: 200,
|
|
245
242
|
message: JSON.parse(response.text),
|
|
246
243
|
};
|
|
247
|
-
|
|
248
|
-
//markMessageAsSuccess
|
|
249
|
-
let findQuery = { documentId };
|
|
250
|
-
findQuery['systemData.policies.storage.privateTo'] = companyId;
|
|
251
|
-
let updateQuery = {};
|
|
252
|
-
const { $URN: externalId, actor } = response.message;
|
|
253
|
-
|
|
254
|
-
updateQuery['systemData.status'] = DOCUMENT_STATUS.SUCCESS;
|
|
255
|
-
updateQuery['authors[0].authorInfo.externalId'] = actor;
|
|
256
|
-
updateQuery['metaData.externalId'] = externalId;
|
|
257
|
-
updateQuery.documentId = Buffer.from(`${externalId}`).toString(
|
|
258
|
-
'base64'
|
|
259
|
-
);
|
|
260
|
-
|
|
261
|
-
updatedDocument = await messagesRepository.updateOne(
|
|
262
|
-
findQuery,
|
|
263
|
-
updateQuery
|
|
264
|
-
);
|
|
265
244
|
} catch (err) {
|
|
266
|
-
|
|
245
|
+
logger.error(
|
|
267
246
|
documentId + ' - exception linkedin publish ' + err,
|
|
268
247
|
err
|
|
269
248
|
);
|
|
270
|
-
|
|
271
|
-
let findQuery = { documentId };
|
|
272
|
-
findQuery['systemData.policies.storage.privateTo'] = companyId;
|
|
273
|
-
let updateQuery = {};
|
|
274
|
-
updateQuery['systemData.status'] = DOCUMENT_STATUS.FAILED;
|
|
275
|
-
updateQuery['systemData.error'] =
|
|
276
|
-
err && err.response && err.response.error
|
|
277
|
-
? err.response.error
|
|
278
|
-
: err;
|
|
279
|
-
updatedDocument = await messagesRepository.updateOne(
|
|
280
|
-
findQuery,
|
|
281
|
-
updateQuery
|
|
282
|
-
);
|
|
283
249
|
}
|
|
284
250
|
|
|
285
|
-
//sendNotification -> needs to wait for temp document to reach user
|
|
286
|
-
setTimeout(() => {
|
|
287
|
-
PushRepository.publish(
|
|
288
|
-
`${companyId}-${documentId}`,
|
|
289
|
-
updatedDocument
|
|
290
|
-
);
|
|
291
|
-
}, 5000);
|
|
292
|
-
|
|
293
251
|
return response;
|
|
294
252
|
}
|
|
295
253
|
|
|
@@ -310,7 +268,7 @@ export class LinkedInApiClient {
|
|
|
310
268
|
|
|
311
269
|
let response;
|
|
312
270
|
try {
|
|
313
|
-
let credential = await this.
|
|
271
|
+
let credential = await this.credentialsAPI.getToken(
|
|
314
272
|
credentialId,
|
|
315
273
|
companyId
|
|
316
274
|
);
|
|
@@ -334,7 +292,7 @@ export class LinkedInApiClient {
|
|
|
334
292
|
};
|
|
335
293
|
return response;
|
|
336
294
|
} catch (err) {
|
|
337
|
-
|
|
295
|
+
logger.error(
|
|
338
296
|
documentId + ' - exception linkedin media register ' + err,
|
|
339
297
|
err
|
|
340
298
|
);
|
|
@@ -362,16 +320,14 @@ export class LinkedInApiClient {
|
|
|
362
320
|
})
|
|
363
321
|
.then((result) => result.body);
|
|
364
322
|
} catch (error) {
|
|
365
|
-
|
|
323
|
+
logger.error(
|
|
366
324
|
`Failed requesting LinkedIn API for profileId ${urn}`,
|
|
367
325
|
error
|
|
368
326
|
);
|
|
369
327
|
return;
|
|
370
328
|
}
|
|
371
329
|
|
|
372
|
-
|
|
373
|
-
`Finished requesting LinkedIn API for profileId ${urn}`
|
|
374
|
-
);
|
|
330
|
+
logger.info(`Finished requesting LinkedIn API for profileId ${urn}`);
|
|
375
331
|
|
|
376
332
|
return profile;
|
|
377
333
|
}
|
|
@@ -397,13 +353,13 @@ export class LinkedInApiClient {
|
|
|
397
353
|
})
|
|
398
354
|
.then((result) => result.body);
|
|
399
355
|
} catch (error) {
|
|
400
|
-
|
|
356
|
+
logger.error(
|
|
401
357
|
`Failed requesting LinkedIn API for organizationId ${urn}`,
|
|
402
358
|
error
|
|
403
359
|
);
|
|
404
360
|
}
|
|
405
361
|
|
|
406
|
-
|
|
362
|
+
logger.info(
|
|
407
363
|
`Finished requesting LinkedIn API for organizationId ${urn}`
|
|
408
364
|
);
|
|
409
365
|
|
|
@@ -429,14 +385,14 @@ export class LinkedInApiClient {
|
|
|
429
385
|
'LinkedIn-Version': '202305',
|
|
430
386
|
});
|
|
431
387
|
} catch (error) {
|
|
432
|
-
|
|
388
|
+
logger.error('Error in getAllStats ' + error, error);
|
|
433
389
|
}
|
|
434
390
|
|
|
435
391
|
let { commentSummary, reactionSummaries } = response.body || {};
|
|
436
392
|
if (commentSummary && reactionSummaries) {
|
|
437
393
|
return { commentSummary, reactionSummaries };
|
|
438
394
|
} else {
|
|
439
|
-
|
|
395
|
+
logger.error(
|
|
440
396
|
`Invalid response getting all Linkedin Stats for externalId = ${externalId} ` +
|
|
441
397
|
response,
|
|
442
398
|
response
|
|
@@ -457,14 +413,14 @@ export class LinkedInApiClient {
|
|
|
457
413
|
reactionSummaries,
|
|
458
414
|
};
|
|
459
415
|
} catch (error) {
|
|
460
|
-
|
|
416
|
+
logger.error('Error in getSocialStats: ' + error, error);
|
|
461
417
|
}
|
|
462
418
|
}
|
|
463
419
|
|
|
464
420
|
async getImageMedia(mediaId, credentialId, companyId) {
|
|
465
421
|
let response = {};
|
|
466
422
|
|
|
467
|
-
let credential = await this.
|
|
423
|
+
let credential = await this.credentialsAPI.getToken(
|
|
468
424
|
credentialId,
|
|
469
425
|
companyId
|
|
470
426
|
);
|
|
@@ -492,14 +448,14 @@ export class LinkedInApiClient {
|
|
|
492
448
|
'LinkedIn-Version': '202305',
|
|
493
449
|
});
|
|
494
450
|
} catch (error) {
|
|
495
|
-
|
|
451
|
+
logger.error('Error in getImageMedia ' + error, error);
|
|
496
452
|
}
|
|
497
453
|
|
|
498
454
|
let { downloadUrl } = response.body || {};
|
|
499
455
|
if (downloadUrl) {
|
|
500
456
|
return { downloadUrl };
|
|
501
457
|
} else {
|
|
502
|
-
|
|
458
|
+
logger.error(
|
|
503
459
|
`Invalid response getting Linkedin media for mediaId = ${mediaId} ` +
|
|
504
460
|
response,
|
|
505
461
|
response
|
|
@@ -512,7 +468,7 @@ export class LinkedInApiClient {
|
|
|
512
468
|
let response = {};
|
|
513
469
|
let tempValue;
|
|
514
470
|
|
|
515
|
-
let credential = await this.
|
|
471
|
+
let credential = await this.credentialsAPI.getToken(
|
|
516
472
|
credentialId,
|
|
517
473
|
companyId
|
|
518
474
|
);
|
|
@@ -548,14 +504,14 @@ export class LinkedInApiClient {
|
|
|
548
504
|
'LinkedIn-Version': '202305',
|
|
549
505
|
});
|
|
550
506
|
} catch (error) {
|
|
551
|
-
|
|
507
|
+
logger.error('Error in getVideoMedia ' + error, error);
|
|
552
508
|
}
|
|
553
509
|
|
|
554
510
|
let { thumbnail, downloadUrl } = response.body || {};
|
|
555
511
|
if (downloadUrl) {
|
|
556
512
|
return { thumbnail, downloadUrl };
|
|
557
513
|
} else {
|
|
558
|
-
|
|
514
|
+
logger.error(
|
|
559
515
|
`Invalid response getting Linkedin media for mediaId = ${mediaId} ` +
|
|
560
516
|
response,
|
|
561
517
|
response
|
|
@@ -596,7 +552,7 @@ export class LinkedInApiClient {
|
|
|
596
552
|
|
|
597
553
|
return {};
|
|
598
554
|
} catch (error) {
|
|
599
|
-
|
|
555
|
+
logger.error('Error in getMedia: ' + error, error);
|
|
600
556
|
}
|
|
601
557
|
}
|
|
602
558
|
|
|
@@ -608,7 +564,7 @@ export class LinkedInApiClient {
|
|
|
608
564
|
) {
|
|
609
565
|
let result;
|
|
610
566
|
try {
|
|
611
|
-
let credential = await this.
|
|
567
|
+
let credential = await this.credentialsAPI.getToken(
|
|
612
568
|
credentialId,
|
|
613
569
|
companyId
|
|
614
570
|
);
|
|
@@ -630,7 +586,7 @@ export class LinkedInApiClient {
|
|
|
630
586
|
})
|
|
631
587
|
.then((result) => result.body);
|
|
632
588
|
} catch (error) {
|
|
633
|
-
|
|
589
|
+
logger.error('Error in getLikesByUser: ' + error, error);
|
|
634
590
|
return false;
|
|
635
591
|
}
|
|
636
592
|
return true;
|
|
@@ -639,7 +595,7 @@ export class LinkedInApiClient {
|
|
|
639
595
|
async uploadMedia(query) {
|
|
640
596
|
const { socialAccountId, url, token, data } = query;
|
|
641
597
|
|
|
642
|
-
|
|
598
|
+
logger.info(
|
|
643
599
|
`Starting to upload media to linkedin for ${socialAccountId}`,
|
|
644
600
|
{ url: query.url }
|
|
645
601
|
);
|
|
@@ -657,7 +613,7 @@ export class LinkedInApiClient {
|
|
|
657
613
|
})
|
|
658
614
|
.send(data)
|
|
659
615
|
.catch((err) => {
|
|
660
|
-
|
|
616
|
+
logger.error(err);
|
|
661
617
|
throw err;
|
|
662
618
|
});
|
|
663
619
|
}
|
|
@@ -1,30 +1,35 @@
|
|
|
1
1
|
import superagent from 'superagent';
|
|
2
|
-
import { removePrefix } from '../../lib/externalId.helpers.js';
|
|
3
2
|
import configuration from '../../lib/configuration.js';
|
|
3
|
+
import logger from '../../lib/logger.js';
|
|
4
|
+
import { removePrefix } from '../../lib/externalId.helpers.js';
|
|
5
|
+
import { CredentialsApiClient } from '../http/credentialsApi.client.js';
|
|
4
6
|
|
|
5
7
|
export class TikTokApiClient {
|
|
6
|
-
constructor(
|
|
8
|
+
constructor() {
|
|
7
9
|
this.tiktokURL = configuration.get('TIKTOK_API_URL');
|
|
8
|
-
this.
|
|
9
|
-
this.logger = services.logger;
|
|
10
|
+
this.credentialsAPI = new CredentialsApiClient();
|
|
10
11
|
}
|
|
11
12
|
|
|
12
13
|
async getAuthorization(document) {
|
|
13
14
|
const {
|
|
14
15
|
documentId,
|
|
15
|
-
systemData: {
|
|
16
|
+
systemData: {
|
|
17
|
+
connectionsCredential: credentialId,
|
|
18
|
+
policies: { storage: { privateTo: companyId } = {} } = {},
|
|
19
|
+
} = {},
|
|
16
20
|
} = document;
|
|
21
|
+
|
|
17
22
|
try {
|
|
18
|
-
const
|
|
19
|
-
credentialId
|
|
23
|
+
const credential = await this.credentialsAPI.getToken(
|
|
24
|
+
credentialId,
|
|
25
|
+
companyId
|
|
20
26
|
);
|
|
21
|
-
return `${
|
|
27
|
+
return `${credential.token}`;
|
|
22
28
|
} catch (error) {
|
|
23
|
-
|
|
29
|
+
logger.error(
|
|
24
30
|
`${documentId} - error getting tiktok token - ${error.code}`,
|
|
25
31
|
error
|
|
26
32
|
);
|
|
27
|
-
return undefined;
|
|
28
33
|
}
|
|
29
34
|
}
|
|
30
35
|
|
|
@@ -43,23 +48,21 @@ export class TikTokApiClient {
|
|
|
43
48
|
.set('Access-Token', await this.getAuthorization(document))
|
|
44
49
|
.send(postData);
|
|
45
50
|
} catch (err) {
|
|
51
|
+
let errorText = '';
|
|
46
52
|
if (
|
|
47
53
|
err &&
|
|
48
54
|
err.response &&
|
|
49
55
|
err.response.body &&
|
|
50
56
|
err.response.body.error
|
|
51
57
|
) {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
);
|
|
58
|
+
errorText = `Failed to call tiktok api for paramString ${paramString}: ${err.response.body.error.message}`;
|
|
59
|
+
logger.error(errorText);
|
|
55
60
|
} else {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
err
|
|
59
|
-
);
|
|
61
|
+
errorText = `Failed to call tiktok api for paramString ${paramString}`;
|
|
62
|
+
logger.error(errorText, err);
|
|
60
63
|
}
|
|
61
64
|
|
|
62
|
-
throw
|
|
65
|
+
throw new Error(errorText);
|
|
63
66
|
}
|
|
64
67
|
|
|
65
68
|
return response;
|
|
@@ -76,27 +79,25 @@ export class TikTokApiClient {
|
|
|
76
79
|
.set('Access-Token', await this.getAuthorization(document))
|
|
77
80
|
.send();
|
|
78
81
|
} catch (err) {
|
|
82
|
+
let errorText = '';
|
|
79
83
|
if (
|
|
80
84
|
err &&
|
|
81
85
|
err.response &&
|
|
82
86
|
err.response.body &&
|
|
83
87
|
err.response.body.error
|
|
84
88
|
) {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
);
|
|
89
|
+
errorText = `Failed to call tiktok api for paramString ${paramString}: ${err.response.body.error.message}`;
|
|
90
|
+
logger.error(errorText);
|
|
88
91
|
} else {
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
err
|
|
92
|
-
);
|
|
92
|
+
errorText = `Failed to call tiktok api for paramString ${paramString}`;
|
|
93
|
+
logger.error(errorText, err);
|
|
93
94
|
}
|
|
94
95
|
|
|
95
|
-
throw
|
|
96
|
+
throw new Error(errorText);
|
|
96
97
|
}
|
|
97
98
|
|
|
98
99
|
if (response.status !== 200) {
|
|
99
|
-
|
|
100
|
+
logger.error(
|
|
100
101
|
`Failed to call tiktok api for documentId ${documentId}`,
|
|
101
102
|
response.body
|
|
102
103
|
);
|
|
@@ -316,7 +317,7 @@ export class TikTokApiClient {
|
|
|
316
317
|
: this.unlike(document));
|
|
317
318
|
return likeResponse;
|
|
318
319
|
} catch (error) {
|
|
319
|
-
|
|
320
|
+
logger.error(`${document} - error recieved - ${error.code}`, error);
|
|
320
321
|
throw error;
|
|
321
322
|
}
|
|
322
323
|
}
|
|
@@ -325,48 +326,29 @@ export class TikTokApiClient {
|
|
|
325
326
|
const {
|
|
326
327
|
documentId,
|
|
327
328
|
appData: { hidden: hiddenOnNative },
|
|
328
|
-
systemData: {
|
|
329
|
-
policies: { storage: { privateTo: companyId } = {} } = {},
|
|
330
|
-
} = {},
|
|
331
329
|
} = document;
|
|
332
|
-
let hideResponse;
|
|
333
|
-
let notificationPayload = document;
|
|
334
330
|
|
|
331
|
+
let response;
|
|
335
332
|
try {
|
|
336
333
|
if (hiddenOnNative) {
|
|
337
|
-
|
|
334
|
+
response = await this.hide(document);
|
|
338
335
|
} else {
|
|
339
|
-
|
|
336
|
+
response = await this.unhide(document);
|
|
340
337
|
}
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
message:
|
|
345
|
-
hideResponse.success === true
|
|
346
|
-
? 'Success'
|
|
347
|
-
: 'Failed to Hide',
|
|
348
|
-
};
|
|
349
|
-
this.logger.debug(
|
|
350
|
-
`Notification payload for ${documentId} on company ${companyId} `,
|
|
351
|
-
notificationPayload
|
|
338
|
+
logger.info(
|
|
339
|
+
`Native Tiktok API Hide Response for documentId ${documentId}`,
|
|
340
|
+
{ status: response.status, ok: response.ok }
|
|
352
341
|
);
|
|
342
|
+
|
|
343
|
+
if (response.message === 'OK') {
|
|
344
|
+
return response;
|
|
345
|
+
}
|
|
353
346
|
} catch (error) {
|
|
354
|
-
|
|
347
|
+
logger.error(
|
|
355
348
|
`${documentId} - error recieved - ${error.code}`,
|
|
356
349
|
error
|
|
357
350
|
);
|
|
358
|
-
// message failed ot send, mark as such
|
|
359
|
-
notificationPayload.appData.hideStatus = {
|
|
360
|
-
code: 405,
|
|
361
|
-
message: 'Exception Occurred',
|
|
362
|
-
};
|
|
363
351
|
}
|
|
364
|
-
setTimeout(() => {
|
|
365
|
-
PushRepository.publish(
|
|
366
|
-
`${companyId}-${documentId}`,
|
|
367
|
-
notificationPayload
|
|
368
|
-
);
|
|
369
|
-
}, 5000);
|
|
370
352
|
}
|
|
371
353
|
|
|
372
354
|
async like(document) {
|
|
@@ -412,15 +394,18 @@ export class TikTokApiClient {
|
|
|
412
394
|
async hide(document) {
|
|
413
395
|
const {
|
|
414
396
|
metaData: {
|
|
415
|
-
|
|
397
|
+
externalId: comment_id,
|
|
398
|
+
source: { id: sourceId },
|
|
399
|
+
inReplyTo: { id: video_id },
|
|
416
400
|
},
|
|
417
401
|
} = document;
|
|
418
402
|
const { body: publishedMessage } =
|
|
419
403
|
(await this.sendPost({
|
|
420
404
|
paramString: 'comment/hide/',
|
|
421
405
|
postData: {
|
|
422
|
-
business_id,
|
|
423
|
-
comment_id,
|
|
406
|
+
business_id: removePrefix(sourceId),
|
|
407
|
+
comment_id: removePrefix(comment_id),
|
|
408
|
+
video_id: removePrefix(video_id),
|
|
424
409
|
action: 'HIDE',
|
|
425
410
|
},
|
|
426
411
|
document,
|
|
@@ -431,15 +416,18 @@ export class TikTokApiClient {
|
|
|
431
416
|
async unhide(document) {
|
|
432
417
|
const {
|
|
433
418
|
metaData: {
|
|
434
|
-
|
|
419
|
+
externalId: comment_id,
|
|
420
|
+
source: { id: sourceId },
|
|
421
|
+
inReplyTo: { id: video_id },
|
|
435
422
|
},
|
|
436
423
|
} = document;
|
|
437
424
|
const { body: publishedMessage } =
|
|
438
425
|
(await this.sendPost({
|
|
439
426
|
paramString: 'comment/hide/',
|
|
440
427
|
postData: {
|
|
441
|
-
business_id,
|
|
442
|
-
comment_id,
|
|
428
|
+
business_id: removePrefix(sourceId),
|
|
429
|
+
comment_id: removePrefix(comment_id),
|
|
430
|
+
video_id: removePrefix(video_id),
|
|
443
431
|
action: 'UNHIDE',
|
|
444
432
|
},
|
|
445
433
|
document,
|
|
@@ -486,4 +474,4 @@ export class TikTokApiClient {
|
|
|
486
474
|
})) || {};
|
|
487
475
|
return profile;
|
|
488
476
|
}
|
|
489
|
-
}
|
|
477
|
+
}
|