Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: missing album/artist/track in database #430

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions apps/server/src/database/queries/album.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
import { storeTrackAlbumArtist } from "../../spotify/dbTools";
import { AlbumModel, InfosModel } from "../Models";
import { User } from "../schemas/user";
import { getAlbums as fetchAlbums } from "../../spotify/dbTools";

export const getAlbums = (albumsId: string[]) =>
AlbumModel.find({ id: { $in: albumsId } });
export const getAlbums = async (userID: string, albumIds: string[]) => {
const albums = await AlbumModel.find({ id: { $in: albumIds } });

const validFetchedAlbums = albums.filter(album => album.name && album.name.trim() !== '');
const foundAlbumIds = validFetchedAlbums.map(album => album.id);
const missingAlbumIds = albumIds.filter(id => !foundAlbumIds.includes(id));

if (missingAlbumIds.length > 0) {
const fetchedAlbums = await fetchAlbums(userID, missingAlbumIds);
await storeTrackAlbumArtist({ albums: fetchedAlbums });

return [...albums, ...fetchedAlbums];
}

return albums;
};

export const searchAlbum = (str: string) =>
AlbumModel.find({ name: { $regex: new RegExp(str, "i") } });
Expand Down
20 changes: 18 additions & 2 deletions apps/server/src/database/queries/artist.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
import { storeTrackAlbumArtist } from "../../spotify/dbTools";
import { Timesplit } from "../../tools/types";
import { ArtistModel, InfosModel } from "../Models";
import { User } from "../schemas/user";
import { getGroupByDateProjection, getGroupingByTimeSplit } from "./statsTools";
import { getArtists as fetchArtists } from "../../spotify/dbTools";

export const getArtists = (artistIds: string[]) =>
ArtistModel.find({ id: { $in: artistIds } });
export const getArtists = async (userID: string, artistIds: string[]) => {
const artists = await ArtistModel.find({ id: { $in: artistIds } });

const validFetchedArtists = artists.filter(artist => artist.name && artist.name.trim() !== '');
const foundArtistIds = validFetchedArtists.map(artist => artist.id);
const missingArtistIds = artistIds.filter(id => !foundArtistIds.includes(id));

if (missingArtistIds.length > 0) {
const fetchedArtists = await fetchArtists(userID, missingArtistIds);
await storeTrackAlbumArtist({ artists: fetchedArtists });

return [...artists, ...fetchedArtists];
}

return artists;
};

export const searchArtist = (str: string) =>
ArtistModel.find({ name: { $regex: new RegExp(str, "i") } });
Expand Down
25 changes: 16 additions & 9 deletions apps/server/src/database/queries/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,22 @@ export const getTracksWithoutAlbum = () =>
]);

export const getAlbumsWithoutArtist = () =>
AlbumModel.aggregate<Album>([
AlbumModel.aggregate<Album>([
{ $unwind: '$artists' },
{
$lookup: {
from: "artists",
as: "full_artist",
localField: "album",
foreignField: "id",
},
from: 'artists',
localField: 'artists',
foreignField: 'id',
as: 'artistDetails'
}
},
{ $unwind: "$full_artist" },
{ $match: { full_artist: null } },
]);
{ $match: { artistDetails: { $eq: [] } } },
{
$group: {
_id: '$id',
album: { $first: '$$ROOT' }
}
},
{ $replaceRoot: { newRoot: '$album' } }
]);
19 changes: 17 additions & 2 deletions apps/server/src/database/queries/track.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,24 @@ import { InfosModel, TrackModel } from "../Models";
import { User } from "../schemas/user";
import { Timesplit } from "../../tools/types";
import { getGroupByDateProjection, getGroupingByTimeSplit } from "./statsTools";
import { getTracks as fetchTracks, storeTrackAlbumArtist } from "../../spotify/dbTools";

export const getTracks = (tracksId: string[]) =>
TrackModel.find({ id: { $in: tracksId } });
export const getTracks = async (userID: string, tracksId: string[]) => {
const tracks = await TrackModel.find({ id: { $in: tracksId } });

const validFetchedTracks = tracks.filter(track => track.name && track.name.trim() !== '');
const foundTrackIds = validFetchedTracks.map(track => track.id);
const missingTrackIds = tracksId.filter(id => !foundTrackIds.includes(id));

if (missingTrackIds.length > 0) {
const fetchedTracks = await fetchTracks(userID, missingTrackIds);
await storeTrackAlbumArtist({ tracks: fetchedTracks });

return [...tracks, ...fetchedTracks];
}

return tracks;
};

export const searchTrack = (str: string) =>
TrackModel.find({ name: { $regex: new RegExp(str, "i") } }).populate(
Expand Down
9 changes: 8 additions & 1 deletion apps/server/src/database/queries/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,10 +241,17 @@ export const getSongs = async (
],
},
});

if (!fullUser) {
return [];
}
return fullUser.tracks;

// Filter out invalid tracks
const validTracks = fullUser.tracks.filter((track: any) => {
return track.track.full_album !== null && track.track.full_artist.length > 0;
});

return validTracks;
};

export const getUserCount = () => UserModel.countDocuments();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export async function up() {

for await (const user of UserModel.find()) {
for await (const info of InfosModel.find({ owner: user._id })) {
const [track] = await getTracks([info.id]);
const [track] = await getTracks(user._id.toString(), [info.id]);
if (!track) {
continue;
}
Expand Down
9 changes: 5 additions & 4 deletions apps/server/src/routes/album.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ router.get(
isLoggedOrGuest,
async (req, res) => {
try {
const { user } = req as LoggedRequest;
const { ids } = req.params as TypedPayload<typeof getAlbumsSchema>;
const albums = await getAlbums(ids.split(","));
const albums = await getAlbums(user._id.toString(), ids.split(","));
if (!albums || albums.length === 0) {
return res.status(404).end();
}
Expand All @@ -47,14 +48,14 @@ router.get(
try {
const { user } = req as LoggedRequest;
const { id } = req.params as TypedPayload<typeof getAlbumStats>;
const [album] = await getAlbums([id]);
const [album] = await getAlbums(user._id.toString(), [id]);
if (!album) {
return res.status(404).end();
}
const promises = [
getFirstAndLastListenedAlbum(user, id),
getAlbumSongs(user, id),
getArtists(album.artists),
getArtists(user._id.toString(), album.artists),
// getTotalListeningOfAlbum(user, id),
];
const [firstLast, tracks, artists] = await Promise.all(promises);
Expand All @@ -79,7 +80,7 @@ router.get(
try {
const { user } = req as LoggedRequest;
const { id } = req.params as TypedPayload<typeof getAlbumStats>;
const [album] = await getAlbums([id]);
const [album] = await getAlbums(user._id.toString(), [id]);
if (!album) {
return res.status(404).end();
}
Expand Down
7 changes: 4 additions & 3 deletions apps/server/src/routes/artist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ router.get(
isLoggedOrGuest,
async (req, res) => {
try {
const { user } = req as LoggedRequest;
const { ids } = req.params as TypedPayload<typeof getArtistsSchema>;
const artists = await getArtists(ids.split(","));
const artists = await getArtists(user._id.toString(), ids.split(","));
if (!artists || artists.length === 0) {
return res.status(404).end();
}
Expand All @@ -58,7 +59,7 @@ router.get(
const { user } = req as LoggedRequest;
const { id } = req.params as TypedPayload<typeof getArtistStats>;

const [artist] = await getArtists([id]);
const [artist] = await getArtists(user._id.toString(), [id]);
if (!artist) {
return res.status(404).end();
}
Expand Down Expand Up @@ -108,7 +109,7 @@ router.get(
const { id } = req.params as TypedPayload<typeof getArtistStats>;

try {
const [artist] = await getArtists([id]);
const [artist] = await getArtists(user._id.toString(), [id]);
if (!artist) {
return res.status(404).end();
}
Expand Down
11 changes: 6 additions & 5 deletions apps/server/src/routes/track.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ router.get(
isLoggedOrGuest,
async (req, res) => {
try {
const { user } = req as LoggedRequest;
const { ids } = req.params as TypedPayload<typeof getTracksSchema>;
const tracks = await getTracks(ids.split(","));
const tracks = await getTracks(user._id.toString(), ids.split(","));
if (!tracks || tracks.length === 0) {
return res.status(404).end();
}
Expand All @@ -52,15 +53,15 @@ router.get(
try {
const { user } = req as LoggedRequest;
const { id } = req.params as TypedPayload<typeof getTrackStats>;
const [track] = await getTracks([id]);
const [track] = await getTracks(user._id.toString(), [id]);
const [trackArtist] = track?.artists ?? [];
if (!track || !trackArtist) {
return res.status(404).end();
}
const promises = [
getAlbums([track.album]),
getAlbums(user._id.toString(), [track.album]),
getTrackListenedCount(user, id),
getArtists([trackArtist]),
getArtists(user._id.toString(), [trackArtist]),
getTrackFirstAndLastListened(user, track.id),
bestPeriodOfTrack(user, track.id),
getTrackRecentHistory(user, track.id),
Expand Down Expand Up @@ -97,7 +98,7 @@ router.get(
const { id } = req.params as TypedPayload<typeof getTrackStats>;

try {
const [track] = await getTracks([id]);
const [track] = await getTracks(user._id.toString(), [id]);
if (!track) {
return res.status(404).end();
}
Expand Down