activities.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. const CoreClass = require("../core.js");
  2. const async = require("async");
  3. const mongoose = require("mongoose");
  4. class ActivitiesModule extends CoreClass {
  5. constructor() {
  6. super("activities");
  7. }
  8. initialize() {
  9. return new Promise((resolve, reject) => {
  10. this.db = this.moduleManager.modules["db"];
  11. this.io = this.moduleManager.modules["io"];
  12. this.utils = this.moduleManager.modules["utils"];
  13. resolve();
  14. });
  15. }
  16. // TODO: Migrate
  17. ADD_ACTIVITY(payload) {
  18. //userId, activityType, payload
  19. return new Promise((resolve, reject) => {
  20. async.waterfall(
  21. [
  22. (next) => {
  23. this.db
  24. .runJob("GET_MODEL", { modelName: "activity" })
  25. .then((res) => {
  26. next(null, res);
  27. })
  28. .catch(next);
  29. },
  30. (activityModel, next) => {
  31. const activity = new activityModel({
  32. userId: payload.userId,
  33. activityType: payload.activityType,
  34. payload: payload.payload,
  35. });
  36. activity.save((err, activity) => {
  37. if (err) return next(err);
  38. next(null, activity);
  39. });
  40. },
  41. (activity, next) => {
  42. this.utils
  43. .runJob("SOCKETS_FROM_USER", {
  44. userId: activity.userId,
  45. })
  46. .then((response) =>
  47. response.sockets.forEach((socket) => {
  48. socket.emit(
  49. "event:activity.create",
  50. activity
  51. );
  52. })
  53. );
  54. },
  55. ],
  56. (err, activity) => {
  57. // cb(err, activity);
  58. }
  59. );
  60. });
  61. }
  62. }
  63. module.exports = new ActivitiesModule();