cloud-action-migration.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /**
  2. * Actions that are safe to route from the current Angular H5 application to
  3. * the new Parse cloud-function gateway. Keep this list narrower than the
  4. * server-side mapping until each page's response shape has passed regression.
  5. */
  6. export const H5_CLOUD_ROUTED_ACTIONS = new Set([
  7. 'app_update',
  8. 'node_get',
  9. 'node_list',
  10. 'user_login_passwd',
  11. ]);
  12. const CONTENT_MODEL_FIELDS: Record<number, Set<string>> = {
  13. 52: new Set(['id', 'lj', 'sy', 'yb', 'yp']),
  14. 53: new Set(['id', 'kcid', 'scid', 'xxcs', 'yhid', 'jrscb']),
  15. 54: new Set(['id', 'pl', 'bxrq', 'dslx', 'dszt', 'fxpl', 'jffs', 'jssj', 'kcid', 'kssj', 'plxm', 'scsj', 'sdsd', 'szmd', 'szyh', 'yysj', 'yykcid']),
  16. 56: new Set(['id', 'pl', 'con', 'djq', 'ygg', 'dqrq', 'dsid', 'fxrl', 'xxqs', 'szmdid', 'userid', 'learned']),
  17. 58: new Set(['id', 'yxx', 'cksl', 'kcid', 'syjd', 'yhid']),
  18. 59: new Set(['id', 'pf', 'jffs', 'jsmz', 'kcid', 'kclx', 'kcmc', 'kzsj', 'pjnr', 'pldp', 'xymz', 'yyds', 'plpjsj', 'szmdid']),
  19. 60: new Set(['id', 'fxzt', 'kcid', 'plid', 'wcsj', 'yhid', 'kywrq', 'kywsj', 'xxjlid', 'orderid']),
  20. 61: new Set(['id', 'df', 'askid', 'wrong', 'userid', 'answerid', 'dontknow', 'prevscore', 'totalscore']),
  21. };
  22. const NODE_MODELS: Record<number, number> = { 28: 58, 29: 54, 32: 53, 291: 56, 296: 59, 327: 52, 388: 60, 389: 61 };
  23. export function usesCloudGateway(action: string, params: Record<string, unknown> = {}, hasParseSession = false): boolean {
  24. if (H5_CLOUD_ROUTED_ACTIONS.has(action)) return true;
  25. if (action !== 'content_list') return false;
  26. const modelId = contentModel(params);
  27. if (!modelId) return !params['myfield'] && !params['myfield2'];
  28. const fields = CONTENT_MODEL_FIELDS[modelId];
  29. if (!fields || (modelId !== 52 && !hasParseSession)) return false;
  30. return filterNames(params).every((field) => fields.has(field));
  31. }
  32. export function requiresCloudSession(action: string, params: Record<string, unknown> = {}): boolean {
  33. return action === 'content_list' && contentModel(params) !== 52 && Boolean(contentModel(params));
  34. }
  35. function contentModel(params: Record<string, unknown>): number {
  36. const explicit = Number(params['modelId'] ?? params['modelid'] ?? params['ModelID'] ?? 0);
  37. if (explicit) return explicit;
  38. const nodes = String(params['nodeid'] ?? params['nid'] ?? params['nodes'] ?? '').split(',').map(Number).filter(Boolean);
  39. return nodes.length === 1 ? NODE_MODELS[nodes[0]] ?? 0 : 0;
  40. }
  41. function filterNames(params: Record<string, unknown>): string[] {
  42. return [params['myfield'], params['myfield2']]
  43. .filter(Boolean)
  44. .flatMap((value) => String(value).split('|'))
  45. .map((part) => part.split('=', 1)[0].replace(/^B\./i, '').toLowerCase());
  46. }