activities.js 5.8 KB

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