apis.js 6.6 KB

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