activities.js 3.0 KB

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