apis.js 6.0 KB

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