@subiz/widget-v4 4.1.2 → 4.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.
Files changed (106) hide show
  1. package/compiled_src/chatbox/ChatButton.js +25 -0
  2. package/compiled_src/lib.js +18 -3
  3. package/compiled_src/store/api.js +116 -108
  4. package/compiled_src/store/api_fake.js +4 -5
  5. package/compiled_src/store/store.js +50 -35
  6. package/dist/132.js +6611 -0
  7. package/dist/178.js +406 -0
  8. package/dist/185588facf6.132.js +1 -0
  9. package/dist/185588facf6.178.js +1 -0
  10. package/dist/185588facf6.213.js +1 -0
  11. package/dist/185588facf6.335.js +1 -0
  12. package/dist/185588facf6.362.js +1 -0
  13. package/dist/185588facf6.441.js +1 -0
  14. package/dist/185588facf6.605.js +1 -0
  15. package/dist/185588facf6.678.js +1 -0
  16. package/dist/185588facf6.782.js +1 -0
  17. package/dist/185588facf6.app.js +1 -0
  18. package/dist/185588facf6.index.html +26 -0
  19. package/dist/185588facf6.library.html +26 -0
  20. package/dist/185588facf6.library.js +1 -0
  21. package/dist/185588facf6.preview.html +759 -0
  22. package/dist/185588facf6.preview.js +1 -0
  23. package/dist/185588facf6.screenshoot.js +1 -0
  24. package/dist/185588facf6.webview.js +1 -0
  25. package/dist/213.js +2774 -0
  26. package/dist/335.js +372 -0
  27. package/dist/362.js +107 -0
  28. package/dist/441.js +2855 -0
  29. package/dist/605.js +297 -0
  30. package/dist/678.js +107 -0
  31. package/dist/782.js +107 -0
  32. package/dist/HASH +1 -0
  33. package/dist/assets/img/Facebook.64607ec.png +0 -0
  34. package/dist/assets/img/Zalo.6845707.png +0 -0
  35. package/dist/assets/img/award1a.1a58e10.png +0 -0
  36. package/dist/assets/img/award3a.3b74423.png +0 -0
  37. package/dist/assets/img/award4a.ed6ccd4.png +0 -0
  38. package/dist/assets/img/award5.39eb7ae.png +0 -0
  39. package/dist/assets/img/bubble_default.7d5e4ab.svg +20 -0
  40. package/dist/assets/img/caret.998eb8f.svg +1 -0
  41. package/dist/assets/img/convos_empty.2353007.svg +72 -0
  42. package/dist/assets/img/default.06e6ee0.jpg +0 -0
  43. package/dist/assets/img/default_avatar.5b74dc1.png +0 -0
  44. package/dist/assets/img/done_gray.d7e8203.svg +4 -0
  45. package/dist/assets/img/done_green.b817fd4.svg +4 -0
  46. package/dist/assets/img/emoji/messenger/sub_23.529_58.824.d5faf14.png +0 -0
  47. package/dist/assets/img/emoji/messenger/sub_23.529_60.784.1420241.png +0 -0
  48. package/dist/assets/img/emoji/messenger/sub_25.490_0.000.1e6b2c0.png +0 -0
  49. package/dist/assets/img/emoji/messenger/sub_25.490_45.098.3dfbc08.png +0 -0
  50. package/dist/assets/img/emoji/messenger/sub_25.490_5.882.4b49e94.png +0 -0
  51. package/dist/assets/img/emoji/messenger/sub_27.451_72.549.7114f58.png +0 -0
  52. package/dist/assets/img/emoji/messenger/sub_27.451_84.314.231e2b7.png +0 -0
  53. package/dist/assets/img/emoji/messenger/sub_27.451_96.078.ddbb008.png +0 -0
  54. package/dist/assets/img/emoji/messenger/sub_29.412_5.882.61f3359.png +0 -0
  55. package/dist/assets/img/emoji/messenger/sub_58.824_100.000.6df72bc.png +0 -0
  56. package/dist/assets/img/emoji/messenger/sub_58.824_45.098.3d7a7ad.png +0 -0
  57. package/dist/assets/img/emoji/messenger/sub_58.824_47.059.0f4d6e2.png +0 -0
  58. package/dist/assets/img/emoji/messenger/sub_58.824_54.902.65724c1.png +0 -0
  59. package/dist/assets/img/emoji/messenger/sub_58.824_64.706.9da3cba.png +0 -0
  60. package/dist/assets/img/emoji/messenger/sub_58.824_72.549.edca5cf.png +0 -0
  61. package/dist/assets/img/emoji/messenger/sub_58.824_78.431.f910f2a.png +0 -0
  62. package/dist/assets/img/emoji/messenger/sub_58.824_88.235.def4e71.png +0 -0
  63. package/dist/assets/img/emoji/messenger/sub_60.784_11.765.49c03d0.png +0 -0
  64. package/dist/assets/img/emoji/messenger/sub_60.784_27.451.46b54e5.png +0 -0
  65. package/dist/assets/img/emoji/messenger/sub_60.784_29.412.e36d284.png +0 -0
  66. package/dist/assets/img/emoji/messenger/sub_60.784_3.922.bb191b0.png +0 -0
  67. package/dist/assets/img/emoji/messenger/sub_60.784_35.294.af7b927.png +0 -0
  68. package/dist/assets/img/emoji/messenger/sub_60.784_7.843.98f2a99.png +0 -0
  69. package/dist/assets/img/empty.29bed2d.svg +23 -0
  70. package/dist/assets/img/empty_1.0b98af0.svg +11 -0
  71. package/dist/assets/img/examplte_logo.f86535f.svg +1 -0
  72. package/dist/assets/img/external-link.90577cc.svg +1 -0
  73. package/dist/assets/img/fb_inverse_circle.36c2cfc.png +0 -0
  74. package/dist/assets/img/ghn.070f8e3.svg +17 -0
  75. package/dist/assets/img/ghtk.326eac1.svg +18 -0
  76. package/dist/assets/img/image_default.be0698b.png +0 -0
  77. package/dist/assets/img/info.ebce7ea.svg +1 -0
  78. package/dist/assets/img/loader.37000a7.svg +1 -0
  79. package/dist/assets/img/loading_static.8f403ae.svg +10 -0
  80. package/dist/assets/img/main-service1.d536cc2.png +0 -0
  81. package/dist/assets/img/main-service2.9b99549.png +0 -0
  82. package/dist/assets/img/not_found_search.2ddb8d1.png +0 -0
  83. package/dist/assets/img/package.8d37d0f.svg +5 -0
  84. package/dist/assets/img/package_fade.763c001.svg +1 -0
  85. package/dist/assets/img/rating--average.1ea492c.svg +12 -0
  86. package/dist/assets/img/rating--great.e3d712e.svg +16 -0
  87. package/dist/assets/img/rating--poor.2938a25.svg +12 -0
  88. package/dist/assets/img/search.f9705ff.svg +1 -0
  89. package/dist/assets/img/send.6db52a9.svg +3 -0
  90. package/dist/assets/img/shop.f7dc7d8.svg +1 -0
  91. package/dist/assets/img/shop_faded.df7845a.svg +1 -0
  92. package/dist/assets/img/subiz-mono.877e9f5.svg +3 -0
  93. package/dist/assets/img/x.26fdfa3.svg +5 -0
  94. package/dist/malicious-app.js +13 -0
  95. package/dist/screenshoot.html +7 -0
  96. package/dist/screenshoot.js +5608 -0
  97. package/inject.js +2 -6
  98. package/package.json +9 -8
  99. package/src/chatbox/ChatButton.js +30 -0
  100. package/src/lib.js +23 -3
  101. package/src/store/api.js +139 -114
  102. package/src/store/api_fake.js +1 -2
  103. package/src/store/store.js +53 -44
  104. package/webpack.dev.js +1 -0
  105. package/webpack.prod.js +1 -0
  106. 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.4",
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(() => {
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
 
@@ -15,6 +15,9 @@ const page_loaded = sb.Date.now()
15
15
 
16
16
  // available localstorage key mask, user, hasManuallyClosed, isopen, close_preview_at, lastview, bot_run_logs, plugin_run_logs
17
17
  export default class Store {
18
+ inmem = false
19
+ cookie = cookie
20
+
18
21
  constructor(ApiURL, FileURL, RealtimeURL, inmem, fakeapi) {
19
22
  this.g_sync = {}
20
23
  this.forceDevice = null
@@ -28,6 +31,17 @@ export default class Store {
28
31
  if (this.fake) this.api = new APIFake(ApiURL, FileURL)
29
32
  else this.api = new API(ApiURL, FileURL)
30
33
 
34
+ // test cookie
35
+ if (!inmem) {
36
+ try {
37
+ document.cookie
38
+ localStorage
39
+ } catch (e) {
40
+ // cookie and localstorage is disable => must inmem
41
+ inmem = true
42
+ }
43
+ }
44
+
31
45
  if (inmem) {
32
46
  this.inmem = inmem
33
47
  this.cookie = inmemCookie
@@ -75,9 +89,6 @@ export default class Store {
75
89
  }, 2000)
76
90
  }
77
91
 
78
- inmem = false
79
- cookie = cookie
80
-
81
92
  // inmem
82
93
  _currentViewingProduct = {}
83
94
  linkProduct(p) {
@@ -350,68 +361,65 @@ export default class Store {
350
361
 
351
362
  now = () => sb.Date.now() + this.timeDiff // millisecond on server
352
363
 
353
- async init(accid, mask) {
364
+ async init(accid, mask, userRef) {
354
365
  this.readyGate.close()
355
366
  accid = (accid || '').trim()
356
367
  this.api.setAuth({accid: accid})
357
-
358
368
  this.g_sync = {}
359
- // try to find mask in cookie
360
- if (!mask) {
369
+ if (!userRef) {
370
+ // try to find userRef in cookie
361
371
  try {
362
- mask = this.cookie.get('__sbmask', '')
372
+ userRef = this.cookie.get('__sbref', '')
363
373
  } catch (e) {}
364
- }
365
374
 
366
- let localstorageMask = this.kv.match('mask')
367
- // cookie is deleted, fallback to local storage
368
- if (!mask && localstorageMask) mask = localstorageMask
369
-
370
- // check mask integrity, mask in cookie should match mask in localstorate
371
- // trust cookie, remove localstorage
372
- if (mask && localstorageMask && mask !== localstorageMask) this.clearBrowserData()
373
-
374
- var userId = ''
375
- if (mask) {
376
- let parts = mask.split('@')
377
- if (parts[0] != accid) {
378
- // invalid account (may be user has changed their injected code), must recreate user
379
- mask = ''
380
- this.clearBrowserData()
381
- } else userId = parts[1]
382
- }
375
+ if (!userRef) {
376
+ // try to find mask in cookie
377
+ if (!mask) {
378
+ try {
379
+ mask = this.cookie.get('__sbmask', '')
380
+ } catch (e) {}
381
+ }
382
+
383
+ if (mask) {
384
+ let parts = mask.split('@')
385
+ if (parts[0] != accid) {
386
+ userRef = sb.randomString(40)
387
+ this.new_visitor = true
388
+ }
383
389
 
384
- if (!mask) {
385
- let user = await this.api.createUser(accid)
386
- this.new_visitor = true
387
- mask = user.mask
388
- userId = user.id
389
- } else {
390
- this.new_visitor = false // for checking returning or new visitor
390
+ this.new_visitor = false // for checking returning or new visitor
391
+ // set cookie forever
392
+ this.cookie.set('__sbmask', mask)
393
+ } else {
394
+ // random
395
+ userRef = sb.randomString(40)
396
+ this.new_visitor = true
397
+ }
398
+ }
391
399
  }
392
400
 
393
401
  // set cookie forever
394
- this.cookie.set('__sbmask', mask)
395
- this.kv.put('mask', mask)
402
+ if (userRef) this.cookie.set('__sbref', userRef)
403
+
404
+ let user = await this.api.setAuth({accid: accid, mask: mask, ref: userRef})
405
+ if (user.error) return
396
406
 
397
407
  if (this.realtime) this.realtime.stop()
398
408
  if (this.fake) this.realtime = new RealtimeFake()
399
- else this.realtime = new Realtime(this.RealtimeURL, {user_mask: mask})
409
+ else this.realtime = new Realtime(this.RealtimeURL, {account_id: accid, user_ref: userRef, user_mask: mask})
400
410
 
401
411
  this.realtime.onEvent(this.onWs.bind(this))
402
- this.realtime.onInterrupted(() => this.reconnectWs(accid, userId))
412
+ this.realtime.onInterrupted(() => this.reconnectWs(accid, user.id))
403
413
 
404
- this.api.setAuth({accid: accid, uid: userId, mask: mask})
405
414
  this.seenLoop()
406
415
  await this.reconnect()
407
416
  this.readyGate.open()
408
- this.reconnectWs(accid, userId)
409
-
410
- return mask
417
+ this.reconnectWs(accid, user.id)
418
+ return user
411
419
  }
412
420
 
413
421
  async login(accid, user_id, tokenGenerator) {
414
- let user = await this.api.setTokenGenerator(accid, user_id, tokenGenerator)
422
+ let user = await this.api.setAuth({accid: accid, profile_id: user_id, tokenGenerator: tokenGenerator})
415
423
  if (user.error) return user
416
424
 
417
425
  this.clearBrowserData()
@@ -436,8 +444,9 @@ export default class Store {
436
444
  return user
437
445
  }
438
446
 
439
- logout(user_id) {
440
- this.api.setTokenGenerator(undefined, undefined, undefined)
447
+ logout(userRef) {
448
+ return this.api.setAuth({ref: userRef})
449
+ // this.api.setTokenGenerator({})
441
450
  }
442
451
 
443
452
  seenQueue = [] // convoid will be seen
package/webpack.dev.js CHANGED
@@ -3,6 +3,7 @@ var HtmlWebpackPlugin = require('html-webpack-plugin')
3
3
  module.exports = {
4
4
  mode: 'development',
5
5
  entry: {
6
+ webview: './webview.js',
6
7
  app: './app.js',
7
8
  inject: './inject.js',
8
9
  preview: './preview.js',
package/webpack.prod.js CHANGED
@@ -6,6 +6,7 @@ var fs = require('fs')
6
6
  webpackconfig.output.publicPath = 'https://vcdn.subiz-cdn.com/widget-v4/public/'
7
7
 
8
8
  webpackconfig.entry = {
9
+ webview: './webview.js',
9
10
  app: './app.js',
10
11
  preview: './preview.js',
11
12
  library: './library.js',