activities.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import async from "async";
  2. import { isLoginRequired } from "./hooks";
  3. import moduleManager from "../../index";
  4. const DBModule = moduleManager.modules.db;
  5. const CacheModule = moduleManager.modules.cache;
  6. const IOModule = moduleManager.modules.io;
  7. const UtilsModule = moduleManager.modules.utils;
  8. CacheModule.runJob("SUB", {
  9. channel: "activity.hide",
  10. cb: res => {
  11. IOModule.runJob("SOCKETS_FROM_USER", { userId: res.userId }, this).then(response =>
  12. response.sockets.forEach(socket => socket.emit("event:activity.hide", res.activityId))
  13. );
  14. IOModule.runJob("EMIT_TO_ROOM", {
  15. room: `profile-${res.userId}-activities`,
  16. args: ["event:activity.hide", res.activityId]
  17. });
  18. }
  19. });
  20. export default {
  21. /**
  22. * Gets a set of activities
  23. *
  24. * @param {object} session - user session
  25. * @param {string} userId - the user whose activities we are looking for
  26. * @param {number} set - the set number to return
  27. * @param {Function} cb - callback
  28. */
  29. async getSet(session, userId, set, cb) {
  30. const userModel = await DBModule.runJob("GET_MODEL", { modelName: "user" }, this);
  31. const activityModel = await DBModule.runJob("GET_MODEL", { modelName: "activity" }, this);
  32. async.waterfall(
  33. [
  34. next => {
  35. // activities should only be viewed if public/owned by the user
  36. if (session.userId !== userId) {
  37. return userModel
  38. .findById(userId)
  39. .then(user => {
  40. if (user) {
  41. if (user.preferences.activityLogPublic) return next();
  42. return next("User's activity log isn't public.");
  43. }
  44. return next("User does not exist.");
  45. })
  46. .catch(next);
  47. }
  48. return next();
  49. },
  50. next => {
  51. activityModel
  52. .find({ userId, hidden: false })
  53. .skip(15 * (set - 1))
  54. .limit(15)
  55. .sort("createdAt")
  56. .exec(next);
  57. }
  58. ],
  59. async (err, activities) => {
  60. if (err) {
  61. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  62. this.log("ERROR", "ACTIVITIES_GET_SET", `Failed to get set ${set} from activities. "${err}"`);
  63. return cb({ status: "failure", message: err });
  64. }
  65. this.log("SUCCESS", "ACTIVITIES_GET_SET", `Set ${set} from activities obtained successfully.`);
  66. return cb({ status: "success", data: activities });
  67. }
  68. );
  69. },
  70. /**
  71. * Hides an activity for a user
  72. *
  73. * @param session
  74. * @param {string} activityId - the activity which should be hidden
  75. * @param cb
  76. */
  77. hideActivity: isLoginRequired(async function hideActivity(session, activityId, cb) {
  78. const activityModel = await DBModule.runJob("GET_MODEL", { modelName: "activity" }, this);
  79. async.waterfall(
  80. [
  81. next => {
  82. activityModel.updateOne({ _id: activityId }, { $set: { hidden: true } }, next);
  83. }
  84. ],
  85. async err => {
  86. if (err) {
  87. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  88. this.log("ERROR", "ACTIVITIES_HIDE_ACTIVITY", `Failed to hide activity ${activityId}. "${err}"`);
  89. return cb({ status: "failure", message: err });
  90. }
  91. CacheModule.runJob("PUB", {
  92. channel: "activity.hide",
  93. value: {
  94. userId: session.userId,
  95. activityId
  96. }
  97. });
  98. this.log("SUCCESS", "ACTIVITIES_HIDE_ACTIVITY", `Successfully hid activity ${activityId}.`);
  99. return cb({ status: "success", message: "Successfully hid activity." });
  100. }
  101. );
  102. })
  103. };