activities.js 5.9 KB

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