activities.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 WSModule = moduleManager.modules.ws;
  7. const UtilsModule = moduleManager.modules.utils;
  8. CacheModule.runJob("SUB", {
  9. channel: "activity.removeAllForUser",
  10. cb: userId => {
  11. WSModule.runJob("SOCKETS_FROM_USER", { userId }, this).then(sockets =>
  12. sockets.forEach(socket => socket.dispatch("event:activity.removeAllForUser"))
  13. );
  14. WSModule.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. WSModule.runJob("SOCKETS_FROM_USER", { userId: res.userId }, this).then(sockets =>
  24. sockets.forEach(socket => socket.dispatch("event:activity.hide", res.activityId))
  25. );
  26. WSModule.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. * Returns how many activities there are for a user
  35. *
  36. * @param {object} session - the session object automatically added by the websocket
  37. * @param {string} userId - the id of the user in question
  38. * @param {Function} cb - callback
  39. */
  40. async length(session, userId, cb) {
  41. const activityModel = await DBModule.runJob("GET_MODEL", { modelName: "activity" }, this);
  42. async.waterfall(
  43. [
  44. next => {
  45. activityModel.countDocuments({ userId, hidden: false }, next);
  46. }
  47. ],
  48. async (err, count) => {
  49. if (err) {
  50. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  51. this.log(
  52. "ERROR",
  53. "SONGS_LENGTH",
  54. `Failed to get length of activities for user ${userId}. "${err}"`
  55. );
  56. return cb({ status: "error", message: err });
  57. }
  58. this.log("SUCCESS", "ACTIVITIES_LENGTH", `Got length of activities for user ${userId} successfully.`);
  59. return cb({
  60. status: "error",
  61. message: "Successfully obtained length of activities.",
  62. data: { length: count }
  63. });
  64. }
  65. );
  66. },
  67. /**
  68. * Gets a set of activities
  69. *
  70. * @param {object} session - user session
  71. * @param {string} userId - the user whose activities we are looking for
  72. * @param {number} set - the set number to return
  73. * @param {number} offset - how many activities to skip (keeps frontend and backend in sync)
  74. * @param {Function} cb - callback
  75. */
  76. async getSet(session, userId, set, offset, cb) {
  77. const userModel = await DBModule.runJob("GET_MODEL", { modelName: "user" }, this);
  78. const activityModel = await DBModule.runJob("GET_MODEL", { modelName: "activity" }, this);
  79. async.waterfall(
  80. [
  81. next => {
  82. // activities should only be viewed if public/owned by the user
  83. if (session.userId !== userId) {
  84. return userModel
  85. .findById(userId)
  86. .then(user => {
  87. if (user) {
  88. if (user.preferences.activityLogPublic) return next();
  89. return next("User's activity log isn't public.");
  90. }
  91. return next("User does not exist.");
  92. })
  93. .catch(next);
  94. }
  95. return next();
  96. },
  97. next => {
  98. activityModel
  99. .find({ userId, hidden: false })
  100. .skip(15 * (set - 1) + offset)
  101. .limit(15)
  102. .sort({ createdAt: -1 })
  103. .exec(next);
  104. }
  105. ],
  106. async (err, activities) => {
  107. if (err) {
  108. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  109. this.log("ERROR", "ACTIVITIES_GET_SET", `Failed to get set ${set} from activities. "${err}"`);
  110. return cb({ status: "error", message: err });
  111. }
  112. this.log("SUCCESS", "ACTIVITIES_GET_SET", `Set ${set} from activities obtained successfully.`);
  113. return cb({ status: "success", data: { activities } });
  114. }
  115. );
  116. },
  117. /**
  118. * Hides an activity for a user
  119. *
  120. * @param session
  121. * @param {string} activityId - the activity which should be hidden
  122. * @param cb
  123. */
  124. hideActivity: isLoginRequired(async function hideActivity(session, activityId, cb) {
  125. const activityModel = await DBModule.runJob("GET_MODEL", { modelName: "activity" }, this);
  126. async.waterfall(
  127. [
  128. next => {
  129. activityModel.updateOne({ _id: activityId }, { $set: { hidden: true } }, next);
  130. }
  131. ],
  132. async err => {
  133. if (err) {
  134. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  135. this.log("ERROR", "ACTIVITIES_HIDE_ACTIVITY", `Failed to hide activity ${activityId}. "${err}"`);
  136. return cb({ status: "error", message: err });
  137. }
  138. CacheModule.runJob("PUB", {
  139. channel: "activity.hide",
  140. value: {
  141. userId: session.userId,
  142. activityId
  143. }
  144. });
  145. this.log("SUCCESS", "ACTIVITIES_HIDE_ACTIVITY", `Successfully hid activity ${activityId}.`);
  146. return cb({ status: "success", message: "Successfully hid activity." });
  147. }
  148. );
  149. }),
  150. /**
  151. * Removes all activities logged for a logged-in user
  152. *
  153. * @param session
  154. * @param cb
  155. */
  156. removeAllForUser: isLoginRequired(async function removeAllForUser(session, cb) {
  157. const activityModel = await DBModule.runJob("GET_MODEL", { modelName: "activity" }, this);
  158. async.waterfall(
  159. [
  160. next => {
  161. activityModel.deleteMany({ userId: session.userId }, next);
  162. },
  163. (res, next) => {
  164. CacheModule.runJob("HDEL", { table: "recentActivities", key: session.userId }, this)
  165. .then(() => next())
  166. .catch(next);
  167. }
  168. ],
  169. async err => {
  170. if (err) {
  171. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  172. this.log(
  173. "ERROR",
  174. "ACTIVITIES_REMOVE_ALL_FOR_USER",
  175. `Failed to delete activities for user ${session.userId}. "${err}"`
  176. );
  177. return cb({ status: "error", message: err });
  178. }
  179. CacheModule.runJob("PUB", {
  180. channel: "activity.removeAllForUser",
  181. value: session.userId
  182. });
  183. this.log(
  184. "SUCCESS",
  185. "ACTIVITIES_REMOVE_ALL_FOR_USER",
  186. `Successfully removed activities for user ${session.userId}.`
  187. );
  188. return cb({ status: "success", message: "Successfully removed your activity logs." });
  189. }
  190. );
  191. })
  192. };