Skip to content

Commit

Permalink
added checks on array indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
Yooooomi committed Nov 2, 2023
1 parent b5ed2f9 commit 0834017
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 40 deletions.
1 change: 0 additions & 1 deletion .github/workflows/nightly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ on:
- server/**
branches:
- master
- release/*

jobs:
build:
Expand Down
5 changes: 3 additions & 2 deletions server/src/routes/track.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,14 @@ router.get(
const { user } = req as LoggedRequest;
const { id } = req.params as TypedPayload<typeof getTrackStats>;
const [track] = await getTracks([id]);
if (!track) {
const [trackArtist] = track?.artists ?? [];
if (!track || !trackArtist) {
return res.status(404).end();
}
const promises = [
getAlbums([track.album]),
getTrackListenedCount(user, id),
getArtists([track.artists[0]]),
getArtists([trackArtist]),
getTrackFirstAndLastListened(user, track.id),
bestPeriodOfTrack(user, track.id),
getTrackRecentHistory(user, track.id),
Expand Down
15 changes: 11 additions & 4 deletions server/src/spotify/dbTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ const getIdsHandlingMax = async <

// Voluntarily waiting in loop to prevent requests limit
for (let i = 0; i < idsArray.length; i += 1) {
const builtUrl = `${url}?ids=${idsArray[i].join(',')}`;
const id = idsArray[i];
if (!id) {
continue;
}
const builtUrl = `${url}?ids=${id.join(',')}`;
// eslint-disable-next-line no-await-in-loop
const { data } = await retryPromise(() => spotifyApi.raw(builtUrl), 10, 30);
datas.push(...data[arrayPath]);
Expand All @@ -62,7 +66,7 @@ const getTracksAndRelatedAlbumArtists = async (

const tracks: Track[] = spotifyTracks.map<Track>(track => {
logger.info(
`Storing non existing track ${track.name} by ${track.artists[0].name}`,
`Storing non existing track ${track.name} by ${track.artists[0]?.name}`,
);

track.artists.forEach(art => {
Expand Down Expand Up @@ -99,7 +103,7 @@ export const getAlbums = async (userId: string, ids: string[]) => {

const albums: Album[] = spotifyAlbums.map(alb => {
logger.info(
`Storing non existing album ${alb.name} by ${alb.artists[0].name}`,
`Storing non existing album ${alb.name} by ${alb.artists[0]?.name}`,
);

return {
Expand Down Expand Up @@ -227,7 +231,10 @@ export async function storeIterationOfLoop(
const min = minOfArray(infos, item => item.played_at.getTime());

if (min) {
await storeFirstListenedAtIfLess(userId, infos[min.minIndex].played_at);
const minInfo = infos[min.minIndex]?.played_at;
if (minInfo) {
await storeFirstListenedAtIfLess(userId, minInfo);
}
}

longWriteDbLock.unlock();
Expand Down
4 changes: 2 additions & 2 deletions server/src/spotify/looper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const loop = async (user: User) => {
);
const infos: Omit<Infos, 'owner'>[] = [];
for (let i = 0; i < items.length; i += 1) {
const item = items[i];
const item = items[i]!;
const date = new Date(item.played_at);
const duplicate = await getCloseTrackId(
user._id.toString(),
Expand All @@ -66,7 +66,7 @@ const loop = async (user: User) => {
);
if (duplicate.length === 0) {
const isBlacklisted = user.settings.blacklistedArtists.find(
a => a === item.track.artists[0].id,
a => a === item.track.artists[0]?.id,
);
infos.push({
played_at: new Date(item.played_at),
Expand Down
2 changes: 1 addition & 1 deletion server/src/tools/apis/spotifyApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export class SpotifyAPI {
private async internAddToPlaylist(id: string, ids: string[]) {
const chunks = chunk(ids, 100);
for (let i = 0; i < chunks.length; i += 1) {
const chk = chunks[i];
const chk = chunks[i]!;
// eslint-disable-next-line no-await-in-loop
await this.client.post(`/playlists/${id}/tracks`, {
uris: chk.map(trackId => `spotify:track:${trackId}`),
Expand Down
9 changes: 9 additions & 0 deletions server/src/tools/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ export class Database {
const [compatibilityMajor, compatibilityMinor] = (
await getCompatibilityVersion()
).featureCompatibilityVersion.version.split('.');
if (
compatibilityMajor === undefined ||
compatibilityMinor === undefined ||
major === undefined ||
minor === undefined
) {
logger.warn('Could not get mongo version');
return;
}
if (+compatibilityMajor > major) {
throw new Error('Cannot downgrade the database');
}
Expand Down
16 changes: 8 additions & 8 deletions server/src/tools/importers/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function getFromCacheString(userId: string, str: string) {
if (!(userId in cache)) {
cache[userId] = {};
}
return cache[userId][str];
return cache[userId]?.[str];
}

export function getFromCache(
Expand All @@ -30,14 +30,14 @@ export function setToCacheString(
str: string,
trackObject: SpotifyTrack,
) {
if (!(userId in cache)) {
cache[userId] = {};
}
const keys = Object.keys(cache[userId]);
if (keys.length > maxCacheSize) {
delete cache[userId][keys[0]];
const userCache = cache[userId] ?? {};
const keys = Object.keys(cache[userId] ?? {});
const [firstKey] = keys;
if (keys.length > maxCacheSize && firstKey) {
delete cache[userId]?.[firstKey];
}
cache[userId][str] = trackObject;
userCache[str] = trackObject;
cache[userId] = userCache;
}

export function setToCache(
Expand Down
22 changes: 14 additions & 8 deletions server/src/tools/importers/full_privacy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export class FullPrivacyImporter
});
const finalInfos: { played_at: Date; id: string }[] = [];
for (let i = 0; i < items.length; i += 1) {
const item = items[i];
const item = items[i]!;
const date = new Date(item.played_at);
const duplicate = await getCloseTrackId(
this.userId.toString(),
Expand All @@ -113,7 +113,7 @@ export class FullPrivacyImporter
);
if (duplicate.length > 0 || currentImportDuplicate) {
logger.info(
`${item.track.name} - ${item.track.artists[0].name} was duplicate`,
`${item.track.name} - ${item.track.artists[0]?.name} was duplicate`,
);
continue;
}
Expand All @@ -126,10 +126,10 @@ export class FullPrivacyImporter
await addTrackIdsToUser(this.userId.toString(), finalInfos);
const min = minOfArray(finalInfos, info => info.played_at.getTime());
if (min) {
await storeFirstListenedAtIfLess(
this.userId,
finalInfos[min.minIndex].played_at,
);
const minInfo = finalInfos[min.minIndex];
if (minInfo) {
await storeFirstListenedAtIfLess(this.userId, minInfo.played_at);
}
}
};

Expand Down Expand Up @@ -199,7 +199,7 @@ export class FullPrivacyImporter
items.push({ track: searchedItem, played_at: pa });
});
logger.info(
`Adding ${searchedItem.name} - ${searchedItem.artists[0].name} from data`,
`Adding ${searchedItem.name} - ${searchedItem.artists[0]?.name} from data`,
);
});
idsToSearch = {};
Expand All @@ -217,7 +217,7 @@ export class FullPrivacyImporter
for (let i = this.currentItem; i < this.elements.length; i += 1) {
this.currentItem = i;
logger.info(`Importing... (${i}/${this.elements.length})`);
const content = this.elements[i];
const content = this.elements[i]!;
if (
!content.spotify_track_uri ||
!content.master_metadata_track_name ||
Expand All @@ -239,6 +239,12 @@ export class FullPrivacyImporter
const spotifyId = FullPrivacyImporter.idFromSpotifyURI(
content.spotify_track_uri,
);
if (!spotifyId) {
logger.warn(
`Could not get spotify id from uri: ${content.spotify_episode_uri}`,
);
continue;
}
const item = getFromCacheString(this.userId.toString(), spotifyId);
if (!item) {
const arrayOfPlayedAt = idsToSearch[spotifyId] ?? [];
Expand Down
16 changes: 8 additions & 8 deletions server/src/tools/importers/privacy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export class PrivacyImporter
await storeTrackAlbumArtist({ tracks, albums, artists });
const finalInfos: { played_at: Date; id: string }[] = [];
for (let i = 0; i < items.length; i += 1) {
const item = items[i];
const item = items[i]!;
const date = new Date(`${item.played_at}Z`);
const duplicate = await getCloseTrackId(
this.userId.toString(),
Expand All @@ -91,7 +91,7 @@ export class PrivacyImporter
);
if (duplicate.length > 0 || currentImportDuplicate) {
logger.info(
`${item.track.name} - ${item.track.artists[0].name} was duplicate`,
`${item.track.name} - ${item.track.artists[0]?.name} was duplicate`,
);
continue;
}
Expand All @@ -104,10 +104,10 @@ export class PrivacyImporter
await addTrackIdsToUser(this.userId.toString(), finalInfos);
const min = minOfArray(finalInfos, info => info.played_at.getTime());
if (min) {
await storeFirstListenedAtIfLess(
this.userId,
finalInfos[min.minIndex].played_at,
);
const minInfo = finalInfos[min.minIndex];
if (minInfo) {
await storeFirstListenedAtIfLess(this.userId, minInfo.played_at);
}
}
};

Expand Down Expand Up @@ -164,7 +164,7 @@ export class PrivacyImporter
}
for (let i = this.currentItem; i < this.elements.length; i += 1) {
this.currentItem = i;
const content = this.elements[i];
const content = this.elements[i]!;
if (content.msPlayed < 30 * 1000) {
// If track was played for less than 30 seconds
logger.info(
Expand Down Expand Up @@ -206,7 +206,7 @@ export class PrivacyImporter
item,
);
logger.info(
`Adding ${item.name} - ${item.artists[0].name} from data (${i}/${this.elements.length})`,
`Adding ${item.name} - ${item.artists[0]?.name} from data (${i}/${this.elements.length})`,
);
items.push({ track: item, played_at: content.endTime });
if (items.length >= 20) {
Expand Down
10 changes: 5 additions & 5 deletions server/src/tools/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,9 @@ const defaultDiacriticsRemovalMap = [

const diacriticsMap: Record<string, string> = {};
for (let i = 0; i < defaultDiacriticsRemovalMap.length; i += 1) {
const { letters } = defaultDiacriticsRemovalMap[i];
const { letters } = defaultDiacriticsRemovalMap[i]!;
for (let j = 0; j < letters.length; j += 1) {
diacriticsMap[letters[j]] = defaultDiacriticsRemovalMap[i].base;
diacriticsMap[letters[j]!] = defaultDiacriticsRemovalMap[i]!.base;
}
}

Expand All @@ -266,7 +266,7 @@ export function removeDiacritics(str: string) {

export function beforeParenthesis(str: string) {
for (let i = 0; i < str.length; i += 1) {
if ('()[]'.includes(str[i])) {
if ('()[]'.includes(str[i]!)) {
return str.slice(0, i).replace(/'/g, ' ');
}
}
Expand Down Expand Up @@ -313,9 +313,9 @@ export const minOfArray = <T>(array: T[], fn: (item: T) => number) => {
}

let minIndex = 0;
let min = fn(array[0]);
let min = fn(array[0]!);
for (let i = 1; i < array.length; i += 1) {
const value = fn(array[i]);
const value = fn(array[i]!);
if (value < min) {
min = value;
minIndex = i;
Expand Down
5 changes: 4 additions & 1 deletion server/src/tools/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ export class Version {

isNewerThan(version: Version) {
for (let i = 0; i < this.parts.length; i += 1) {
const currentPart = this.parts[i];
const currentPart = this.parts[i]!;
const currentVersionPart = version.parts[i];

if (!currentVersionPart) {
return false;
}
if (currentPart && !currentVersionPart) {
return true;
}
Expand Down

0 comments on commit 0834017

Please sign in to comment.