apis.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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("edit-playlist.") ||
  127. room.startsWith("view-report.") ||
  128. room.startsWith("edit-user.") ||
  129. room.startsWith("view-api-request.") ||
  130. room.startsWith("view-youtube-video.") ||
  131. room.startsWith("view-punishment.") ||
  132. room === "import-album" ||
  133. room === "edit-songs"
  134. ) {
  135. WSModule.runJob("SOCKET_JOIN_ROOM", {
  136. socketId: session.socketId,
  137. room
  138. })
  139. .then(() => {})
  140. .catch(err => {
  141. this.log("ERROR", `Joining room failed: ${err.message}`);
  142. });
  143. }
  144. cb({ status: "success", message: "Successfully joined room." });
  145. },
  146. /**
  147. * Leaves a room
  148. *
  149. * @param {object} session - user session
  150. * @param {string} room - the room to leave
  151. * @param {Function} cb - callback
  152. */
  153. leaveRoom(session, room, cb) {
  154. if (
  155. room === "home" ||
  156. room.startsWith("profile.") ||
  157. room.startsWith("manage-station.") ||
  158. room.startsWith("edit-song.") ||
  159. room.startsWith("view-report.") ||
  160. room === "import-album" ||
  161. room === "edit-songs"
  162. ) {
  163. WSModule.runJob("SOCKET_LEAVE_ROOM", {
  164. socketId: session.socketId,
  165. room
  166. })
  167. .then(() => {})
  168. .catch(err => {
  169. this.log("ERROR", `Leaving room failed: ${err.message}`);
  170. });
  171. }
  172. cb({ status: "success", message: "Successfully left room." });
  173. },
  174. /**
  175. * Joins an admin room
  176. *
  177. * @param {object} session - user session
  178. * @param {string} page - the admin room to join
  179. * @param {Function} cb - callback
  180. */
  181. joinAdminRoom(session, page, cb) {
  182. if (
  183. page === "songs" ||
  184. page === "stations" ||
  185. page === "reports" ||
  186. page === "news" ||
  187. page === "playlists" ||
  188. page === "users" ||
  189. page === "statistics" ||
  190. page === "punishments" ||
  191. page === "youtube" ||
  192. page === "youtubeVideos" ||
  193. page === "import" ||
  194. page === "dataRequests"
  195. ) {
  196. hasPermission(`admin.view.${page}`, session.userId)
  197. .then(() =>
  198. WSModule.runJob("SOCKET_LEAVE_ROOMS", { socketId: session.socketId }).then(() => {
  199. WSModule.runJob(
  200. "SOCKET_JOIN_ROOM",
  201. {
  202. socketId: session.socketId,
  203. room: `admin.${page}`
  204. },
  205. this
  206. ).then(() => cb({ status: "success", message: "Successfully joined admin room." }));
  207. })
  208. )
  209. .catch(() => cb({ status: "error", message: "Failed to join admin room." }));
  210. }
  211. },
  212. /**
  213. * Leaves all rooms
  214. *
  215. * @param {object} session - user session
  216. * @param {Function} cb - callback
  217. */
  218. leaveRooms(session, cb) {
  219. WSModule.runJob("SOCKET_LEAVE_ROOMS", { socketId: session.socketId });
  220. cb({ status: "success", message: "Successfully left all rooms." });
  221. },
  222. /**
  223. * Returns current date
  224. *
  225. * @param {object} session - user session
  226. * @param {Function} cb - callback
  227. */
  228. ping(session, cb) {
  229. cb({ status: "success", message: "Successfully pinged.", data: { date: Date.now() } });
  230. }
  231. };