activities.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 userModel = await DBModule.runJob("GET_MODEL", { modelName: "user" }, this);
  17. const activityModel = await DBModule.runJob("GET_MODEL", { modelName: "activity" }, this);
  18. async.waterfall(
  19. [
  20. next => {
  21. // activities should only be viewed if public/owned by the user
  22. if (session.userId !== userId) {
  23. return userModel
  24. .findById(userId)
  25. .then(user => {
  26. if (user) {
  27. if (user.preferences.activityLogPublic) return next();
  28. return next("User's activity log isn't public.");
  29. }
  30. return next("User does not exist.");
  31. })
  32. .catch(next);
  33. }
  34. return next();
  35. },
  36. next => {
  37. activityModel
  38. .find({ userId, hidden: false })
  39. .skip(15 * (set - 1))
  40. .limit(15)
  41. .sort("createdAt")
  42. .exec(next);
  43. }
  44. ],
  45. async (err, activities) => {
  46. if (err) {
  47. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  48. this.log("ERROR", "ACTIVITIES_GET_SET", `Failed to get set ${set} from activities. "${err}"`);
  49. return cb({ status: "failure", message: err });
  50. }
  51. this.log("SUCCESS", "ACTIVITIES_GET_SET", `Set ${set} from activities obtained successfully.`);
  52. return cb({ status: "success", data: activities });
  53. }
  54. );
  55. },
  56. /**
  57. * Hides an activity for a user
  58. *
  59. * @param session
  60. * @param {string} activityId - the activity which should be hidden
  61. * @param cb
  62. */
  63. hideActivity: isLoginRequired(async function hideActivity(session, activityId, cb) {
  64. const activityModel = await DBModule.runJob("GET_MODEL", { modelName: "activity" }, this);
  65. async.waterfall(
  66. [
  67. next => {
  68. activityModel.updateOne({ _id: activityId }, { $set: { hidden: true } }, next);
  69. }
  70. ],
  71. async err => {
  72. if (err) {
  73. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  74. this.log("ERROR", "ACTIVITIES_HIDE_ACTIVITY", `Failed to hide activity ${activityId}. "${err}"`);
  75. return cb({ status: "failure", message: err });
  76. }
  77. this.log("SUCCESS", "ACTIVITIES_HIDE_ACTIVITY", `Successfully hid activity ${activityId}.`);
  78. return cb({ status: "success" });
  79. }
  80. );
  81. })
  82. };