soundcloud.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. import async from "async";
  2. import isLoginRequired from "../hooks/loginRequired";
  3. import { useHasPermission } from "../hooks/hasPermission";
  4. // eslint-disable-next-line
  5. import moduleManager from "../../index";
  6. const DBModule = moduleManager.modules.db;
  7. const UtilsModule = moduleManager.modules.utils;
  8. const SoundcloudModule = moduleManager.modules.soundcloud;
  9. export default {
  10. /**
  11. * Fetches new SoundCloud API key
  12. *
  13. * @returns {{status: string, data: object}}
  14. */
  15. fetchNewApiKey: useHasPermission("soundcloud.fetchNewApiKey", function fetchNewApiKey(session, cb) {
  16. SoundcloudModule.runJob("GENERATE_SOUNDCLOUD_API_KEY", {}, this)
  17. .then(response => {
  18. this.log("SUCCESS", "SOUNDCLOUD_FETCH_NEW_API_KEY", `Fetching new API key was successful.`);
  19. return cb({ status: "success", data: { status: response.status } });
  20. })
  21. .catch(async err => {
  22. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  23. this.log("ERROR", "SOUNDCLOUD_FETCH_NEW_API_KEY", `Fetching new API key failed. "${err}"`);
  24. return cb({ status: "error", message: err });
  25. });
  26. }),
  27. /**
  28. * Tests SoundCloud API key
  29. *
  30. * @returns {{status: string, data: object}}
  31. */
  32. testApiKey: useHasPermission("soundcloud.testApiKey", function testApiKey(session, cb) {
  33. SoundcloudModule.runJob("TEST_SOUNDCLOUD_API_KEY", {}, this)
  34. .then(response => {
  35. this.log(
  36. "SUCCESS",
  37. "SOUNDCLOUD_TEST_API_KEY",
  38. `Testing API key was successful. Response: ${response}.`
  39. );
  40. return cb({ status: "success", data: { status: response.status } });
  41. })
  42. .catch(async err => {
  43. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  44. this.log("ERROR", "SOUNDCLOUD_TEST_API_KEY", `Testing API key failed. "${err}"`);
  45. return cb({ status: "error", message: err });
  46. });
  47. }),
  48. /**
  49. * Get a Soundcloud artist from ID
  50. *
  51. * @returns {{status: string, data: object}}
  52. */
  53. getArtist: useHasPermission("soundcloud.getArtist", function getArtist(session, userPermalink, cb) {
  54. return SoundcloudModule.runJob("GET_ARTISTS_FROM_PERMALINKS", { userPermalinks: [userPermalink] }, this)
  55. .then(res => {
  56. if (res.artists.length === 0) {
  57. this.log("ERROR", "SOUNDCLOUD_GET_ARTISTS_FROM_PERMALINKS", `Fetching artist failed.`);
  58. return cb({ status: "error", message: "Failed to get artist" });
  59. }
  60. this.log("SUCCESS", "SOUNDCLOUD_GET_ARTISTS_FROM_PERMALINKS", `Fetching artist was successful.`);
  61. return cb({
  62. status: "success",
  63. message: "Successfully fetched Soundcloud artist",
  64. data: res.artists[0]
  65. });
  66. })
  67. .catch(async err => {
  68. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  69. this.log("ERROR", "SOUNDCLOUD_GET_ARTISTS_FROM_PERMALINKS", `Fetching artist failed. "${err}"`);
  70. return cb({ status: "error", message: err });
  71. });
  72. }),
  73. /**
  74. * Gets videos, used in the admin youtube page by the AdvancedTable component
  75. *
  76. * @param {object} session - the session object automatically added by the websocket
  77. * @param page - the page
  78. * @param pageSize - the size per page
  79. * @param properties - the properties to return for each news item
  80. * @param sort - the sort object
  81. * @param queries - the queries array
  82. * @param operator - the operator for queries
  83. * @param cb
  84. */
  85. getTracks: useHasPermission(
  86. "admin.view.soundcloudTracks",
  87. async function getTracks(session, page, pageSize, properties, sort, queries, operator, cb) {
  88. async.waterfall(
  89. [
  90. next => {
  91. DBModule.runJob(
  92. "GET_DATA",
  93. {
  94. page,
  95. pageSize,
  96. properties,
  97. sort,
  98. queries,
  99. operator,
  100. modelName: "soundcloudTrack",
  101. blacklistedProperties: [],
  102. specialProperties: {
  103. songId: [
  104. // Fetch songs from songs collection with a matching mediaSource
  105. {
  106. $lookup: {
  107. from: "songs", // TODO fix this to support mediasource, so start with youtube:, so add a new pipeline steps
  108. localField: "trackId",
  109. foreignField: "trackId",
  110. as: "song"
  111. }
  112. },
  113. // Turn the array of songs returned in the last step into one object, since only one song should have been returned maximum
  114. {
  115. $unwind: {
  116. path: "$song",
  117. preserveNullAndEmptyArrays: true
  118. }
  119. },
  120. // Add new field songId, which grabs the song object's _id and tries turning it into a string
  121. {
  122. $addFields: {
  123. songId: {
  124. $convert: {
  125. input: "$song._id",
  126. to: "string",
  127. onError: "",
  128. onNull: ""
  129. }
  130. }
  131. }
  132. },
  133. // Cleanup, don't return the song object for any further steps
  134. {
  135. $project: {
  136. song: 0
  137. }
  138. }
  139. ]
  140. },
  141. specialQueries: {},
  142. specialFilters: {
  143. // importJob: importJobId => [
  144. // {
  145. // $lookup: {
  146. // from: "importjobs",
  147. // let: { trackId: "$trackId" },
  148. // pipeline: [
  149. // {
  150. // $match: {
  151. // _id: mongoose.Types.ObjectId(importJobId)
  152. // }
  153. // },
  154. // {
  155. // $addFields: {
  156. // importJob: {
  157. // $in: ["$$trackId", "$response.successfulVideoIds"]
  158. // }
  159. // }
  160. // },
  161. // {
  162. // $project: {
  163. // importJob: 1,
  164. // _id: 0
  165. // }
  166. // }
  167. // ],
  168. // as: "importJob"
  169. // }
  170. // },
  171. // {
  172. // $unwind: "$importJob"
  173. // },
  174. // {
  175. // $set: {
  176. // importJob: "$importJob.importJob"
  177. // }
  178. // }
  179. // ]
  180. }
  181. },
  182. this
  183. )
  184. .then(response => {
  185. next(null, response);
  186. })
  187. .catch(err => {
  188. next(err);
  189. });
  190. }
  191. ],
  192. async (err, response) => {
  193. if (err && err !== true) {
  194. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  195. this.log("ERROR", "SOUNDCLOUD_GET_VIDEOS", `Failed to get SoundCloud tracks. "${err}"`);
  196. return cb({ status: "error", message: err });
  197. }
  198. this.log("SUCCESS", "SOUNDCLOUD_GET_VIDEOS", `Fetched SoundCloud tracks successfully.`);
  199. return cb({
  200. status: "success",
  201. message: "Successfully fetched SoundCloud tracks.",
  202. data: response
  203. });
  204. }
  205. );
  206. }
  207. ),
  208. /**
  209. * Get a SoundCloud track
  210. *
  211. * @returns {{status: string, data: object}}
  212. */
  213. getTrack: isLoginRequired(function getTrack(session, identifier, createMissing, cb) {
  214. return SoundcloudModule.runJob("GET_TRACK", { identifier, createMissing }, this)
  215. .then(res => {
  216. this.log("SUCCESS", "SOUNDCLOUD_GET_VIDEO", `Fetching track was successful.`);
  217. return cb({ status: "success", message: "Successfully fetched SoundCloud video", data: res.track });
  218. })
  219. .catch(async err => {
  220. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  221. this.log("ERROR", "SOUNDCLOUD_GET_VIDEO", `Fetching track failed. "${err}"`);
  222. return cb({ status: "error", message: err });
  223. });
  224. })
  225. };