GetUserService.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. const BaseService = require("./BaseService");
  2. const { DB_READ } = require("./database/consts");
  3. /**
  4. * Get user by one of a variety of identifying properties.
  5. *
  6. * Pass `cached: false` to options to force a database read.
  7. * Pass `force: true` to options to force a primary database read.
  8. *
  9. * This provides the functionality of `get_user` (helpers.js)
  10. * as a service so that other services can register identifying
  11. * properties for caching.
  12. *
  13. * The original `get_user` function now uses this service.
  14. */
  15. class GetUserService extends BaseService {
  16. _construct () {
  17. this.id_properties = new Set();
  18. this.id_properties.add('username');
  19. this.id_properties.add('uuid');
  20. this.id_properties.add('id');
  21. this.id_properties.add('email');
  22. this.id_properties.add('referral_code');
  23. }
  24. async _init () {
  25. }
  26. async get_user (options) {
  27. const user = await this.get_user_(options);
  28. if ( ! user ) return null;
  29. const svc_whoami = this.services.get('whoami');
  30. await svc_whoami.get_details({ user }, user);
  31. return user;
  32. }
  33. async get_user_ (options) {
  34. const services = this.services;
  35. /** @type BaseDatabaseAccessService */
  36. const db = services.get('database').get(DB_READ, 'filesystem');
  37. const cached = options.cached ?? true;
  38. if ( cached && ! options.force ) {
  39. for ( const prop of this.id_properties ) {
  40. if ( options.hasOwnProperty(prop) ) {
  41. const user = kv.get(`users:${prop}:${options[prop]}`);
  42. if ( user ) return user;
  43. }
  44. }
  45. }
  46. let user;
  47. if ( ! options.force ) {
  48. for ( const prop of this.id_properties ) {
  49. if ( options.hasOwnProperty(prop) ) {
  50. [user] = await db.read(`SELECT * FROM \`user\` WHERE \`${prop}\` = ? LIMIT 1`, [options[prop]]);
  51. if ( user ) break;
  52. }
  53. }
  54. }
  55. if ( ! user || ! user[0] ) {
  56. for ( const prop of this.id_properties ) {
  57. if ( options.hasOwnProperty(prop) ) {
  58. [user] = await db.pread(`SELECT * FROM \`user\` WHERE \`${prop}\` = ? LIMIT 1`, [options[prop]]);
  59. if ( user ) break;
  60. }
  61. }
  62. }
  63. if ( ! user ) return null;
  64. try {
  65. for ( const prop of this.id_properties ) {
  66. if ( user[prop] ) {
  67. kv.set(`users:${prop}:${user[prop]}`, user);
  68. }
  69. }
  70. // kv.set('users:username:' + user.username, user);
  71. // kv.set('users:email:' + user.email, user);
  72. // kv.set('users:uuid:' + user.uuid, user);
  73. // kv.set('users:id:' + user.id, user);
  74. // kv.set('users:referral_code:' + user.referral_code, user);
  75. } catch (e) {
  76. console.error(e);
  77. }
  78. return user;
  79. }
  80. register_id_property (prop) {
  81. this.id_properties.add(prop);
  82. }
  83. }
  84. module.exports = { GetUserService };