activities.js 2.0 KB

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