activities.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. }
  15. });
  16. CacheModule.runJob("SUB", {
  17. channel: "activity.hide",
  18. cb: res => {
  19. const { activityId, userId } = res;
  20. WSModule.runJob("SOCKETS_FROM_USER", { userId }, this).then(sockets =>
  21. sockets.forEach(socket => socket.dispatch("event:activity.hidden", { data: { activityId } }))
  22. );
  23. }
  24. });
  25. export default {
  26. /**
  27. * Returns how many activities there are for a user
  28. *
  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. *
  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. async 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. *
  113. * @param session
  114. * @param {string} activityId - the activity which should be hidden
  115. * @param cb
  116. */
  117. hideActivity: isLoginRequired(async function hideActivity(session, activityId, cb) {
  118. const activityModel = await DBModule.runJob("GET_MODEL", { modelName: "activity" }, this);
  119. async.waterfall(
  120. [
  121. next => {
  122. activityModel.updateOne({ _id: activityId }, { $set: { hidden: true } }, next);
  123. }
  124. ],
  125. async err => {
  126. if (err) {
  127. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  128. this.log("ERROR", "ACTIVITIES_HIDE_ACTIVITY", `Failed to hide activity ${activityId}. "${err}"`);
  129. return cb({ status: "error", message: err });
  130. }
  131. CacheModule.runJob("PUB", {
  132. channel: "activity.hide",
  133. value: {
  134. userId: session.userId,
  135. activityId
  136. }
  137. });
  138. this.log("SUCCESS", "ACTIVITIES_HIDE_ACTIVITY", `Successfully hid activity ${activityId}.`);
  139. return cb({ status: "success", message: "Successfully hid activity." });
  140. }
  141. );
  142. }),
  143. /**
  144. * Removes all activities logged for a logged-in user
  145. *
  146. * @param session
  147. * @param cb
  148. */
  149. removeAllForUser: isLoginRequired(async function removeAllForUser(session, cb) {
  150. const activityModel = await DBModule.runJob("GET_MODEL", { modelName: "activity" }, this);
  151. async.waterfall(
  152. [
  153. next => {
  154. activityModel.deleteMany({ userId: session.userId }, next);
  155. },
  156. (res, next) => {
  157. CacheModule.runJob("HDEL", { table: "recentActivities", key: session.userId }, this)
  158. .then(() => next())
  159. .catch(next);
  160. }
  161. ],
  162. async err => {
  163. if (err) {
  164. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  165. this.log(
  166. "ERROR",
  167. "ACTIVITIES_REMOVE_ALL_FOR_USER",
  168. `Failed to delete activities for user ${session.userId}. "${err}"`
  169. );
  170. return cb({ status: "error", message: err });
  171. }
  172. CacheModule.runJob("PUB", {
  173. channel: "activity.removeAllForUser",
  174. value: session.userId
  175. });
  176. this.log(
  177. "SUCCESS",
  178. "ACTIVITIES_REMOVE_ALL_FOR_USER",
  179. `Successfully removed activities for user ${session.userId}.`
  180. );
  181. return cb({ status: "success", message: "Successfully removed your activity logs." });
  182. }
  183. );
  184. })
  185. };