apis.js 5.9 KB

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