activities.js 5.9 KB

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