Event.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import { HydratedDocument } from "mongoose";
  2. import { UserSchema } from "../DataModule/models/users/schema";
  3. export default abstract class Event {
  4. protected static _namespace: string;
  5. protected static _name: string;
  6. protected static _type: "event" | "schedule" = "event";
  7. protected _data: any;
  8. protected _scope?: string;
  9. public constructor(data: any, scope?: string) {
  10. this._data = data;
  11. this._scope = scope;
  12. }
  13. public static getNamespace() {
  14. return this._namespace;
  15. }
  16. public static getName() {
  17. return this._name;
  18. }
  19. public static getPath() {
  20. return `${this.getNamespace()}.${this.getName()}`;
  21. }
  22. public static getKey(scope?: string) {
  23. const path = this.getPath();
  24. if (scope) return `${path}:${scope}`;
  25. return path;
  26. }
  27. public static parseKey(key: string) {
  28. const [path, scope] = key.split(":");
  29. return {
  30. path,
  31. scope
  32. };
  33. }
  34. public static getType() {
  35. return this._type;
  36. }
  37. public static makeMessage(data: any) {
  38. if (["object", "array"].includes(typeof data))
  39. return JSON.stringify(data);
  40. return data;
  41. }
  42. public static parseMessage(message: string) {
  43. let parsedMessage = message;
  44. if (parsedMessage.startsWith("[") || parsedMessage.startsWith("{"))
  45. try {
  46. parsedMessage = JSON.parse(parsedMessage);
  47. } catch (err) {
  48. console.error(err);
  49. }
  50. else if (parsedMessage.startsWith('"') && parsedMessage.endsWith('"'))
  51. parsedMessage = parsedMessage
  52. .substring(1)
  53. .substring(0, parsedMessage.length - 2);
  54. return parsedMessage;
  55. }
  56. public getNamespace() {
  57. return (this.constructor as typeof Event).getNamespace();
  58. }
  59. public getName() {
  60. return (this.constructor as typeof Event).getName();
  61. }
  62. public getPath() {
  63. return (this.constructor as typeof Event).getPath();
  64. }
  65. public getKey() {
  66. return (this.constructor as typeof Event).getKey(this._scope);
  67. }
  68. public getData() {
  69. return this._data;
  70. }
  71. public makeMessage() {
  72. return (this.constructor as typeof Event).makeMessage(this._data);
  73. }
  74. protected static _hasPermission:
  75. | boolean
  76. | CallableFunction
  77. | (boolean | CallableFunction)[] = false;
  78. // Check if a given user has generic permission to subscribe to an event, using _hasPermission
  79. public static async hasPermission(
  80. user: HydratedDocument<UserSchema> | null
  81. ) {
  82. const options = Array.isArray(this._hasPermission)
  83. ? this._hasPermission
  84. : [this._hasPermission];
  85. return options.reduce(async (previous, option) => {
  86. if (await previous) return true;
  87. if (typeof option === "boolean") return option;
  88. if (typeof option === "function") return option(user);
  89. return false;
  90. }, Promise.resolve(false));
  91. }
  92. }
  93. export type EventClass = {
  94. new (...params: ConstructorParameters<typeof Event>): Event;
  95. } & typeof Event;