@meltwater/conversations-api-services 1.0.35 → 1.0.36

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.
@@ -4,6 +4,7 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.assetManagerTvmRepository = void 0;
7
+ exports.getAssetManagerTVM = getAssetManagerTVM;
7
8
  var _superagent = _interopRequireDefault(require("superagent"));
8
9
  var _logger = _interopRequireDefault(require("../../lib/logger.js"));
9
10
  var _loggerHelpers = require("../../lib/logger.helpers.js");
@@ -16,20 +17,24 @@ class AssetManagerTVMRepository {
16
17
  apiKey;
17
18
  constructor() {}
18
19
  async get(companyId) {
19
- let response;
20
- try {
21
- response = await _superagent.default.get(`https://${ASSET_MANAGER_TVM_URL}/tvm`).query({
22
- companyId
23
- }).timeout(5000).set({
24
- 'x-api-key': ASSET_MANAGER_TVM_API_KEY
25
- }).then(result => result.body);
26
- } catch (error) {
27
- _logger.default.error("Failed requesting Asset Manager TVM API", error, {
28
- [_loggerHelpers.MeltwaterAttributes.COMPANYID]: companyId
29
- });
30
- throw error;
31
- }
32
- return response;
20
+ return getAssetManagerTVM(companyId);
33
21
  }
34
22
  }
23
+ async function getAssetManagerTVM(companyId) {
24
+ let timeout = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 5000;
25
+ let response;
26
+ try {
27
+ response = await _superagent.default.get(`https://${ASSET_MANAGER_TVM_URL}/tvm`).query({
28
+ companyId
29
+ }).timeout(timeout).set({
30
+ 'x-api-key': ASSET_MANAGER_TVM_API_KEY
31
+ }).then(result => result.body);
32
+ } catch (error) {
33
+ _logger.default.error("Failed requesting Asset Manager TVM API", error, {
34
+ [_loggerHelpers.MeltwaterAttributes.COMPANYID]: companyId
35
+ });
36
+ throw error;
37
+ }
38
+ return response;
39
+ }
35
40
  const assetManagerTvmRepository = exports.assetManagerTvmRepository = new AssetManagerTVMRepository();
@@ -3,13 +3,17 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.getURL = getURL;
6
+ exports.getAsset = getAsset;
7
+ exports.getAssetDataBuffer = getAssetDataBuffer;
7
8
  var _loggerHelpers = require("../../lib/logger.helpers.js");
8
9
  var _superagent = _interopRequireDefault(require("superagent"));
9
10
  var _configuration = _interopRequireDefault(require("../../lib/configuration.js"));
11
+ var _assetManagerTvmClient = require("./asset-manager-tvm.client.js");
12
+ var _awsSdk = _interopRequireDefault(require("aws-sdk"));
10
13
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
11
14
  const gatewayURL = _configuration.default.get('ASSETMANAGER_GATEWAY_URL');
12
- async function getURL(jwt, assetId, logger) {
15
+ const ASSET_LIBRARY_S3_BUCKET = _configuration.default.get('ASSET_LIBRARY_S3_BUCKET');
16
+ async function getAsset(jwt, assetId, logger) {
13
17
  try {
14
18
  const response = await _superagent.default.post(`${gatewayURL}`).set('apollographql-client-name', 'phoenix-conversations').set('Authorization', jwt).send({
15
19
  query: `
@@ -19,7 +23,14 @@ async function getURL(jwt, assetId, logger) {
19
23
  assets(filter: $filter) {
20
24
  edges {
21
25
  node {
26
+ fileName
22
27
  url
28
+ size {
29
+ bytes
30
+ }
31
+ path
32
+ type
33
+ mimeType
23
34
  }
24
35
  }
25
36
  }
@@ -33,10 +44,44 @@ async function getURL(jwt, assetId, logger) {
33
44
  }
34
45
  }
35
46
  });
36
- return response.body;
47
+ return response.body?.data?.assets?.edges[0]?.node;
37
48
  } catch (exception) {
38
49
  // How would you prefer to handle this
39
50
  (0, _loggerHelpers.loggerError)(logger, 'Error getting asset url', exception);
40
51
  throw exception;
41
52
  }
53
+ }
54
+ async function getAssetManagerS3Instance(companyId) {
55
+ let timeout = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 5000;
56
+ const {
57
+ Credentials: {
58
+ AccessKeyId,
59
+ SecretAccessKey,
60
+ SessionToken
61
+ } = {}
62
+ } = await (0, _assetManagerTvmClient.getAssetManagerTVM)(companyId);
63
+ if (!AccessKeyId || !SecretAccessKey || !SessionToken) {
64
+ // probably a bit extreme, what would you prefer to do
65
+ throw 'No Credentials available';
66
+ }
67
+ const s3 = new _awsSdk.default.S3({
68
+ accessKeyId: AccessKeyId,
69
+ secretAccessKey: SecretAccessKey,
70
+ sessionToken: SessionToken,
71
+ useAccelerateEndpoint: true
72
+ });
73
+ return s3;
74
+ }
75
+ function sanitizeAssetLibraryFilePath(filePath) {
76
+ const [_, ...sanitizedFilePath] = filePath.split('/');
77
+ return sanitizedFilePath.join('/');
78
+ }
79
+ async function getAssetDataBuffer(path, fileName, companyId, logger) {
80
+ const s3 = await getAssetManagerS3Instance(companyId, 15000);
81
+ const config = {
82
+ Bucket: ASSET_LIBRARY_S3_BUCKET,
83
+ Key: sanitizeAssetLibraryFilePath(path + fileName).replace(`/${ASSET_LIBRARY_S3_BUCKET}/`, '')
84
+ };
85
+ const s3Response = await s3.getObject(config).promise();
86
+ return s3Response.Body;
42
87
  }
@@ -23,7 +23,7 @@ async function comment(token, business_id, video_id, text, logger) {
23
23
  body: publishedMessage
24
24
  } = (await sendPost(token, 'comment/create/', {
25
25
  business_id: business_id,
26
- video_id: video_id.startsWith('tt_') ? video_id.split('_')[2] : video_id,
26
+ video_id: (0, _externalIdHelpers.removePrefix)(video_id.startsWith('tt_') ? video_id.split('_')[2] : video_id),
27
27
  text
28
28
  }, logger)) || {};
29
29
  return publishedMessage;
@@ -28,7 +28,11 @@ var _crypto = _interopRequireDefault(require("crypto"));
28
28
  var _axios = _interopRequireDefault(require("axios"));
29
29
  var _fs = _interopRequireDefault(require("fs"));
30
30
  var _socialTwit = _interopRequireDefault(require("@meltwater/social-twit"));
31
+ var _url = require("url");
32
+ var _path = require("path");
31
33
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
34
+ const _filename = (0, _url.fileURLToPath)(import.meta.url);
35
+ const _dirname = (0, _path.dirname)(_filename);
32
36
  function addOAuthToToken(token, TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET) {
33
37
  if (!token.oauth) {
34
38
  token.oauth = (0, _oauth.default)({
@@ -531,7 +535,7 @@ async function downloadImage(url, fileName) {
531
535
 
532
536
  // local
533
537
  function generateFilePath(mimeType) {
534
- let dir = `${__dirname}/temporaryTwitterMedia`;
538
+ let dir = `${_dirname}/temporaryTwitterMedia`;
535
539
  if (!_fs.default.existsSync(dir)) {
536
540
  _fs.default.mkdirSync(dir);
537
541
  }
@@ -9,20 +9,24 @@ class AssetManagerTVMRepository {
9
9
  apiKey;
10
10
  constructor() {}
11
11
  async get(companyId) {
12
- let response;
13
- try {
14
- response = await superagent.get(`https://${ASSET_MANAGER_TVM_URL}/tvm`).query({
15
- companyId
16
- }).timeout(5000).set({
17
- 'x-api-key': ASSET_MANAGER_TVM_API_KEY
18
- }).then(result => result.body);
19
- } catch (error) {
20
- logger.error("Failed requesting Asset Manager TVM API", error, {
21
- [MeltwaterAttributes.COMPANYID]: companyId
22
- });
23
- throw error;
24
- }
25
- return response;
12
+ return getAssetManagerTVM(companyId);
26
13
  }
27
14
  }
15
+ export async function getAssetManagerTVM(companyId) {
16
+ let timeout = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 5000;
17
+ let response;
18
+ try {
19
+ response = await superagent.get(`https://${ASSET_MANAGER_TVM_URL}/tvm`).query({
20
+ companyId
21
+ }).timeout(timeout).set({
22
+ 'x-api-key': ASSET_MANAGER_TVM_API_KEY
23
+ }).then(result => result.body);
24
+ } catch (error) {
25
+ logger.error("Failed requesting Asset Manager TVM API", error, {
26
+ [MeltwaterAttributes.COMPANYID]: companyId
27
+ });
28
+ throw error;
29
+ }
30
+ return response;
31
+ }
28
32
  export const assetManagerTvmRepository = new AssetManagerTVMRepository();
@@ -1,8 +1,11 @@
1
- import { loggerError } from '../../lib/logger.helpers.js';
1
+ import { loggerError, loggerInfo } from '../../lib/logger.helpers.js';
2
2
  import superagent from 'superagent';
3
3
  import configuration from '../../lib/configuration.js';
4
+ import { getAssetManagerTVM } from './asset-manager-tvm.client.js';
5
+ import AWS from 'aws-sdk';
4
6
  const gatewayURL = configuration.get('ASSETMANAGER_GATEWAY_URL');
5
- export async function getURL(jwt, assetId, logger) {
7
+ const ASSET_LIBRARY_S3_BUCKET = configuration.get('ASSET_LIBRARY_S3_BUCKET');
8
+ export async function getAsset(jwt, assetId, logger) {
6
9
  try {
7
10
  const response = await superagent.post(`${gatewayURL}`).set('apollographql-client-name', 'phoenix-conversations').set('Authorization', jwt).send({
8
11
  query: `
@@ -12,7 +15,14 @@ export async function getURL(jwt, assetId, logger) {
12
15
  assets(filter: $filter) {
13
16
  edges {
14
17
  node {
18
+ fileName
15
19
  url
20
+ size {
21
+ bytes
22
+ }
23
+ path
24
+ type
25
+ mimeType
16
26
  }
17
27
  }
18
28
  }
@@ -26,10 +36,44 @@ export async function getURL(jwt, assetId, logger) {
26
36
  }
27
37
  }
28
38
  });
29
- return response.body;
39
+ return response.body?.data?.assets?.edges[0]?.node;
30
40
  } catch (exception) {
31
41
  // How would you prefer to handle this
32
42
  loggerError(logger, 'Error getting asset url', exception);
33
43
  throw exception;
34
44
  }
45
+ }
46
+ async function getAssetManagerS3Instance(companyId) {
47
+ let timeout = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 5000;
48
+ const {
49
+ Credentials: {
50
+ AccessKeyId,
51
+ SecretAccessKey,
52
+ SessionToken
53
+ } = {}
54
+ } = await getAssetManagerTVM(companyId);
55
+ if (!AccessKeyId || !SecretAccessKey || !SessionToken) {
56
+ // probably a bit extreme, what would you prefer to do
57
+ throw 'No Credentials available';
58
+ }
59
+ const s3 = new AWS.S3({
60
+ accessKeyId: AccessKeyId,
61
+ secretAccessKey: SecretAccessKey,
62
+ sessionToken: SessionToken,
63
+ useAccelerateEndpoint: true
64
+ });
65
+ return s3;
66
+ }
67
+ function sanitizeAssetLibraryFilePath(filePath) {
68
+ const [_, ...sanitizedFilePath] = filePath.split('/');
69
+ return sanitizedFilePath.join('/');
70
+ }
71
+ export async function getAssetDataBuffer(path, fileName, companyId, logger) {
72
+ const s3 = await getAssetManagerS3Instance(companyId, 15000);
73
+ const config = {
74
+ Bucket: ASSET_LIBRARY_S3_BUCKET,
75
+ Key: sanitizeAssetLibraryFilePath(path + fileName).replace(`/${ASSET_LIBRARY_S3_BUCKET}/`, '')
76
+ };
77
+ const s3Response = await s3.getObject(config).promise();
78
+ return s3Response.Body;
35
79
  }
@@ -7,7 +7,7 @@ export async function comment(token, business_id, video_id, text, logger) {
7
7
  body: publishedMessage
8
8
  } = (await sendPost(token, 'comment/create/', {
9
9
  business_id: business_id,
10
- video_id: video_id.startsWith('tt_') ? video_id.split('_')[2] : video_id,
10
+ video_id: removePrefix(video_id.startsWith('tt_') ? video_id.split('_')[2] : video_id),
11
11
  text
12
12
  }, logger)) || {};
13
13
  return publishedMessage;
@@ -6,6 +6,10 @@ import crypto from 'crypto';
6
6
  import axios from 'axios';
7
7
  import fs from 'fs';
8
8
  import Twit from '@meltwater/social-twit';
9
+ import { fileURLToPath } from 'url';
10
+ import { dirname } from 'path';
11
+ const __filename = fileURLToPath(import.meta.url);
12
+ const __dirname = dirname(__filename);
9
13
  export function addOAuthToToken(token, TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET) {
10
14
  if (!token.oauth) {
11
15
  token.oauth = OAuth({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@meltwater/conversations-api-services",
3
- "version": "1.0.35",
3
+ "version": "1.0.36",
4
4
  "description": "Repository to contain all conversations api services shared across our services",
5
5
  "main": "dist/cjs/data-access/index.js",
6
6
  "module": "dist/esm/data-access/index.js",
@@ -9,27 +9,32 @@ class AssetManagerTVMRepository {
9
9
  constructor() { }
10
10
 
11
11
  async get(companyId) {
12
- let response;
13
- try {
14
- response = await superagent
15
- .get(`https://${ASSET_MANAGER_TVM_URL}/tvm`)
16
- .query({
17
- companyId,
18
- })
19
- .timeout(5000)
20
- .set({
21
- 'x-api-key': ASSET_MANAGER_TVM_API_KEY,
22
- })
23
- .then((result) => result.body);
24
- } catch (error) {
25
- logger.error("Failed requesting Asset Manager TVM API", error, {
26
- [MeltwaterAttributes.COMPANYID]: companyId
27
- });
28
- throw error;
29
- }
12
+ return getAssetManagerTVM(companyId)
13
+ }
14
+ }
30
15
 
31
- return response;
16
+ export async function getAssetManagerTVM(companyId, timeout = 5000){
17
+
18
+ let response;
19
+ try {
20
+ response = await superagent
21
+ .get(`https://${ASSET_MANAGER_TVM_URL}/tvm`)
22
+ .query({
23
+ companyId,
24
+ })
25
+ .timeout(timeout)
26
+ .set({
27
+ 'x-api-key': ASSET_MANAGER_TVM_API_KEY,
28
+ })
29
+ .then((result) => result.body);
30
+ } catch (error) {
31
+ logger.error("Failed requesting Asset Manager TVM API", error, {
32
+ [MeltwaterAttributes.COMPANYID]: companyId
33
+ });
34
+ throw error;
32
35
  }
36
+
37
+ return response;
33
38
  }
34
39
 
35
40
  export const assetManagerTvmRepository = new AssetManagerTVMRepository();
@@ -1,11 +1,17 @@
1
- import { loggerError } from '../../lib/logger.helpers.js';
1
+ import { loggerError, loggerInfo } from '../../lib/logger.helpers.js';
2
2
  import superagent from 'superagent';
3
3
  import configuration from '../../lib/configuration.js';
4
+ import { getAssetManagerTVM } from './asset-manager-tvm.client.js';
5
+ import AWS from 'aws-sdk';
6
+
4
7
  const gatewayURL = configuration.get(
5
8
  'ASSETMANAGER_GATEWAY_URL'
6
9
  );
7
10
 
8
- export async function getURL(jwt, assetId, logger) {
11
+ const ASSET_LIBRARY_S3_BUCKET= configuration.get(
12
+ 'ASSET_LIBRARY_S3_BUCKET'
13
+ );
14
+ export async function getAsset(jwt, assetId, logger) {
9
15
  try {
10
16
  const response = await superagent
11
17
  .post(`${gatewayURL}`)
@@ -19,7 +25,14 @@ export async function getURL(jwt, assetId, logger) {
19
25
  assets(filter: $filter) {
20
26
  edges {
21
27
  node {
28
+ fileName
22
29
  url
30
+ size {
31
+ bytes
32
+ }
33
+ path
34
+ type
35
+ mimeType
23
36
  }
24
37
  }
25
38
  }
@@ -36,10 +49,48 @@ export async function getURL(jwt, assetId, logger) {
36
49
  },
37
50
  },
38
51
  });
39
- return response.body;
52
+ return response.body?.data?.assets?.edges[0]?.node;
40
53
  } catch (exception) {
41
54
  // How would you prefer to handle this
42
55
  loggerError(logger,'Error getting asset url', exception);
43
56
  throw exception;
44
57
  }
58
+ }
59
+
60
+ async function getAssetManagerS3Instance(companyId, timeout = 5000) {
61
+ const {
62
+ Credentials: { AccessKeyId, SecretAccessKey, SessionToken } = {},
63
+ } = await getAssetManagerTVM(companyId);
64
+
65
+ if (!AccessKeyId || !SecretAccessKey || !SessionToken) {
66
+ // probably a bit extreme, what would you prefer to do
67
+ throw 'No Credentials available';
68
+ }
69
+
70
+ const s3 = new AWS.S3({
71
+ accessKeyId: AccessKeyId,
72
+ secretAccessKey: SecretAccessKey,
73
+ sessionToken: SessionToken,
74
+ useAccelerateEndpoint: true,
75
+ });
76
+
77
+ return s3;
78
+ }
79
+
80
+ function sanitizeAssetLibraryFilePath(filePath) {
81
+ const [_, ...sanitizedFilePath] = filePath.split('/');
82
+ return sanitizedFilePath.join('/');
83
+ }
84
+
85
+ export async function getAssetDataBuffer(path, fileName, companyId,logger) {
86
+ const s3 = await getAssetManagerS3Instance(companyId, 15000);
87
+ const config = {
88
+ Bucket: ASSET_LIBRARY_S3_BUCKET,
89
+ Key: sanitizeAssetLibraryFilePath(
90
+ path + fileName
91
+ ).replace(`/${ASSET_LIBRARY_S3_BUCKET}/`, ''),
92
+ };
93
+
94
+ const s3Response = await s3.getObject(config).promise();
95
+ return s3Response.Body;
45
96
  }
@@ -11,9 +11,9 @@ export async function comment(token,business_id,video_id,text,logger) {
11
11
  'comment/create/',
12
12
  {
13
13
  business_id: business_id,
14
- video_id: video_id.startsWith('tt_')
14
+ video_id: removePrefix(video_id.startsWith('tt_')
15
15
  ? video_id.split('_')[2]
16
- : video_id,
16
+ : video_id),
17
17
  text,
18
18
  },
19
19
  logger,
@@ -7,6 +7,12 @@ import axios from 'axios';
7
7
  import fs from 'fs';
8
8
  import Twit from '@meltwater/social-twit';
9
9
 
10
+ import { fileURLToPath } from 'url';
11
+ import { dirname } from 'path';
12
+
13
+ const __filename = fileURLToPath(import.meta.url);
14
+ const __dirname = dirname(__filename);
15
+
10
16
  export function addOAuthToToken(token, TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET) {
11
17
  if(!token.oauth){
12
18
  token.oauth = OAuth({