@abtnode/core 1.16.14 → 1.16.15-beta-04d1e821

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/lib/api/team.js CHANGED
@@ -13,6 +13,7 @@ const {
13
13
  MAX_USER_PAGE_SIZE,
14
14
  STORE_DETAIL_PAGE_PATH_PREFIX,
15
15
  MAIN_CHAIN_ENDPOINT,
16
+ USER_AVATAR_URL_PREFIX,
16
17
  } = require('@abtnode/constant');
17
18
  const { isValid: isValidDid } = require('@arcblock/did');
18
19
  const { BlockletEvents } = require('@blocklet/constant');
@@ -25,9 +26,10 @@ const {
25
26
  } = require('@abtnode/auth/lib/passport');
26
27
  const { getPassportStatusEndpoint } = require('@abtnode/auth/lib/auth');
27
28
  const getBlockletInfo = require('@blocklet/meta/lib/info');
28
- const { getUserAvatarUrl, getAppAvatarUrl } = require('@abtnode/util/lib/user');
29
+ const { getUserAvatarUrl, getAppAvatarUrl, extractUserAvatar, getAvatarByUrl } = require('@abtnode/util/lib/user');
29
30
  const { getChainClient } = require('@abtnode/util/lib/get-chain-client');
30
31
  const { getWalletDid } = require('@blocklet/meta/lib/did-utils');
32
+ const isUrl = require('is-url');
31
33
 
32
34
  const { validateTrustedPassportIssuers } = require('../validators/trusted-passport');
33
35
  const { validateTrustedFactories } = require('../validators/trusted-factory');
@@ -36,6 +38,7 @@ const { validateCreatePermission, validateUpdatePermission } = require('../valid
36
38
 
37
39
  const { getBlocklet } = require('../util/blocklet');
38
40
  const StoreUtil = require('../util/store');
41
+ const { profileSchema } = require('../validators/user');
39
42
 
40
43
  const sanitizeUrl = (url) => {
41
44
  if (!url) {
@@ -466,6 +469,46 @@ class TeamAPI extends EventEmitter {
466
469
  return doc;
467
470
  }
468
471
 
472
+ async switchProfile({ teamDid, userDid, profile }) {
473
+ const state = await this.getUserState(teamDid);
474
+ // NOTICE: 这个 schema 没有对数据做任何 default 操作,所以没必要用 validate 之后的值
475
+ const { error } = profileSchema.validate({ ...profile, did: userDid });
476
+ if (error) {
477
+ throw new Error(error);
478
+ }
479
+ const oldUser = await state.getUser(userDid);
480
+ if (!oldUser) {
481
+ throw new Error('User is not exist', { userDid, teamDid });
482
+ }
483
+
484
+ let { avatar } = profile;
485
+ if (avatar.startsWith(USER_AVATAR_URL_PREFIX)) {
486
+ // pass
487
+ } else if (isUrl(avatar)) {
488
+ try {
489
+ avatar = await getAvatarByUrl(avatar);
490
+ avatar = await extractUserAvatar(avatar, { dataDirs: this.dataDirs });
491
+ } catch {
492
+ logger.error('Failed to convert external avatar', { teamDid, userDid, avatar });
493
+ throw new Error('Failed to convert external avatar');
494
+ }
495
+ } else {
496
+ // 仅当 avatar 是 base64 时,对 avatar 进行处理
497
+ const regex = /^data:image\/(\w+);base64,/;
498
+ const match = regex.exec(avatar);
499
+ if (match) {
500
+ avatar = await extractUserAvatar(avatar, { dataDirs: this.dataDirs });
501
+ } else {
502
+ logger.error('Profile avatar is invalid', { teamDid, userDid, profile });
503
+ throw new Error('Profile avatar is invalid');
504
+ }
505
+ }
506
+ const doc = await state.updateUser(userDid, { ...profile, avatar });
507
+ logger.info('User switch-profile successfully', { teamDid, userDid });
508
+ this.emit(EVENTS.USER_UPDATED, { teamDid, user: doc });
509
+ return doc;
510
+ }
511
+
469
512
  // Invite member
470
513
 
471
514
  /**
package/lib/index.js CHANGED
@@ -354,6 +354,7 @@ function ABTNode(options) {
354
354
  getUserByDid: teamAPI.getUserByDid.bind(teamAPI),
355
355
  isPassportValid: teamAPI.isPassportValid.bind(teamAPI),
356
356
  isConnectedAccount: teamAPI.isConnectedAccount.bind(teamAPI),
357
+ switchProfile: teamAPI.switchProfile.bind(teamAPI),
357
358
 
358
359
  // Access Control
359
360
  getRBAC: (did = options.nodeDid) => teamManager.getRBAC(did),
@@ -194,6 +194,23 @@ Router.formatSites = (sites = []) => {
194
194
  const rules = Array.isArray(site.rules) ? cloneDeep(site.rules) : [];
195
195
  const grouped = {};
196
196
 
197
+ // 0. serve robots.txt/sitemap.xml from root and proxy to daemon
198
+ if (isBlockletSite(site.domain) && !site.rules.find((x) => x.from.pathPrefix === '/robots.txt')) {
199
+ site.rules.push({
200
+ dynamic: true,
201
+ from: {
202
+ pathPrefix: '/',
203
+ groupPathPrefix: '/',
204
+ pathSuffix: '/(robots.txt|sitemap.xml)',
205
+ },
206
+ to: {
207
+ type: ROUTING_RULE_TYPES.DAEMON,
208
+ port: daemonRule.to.port,
209
+ did: site.blockletDid,
210
+ },
211
+ });
212
+ }
213
+
197
214
  // 1. serve blocklet.js for each component: both prefix and suffix do not contain trailing slash
198
215
  rules.forEach((rule) => {
199
216
  if ([ROUTING_RULE_TYPES.BLOCKLET].includes(rule.to.type) === false) {
@@ -209,29 +226,12 @@ Router.formatSites = (sites = []) => {
209
226
  }
210
227
 
211
228
  grouped[rule.from.groupPathPrefix] = rule;
212
-
213
- site.rules.push({
214
- dynamic: true,
215
- from: {
216
- pathPrefix: rule.from.pathPrefix,
217
- groupPathPrefix: rule.from.groupPathPrefix,
218
- pathSuffix: '/__meta__.js',
219
- },
220
- to: {
221
- type: ROUTING_RULE_TYPES.DAEMON,
222
- port: daemonRule.to.port,
223
- did: rule.to.did,
224
- componentId: rule.to.componentId,
225
- cacheGroup: site.mode === BLOCKLET_MODES.PRODUCTION ? 'blockletJs' : '',
226
- pageGroup: rule.to.pageGroup,
227
- },
228
- });
229
229
  site.rules.push({
230
230
  dynamic: true,
231
231
  from: {
232
232
  pathPrefix: rule.from.pathPrefix,
233
233
  groupPathPrefix: rule.from.groupPathPrefix,
234
- pathSuffix: '/__blocklet__.js',
234
+ pathSuffix: '/__(meta|blocklet)__.js',
235
235
  },
236
236
  to: {
237
237
  type: ROUTING_RULE_TYPES.DAEMON,
@@ -259,7 +259,7 @@ Router.formatSites = (sites = []) => {
259
259
  const rootFrom = {
260
260
  pathPrefix: groupPathPrefix,
261
261
  groupPathPrefix,
262
- pathSuffix: '/__blocklet__.js',
262
+ pathSuffix: '/__(meta|blocklet)__.js',
263
263
  };
264
264
 
265
265
  const rule = grouped[groupPathPrefix];
@@ -40,4 +40,12 @@ const loginSchema = Joi.object({
40
40
  .required(),
41
41
  }).unknown();
42
42
 
43
+ const profileSchema = Joi.object({
44
+ did: Joi.DID().trim().required(),
45
+ fullName: Joi.string().empty(''),
46
+ avatar: Joi.string().empty(''),
47
+ email: Joi.string().empty(''),
48
+ });
49
+
43
50
  exports.loginSchema = loginSchema;
51
+ exports.profileSchema = profileSchema;
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.16.14",
6
+ "version": "1.16.15-beta-04d1e821",
7
7
  "description": "",
8
8
  "main": "lib/index.js",
9
9
  "files": [
@@ -19,19 +19,19 @@
19
19
  "author": "wangshijun <wangshijun2010@gmail.com> (http://github.com/wangshijun)",
20
20
  "license": "Apache-2.0",
21
21
  "dependencies": {
22
- "@abtnode/analytics": "1.16.14",
23
- "@abtnode/auth": "1.16.14",
24
- "@abtnode/certificate-manager": "1.16.14",
25
- "@abtnode/constant": "1.16.14",
26
- "@abtnode/cron": "1.16.14",
27
- "@abtnode/logger": "1.16.14",
28
- "@abtnode/models": "1.16.14",
29
- "@abtnode/queue": "1.16.14",
30
- "@abtnode/rbac": "1.16.14",
31
- "@abtnode/router-provider": "1.16.14",
32
- "@abtnode/static-server": "1.16.14",
33
- "@abtnode/timemachine": "1.16.14",
34
- "@abtnode/util": "1.16.14",
22
+ "@abtnode/analytics": "1.16.15-beta-04d1e821",
23
+ "@abtnode/auth": "1.16.15-beta-04d1e821",
24
+ "@abtnode/certificate-manager": "1.16.15-beta-04d1e821",
25
+ "@abtnode/constant": "1.16.15-beta-04d1e821",
26
+ "@abtnode/cron": "1.16.15-beta-04d1e821",
27
+ "@abtnode/logger": "1.16.15-beta-04d1e821",
28
+ "@abtnode/models": "1.16.15-beta-04d1e821",
29
+ "@abtnode/queue": "1.16.15-beta-04d1e821",
30
+ "@abtnode/rbac": "1.16.15-beta-04d1e821",
31
+ "@abtnode/router-provider": "1.16.15-beta-04d1e821",
32
+ "@abtnode/static-server": "1.16.15-beta-04d1e821",
33
+ "@abtnode/timemachine": "1.16.15-beta-04d1e821",
34
+ "@abtnode/util": "1.16.15-beta-04d1e821",
35
35
  "@arcblock/did": "1.18.89",
36
36
  "@arcblock/did-auth": "1.18.89",
37
37
  "@arcblock/did-ext": "^1.18.89",
@@ -42,10 +42,10 @@
42
42
  "@arcblock/pm2-events": "^0.0.5",
43
43
  "@arcblock/validator": "^1.18.89",
44
44
  "@arcblock/vc": "1.18.89",
45
- "@blocklet/constant": "1.16.14",
46
- "@blocklet/meta": "1.16.14",
47
- "@blocklet/resolver": "1.16.14",
48
- "@blocklet/sdk": "1.16.14",
45
+ "@blocklet/constant": "1.16.15-beta-04d1e821",
46
+ "@blocklet/meta": "1.16.15-beta-04d1e821",
47
+ "@blocklet/resolver": "1.16.15-beta-04d1e821",
48
+ "@blocklet/sdk": "1.16.15-beta-04d1e821",
49
49
  "@did-space/client": "^0.2.141",
50
50
  "@fidm/x509": "^1.2.1",
51
51
  "@ocap/mcrypto": "1.18.89",
@@ -98,5 +98,5 @@
98
98
  "jest": "^27.5.1",
99
99
  "unzipper": "^0.10.11"
100
100
  },
101
- "gitHead": "a2f588d8d5cbe9312260ffbd8fb4cb3bd4104853"
101
+ "gitHead": "42e1523e5b92a3a57d9083ae29455df429afb0f6"
102
102
  }