activities.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. 'use strict';
  2. const async = require('async');
  3. const hooks = require('./hooks');
  4. const moduleManager = require("../../index");
  5. const db = moduleManager.modules["db"];
  6. const utils = moduleManager.modules["utils"];
  7. const logger = moduleManager.modules["logger"];
  8. const activities = moduleManager.modules["activities"];
  9. module.exports = {
  10. /**
  11. * Gets a set of activities
  12. *
  13. * @param session
  14. * @param {String} userId - the user whose activities we are looking for
  15. * @param {Integer} set - the set number to return
  16. * @param cb
  17. */
  18. getSet: (session, userId, set, cb) => {
  19. async.waterfall([
  20. next => {
  21. db.models.activity.find({ userId, hidden: false }).skip(15 * (set - 1)).limit(15).sort("createdAt").exec(next);
  22. },
  23. ], async (err, activities) => {
  24. if (err) {
  25. err = await utils.getError(err);
  26. logger.error("ACTIVITIES_GET_SET", `Failed to get set ${set} from activities. "${err}"`);
  27. return cb({ status: 'failure', message: err });
  28. }
  29. logger.success("ACTIVITIES_GET_SET", `Set ${set} from activities obtained successfully.`);
  30. cb({ status: "success", data: activities });
  31. });
  32. },
  33. /**
  34. * Hides an activity for a user
  35. *
  36. * @param session
  37. * @param {String} activityId - the activity which should be hidden
  38. * @param cb
  39. */
  40. hideActivity: hooks.loginRequired((session, activityId, cb) => {
  41. async.waterfall([
  42. next => {
  43. db.models.activity.updateOne({ _id: activityId }, { $set: { hidden: true } }, next);
  44. }
  45. ], async err => {
  46. if (err) {
  47. err = await utils.getError(err);
  48. logger.error("ACTIVITIES_HIDE_ACTIVITY", `Failed to hide activity ${activityId}. "${err}"`);
  49. return cb({ status: "failure", message: err });
  50. }
  51. logger.success("ACTIVITIES_HIDE_ACTIVITY", `Successfully hid activity ${activityId}.`);
  52. cb({ status: "success" })
  53. });
  54. })
  55. };