activities.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. 'use strict';
  2. const coreClass = require("../core");
  3. const async = require('async');
  4. const mongoose = require('mongoose');
  5. module.exports = class extends coreClass {
  6. constructor(name, moduleManager) {
  7. super(name, moduleManager);
  8. this.dependsOn = ["db", "utils"];
  9. }
  10. initialize() {
  11. return new Promise((resolve, reject) => {
  12. this.setStage(1);
  13. this.db = this.moduleManager.modules["db"];
  14. this.io = this.moduleManager.modules["io"];
  15. this.utils = this.moduleManager.modules["utils"];
  16. resolve();
  17. });
  18. }
  19. /**
  20. *
  21. * @param {String} userId - the id of the user
  22. * @param {String} activityType - what type of activity the user has completed e.g. liked a song
  23. * @param {Array} payload - what the activity was specifically related to e.g. the liked song(s)
  24. */
  25. async addActivity(userId, activityType, payload) {
  26. try { await this._validateHook(); } catch { return; }
  27. async.waterfall([
  28. next => {
  29. const activity = new this.db.models.activity({
  30. userId,
  31. activityType,
  32. payload
  33. });
  34. activity.save((err, activity) => {
  35. if (err) return next(err);
  36. next(null, activity);
  37. });
  38. },
  39. (activity, next) => {
  40. this.utils.socketsFromUser(activity.userId, sockets => {
  41. sockets.forEach(socket => {
  42. socket.emit('event:activity.create', activity);
  43. });
  44. });
  45. }
  46. ], (err, activity) => {
  47. // cb(err, activity);
  48. });
  49. }
  50. }