activities.js 5.8 KB

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