activities.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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.removeAllForUser",
  10. cb: userId => {
  11. IOModule.runJob("SOCKETS_FROM_USER", { userId }, this).then(res =>
  12. res.sockets.forEach(socket => socket.emit("event:activity.removeAllForUser"))
  13. );
  14. IOModule.runJob("EMIT_TO_ROOM", {
  15. room: `profile-${userId}-activities`,
  16. args: ["event:activity.removeAllForUser"]
  17. });
  18. }
  19. });
  20. CacheModule.runJob("SUB", {
  21. channel: "activity.hide",
  22. cb: res => {
  23. IOModule.runJob("SOCKETS_FROM_USER", { userId: res.userId }, this).then(response =>
  24. response.sockets.forEach(socket => socket.emit("event:activity.hide", res.activityId))
  25. );
  26. IOModule.runJob("EMIT_TO_ROOM", {
  27. room: `profile-${res.userId}-activities`,
  28. args: ["event:activity.hide", res.activityId]
  29. });
  30. }
  31. });
  32. export default {
  33. /**
  34. * Gets a set of activities
  35. *
  36. * @param {object} session - user session
  37. * @param {string} userId - the user whose activities we are looking for
  38. * @param {number} set - the set number to return
  39. * @param {Function} cb - callback
  40. */
  41. async getSet(session, userId, set, cb) {
  42. const userModel = await DBModule.runJob("GET_MODEL", { modelName: "user" }, this);
  43. const activityModel = await DBModule.runJob("GET_MODEL", { modelName: "activity" }, this);
  44. async.waterfall(
  45. [
  46. next => {
  47. // activities should only be viewed if public/owned by the user
  48. if (session.userId !== userId) {
  49. return userModel
  50. .findById(userId)
  51. .then(user => {
  52. if (user) {
  53. if (user.preferences.activityLogPublic) return next();
  54. return next("User's activity log isn't public.");
  55. }
  56. return next("User does not exist.");
  57. })
  58. .catch(next);
  59. }
  60. return next();
  61. },
  62. next => {
  63. activityModel
  64. .find({ userId, hidden: false })
  65. .skip(15 * (set - 1))
  66. .limit(15)
  67. .sort({ createdAt: -1 })
  68. .exec(next);
  69. }
  70. ],
  71. async (err, activities) => {
  72. if (err) {
  73. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  74. this.log("ERROR", "ACTIVITIES_GET_SET", `Failed to get set ${set} from activities. "${err}"`);
  75. return cb({ status: "failure", message: err });
  76. }
  77. this.log("SUCCESS", "ACTIVITIES_GET_SET", `Set ${set} from activities obtained successfully.`);
  78. return cb({ status: "success", data: activities });
  79. }
  80. );
  81. },
  82. /**
  83. * Hides an activity for a user
  84. *
  85. * @param session
  86. * @param {string} activityId - the activity which should be hidden
  87. * @param cb
  88. */
  89. hideActivity: isLoginRequired(async function hideActivity(session, activityId, cb) {
  90. const activityModel = await DBModule.runJob("GET_MODEL", { modelName: "activity" }, this);
  91. async.waterfall(
  92. [
  93. next => {
  94. activityModel.updateOne({ _id: activityId }, { $set: { hidden: true } }, next);
  95. }
  96. ],
  97. async err => {
  98. if (err) {
  99. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  100. this.log("ERROR", "ACTIVITIES_HIDE_ACTIVITY", `Failed to hide activity ${activityId}. "${err}"`);
  101. return cb({ status: "failure", message: err });
  102. }
  103. CacheModule.runJob("PUB", {
  104. channel: "activity.hide",
  105. value: {
  106. userId: session.userId,
  107. activityId
  108. }
  109. });
  110. this.log("SUCCESS", "ACTIVITIES_HIDE_ACTIVITY", `Successfully hid activity ${activityId}.`);
  111. return cb({ status: "success", message: "Successfully hid activity." });
  112. }
  113. );
  114. }),
  115. /**
  116. * Removes all activities logged for a logged-in user
  117. *
  118. * @param session
  119. * @param cb
  120. */
  121. removeAllForUser: isLoginRequired(async function removeAllForUser(session, cb) {
  122. const activityModel = await DBModule.runJob("GET_MODEL", { modelName: "activity" }, this);
  123. async.waterfall(
  124. [
  125. next => {
  126. activityModel.deleteMany({ userId: session.userId }, next);
  127. }
  128. ],
  129. async err => {
  130. if (err) {
  131. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  132. this.log(
  133. "ERROR",
  134. "ACTIVITIES_REMOVE_ALL_FOR_USER",
  135. `Failed to delete activities for user ${session.userId}. "${err}"`
  136. );
  137. return cb({ status: "failure", message: err });
  138. }
  139. CacheModule.runJob("PUB", {
  140. channel: "activity.removeAllForUser",
  141. value: session.userId
  142. });
  143. this.log(
  144. "SUCCESS",
  145. "ACTIVITIES_REMOVE_ALL_FOR_USER",
  146. `Successfully removed activities for user ${session.userId}.`
  147. );
  148. return cb({ status: "success", message: "Successfully removed your activity logs." });
  149. }
  150. );
  151. })
  152. };