activities.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import async from "async";
  2. import CoreClass from "../core";
  3. // let ActivitiesModule;
  4. let DBModule;
  5. let UtilsModule;
  6. let IOModule;
  7. class _ActivitiesModule extends CoreClass {
  8. // eslint-disable-next-line require-jsdoc
  9. constructor() {
  10. super("activities");
  11. // ActivitiesModule = this;
  12. }
  13. /**
  14. * Initialises the activities module
  15. *
  16. * @returns {Promise} - returns promise (reject, resolve)
  17. */
  18. initialize() {
  19. return new Promise(resolve => {
  20. DBModule = this.moduleManager.modules.db;
  21. UtilsModule = this.moduleManager.modules.utils;
  22. IOModule = this.moduleManager.modules.io;
  23. resolve();
  24. });
  25. }
  26. // TODO: Migrate
  27. /**
  28. * Adds a new activity to the database
  29. *
  30. * @param {object} payload - object that contains the payload
  31. * @param {string} payload.userId - the id of the user who's activity is to be added
  32. * @param {string} payload.type - the type of activity (enum specified in schema)
  33. * @param {object} payload.payload - the details of the activity e.g. an array of songs that were added
  34. * @param {string} payload.payload.message - the main message describing the activity e.g. 50 songs added to playlist 'playlist name'
  35. * @param {string} payload.payload.thumbnail - url to a thumbnail e.g. song album art to be used when display an activity
  36. * @param {string} payload.payload.songId - (optional) if relevant, the id of the song related to the activity
  37. * @param {string} payload.payload.playlistId - (optional) if relevant, the id of the playlist related to the activity
  38. * @param {string} payload.payload.stationId - (optional) if relevant, the id of the station related to the activity
  39. * @returns {Promise} - returns promise (reject, resolve)
  40. */
  41. ADD_ACTIVITY(payload) {
  42. return new Promise((resolve, reject) => {
  43. async.waterfall(
  44. [
  45. next => {
  46. DBModule.runJob("GET_MODEL", { modelName: "activity" }, this)
  47. .then(res => next(null, res))
  48. .catch(next);
  49. },
  50. (ActivityModel, next) => {
  51. const { userId, type } = payload;
  52. const activity = new ActivityModel({
  53. userId,
  54. type,
  55. payload: payload.payload
  56. });
  57. activity.save(next);
  58. },
  59. (activity, next) => {
  60. IOModule.runJob("SOCKETS_FROM_USER", { userId: activity.userId }, this)
  61. .then(res => {
  62. res.sockets.forEach(socket => socket.emit("event:activity.create", activity));
  63. next(null, activity);
  64. })
  65. .catch(next);
  66. },
  67. (activity, next) => {
  68. IOModule.runJob("EMIT_TO_ROOM", {
  69. room: `profile-${activity.userId}-activities`,
  70. args: ["event:activity.create", activity]
  71. });
  72. return next(null, activity);
  73. }
  74. ],
  75. async (err, activity) => {
  76. if (err) {
  77. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  78. reject(new Error(err));
  79. } else {
  80. resolve(activity);
  81. }
  82. }
  83. );
  84. });
  85. }
  86. }
  87. export default new _ActivitiesModule();