@subiz/widget-v4 4.1.2 → 4.1.5

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.
Files changed (108) hide show
  1. package/compiled_src/chatbox/ChatButton.js +25 -0
  2. package/compiled_src/lib.js +18 -3
  3. package/compiled_src/notif/notif.js +17 -11
  4. package/compiled_src/store/api.js +116 -108
  5. package/compiled_src/store/api_fake.js +4 -5
  6. package/compiled_src/store/store.js +51 -35
  7. package/dist/132.js +6611 -0
  8. package/dist/178.js +406 -0
  9. package/dist/18575b13d4f.132.js +1 -0
  10. package/dist/18575b13d4f.178.js +1 -0
  11. package/dist/18575b13d4f.213.js +1 -0
  12. package/dist/18575b13d4f.335.js +1 -0
  13. package/dist/18575b13d4f.362.js +1 -0
  14. package/dist/18575b13d4f.441.js +1 -0
  15. package/dist/18575b13d4f.605.js +1 -0
  16. package/dist/18575b13d4f.678.js +1 -0
  17. package/dist/18575b13d4f.782.js +1 -0
  18. package/dist/18575b13d4f.app.js +1 -0
  19. package/dist/18575b13d4f.index.html +26 -0
  20. package/dist/18575b13d4f.library.html +26 -0
  21. package/dist/18575b13d4f.library.js +1 -0
  22. package/dist/18575b13d4f.preview.html +759 -0
  23. package/dist/18575b13d4f.preview.js +1 -0
  24. package/dist/18575b13d4f.screenshoot.js +1 -0
  25. package/dist/18575b13d4f.webview.js +1 -0
  26. package/dist/213.js +2774 -0
  27. package/dist/335.js +372 -0
  28. package/dist/362.js +107 -0
  29. package/dist/441.js +2855 -0
  30. package/dist/605.js +303 -0
  31. package/dist/678.js +107 -0
  32. package/dist/782.js +107 -0
  33. package/dist/HASH +1 -0
  34. package/dist/assets/img/Facebook.64607ec.png +0 -0
  35. package/dist/assets/img/Zalo.6845707.png +0 -0
  36. package/dist/assets/img/award1a.1a58e10.png +0 -0
  37. package/dist/assets/img/award3a.3b74423.png +0 -0
  38. package/dist/assets/img/award4a.ed6ccd4.png +0 -0
  39. package/dist/assets/img/award5.39eb7ae.png +0 -0
  40. package/dist/assets/img/bubble_default.7d5e4ab.svg +20 -0
  41. package/dist/assets/img/caret.998eb8f.svg +1 -0
  42. package/dist/assets/img/convos_empty.2353007.svg +72 -0
  43. package/dist/assets/img/default.06e6ee0.jpg +0 -0
  44. package/dist/assets/img/default_avatar.5b74dc1.png +0 -0
  45. package/dist/assets/img/done_gray.d7e8203.svg +4 -0
  46. package/dist/assets/img/done_green.b817fd4.svg +4 -0
  47. package/dist/assets/img/emoji/messenger/sub_23.529_58.824.d5faf14.png +0 -0
  48. package/dist/assets/img/emoji/messenger/sub_23.529_60.784.1420241.png +0 -0
  49. package/dist/assets/img/emoji/messenger/sub_25.490_0.000.1e6b2c0.png +0 -0
  50. package/dist/assets/img/emoji/messenger/sub_25.490_45.098.3dfbc08.png +0 -0
  51. package/dist/assets/img/emoji/messenger/sub_25.490_5.882.4b49e94.png +0 -0
  52. package/dist/assets/img/emoji/messenger/sub_27.451_72.549.7114f58.png +0 -0
  53. package/dist/assets/img/emoji/messenger/sub_27.451_84.314.231e2b7.png +0 -0
  54. package/dist/assets/img/emoji/messenger/sub_27.451_96.078.ddbb008.png +0 -0
  55. package/dist/assets/img/emoji/messenger/sub_29.412_5.882.61f3359.png +0 -0
  56. package/dist/assets/img/emoji/messenger/sub_58.824_100.000.6df72bc.png +0 -0
  57. package/dist/assets/img/emoji/messenger/sub_58.824_45.098.3d7a7ad.png +0 -0
  58. package/dist/assets/img/emoji/messenger/sub_58.824_47.059.0f4d6e2.png +0 -0
  59. package/dist/assets/img/emoji/messenger/sub_58.824_54.902.65724c1.png +0 -0
  60. package/dist/assets/img/emoji/messenger/sub_58.824_64.706.9da3cba.png +0 -0
  61. package/dist/assets/img/emoji/messenger/sub_58.824_72.549.edca5cf.png +0 -0
  62. package/dist/assets/img/emoji/messenger/sub_58.824_78.431.f910f2a.png +0 -0
  63. package/dist/assets/img/emoji/messenger/sub_58.824_88.235.def4e71.png +0 -0
  64. package/dist/assets/img/emoji/messenger/sub_60.784_11.765.49c03d0.png +0 -0
  65. package/dist/assets/img/emoji/messenger/sub_60.784_27.451.46b54e5.png +0 -0
  66. package/dist/assets/img/emoji/messenger/sub_60.784_29.412.e36d284.png +0 -0
  67. package/dist/assets/img/emoji/messenger/sub_60.784_3.922.bb191b0.png +0 -0
  68. package/dist/assets/img/emoji/messenger/sub_60.784_35.294.af7b927.png +0 -0
  69. package/dist/assets/img/emoji/messenger/sub_60.784_7.843.98f2a99.png +0 -0
  70. package/dist/assets/img/empty.29bed2d.svg +23 -0
  71. package/dist/assets/img/empty_1.0b98af0.svg +11 -0
  72. package/dist/assets/img/examplte_logo.f86535f.svg +1 -0
  73. package/dist/assets/img/external-link.90577cc.svg +1 -0
  74. package/dist/assets/img/fb_inverse_circle.36c2cfc.png +0 -0
  75. package/dist/assets/img/ghn.070f8e3.svg +17 -0
  76. package/dist/assets/img/ghtk.326eac1.svg +18 -0
  77. package/dist/assets/img/image_default.be0698b.png +0 -0
  78. package/dist/assets/img/info.ebce7ea.svg +1 -0
  79. package/dist/assets/img/loader.37000a7.svg +1 -0
  80. package/dist/assets/img/loading_static.8f403ae.svg +10 -0
  81. package/dist/assets/img/main-service1.d536cc2.png +0 -0
  82. package/dist/assets/img/main-service2.9b99549.png +0 -0
  83. package/dist/assets/img/not_found_search.2ddb8d1.png +0 -0
  84. package/dist/assets/img/package.8d37d0f.svg +5 -0
  85. package/dist/assets/img/package_fade.763c001.svg +1 -0
  86. package/dist/assets/img/rating--average.1ea492c.svg +12 -0
  87. package/dist/assets/img/rating--great.e3d712e.svg +16 -0
  88. package/dist/assets/img/rating--poor.2938a25.svg +12 -0
  89. package/dist/assets/img/search.f9705ff.svg +1 -0
  90. package/dist/assets/img/send.6db52a9.svg +3 -0
  91. package/dist/assets/img/shop.f7dc7d8.svg +1 -0
  92. package/dist/assets/img/shop_faded.df7845a.svg +1 -0
  93. package/dist/assets/img/subiz-mono.877e9f5.svg +3 -0
  94. package/dist/assets/img/x.26fdfa3.svg +5 -0
  95. package/dist/malicious-app.js +13 -0
  96. package/dist/screenshoot.html +7 -0
  97. package/dist/screenshoot.js +5608 -0
  98. package/inject.js +2 -6
  99. package/package.json +9 -8
  100. package/src/chatbox/ChatButton.js +30 -0
  101. package/src/lib.js +23 -3
  102. package/src/notif/notif.js +40 -30
  103. package/src/store/api.js +139 -114
  104. package/src/store/api_fake.js +1 -2
  105. package/src/store/store.js +53 -44
  106. package/webpack.dev.js +1 -0
  107. package/webpack.prod.js +1 -0
  108. package/webview.js +43 -0
package/inject.js CHANGED
@@ -173,10 +173,7 @@ function runCallButton(subiz) {
173
173
  layout: 'classic',
174
174
  classic_layout: {
175
175
  picture_url: '',
176
- message_style: {
177
-
178
- },
179
-
176
+ message_style: {},
180
177
  },
181
178
  image_url: '',
182
179
 
@@ -478,8 +475,7 @@ function runPopup2(subiz) {
478
475
  z_index: '2',
479
476
  },
480
477
  image: {
481
- url:
482
- 'https://images.unsplash.com/photo-1534349735944-2b3a6f7a268f?crop=entropy&cs=tinysrgb&fit=max&fm=jpg&ixid=MnwxMjA3fDB8MXxzZWFyY2h8NTJ8fGRpc2NvdW50fGVufDB8fHx8MTYyMDg3OTM2OQ&ixlib=rb-1.2.1&q=80&w=1080',
478
+ url: 'https://images.unsplash.com/photo-1534349735944-2b3a6f7a268f?crop=entropy&cs=tinysrgb&fit=max&fm=jpg&ixid=MnwxMjA3fDB8MXxzZWFyY2h8NTJ8fGRpc2NvdW50fGVufDB8fHx8MTYyMDg3OTM2OQ&ixlib=rb-1.2.1&q=80&w=1080',
483
479
  },
484
480
  _id: 'xmfemhpj_0',
485
481
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@subiz/widget-v4",
3
- "version": "4.1.2",
3
+ "version": "4.1.5",
4
4
  "main": "./compiled_src/lib.js",
5
5
  "scripts": {
6
6
  "dev": "npx webpack serve --config webpack.dev.js",
@@ -12,26 +12,27 @@
12
12
  "@subiz/ajax": "^3.0.12",
13
13
  "@subiz/flow": "^1.0.9",
14
14
  "@subiz/storage": "0.0.8",
15
- "@subiz/wsclient": "^3.1.7",
15
+ "@subiz/wsclient": "^3.1.9",
16
16
  "cookie_js": "1.4.2",
17
- "core-js": "^3.26.1",
17
+ "core-js": "^3.27.0",
18
18
  "css-loader": "5.2.7",
19
19
  "file-loader": "^6.2.0",
20
20
  "less": "^4.1.3",
21
21
  "less-loader": "^11.1.0",
22
- "marked": "^4.2.4",
22
+ "marked": "^4.2.5",
23
23
  "preact": "^10.11.3"
24
24
  },
25
25
  "devDependencies": {
26
- "@babel/cli": "^7.19.3",
27
- "@babel/core": "^7.20.5",
26
+ "@babel/cli": "^7.20.7",
27
+ "@babel/core": "^7.20.7",
28
28
  "@babel/plugin-proposal-class-properties": "^7.18.6",
29
- "@babel/plugin-transform-react-jsx": "^7.19.0",
29
+ "@babel/plugin-transform-react-jsx": "^7.20.7",
30
30
  "@babel/preset-env": "^7.20.2",
31
31
  "babel-loader": "^9.1.0",
32
32
  "babel-plugin-syntax-jsx": "^6.18.0",
33
33
  "html-webpack-plugin": "^5.5.0",
34
- "puppeteer": "^19.4.0",
34
+ "prettier": "^2.8.1",
35
+ "puppeteer": "^19.4.1",
35
36
  "webpack": "^5.75.0",
36
37
  "webpack-bundle-analyzer": "^4.7.0",
37
38
  "webpack-cli": "^5.0.1",
@@ -343,6 +343,9 @@ export default class ChatButton extends Com {
343
343
  let message = sb.get(ev, 'data.message.text')
344
344
  var format = sb.get(ev, 'data.message.format')
345
345
 
346
+ // render delta preview message
347
+ if (format === 'delta') return this.renderDeltaMessagePreview(ev, defmsg)
348
+
346
349
  // cant display message
347
350
  if (format && format !== 'plaintext') message = defmsg
348
351
  if (!message && sb.size(sb.get(ev, 'data.message.attachments')) > 0) message = defmsg
@@ -357,6 +360,33 @@ export default class ChatButton extends Com {
357
360
  return tags
358
361
  }
359
362
 
363
+ renderDeltaMessagePreview(ev, defmsg) {
364
+ defmsg = defmsg || 'Sent you a message'
365
+
366
+ let message = sb.get(ev, 'data.message.quill_delta')
367
+ if (!message && sb.size(sb.get(ev, 'data.message.attachments')) > 0) message = defmsg
368
+ if (!message) return
369
+
370
+ let quilldeltas = sb.parseJSON(message) || {}
371
+ // deltas = deltas.ops || []
372
+ if (!quilldeltas || sb.size(quilldeltas.ops) === 0) return null
373
+ let $deltas = sb.map(quilldeltas.ops, (delta) => {
374
+ let html = ''
375
+ let insert = delta.insert
376
+ let attributes = delta.attributes
377
+
378
+ if (insert.dynamicField) {
379
+ return sb.get(insert, 'dynamicField.text', '')
380
+ }
381
+ if (insert.emoji) return <Emoji emojiCode={`:${insert.emoji}:`} />
382
+ if (typeof insert === 'string') {
383
+ return insert
384
+ }
385
+ return null
386
+ })
387
+ return $deltas
388
+ }
389
+
360
390
  closePreview(e) {
361
391
  if (e) {
362
392
  e.preventDefault()
package/src/lib.js CHANGED
@@ -43,14 +43,34 @@ class Widget {
43
43
  return user
44
44
  }
45
45
 
46
- logout(accid) {
47
- return this.store.logout()
46
+ logout(userref) {
47
+ return this.store.logout(userref)
48
+ }
49
+
50
+ async init(param, $container) {
51
+ param = param || {}
52
+ let accid = param.account_id
53
+ let ref = param.ref
54
+
55
+ let user = await this.store.init(accid, undefined, ref)
56
+ // add container to client site
57
+ if (!$container) $container = document.body || document
58
+ this.pluginMgr = new PluginMgr(this, $container)
59
+
60
+ let clean = setInterval(() => {
61
+ // check if the dom is deleted
62
+ if ($container && $container.isConnected) return
63
+ this.pluginMgr.destroy()
64
+ clearInterval(clean)
65
+ }, 1000)
66
+ this.domGate.open()
67
+ return user
48
68
  }
49
69
 
50
70
  async setAccount(id, $container, mask, cb) {
51
71
  let outmask = await this.store.init(id, mask)
52
72
  // add container to client site
53
- if (!$container) $container = document.body
73
+ if (!$container) $container = document.body || document
54
74
  this.pluginMgr = new PluginMgr(this, $container)
55
75
 
56
76
  let clean = setInterval(() => {
@@ -76,7 +76,8 @@ export default class Notif extends Com {
76
76
  height='24'
77
77
  viewBox='0 0 24 24'
78
78
  stroke-linecap='round'
79
- stroke-linejoin='round'>
79
+ stroke-linejoin='round'
80
+ >
80
81
  <line x1='18' y1='6' x2='6' y2='18' style='stroke-width: 3 !important;'></line>
81
82
  <line x1='6' y1='6' x2='18' y2='18' style='stroke-width: 3 !important;'></line>
82
83
  </svg>
@@ -196,22 +197,24 @@ export default class Notif extends Com {
196
197
  renderTextFromQuillDelta(quilldelta, profile) {
197
198
  let qd = sb.parseJSON(quilldelta)
198
199
  if (!qd || !qd.ops) return null
199
- return sb.map(qd.ops, (op) => {
200
- let out = null
201
- if (!op.insert) return null
202
-
203
- if (typeof op.insert == 'string') out = op.insert
204
- if (sb.get(op, 'insert.dynamicField')) {
205
- let key = sb.get(op, 'insert.dynamicField.key')
206
- if (key === 'user.fullname') out = profile.fullname
207
- if (key === 'user.email') out = profile.email
208
- if (key === 'user.address') out = profile.address
209
- if (key === 'user.phone') out = profile.phone
210
- }
211
- if (sb.get(op, 'attributes.bold')) out = <b>{out}</b>
212
- if (sb.get(op, 'attributes.italic')) out = <b>{out}</b>
213
- return out
214
- })
200
+ return sb
201
+ .map(qd.ops, (op) => {
202
+ let out = null
203
+ if (!op.insert) return null
204
+
205
+ if (typeof op.insert == 'string') out = op.insert
206
+ if (sb.get(op, 'insert.dynamicField')) {
207
+ let key = sb.get(op, 'insert.dynamicField.key')
208
+ if (key === 'user.fullname') out = profile.fullname
209
+ if (key === 'user.email') out = profile.email
210
+ if (key === 'user.address') out = profile.address
211
+ if (key === 'user.phone') out = profile.phone
212
+ }
213
+ if (sb.get(op, 'attributes.bold')) out = <b>{out}</b>
214
+ if (sb.get(op, 'attributes.italic')) out = <b>{out}</b>
215
+ return out
216
+ })
217
+ .filter((a) => a)
215
218
  }
216
219
 
217
220
  render() {
@@ -245,13 +248,27 @@ export default class Notif extends Com {
245
248
  let st = 'position: fixed !important; z-index: 1000000000 !important'
246
249
  if (this.props.plugin._preview) st = 'position: absolute !important;'
247
250
  if (this.state.closeAnimation) animation = 'fadeOut'
251
+ let $avatar = null
252
+ if (user.avatar_url) $avatar = <img alt='notification avatar' class='notif_avatar__img' src={user.avatar_url} />
253
+
254
+ let $time = (
255
+ <div class='notif_time' style={sb.js2css(notif.subtext_style)}>
256
+ {this.state.timestamp}
257
+ </div>
258
+ )
259
+
260
+ let $title = this.renderTextFromQuillDelta(this.state.title, user)
261
+ let $message = this.renderTextFromQuillDelta(this.state.message, user)
262
+ if (!lo.size($title) && !lo.size($message)) $time = null
263
+
248
264
  return (
249
265
  <div
250
266
  class={animation + ' notif' + ' notif__' + pos}
251
267
  onClick={(e) => this.onClick(e)}
252
268
  onmouseover={(e) => this.setOver(true)}
253
269
  style={st}
254
- onmouseleave={(e) => this.setOver(false)}>
270
+ onmouseleave={(e) => this.setOver(false)}
271
+ >
255
272
  <div style='position: relative !important; display: flex !important; align-items: center !important;'>
256
273
  <img
257
274
  alt='notification background'
@@ -261,18 +278,16 @@ export default class Notif extends Com {
261
278
  />
262
279
  {this.renderCloseButton()}
263
280
  <div class='notif_avatar' style={sb.js2css(notif.avatar_style)}>
264
- <img alt='notification avatar' class='notif_avatar__img' src={user.avatar_url} />
281
+ {$avatar}
265
282
  </div>
266
283
  <div class='notif_body'>
267
284
  <div class='notif_title' style={sb.js2css(notif.title_style)}>
268
- {this.renderTextFromQuillDelta(this.state.title, user)}
285
+ {$title}
269
286
  </div>
270
287
  <div class='notif_message' style={sb.js2css(notif.message_style)}>
271
- {this.renderTextFromQuillDelta(this.state.message, user)}
272
- </div>
273
- <div class='notif_time' style={sb.js2css(notif.subtext_style)}>
274
- {this.state.timestamp}
288
+ {$message}
275
289
  </div>
290
+ {$time}
276
291
  </div>
277
292
  </div>
278
293
  </div>
@@ -303,12 +318,7 @@ function ramdomPhone() {
303
318
  }
304
319
 
305
320
  function randomEmail() {
306
- return (
307
- randomName()
308
- .split(' ')
309
- .join('.')
310
- .toLowerCase() + '@gmail.com'
311
- )
321
+ return randomName().split(' ').join('.').toLowerCase() + '@gmail.com'
312
322
  }
313
323
 
314
324
  function randomAddress() {
package/src/store/api.js CHANGED
@@ -3,8 +3,6 @@ import sb from '../util/index.js'
3
3
  const ajax = require('@subiz/ajax/src/ajax.js')
4
4
  ajax.env.XMLHttpRequest = window.XMLHttpRequest
5
5
 
6
- const findAnswer = (challenge) => challenge.answer
7
-
8
6
  function jsonifyTextPlain(param, done) {
9
7
  let req = param.request
10
8
  if (req.content_type === 'text/plain' && req.body !== undefined) req.body = sb.stringify(req.body)
@@ -38,7 +36,9 @@ export default class {
38
36
  return
39
37
  }
40
38
 
41
- if (this.mask) param.request = param.request.addQuery('x-user-mask', this.mask)
39
+ if (this.ref) param.request = param.request.addQuery('x-user-ref', this.ref)
40
+ else if (this.mask) param.request = param.request.addQuery('x-user-mask', this.mask) // deprecating
41
+
42
42
  return done(true)
43
43
  })
44
44
  // .beforeHook((param, done) => setTimeout((_) => done(true), 1000))
@@ -48,115 +48,155 @@ export default class {
48
48
  this.agentid = agid
49
49
  }
50
50
 
51
- setAuth(obj) {
52
- this.accid = obj.accid
53
- this.mask = obj.mask
54
- this.uid = obj.uid
55
- }
56
-
57
- async setTokenGenerator(accid, uid, tokenGenerator) {
58
- this.tokensession = this.tokensession || 0
59
- this.tokensession++
60
- let tokensession = this.tokensession
51
+ async setAuth(obj) {
52
+ if (obj.accid) this.accid = obj.accid
53
+ if (obj.ref) this.ref = obj.ref
61
54
 
62
- if (!accid || !sb.startsWith(accid, 'ac')) return {error: 'invalid_account_id'}
63
- if (!uid) return {error: 'invalid_user_id'}
64
- if (typeof tokenGenerator !== 'function') return {error: 'invalid_token_generator'}
65
-
66
- this.accid = accid
67
- this.profile_id = uid
68
- this.token_generator = async (force) => {
69
- await this.refreshLock.lock()
70
- if (tokensession != this.tokensession) {
71
- this.refreshLock.unlock()
72
- return // outdated
55
+ this.mask = obj.mask
56
+ this.profile_id = obj.profile_id
57
+ if (obj.ref) {
58
+ let outuser
59
+ for (let i = 0; true; i++) {
60
+ outuser = await ajax
61
+ .setBaseUrl(this.ApiURL)
62
+ .withCredentials(true)
63
+ .addQuery('v', '6')
64
+ .addQuery('x-user-ref', this.ref)
65
+ .setParser('json')
66
+ .get(`/accounts/${this.accid}/users/${this.ref}`)
67
+ if (!sb.get(outuser, 'body.id')) {
68
+ let sleeptime = Math.min(2000 + i * 200, 60000)
69
+ await sb.sleep(sleeptime)
70
+ continue
71
+ }
72
+ break
73
73
  }
74
- let age = (sb.Date.now() - (this.last_token_at || 0)) / 1000
75
74
 
76
- // very good token, use this one
77
- if (!force && this.last_token && age < 1800) {
78
- this.refreshLock.unlock()
79
- return this.last_token
75
+ if (outuser.error) {
76
+ if (sb.get(outuser.body.code)) return {error: outuser.body.code}
77
+ return outuser
80
78
  }
79
+ this.uid = outuser.body.id
80
+ return outuser.body
81
+ }
81
82
 
82
- if (!force && this.last_token && age < 3000) {
83
- // the token is still good, but should get new one
84
- if (this.token_generator)
85
- setTimeout(async () => {
86
- let token, err
87
- try {
88
- token = await tokenGenerator(uid)
89
- } catch (e) {
90
- err = e
91
- }
92
- if (tokensession != this.tokensession) {
83
+ if (obj.tokenGenerator) {
84
+ this.tokensession = this.tokensession || 0
85
+ this.tokensession++
86
+ let tokensession = this.tokensession
87
+
88
+ // validating input
89
+ if (!this.accid || !sb.startsWith(this.accid, 'ac')) return {error: 'invalid_account_id'}
90
+ if (!this.profile_id) return {error: 'invalid_profile_id'}
91
+ if (typeof this.tokenGenerator !== 'function') return {error: 'invalid_token_generator'}
92
+
93
+ let profile_id = this.profile_id
94
+ this.token_generator = async (force) => {
95
+ await this.refreshLock.lock()
96
+ if (tokensession != this.tokensession) {
97
+ this.refreshLock.unlock()
98
+ return // outdated
99
+ }
100
+ let age = (sb.Date.now() - (this.last_token_at || 0)) / 1000
101
+
102
+ // very good token, use this one
103
+ if (!force && this.last_token && age < 1800) {
104
+ this.refreshLock.unlock()
105
+ return this.last_token
106
+ }
107
+
108
+ if (!force && this.last_token && age < 3000) {
109
+ // the token is still good, but should get new one
110
+ if (this.token_generator)
111
+ setTimeout(async () => {
112
+ let token, err
113
+ try {
114
+ token = await tokenGenerator(profile_id)
115
+ } catch (e) {
116
+ err = e
117
+ }
118
+ if (tokensession != this.tokensession) {
119
+ this.refreshLock.unlock()
120
+ return // outdated
121
+ }
122
+
123
+ if (err) this.last_token = 'error'
124
+ else this.last_token = token
125
+ this.last_token_at = sb.Date.now()
93
126
  this.refreshLock.unlock()
94
- return // outdated
95
- }
127
+ })
128
+ return this.last_token
129
+ }
96
130
 
97
- if (err) this.last_token = 'error'
98
- else this.last_token = token
99
- this.last_token_at = sb.Date.now()
100
- this.refreshLock.unlock()
101
- })
102
- return this.last_token
103
- }
131
+ // the token is outdated, must get new one
132
+ let token
133
+ let err
134
+ try {
135
+ token = await tokenGenerator(profile_id)
136
+ } catch (e) {
137
+ err = e
138
+ }
104
139
 
105
- // the token is outdated, must get new one
106
- let token
107
- let err
108
- try {
109
- token = await tokenGenerator(uid)
110
- } catch (e) {
111
- err = e
112
- }
140
+ if (tokensession != this.tokensession) {
141
+ this.refreshLock.unlock()
142
+ return // outdated
143
+ }
113
144
 
114
- if (tokensession != this.tokensession) {
145
+ if (err) this.last_token = 'error'
146
+ else this.last_token = token
147
+ this.last_token_at = sb.Date.now()
115
148
  this.refreshLock.unlock()
116
- return // outdated
149
+ return this.last_token
117
150
  }
118
151
 
119
- if (err) this.last_token = 'error'
120
- else this.last_token = token
121
- this.last_token_at = sb.Date.now()
122
- this.refreshLock.unlock()
123
- return this.last_token
124
- }
125
-
126
- let outuser
127
- for (let i = 0; true; i++) {
128
- await this.token_generator(true)
129
- if (tokensession != this.tokensession) break // outdated
130
- outuser = await ajax
131
- .setBaseUrl(this.ApiURL)
132
- .withCredentials(true)
133
- .addQuery('v', '6')
134
- .addQuery('x-access-token', this.last_token)
135
- .setParser('json')
136
- .get(`/accounts/${accid}/users/${uid}`)
137
- if (!sb.get(outuser, 'body.id')) {
138
- let sleeptime = Math.min(2000 + i * 100, 120000)
139
- await sb.sleep(sleeptime)
140
- continue
152
+ let outuser
153
+ for (let i = 0; true; i++) {
154
+ await this.token_generator(true)
155
+ if (tokensession != this.tokensession) break // outdated
156
+ outuser = await ajax
157
+ .setBaseUrl(this.ApiURL)
158
+ .withCredentials(true)
159
+ .addQuery('v', '6')
160
+ .addQuery('x-access-token', this.last_token)
161
+ .setParser('json')
162
+ .get(`/accounts/${accid}/users/${profile_id}`)
163
+ if (!sb.get(outuser, 'body.id')) {
164
+ let sleeptime = Math.min(2000 + i * 100, 120000)
165
+ await sb.sleep(sleeptime)
166
+ continue
167
+ }
168
+ break
141
169
  }
142
- break
170
+ if (outuser.error) {
171
+ if (sb.get(outuser.body.code)) return {error: outuser.body.code}
172
+ return outuser
173
+ }
174
+ this.uid = outuser.body.id
175
+ return outuser.body
143
176
  }
144
- if (outuser.error) {
145
- if (sb.get(outuser.body.code)) return {error: outuser.body.code}
146
- return outuser
177
+
178
+ if (this.mask) {
179
+ let parts = this.mask.split('@')
180
+ this.uid = parts[1]
181
+ return {id: this.uid}
147
182
  }
148
- this.uid = outuser.body.id
149
- return outuser.body
183
+
184
+ return {error: 'invalid_auth'}
150
185
  }
151
186
 
187
+ async setTokenGenerator(accid, uid, tokenGenerator) {}
188
+
152
189
  listConversations = (_) =>
153
190
  this.apireq
154
191
  .addQuery('user_id', this.uid)
155
192
  .addQuery('integration_id', `${this.accid}.subizv4.subikon`)
156
- .get('/conversations')
193
+ .get(`/accounts/${this.accid}/conversations`)
157
194
 
158
195
  listMessages = (cid, limit, start_id) =>
159
- this.apireq.addQuery('limit', limit).addQuery('start_id', start_id).get(`/conversations/${cid}/messages`)
196
+ this.apireq
197
+ .addQuery('limit', limit)
198
+ .addQuery('start_id', start_id)
199
+ .get(`/accounts/${this.accid}/conversations/${cid}/messages`)
160
200
 
161
201
  ajax = ajax
162
202
 
@@ -166,18 +206,20 @@ export default class {
166
206
  data[k] = v
167
207
  })
168
208
 
169
- return this.apireq.post('/conversations', data)
209
+ return this.apireq.post(`/accounts/${this.accid}/conversations`, data)
170
210
  }
171
211
 
172
212
  sendConvoEvent = (cid, ev) => this.apireq.post(`/accounts/${this.accid}/conversations/${cid}/events`, ev)
173
213
  createPostback = (cid, p) => this.apireq.post(`/accounts/${this.accid}/conversations/${cid}/postbacks`, p)
174
- markAsReceived = (cid, messageId) => this.apireq.post(`/conversations/${cid}/messages/${messageId}/receive`)
175
- markAsSeen = (cid, messageId) => this.apireq.post(`/conversations/${cid}/messages/${messageId}/seen`)
214
+ markAsReceived = (cid, messageId) =>
215
+ this.apireq.post(`/accounts/${this.accid}/conversations/${cid}/messages/${messageId}/receive`)
216
+ markAsSeen = (cid, messageId) =>
217
+ this.apireq.post(`/accounts/${this.accid}/conversations/${cid}/messages/${messageId}/seen`)
176
218
 
177
- getConversation = (cid) => this.apireq.get(`/conversations/${cid}`)
219
+ getConversation = (cid) => this.apireq.get(`/accounts/${this.accid}/conversations/${cid}`)
178
220
  getLocaleContent = (locale) => this.apireq.get(`/accounts/${this.accid}/locales/${locale}`)
179
- sendEvent = (data) => this.apireq.post(`/users/${this.uid}/events`, data)
180
- sendTyping = (cid) => this.apireq.post(`/conversations/${cid}/typing`)
221
+ sendEvent = (data) => this.apireq.post(`/accounts/${this.accid}/users/${this.uid}/events`, data)
222
+ sendTyping = (cid) => this.apireq.post(`/accounts/${this.accid}/conversations/${cid}/typing`)
181
223
 
182
224
  sendLostFocus = (device, productid, source_id, sku) =>
183
225
  this.sendEvent({
@@ -226,25 +268,8 @@ export default class {
226
268
  return {url: this.FileURL + body.url}
227
269
  }
228
270
 
229
- getChallenge = (accid) => this.apireq.post(`/accounts/${accid}/scrypt-challenges`)
230
-
231
- async createUser(accid) {
232
- for (var i = 0; i < 10; i++) {
233
- const {body: challenge} = await this.getChallenge(accid)
234
- const answer = findAnswer(challenge)
235
- const data = {challenge_id: challenge.id, answer: '' + answer}
236
-
237
- let {body: user, error} = await this.apireq.post(`/accounts/${accid}/scrypt-challenges/users`, data)
238
- if (error) {
239
- await sb.sleep(1000)
240
- continue
241
- }
242
- return user
243
- }
244
- }
245
-
246
- time = (_) => this.apireq.get('/time')
247
- updateUser = (data) => this.apireq.post(`/users/${this.uid}`, data)
271
+ time = (_) => ajax.setBaseUrl(this.ApiURL).withCredentials(true).setParser('json').get('/time')
272
+ updateUser = (data) => this.apireq.post(`/accounts/${this.accid}/users/${this.uid}`, data)
248
273
 
249
274
  getUser = (_) => this.apireq.get(`/accounts/${this.accid}/users/${this.uid}`)
250
275
  getAccountSetting = () => this.apireq.get(`/accounts/${this.accid}/settings`)
@@ -32,6 +32,7 @@ export default class {
32
32
  this.accid = obj.accid
33
33
  this.mask = obj.mask
34
34
  this.uid = obj.uid
35
+ return {account_id: accid, id: 'usqzgtgfake'}
35
36
  }
36
37
 
37
38
  listConversations = (_) => {
@@ -65,8 +66,6 @@ export default class {
65
66
  uploadToS3 = async (file) => OK200
66
67
  getChallenge = (accid) => OK200
67
68
 
68
- createUser = (accid) => Promise.resolve({account_id: accid, id: 'usqzgtgfake', mask: 'fakemask'})
69
-
70
69
  time = (_) => Promise.resolve({code: 200, body: new sb.Date().toISOString()})
71
70
  updateUser = (data) => OK200
72
71