apis.js 6.3 KB

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