apis.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. import config from "config";
  2. import async from "async";
  3. import axios from "axios";
  4. import isLoginRequired from "../hooks/loginRequired";
  5. import { hasPermission, useHasPermission } from "../hooks/hasPermission";
  6. // eslint-disable-next-line
  7. import moduleManager from "../../index";
  8. const UtilsModule = moduleManager.modules.utils;
  9. const WSModule = moduleManager.modules.ws;
  10. const YouTubeModule = moduleManager.modules.youtube;
  11. export default {
  12. /**
  13. * Fetches a list of songs from Youtube's API
  14. *
  15. * @param {object} session - user session
  16. * @param {string} query - the query we'll pass to youtubes api
  17. * @param {Function} cb - callback
  18. * @returns {{status: string, data: object}} - returns an object
  19. */
  20. searchYoutube: isLoginRequired(function searchYoutube(session, query, cb) {
  21. return YouTubeModule.runJob("SEARCH", { query }, this)
  22. .then(data => {
  23. this.log("SUCCESS", "APIS_SEARCH_YOUTUBE", `Searching YouTube successful with query "${query}".`);
  24. return cb({ status: "success", data });
  25. })
  26. .catch(async err => {
  27. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  28. this.log("ERROR", "APIS_SEARCH_YOUTUBE", `Searching youtube failed with query "${query}". "${err}"`);
  29. return cb({ status: "error", message: err });
  30. });
  31. }),
  32. /**
  33. * Fetches a specific page of search results from Youtube's API
  34. *
  35. * @param {object} session - user session
  36. * @param {string} query - the query we'll pass to youtubes api
  37. * @param {string} pageToken - identifies a specific page in the result set that should be retrieved
  38. * @param {Function} cb - callback
  39. * @returns {{status: string, data: object}} - returns an object
  40. */
  41. searchYoutubeForPage: isLoginRequired(function searchYoutubeForPage(session, query, pageToken, cb) {
  42. return YouTubeModule.runJob("SEARCH", { query, pageToken }, this)
  43. .then(data => {
  44. this.log(
  45. "SUCCESS",
  46. "APIS_SEARCH_YOUTUBE_FOR_PAGE",
  47. `Searching YouTube successful with query "${query}".`
  48. );
  49. return cb({ status: "success", data });
  50. })
  51. .catch(async err => {
  52. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  53. this.log(
  54. "ERROR",
  55. "APIS_SEARCH_YOUTUBE_FOR_PAGE",
  56. `Searching youtube failed with query "${query}". "${err}"`
  57. );
  58. return cb({ status: "error", message: err });
  59. });
  60. }),
  61. /**
  62. * Gets Discogs data
  63. *
  64. * @param session
  65. * @param query - the query
  66. * @param {Function} cb
  67. */
  68. searchDiscogs: useHasPermission("apis.searchDiscogs", function searchDiscogs(session, query, page, cb) {
  69. async.waterfall(
  70. [
  71. next => {
  72. const options = {
  73. params: { q: query, per_page: 20, page },
  74. headers: {
  75. "User-Agent": "Request",
  76. Authorization: `Discogs key=${config.get("apis.discogs.client")}, secret=${config.get(
  77. "apis.discogs.secret"
  78. )}`
  79. }
  80. };
  81. axios
  82. .get("https://api.discogs.com/database/search", options)
  83. .then(res => next(null, res.data))
  84. .catch(err => next(err));
  85. }
  86. ],
  87. async (err, body) => {
  88. if (err) {
  89. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  90. this.log(
  91. "ERROR",
  92. "APIS_SEARCH_DISCOGS",
  93. `Searching discogs failed with query "${query}". "${err}"`
  94. );
  95. return cb({ status: "error", message: err });
  96. }
  97. this.log(
  98. "SUCCESS",
  99. "APIS_SEARCH_DISCOGS",
  100. `User "${session.userId}" searched Discogs succesfully for query "${query}".`
  101. );
  102. return cb({
  103. status: "success",
  104. data: {
  105. results: body.results,
  106. pages: body.pagination.pages
  107. }
  108. });
  109. }
  110. );
  111. }),
  112. /**
  113. * Joins a room
  114. *
  115. * @param {object} session - user session
  116. * @param {string} room - the room to join
  117. * @param {Function} cb - callback
  118. */
  119. joinRoom(session, room, cb) {
  120. if (
  121. room === "home" ||
  122. room === "news" ||
  123. room.startsWith("profile.") ||
  124. room.startsWith("manage-station.") ||
  125. room.startsWith("edit-song.") ||
  126. room.startsWith("view-report.") ||
  127. room.startsWith("edit-user.") ||
  128. room.startsWith("view-api-request.") ||
  129. room.startsWith("view-youtube-video.") ||
  130. room.startsWith("view-punishment.") ||
  131. room === "import-album" ||
  132. room === "edit-songs"
  133. ) {
  134. WSModule.runJob("SOCKET_JOIN_ROOM", {
  135. socketId: session.socketId,
  136. room
  137. })
  138. .then(() => {})
  139. .catch(err => {
  140. this.log("ERROR", `Joining room failed: ${err.message}`);
  141. });
  142. }
  143. cb({ status: "success", message: "Successfully joined room." });
  144. },
  145. /**
  146. * Leaves a room
  147. *
  148. * @param {object} session - user session
  149. * @param {string} room - the room to leave
  150. * @param {Function} cb - callback
  151. */
  152. leaveRoom(session, room, cb) {
  153. if (
  154. room === "home" ||
  155. room.startsWith("profile.") ||
  156. room.startsWith("manage-station.") ||
  157. room.startsWith("edit-song.") ||
  158. room.startsWith("view-report.") ||
  159. room === "import-album" ||
  160. room === "edit-songs"
  161. ) {
  162. WSModule.runJob("SOCKET_LEAVE_ROOM", {
  163. socketId: session.socketId,
  164. room
  165. })
  166. .then(() => {})
  167. .catch(err => {
  168. this.log("ERROR", `Leaving room failed: ${err.message}`);
  169. });
  170. }
  171. cb({ status: "success", message: "Successfully left room." });
  172. },
  173. /**
  174. * Joins an admin room
  175. *
  176. * @param {object} session - user session
  177. * @param {string} page - the admin room to join
  178. * @param {Function} cb - callback
  179. */
  180. joinAdminRoom(session, page, cb) {
  181. if (
  182. page === "songs" ||
  183. page === "stations" ||
  184. page === "reports" ||
  185. page === "news" ||
  186. page === "playlists" ||
  187. page === "users" ||
  188. page === "statistics" ||
  189. page === "punishments" ||
  190. page === "youtube" ||
  191. page === "youtubeVideos" ||
  192. page === "import" ||
  193. page === "dataRequests"
  194. ) {
  195. hasPermission(`admin.view.${page}`, session.userId)
  196. .then(() =>
  197. WSModule.runJob("SOCKET_LEAVE_ROOMS", { socketId: session.socketId }).then(() => {
  198. WSModule.runJob(
  199. "SOCKET_JOIN_ROOM",
  200. {
  201. socketId: session.socketId,
  202. room: `admin.${page}`
  203. },
  204. this
  205. ).then(() => cb({ status: "success", message: "Successfully joined admin room." }));
  206. })
  207. )
  208. .catch(() => cb({ status: "error", message: "Failed to join admin room." }));
  209. }
  210. },
  211. /**
  212. * Leaves all rooms
  213. *
  214. * @param {object} session - user session
  215. * @param {Function} cb - callback
  216. */
  217. leaveRooms(session, cb) {
  218. WSModule.runJob("SOCKET_LEAVE_ROOMS", { socketId: session.socketId });
  219. cb({ status: "success", message: "Successfully left all rooms." });
  220. },
  221. /**
  222. * Returns current date
  223. *
  224. * @param {object} session - user session
  225. * @param {Function} cb - callback
  226. */
  227. ping(session, cb) {
  228. cb({ status: "success", message: "Successfully pinged.", data: { date: Date.now() } });
  229. }
  230. };