ratings.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. * @param {string} payload.createMissing - whether to create missing ratings
  198. * @returns {Promise} - returns a promise (resolve, reject)
  199. */
  200. GET_RATINGS(payload) {
  201. return new Promise((resolve, reject) => {
  202. async.waterfall(
  203. [
  204. next =>
  205. CacheModule.runJob("HGET", { table: "ratings", key: payload.youtubeId }, this)
  206. .then(ratings => next(null, ratings))
  207. .catch(next),
  208. (ratings, next) => {
  209. if (ratings) return next(true, ratings);
  210. return RatingsModule.RatingsModel.findOne({ youtubeId: payload.youtubeId }, next);
  211. },
  212. (ratings, next) => {
  213. if (ratings)
  214. return CacheModule.runJob(
  215. "HSET",
  216. {
  217. table: "ratings",
  218. key: payload.youtubeId,
  219. value: ratings
  220. },
  221. this
  222. ).then(ratings => next(true, ratings));
  223. if (!payload.createMissing) return next("Ratings not found.");
  224. return RatingsModule.runJob("RECALCULATE_RATINGS", { youtubeId: payload.youtubeId }, this)
  225. .then(() => next())
  226. .catch(next);
  227. },
  228. next =>
  229. RatingsModule.runJob("GET_RATINGS", { youtubeId: payload.youtubeId }, this)
  230. .then(res => next(null, res.ratings))
  231. .catch(next)
  232. ],
  233. (err, ratings) => {
  234. if (err && err !== true) return reject(new Error(err));
  235. return resolve({ ratings });
  236. }
  237. );
  238. });
  239. }
  240. /**
  241. * Remove ratings by id from the cache and Mongo
  242. *
  243. * @param {object} payload - object containing the payload
  244. * @param {string} payload.youtubeIds - the youtube id
  245. * @returns {Promise} - returns a promise (resolve, reject)
  246. */
  247. REMOVE_RATINGS(payload) {
  248. return new Promise((resolve, reject) => {
  249. let { youtubeIds } = payload;
  250. if (!Array.isArray(youtubeIds)) youtubeIds = [youtubeIds];
  251. async.eachLimit(
  252. youtubeIds,
  253. 1,
  254. (youtubeId, next) => {
  255. async.waterfall(
  256. [
  257. next => {
  258. RatingsModule.RatingsModel.deleteOne({ youtubeId }, err => {
  259. if (err) next(err);
  260. else next();
  261. });
  262. },
  263. next => {
  264. CacheModule.runJob("HDEL", { table: "ratings", key: youtubeId }, this)
  265. .then(() => {
  266. next();
  267. })
  268. .catch(next);
  269. }
  270. ],
  271. next
  272. );
  273. },
  274. err => {
  275. if (err && err !== true) return reject(new Error(err));
  276. return resolve();
  277. }
  278. );
  279. });
  280. }
  281. }
  282. export default new _RatingsModule();