|
@@ -358,6 +358,8 @@ export default {
|
|
|
* @param {Function} cb - gets called with the result
|
|
|
*/
|
|
|
requestSetAdmin: isAdminRequired(async function requestSetAdmin(session, url, musicOnly, returnVideos, cb) {
|
|
|
+ const importJobModel = await DBModule.runJob("GET_MODEL", { modelName: "importJob" }, this);
|
|
|
+
|
|
|
this.keepLongJob();
|
|
|
this.publishProgress({
|
|
|
status: "started",
|
|
@@ -365,162 +367,85 @@ export default {
|
|
|
message: "Importing playlist.",
|
|
|
id: this.toString()
|
|
|
});
|
|
|
- await CacheModule.runJob("RPUSH", { key: `longJobs.requestSetAdmin`, value: this.toString() }, this);
|
|
|
- await CacheModule.runJob(
|
|
|
- "HSET",
|
|
|
- {
|
|
|
- table: `requestSetAdmin`,
|
|
|
- key: this.toString(),
|
|
|
- value: { payload: { url, musicOnly, returnVideos }, response: null }
|
|
|
- },
|
|
|
- this
|
|
|
- );
|
|
|
+ await CacheModule.runJob("RPUSH", { key: `longJobs.${session.userId}`, value: this.toString() }, this);
|
|
|
|
|
|
- YouTubeModule.runJob("REQUEST_SET", { url, musicOnly, returnVideos }, this)
|
|
|
- .then(async response => {
|
|
|
- this.log(
|
|
|
- "SUCCESS",
|
|
|
- "REQUEST_SET_ADMIN",
|
|
|
- `Successfully imported a YouTube playlist to be requested for admin "${session.userId}".`
|
|
|
- );
|
|
|
- this.publishProgress({
|
|
|
- status: "success",
|
|
|
- message: `Playlist is done importing. ${response.successful} were added succesfully, ${response.failed} failed (${response.alreadyInDatabase} were already in database)`
|
|
|
- });
|
|
|
- await CacheModule.runJob(
|
|
|
- "HSET",
|
|
|
- {
|
|
|
- table: `requestSetAdmin`,
|
|
|
- key: this.toString(),
|
|
|
- value: { payload: { url, musicOnly, returnVideos }, response }
|
|
|
- },
|
|
|
- this
|
|
|
- );
|
|
|
- return cb({
|
|
|
- status: "success",
|
|
|
- message: `Playlist is done importing. ${response.successful} were added succesfully, ${response.failed} failed (${response.alreadyInDatabase} were already in database)`,
|
|
|
- videos: returnVideos ? response.videos : null
|
|
|
- });
|
|
|
- })
|
|
|
- .catch(async err => {
|
|
|
- err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
|
|
|
- this.log(
|
|
|
- "ERROR",
|
|
|
- "REQUEST_SET_ADMIN",
|
|
|
- `Importing a YouTube playlist to be requested failed for admin "${session.userId}". "${err}"`
|
|
|
- );
|
|
|
- this.publishProgress({
|
|
|
- status: "error",
|
|
|
- message: err
|
|
|
- });
|
|
|
- return cb({ status: "error", message: err });
|
|
|
- });
|
|
|
- }),
|
|
|
-
|
|
|
- /**
|
|
|
- * Gets request set admin long jobs
|
|
|
- *
|
|
|
- * @param {object} session - the session object automatically added by the websocket
|
|
|
- * @param {Function} cb - gets called with the result
|
|
|
- */
|
|
|
- getRequestSetAdminLongJobs: isLoginRequired(async function getRequestSetAdminLongJobs(session, cb) {
|
|
|
async.waterfall(
|
|
|
[
|
|
|
next => {
|
|
|
- CacheModule.runJob(
|
|
|
- "LRANGE",
|
|
|
+ importJobModel.create(
|
|
|
{
|
|
|
- key: `longJobs.requestSetAdmin`
|
|
|
+ type: "youtube",
|
|
|
+ query: {
|
|
|
+ url,
|
|
|
+ musicOnly
|
|
|
+ },
|
|
|
+ status: "in-progress",
|
|
|
+ response: {},
|
|
|
+ requestedBy: session.userId,
|
|
|
+ requestedAt: Date.now()
|
|
|
},
|
|
|
- this
|
|
|
- )
|
|
|
- .then(longJobUuids => next(null, longJobUuids))
|
|
|
- .catch(next);
|
|
|
+ next
|
|
|
+ );
|
|
|
},
|
|
|
|
|
|
- (longJobUuids, next) => {
|
|
|
- next(
|
|
|
- null,
|
|
|
- longJobUuids.map(longJobUuid => ({
|
|
|
- job: moduleManager.jobManager.getJob(longJobUuid),
|
|
|
- uuid: longJobUuid
|
|
|
- }))
|
|
|
- );
|
|
|
+ (importJob, next) => {
|
|
|
+ YouTubeModule.runJob("REQUEST_SET", { url, musicOnly, returnVideos }, this)
|
|
|
+ .then(response => {
|
|
|
+ next(null, importJob, response);
|
|
|
+ })
|
|
|
+ .catch(err => {
|
|
|
+ next(err, importJob);
|
|
|
+ });
|
|
|
},
|
|
|
|
|
|
- (longJobs, next) => {
|
|
|
- async.eachLimit(
|
|
|
- longJobs,
|
|
|
- 1,
|
|
|
- (longJob, next) => {
|
|
|
- CacheModule.runJob("HGET", { table: "requestSetAdmin", key: longJob.uuid }, this)
|
|
|
- .then(value => {
|
|
|
- if (value) {
|
|
|
- const { payload, response } = value;
|
|
|
- longJob.payload = payload;
|
|
|
- longJob.response = response;
|
|
|
- }
|
|
|
- next();
|
|
|
- })
|
|
|
- .catch(console.log);
|
|
|
+ (importJob, response, next) => {
|
|
|
+ importJobModel.updateOne(
|
|
|
+ { _id: importJob._id },
|
|
|
+ {
|
|
|
+ $set: {
|
|
|
+ status: "success",
|
|
|
+ response: {
|
|
|
+ failed: response.failed,
|
|
|
+ successful: response.successful,
|
|
|
+ alreadyInDatabase: response.alreadyInDatabase,
|
|
|
+ successfulVideoIds: response.successfulVideoIds,
|
|
|
+ failedVideoIds: response.failedVideoIds
|
|
|
+ }
|
|
|
+ }
|
|
|
},
|
|
|
err => {
|
|
|
- next(err, longJobs);
|
|
|
+ next(err, importJob, response);
|
|
|
}
|
|
|
);
|
|
|
- },
|
|
|
-
|
|
|
- (longJobs, next) => {
|
|
|
- longJobs.forEach(({ job }) => {
|
|
|
- if (job && job.onProgress)
|
|
|
- job.onProgress.on("progress", data => {
|
|
|
- this.publishProgress(
|
|
|
- {
|
|
|
- id: job.toString(),
|
|
|
- ...data
|
|
|
- },
|
|
|
- true
|
|
|
- );
|
|
|
- });
|
|
|
- });
|
|
|
-
|
|
|
- next(
|
|
|
- null,
|
|
|
- longJobs.map(({ job, uuid, payload, response }) => ({
|
|
|
- id: uuid,
|
|
|
- name: job ? job.longJobTitle : "",
|
|
|
- status: job ? job.lastProgressData.status : "",
|
|
|
- message: job ? job.lastProgressData.message : "",
|
|
|
- payload,
|
|
|
- response
|
|
|
- }))
|
|
|
- );
|
|
|
}
|
|
|
],
|
|
|
- async (err, longJobs) => {
|
|
|
+ async (err, importJob, response) => {
|
|
|
if (err) {
|
|
|
err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
|
|
|
-
|
|
|
this.log(
|
|
|
"ERROR",
|
|
|
- "GET_REQUEST_SET_ADMIN_LONG_JOBS",
|
|
|
- `Couldn't get request set admin long jobs for user "${session.userId}". "${err}"`
|
|
|
+ "REQUEST_SET_ADMIN",
|
|
|
+ `Importing a YouTube playlist to be requested failed for admin "${session.userId}". "${err}"`
|
|
|
);
|
|
|
-
|
|
|
+ importJobModel.updateOne({ _id: importJob._id }, { $set: { status: "error" } });
|
|
|
return cb({ status: "error", message: err });
|
|
|
}
|
|
|
|
|
|
this.log(
|
|
|
"SUCCESS",
|
|
|
- "GET_REQUEST_SET_ADMIN_LONG_JOBS",
|
|
|
- `Got request set admin long jobs for user "${session.userId}".`
|
|
|
+ "REQUEST_SET_ADMIN",
|
|
|
+ `Successfully imported a YouTube playlist to be requested for admin "${session.userId}".`
|
|
|
);
|
|
|
|
|
|
+ this.publishProgress({
|
|
|
+ status: "success",
|
|
|
+ message: `Playlist is done importing. ${response.successful} were added succesfully, ${response.failed} failed (${response.alreadyInDatabase} were already in database)`
|
|
|
+ });
|
|
|
+
|
|
|
return cb({
|
|
|
status: "success",
|
|
|
- data: {
|
|
|
- longJobs
|
|
|
- }
|
|
|
+ message: `Playlist is done importing. ${response.successful} were added succesfully, ${response.failed} failed (${response.alreadyInDatabase} were already in database)`,
|
|
|
+ videos: returnVideos ? response.videos : null
|
|
|
});
|
|
|
}
|
|
|
);
|