Browse Source

Hopefully some more fixes in addition to previous commit

Kristian Vos 4 years ago
parent
commit
ecf071e16c
2 changed files with 7 additions and 7 deletions
  1. 1 1
      backend/logic/db/schemas/song.js
  2. 6 6
      backend/logic/songs.js

+ 1 - 1
backend/logic/db/schemas/song.js

@@ -1,5 +1,5 @@
 export default {
-	songId: { type: String, min: 11, max: 11, required: true, index: true },
+	songId: { type: String, min: 11, max: 11, required: true, index: true, unique: true },
 	title: { type: String, required: true },
 	artists: [{ type: String, default: [] }],
 	genres: [{ type: String, default: [] }],

+ 6 - 6
backend/logic/songs.js

@@ -488,24 +488,24 @@ class _SongsModule extends CoreClass {
 						SongsModule.SongModel.find({}, { _id: true }, (err, songs) => {
 							if (err) reject(new Error(err));
 							else {
-								const songIds = songs.map(song => song._id.toString());
-								const orphanedSongIds = [];
+								const songIds = songs.map(song => song.songId);
+								const orphanedSongIds = new Set();
 								async.eachLimit(
 									playlists,
 									1,
 									(playlist, next) => {
 										playlist.songs.forEach(song => {
 											if (
-												(song._id && songIds.indexOf(song._id.toString()) === -1) ||
-												orphanedSongIds.indexOf(song.songId) === -1
+												songIds.indexOf(song.songId) === -1 &&
+												!orphanedSongIds.has(song.songId)
 											) {
-												orphanedSongIds.push(song.songId);
+												orphanedSongIds.add(song.songId);
 											}
 										});
 										next();
 									},
 									() => {
-										resolve({ songIds: orphanedSongIds });
+										resolve({ songIds: Array.from(orphanedSongIds) });
 									}
 								);
 							}