apis.js 6.1 KB

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