feat(api): poll listens less often if user is inactive

To save on Spotify API requests we have two different classes of
polling intervals:

- all users are polled at least every 10 minutes, this is a safe interval
  and no listens will be ever missed
- if a user listened to a song within the last 60 minutes, we poll every
  minute to ensure that the UI shows new listens immediately
This commit is contained in:
Julian Tölle 2023-03-12 00:48:22 +01:00
parent b9f92bbdfa
commit 14478a5418
3 changed files with 59 additions and 6 deletions

View file

@ -36,8 +36,27 @@ export class SpotifyService {
) {}
@Span()
async getCrawlableUserInfo(): Promise<User[]> {
return this.usersService.findAll();
async getCrawlableUserInfo(): Promise<{ user: User; lastListen: Date }[]> {
// All of this is kinda inefficient, we do two db queries and join in code,
// i can't be bothered to do this properly in the db for now.
// Should be refactored if listory gets hundreds of users (lol).
const [users, listens] = await Promise.all([
this.usersService.findAll(),
this.listensService.getMostRecentListenPerUser(),
]);
return users.map((user) => {
const lastListen = listens.find((listen) => listen.user.id === user.id);
return {
user,
// Return 1970 if no listen exists
lastListen: lastListen ? lastListen.playedAt : new Date(0),
};
});
return;
}
@ImportSpotifyJob.Handle()