apis.js 5.8 KB

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