ratings.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. import async from "async";
  2. import CoreClass from "../core";
  3. let RatingsModule;
  4. let CacheModule;
  5. let DBModule;
  6. let UtilsModule;
  7. let YouTubeModule;
  8. let SongsModule;
  9. class _RatingsModule extends CoreClass {
  10. // eslint-disable-next-line require-jsdoc
  11. constructor() {
  12. super("ratings");
  13. RatingsModule = this;
  14. }
  15. /**
  16. * Initialises the ratings module
  17. *
  18. * @returns {Promise} - returns promise (reject, resolve)
  19. */
  20. async initialize() {
  21. this.setStage(1);
  22. CacheModule = this.moduleManager.modules.cache;
  23. DBModule = this.moduleManager.modules.db;
  24. UtilsModule = this.moduleManager.modules.utils;
  25. YouTubeModule = this.moduleManager.modules.youtube;
  26. SongsModule = this.moduleManager.modules.songs;
  27. this.RatingsModel = await DBModule.runJob("GET_MODEL", { modelName: "ratings" });
  28. this.RatingsSchemaCache = await CacheModule.runJob("GET_SCHEMA", { schemaName: "ratings" });
  29. this.setStage(2);
  30. return new Promise((resolve, reject) => {
  31. async.waterfall(
  32. [
  33. next => {
  34. this.setStage(2);
  35. CacheModule.runJob("HGETALL", { table: "ratings" })
  36. .then(ratings => {
  37. next(null, ratings);
  38. })
  39. .catch(next);
  40. },
  41. (ratings, next) => {
  42. this.setStage(3);
  43. if (!ratings) return next();
  44. const youtubeIds = Object.keys(ratings);
  45. return async.each(
  46. youtubeIds,
  47. (youtubeId, next) => {
  48. RatingsModule.RatingsModel.findOne({ youtubeId }, (err, rating) => {
  49. if (err) next(err);
  50. else if (!rating)
  51. CacheModule.runJob("HDEL", {
  52. table: "ratings",
  53. key: youtubeId
  54. })
  55. .then(() => next())
  56. .catch(next);
  57. else next();
  58. });
  59. },
  60. next
  61. );
  62. },
  63. next => {
  64. this.setStage(4);
  65. RatingsModule.RatingsModel.find({}, next);
  66. },
  67. (ratings, next) => {
  68. this.setStage(5);
  69. async.each(
  70. ratings,
  71. (rating, next) => {
  72. CacheModule.runJob("HSET", {
  73. table: "ratings",
  74. key: rating.youtubeId,
  75. value: RatingsModule.RatingsSchemaCache(rating)
  76. })
  77. .then(() => next())
  78. .catch(next);
  79. },
  80. next
  81. );
  82. }
  83. ],
  84. async err => {
  85. if (err) {
  86. err = await UtilsModule.runJob("GET_ERROR", { error: err });
  87. reject(new Error(err));
  88. } else resolve();
  89. }
  90. );
  91. });
  92. }
  93. /**
  94. * Recalculates dislikes and likes
  95. *
  96. * @param {object} payload - returns an object containing the payload
  97. * @param {string} payload.youtubeId - the youtube id
  98. * @returns {Promise} - returns a promise (resolve, reject)
  99. */
  100. async RECALCULATE_RATINGS(payload) {
  101. const playlistModel = await DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this);
  102. return new Promise((resolve, reject) => {
  103. async.waterfall(
  104. [
  105. next => {
  106. playlistModel.countDocuments(
  107. { songs: { $elemMatch: { youtubeId: payload.youtubeId } }, type: "user-liked" },
  108. (err, likes) => {
  109. if (err) return next(err);
  110. return next(null, likes);
  111. }
  112. );
  113. },
  114. (likes, next) => {
  115. playlistModel.countDocuments(
  116. { songs: { $elemMatch: { youtubeId: payload.youtubeId } }, type: "user-disliked" },
  117. (err, dislikes) => {
  118. if (err) return next(err);
  119. return next(err, { likes, dislikes });
  120. }
  121. );
  122. },
  123. ({ likes, dislikes }, next) => {
  124. RatingsModule.RatingsModel.updateOne(
  125. { youtubeId: payload.youtubeId },
  126. {
  127. $set: {
  128. likes,
  129. dislikes
  130. }
  131. },
  132. { upsert: true },
  133. err => next(err, { likes, dislikes })
  134. );
  135. }
  136. ],
  137. (err, { likes, dislikes }) => {
  138. if (err) return reject(new Error(err));
  139. return resolve({ likes, dislikes });
  140. }
  141. );
  142. });
  143. }
  144. /**
  145. * Recalculates all dislikes and likes
  146. *
  147. * @returns {Promise} - returns a promise (resolve, reject)
  148. */
  149. RECALCULATE_ALL_RATINGS() {
  150. return new Promise((resolve, reject) => {
  151. async.waterfall(
  152. [
  153. next => {
  154. SongsModule.SongModel.find({}, { youtubeId: true }, next);
  155. },
  156. (songs, next) => {
  157. YouTubeModule.youtubeVideoModel.find({}, { youtubeId: true }, (err, videos) => {
  158. if (err) next(err);
  159. else
  160. next(null, [
  161. ...songs.map(song => song.youtubeId),
  162. ...videos.map(video => video.youtubeId)
  163. ]);
  164. });
  165. },
  166. (youtubeIds, next) => {
  167. async.eachLimit(
  168. youtubeIds,
  169. 2,
  170. (youtubeId, next) => {
  171. RatingsModule.runJob("RECALCULATE_RATINGS", { youtubeId }, this)
  172. .then(() => {
  173. next();
  174. })
  175. .catch(err => {
  176. next(err);
  177. });
  178. },
  179. err => {
  180. next(err);
  181. }
  182. );
  183. }
  184. ],
  185. err => {
  186. if (err) return reject(new Error(err));
  187. return resolve();
  188. }
  189. );
  190. });
  191. }
  192. /**
  193. * Gets ratings by id from the cache or Mongo, and if it isn't in the cache yet, adds it the cache
  194. *
  195. * @param {object} payload - object containing the payload
  196. * @param {string} payload.youtubeId - the youtube id
  197. * @returns {Promise} - returns a promise (resolve, reject)
  198. */
  199. GET_RATINGS(payload) {
  200. return new Promise((resolve, reject) => {
  201. async.waterfall(
  202. [
  203. next =>
  204. CacheModule.runJob("HGET", { table: "ratings", key: payload.youtubeId }, this)
  205. .then(ratings => next(null, ratings))
  206. .catch(next),
  207. (ratings, next) => {
  208. if (ratings) return next(true, ratings);
  209. return RatingsModule.RatingsModel.findOne({ youtubeId: payload.youtubeId }, next);
  210. },
  211. (ratings, next) => {
  212. if (ratings) {
  213. CacheModule.runJob(
  214. "HSET",
  215. {
  216. table: "ratings",
  217. key: payload.youtubeId,
  218. value: ratings
  219. },
  220. this
  221. ).then(ratings => next(null, ratings));
  222. } else next("Ratings not found.");
  223. }
  224. ],
  225. (err, ratings) => {
  226. if (err && err !== true) return reject(new Error(err));
  227. return resolve({ ratings });
  228. }
  229. );
  230. });
  231. }
  232. }
  233. export default new _RatingsModule();