soundcloud.js 6.4 KB

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