songs.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. import async from "async";
  2. import mongoose from "mongoose";
  3. import CoreClass from "../core";
  4. let SongsModule;
  5. let CacheModule;
  6. let DBModule;
  7. let UtilsModule;
  8. class _SongsModule extends CoreClass {
  9. // eslint-disable-next-line require-jsdoc
  10. constructor() {
  11. super("songs");
  12. SongsModule = this;
  13. }
  14. /**
  15. * Initialises the songs module
  16. *
  17. * @returns {Promise} - returns promise (reject, resolve)
  18. */
  19. async initialize() {
  20. this.setStage(1);
  21. CacheModule = this.moduleManager.modules.cache;
  22. DBModule = this.moduleManager.modules.db;
  23. UtilsModule = this.moduleManager.modules.utils;
  24. this.songModel = await DBModule.runJob("GET_MODEL", { modelName: "song" });
  25. this.songSchemaCache = await CacheModule.runJob("GET_SCHEMA", { schemaName: "song" });
  26. this.setStage(2);
  27. return new Promise((resolve, reject) =>
  28. async.waterfall(
  29. [
  30. next => {
  31. this.setStage(2);
  32. CacheModule.runJob("HGETALL", { table: "songs" })
  33. .then(songs => {
  34. next(null, songs);
  35. })
  36. .catch(next);
  37. },
  38. (songs, next) => {
  39. this.setStage(3);
  40. if (!songs) return next();
  41. const songIds = Object.keys(songs);
  42. return async.each(
  43. songIds,
  44. (songId, next) => {
  45. SongsModule.songModel.findOne({ songId }, (err, song) => {
  46. if (err) next(err);
  47. else if (!song)
  48. CacheModule.runJob("HDEL", {
  49. table: "songs",
  50. key: songId
  51. })
  52. .then(() => next())
  53. .catch(next);
  54. else next();
  55. });
  56. },
  57. next
  58. );
  59. },
  60. next => {
  61. this.setStage(4);
  62. SongsModule.songModel.find({}, next);
  63. },
  64. (songs, next) => {
  65. this.setStage(5);
  66. async.each(
  67. songs,
  68. (song, next) => {
  69. CacheModule.runJob("HSET", {
  70. table: "songs",
  71. key: song.songId,
  72. value: SongsModule.songSchemaCache(song)
  73. })
  74. .then(() => next())
  75. .catch(next);
  76. },
  77. next
  78. );
  79. }
  80. ],
  81. async err => {
  82. if (err) {
  83. err = await UtilsModule.runJob("GET_ERROR", { error: err });
  84. reject(new Error(err));
  85. } else resolve();
  86. }
  87. )
  88. );
  89. }
  90. /**
  91. * Gets a song by id from the cache or Mongo, and if it isn't in the cache yet, adds it the cache
  92. *
  93. * @param {object} payload - object containing the payload
  94. * @param {string} payload.id - the id of the song we are trying to get
  95. * @returns {Promise} - returns a promise (resolve, reject)
  96. */
  97. GET_SONG(payload) {
  98. return new Promise((resolve, reject) =>
  99. async.waterfall(
  100. [
  101. next => {
  102. console.log(payload);
  103. if (!mongoose.Types.ObjectId.isValid(payload.id)) return next("Id is not a valid ObjectId.");
  104. return CacheModule.runJob("HGET", { table: "songs", key: payload.id }, this)
  105. .then(song => {
  106. next(null, song);
  107. })
  108. .catch(next);
  109. },
  110. (song, next) => {
  111. if (song) return next(true, song);
  112. return SongsModule.songModel.findOne({ _id: payload.id }, next);
  113. },
  114. (song, next) => {
  115. if (song) {
  116. CacheModule.runJob(
  117. "HSET",
  118. {
  119. table: "songs",
  120. key: payload.id,
  121. value: song
  122. },
  123. this
  124. ).then(song => next(null, song));
  125. } else next("Song not found.");
  126. }
  127. ],
  128. (err, song) => {
  129. if (err && err !== true) return reject(new Error(err));
  130. return resolve({ song });
  131. }
  132. )
  133. );
  134. }
  135. /**
  136. * Gets a song by song id from the cache or Mongo, and if it isn't in the cache yet, adds it the cache
  137. *
  138. * @param {object} payload - an object containing the payload
  139. * @param {string} payload.songId - the mongo id of the song we are trying to get
  140. * @returns {Promise} - returns a promise (resolve, reject)
  141. */
  142. GET_SONG_FROM_ID(payload) {
  143. return new Promise((resolve, reject) =>
  144. async.waterfall(
  145. [
  146. next => {
  147. SongsModule.songModel.findOne({ songId: payload.songId }, next);
  148. }
  149. ],
  150. (err, song) => {
  151. if (err && err !== true) return reject(new Error(err));
  152. return resolve({ song });
  153. }
  154. )
  155. );
  156. }
  157. /**
  158. * Gets a song from id from Mongo and updates the cache with it
  159. *
  160. * @param {object} payload - an object containing the payload
  161. * @param {string} payload.songId - the id of the song we are trying to update
  162. * @returns {Promise} - returns a promise (resolve, reject)
  163. */
  164. UPDATE_SONG(payload) {
  165. // songId, cb
  166. return new Promise((resolve, reject) =>
  167. async.waterfall(
  168. [
  169. next => {
  170. SongsModule.songModel.findOne({ _id: payload.songId }, next);
  171. },
  172. (song, next) => {
  173. if (!song) {
  174. CacheModule.runJob("HDEL", {
  175. table: "songs",
  176. key: payload.songId
  177. });
  178. return next("Song not found.");
  179. }
  180. return CacheModule.runJob(
  181. "HSET",
  182. {
  183. table: "songs",
  184. key: payload.songId,
  185. value: song
  186. },
  187. this
  188. )
  189. .then(song => {
  190. next(null, song);
  191. })
  192. .catch(next);
  193. }
  194. ],
  195. (err, song) => {
  196. if (err && err !== true) return reject(new Error(err));
  197. return resolve(song);
  198. }
  199. )
  200. );
  201. }
  202. /**
  203. * Deletes song from id from Mongo and cache
  204. *
  205. * @param {object} payload - returns an object containing the payload
  206. * @param {string} payload.songId - the id of the song we are trying to delete
  207. * @returns {Promise} - returns a promise (resolve, reject)
  208. */
  209. DELETE_SONG(payload) {
  210. return new Promise((resolve, reject) =>
  211. async.waterfall(
  212. [
  213. next => {
  214. SongsModule.songModel.deleteOne({ songId: payload.songId }, next);
  215. },
  216. next => {
  217. CacheModule.runJob(
  218. "HDEL",
  219. {
  220. table: "songs",
  221. key: payload.songId
  222. },
  223. this
  224. )
  225. .then(() => next())
  226. .catch(next);
  227. }
  228. ],
  229. err => {
  230. if (err && err !== true) return reject(new Error(err));
  231. return resolve();
  232. }
  233. )
  234. );
  235. }
  236. /**
  237. * Recalculates dislikes and likes for a song
  238. *
  239. * @param {object} payload - returns an object containing the payload
  240. * @param {string} payload.musareSongId - the (musare) id of the song
  241. * @param {string} payload.songId - the (mongodb) id of the song
  242. * @returns {Promise} - returns a promise (resolve, reject)
  243. */
  244. async RECALCULATE_SONG_RATINGS(payload) {
  245. const playlistModel = await DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this);
  246. return new Promise((resolve, reject) => {
  247. async.waterfall(
  248. [
  249. next => {
  250. playlistModel.countDocuments(
  251. { songs: { $elemMatch: { songId: payload.musareSongId } }, displayName: "Liked Songs" },
  252. (err, likes) => {
  253. if (err) return next(err);
  254. return next(null, likes);
  255. }
  256. );
  257. },
  258. (likes, next) => {
  259. playlistModel.countDocuments(
  260. { songs: { $elemMatch: { songId: payload.musareSongId } }, displayName: "Disliked Songs" },
  261. (err, dislikes) => {
  262. if (err) return next(err);
  263. return next(err, { likes, dislikes });
  264. }
  265. );
  266. },
  267. ({ likes, dislikes }, next) => {
  268. SongsModule.songModel.updateOne(
  269. { _id: payload.songId },
  270. {
  271. $set: {
  272. likes,
  273. dislikes
  274. }
  275. },
  276. err => next(err, { likes, dislikes })
  277. );
  278. }
  279. ],
  280. (err, { likes, dislikes }) => {
  281. if (err) return reject(new Error(err));
  282. return resolve({ likes, dislikes });
  283. }
  284. );
  285. });
  286. }
  287. /**
  288. * Gets an array of all genres
  289. *
  290. * @returns {Promise} - returns a promise (resolve, reject)
  291. */
  292. GET_ALL_GENRES() {
  293. return new Promise((resolve, reject) =>
  294. async.waterfall(
  295. [
  296. next => {
  297. SongsModule.songModel.find({}, { genres: 1, _id: false }, next);
  298. },
  299. (songs, next) => {
  300. let allGenres = [];
  301. songs.forEach(song => {
  302. allGenres = allGenres.concat(song.genres);
  303. });
  304. const lowerCaseGenres = allGenres.map(genre => genre.toLowerCase());
  305. const uniqueGenres = lowerCaseGenres.filter(
  306. (value, index, self) => self.indexOf(value) === index
  307. );
  308. next(null, uniqueGenres);
  309. }
  310. ],
  311. (err, genres) => {
  312. if (err && err !== true) return reject(new Error(err));
  313. return resolve({ genres });
  314. }
  315. )
  316. );
  317. }
  318. /**
  319. * Gets an array of all songs with a specific genre
  320. *
  321. * @param {object} payload - returns an object containing the payload
  322. * @param {string} payload.genre - the genre
  323. * @returns {Promise} - returns a promise (resolve, reject)
  324. */
  325. GET_ALL_SONGS_WITH_GENRE(payload) {
  326. return new Promise((resolve, reject) =>
  327. async.waterfall(
  328. [
  329. next => {
  330. SongsModule.songModel.find(
  331. { genres: { $regex: new RegExp(`^${payload.genre.toLowerCase()}$`, "i") } },
  332. next
  333. );
  334. }
  335. ],
  336. (err, songs) => {
  337. if (err && err !== true) return reject(new Error(err));
  338. return resolve({ songs });
  339. }
  340. )
  341. );
  342. }
  343. }
  344. export default new _SongsModule();