activities.js 6.0 KB

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