soundcloud.js 7.9 KB

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