apis.js 6.0 KB

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