Skip to content
Merged
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
94 changes: 69 additions & 25 deletions packages/backend/src/routes/artists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
getSpotifyClientToken,
isSpotifyAuthConfigured,
} from '../services/SpotifyAuthService'
import { spotifyLinkService } from '@lucky/shared/services'
import { spotifyLinkService, redisClient } from '@lucky/shared/services'

const FALLBACK_SUGGESTIONS_CACHE_KEY = 'artist:suggestions:fallback:v1'
const FALLBACK_SUGGESTIONS_TTL_SECONDS = 60 * 60 // 1 hour

const saveArtistBody = z.object({
guildId: z.string().min(1),
Expand Down Expand Up @@ -46,7 +49,7 @@
'/api/artists/suggestions',
apiLimiter,
requireAuth,
async (req: AuthenticatedRequest, res: Response) => {

Check warning on line 52 in packages/backend/src/routes/artists.ts

View workflow job for this annotation

GitHub Actions / Quality Gates

Async arrow function has a complexity of 34. Maximum allowed is 15
try {
const discordUserId = req.user?.id
if (!discordUserId) {
Expand Down Expand Up @@ -93,7 +96,7 @@
popularity?: number
genres?: string[]
}
if (

Check warning on line 99 in packages/backend/src/routes/artists.ts

View workflow job for this annotation

GitHub Actions / Quality Gates

Blocks are nested too deeply (7). Maximum allowed is 6
artist.id &&
artist.name &&
suggestions.size < 24
Expand All @@ -116,32 +119,73 @@
}

if (suggestions.size < 24) {
const suggestQueries = [
'Drake',
'The Weeknd',
'Dua Lipa',
'Billie Eilish',
'Bad Bunny',
'Ariana Grande',
'Taylor Swift',
'Ed Sheeran',
]
for (const query of suggestQueries) {
if (suggestions.size >= 24) break
try {
const artists = await searchSpotifyArtists(
clientToken,
query,
2,
)
for (const artist of artists) {
if (suggestions.size >= 24) break
if (!suggestions.has(artist.id)) {
suggestions.set(artist.id, artist)
// Cached fallback: avoid hammering Spotify search (429s on
// every page load otherwise). Cache the popular-artists
// bundle in Redis for 1h.
let fallback: SpotifyArtist[] = []
try {
const cached = await redisClient.get(
FALLBACK_SUGGESTIONS_CACHE_KEY,
)
if (cached) {
fallback = JSON.parse(cached) as SpotifyArtist[]
}
Comment thread
LucasSantana-Dev marked this conversation as resolved.
} catch {
// Redis miss/error — fall through to live fetch
}

if (fallback.length === 0) {
const suggestQueries = [
'Drake',
'The Weeknd',
'Dua Lipa',
'Billie Eilish',
'Bad Bunny',
'Ariana Grande',
'Taylor Swift',
'Ed Sheeran',
'Bruno Mars',
'Olivia Rodrigo',
'Sabrina Carpenter',
'Kendrick Lamar',
]
const seen = new Set<string>()
for (const query of suggestQueries) {
if (fallback.length >= 24) break
try {
const artists = await searchSpotifyArtists(
clientToken,
query,
2,
)
for (const artist of artists) {
if (fallback.length >= 24) break

Check warning on line 162 in packages/backend/src/routes/artists.ts

View workflow job for this annotation

GitHub Actions / Quality Gates

Blocks are nested too deeply (7). Maximum allowed is 6
if (!seen.has(artist.id)) {

Check warning on line 163 in packages/backend/src/routes/artists.ts

View workflow job for this annotation

GitHub Actions / Quality Gates

Blocks are nested too deeply (7). Maximum allowed is 6
seen.add(artist.id)
fallback.push(artist)
}
}
} catch {
// continue to next query
}
}
if (fallback.length > 0) {
try {
await redisClient.setex(
FALLBACK_SUGGESTIONS_CACHE_KEY,
FALLBACK_SUGGESTIONS_TTL_SECONDS,
JSON.stringify(fallback),
)
} catch {
// Cache write failure is non-fatal
}
Comment thread
LucasSantana-Dev marked this conversation as resolved.
} catch {
// continue to next query
}
}

for (const artist of fallback) {
if (suggestions.size >= 24) break
if (!suggestions.has(artist.id)) {
suggestions.set(artist.id, artist)
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions packages/backend/tests/unit/routes/artists.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ jest.mock('@lucky/shared/services', () => ({
spotifyLinkService: {
getValidAccessToken: jest.fn(),
},
redisClient: {
get: jest.fn().mockResolvedValue(null),
setex: jest.fn().mockResolvedValue('OK'),
},
})

jest.mock('../../../src/middleware/rateLimit', () => ({
Expand Down