activities.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import async from "async";
  2. import { isLoginRequired } from "./hooks";
  3. import moduleManager from "../../index";
  4. const DBModule = moduleManager.modules.db;
  5. const UtilsModule = moduleManager.modules.utils;
  6. export default {
  7. /**
  8. * Gets a set of activities
  9. *
  10. * @param {object} session - user session
  11. * @param {string} userId - the user whose activities we are looking for
  12. * @param {number} set - the set number to return
  13. * @param {Function} cb - callback
  14. */
  15. async getSet(session, userId, set, cb) {
  16. const activityModel = await DBModule.runJob(
  17. "GET_MODEL",
  18. {
  19. modelName: "activity"
  20. },
  21. this
  22. );
  23. async.waterfall(
  24. [
  25. next => {
  26. activityModel
  27. .find({ userId, hidden: false })
  28. .skip(15 * (set - 1))
  29. .limit(15)
  30. .sort("createdAt")
  31. .exec(next);
  32. }
  33. ],
  34. async (err, activities) => {
  35. if (err) {
  36. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  37. this.log("ERROR", "ACTIVITIES_GET_SET", `Failed to get set ${set} from activities. "${err}"`);
  38. return cb({ status: "failure", message: err });
  39. }
  40. this.log("SUCCESS", "ACTIVITIES_GET_SET", `Set ${set} from activities obtained successfully.`);
  41. return cb({ status: "success", data: activities });
  42. }
  43. );
  44. },
  45. /**
  46. * Hides an activity for a user
  47. *
  48. * @param session
  49. * @param {string} activityId - the activity which should be hidden
  50. * @param cb
  51. */
  52. hideActivity: isLoginRequired(async function hideActivity(session, activityId, cb) {
  53. const activityModel = await DBModule.runJob(
  54. "GET_MODEL",
  55. {
  56. modelName: "activity"
  57. },
  58. this
  59. );
  60. async.waterfall(
  61. [
  62. next => {
  63. activityModel.updateOne({ _id: activityId }, { $set: { hidden: true } }, next);
  64. }
  65. ],
  66. async err => {
  67. if (err) {
  68. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  69. this.log("ERROR", "ACTIVITIES_HIDE_ACTIVITY", `Failed to hide activity ${activityId}. "${err}"`);
  70. return cb({ status: "failure", message: err });
  71. }
  72. this.log("SUCCESS", "ACTIVITIES_HIDE_ACTIVITY", `Successfully hid activity ${activityId}.`);
  73. return cb({ status: "success" });
  74. }
  75. );
  76. })
  77. };