activities.js 2.0 KB

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