Skip to content

Commit

Permalink
only pick active user with imports
Browse files Browse the repository at this point in the history
  • Loading branch information
th0mk committed Jan 13, 2025
1 parent 80c6998 commit eba51bb
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/FMBot.Bot/Factories/DataSourceFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,15 @@ public async Task<User> GetCachedImportUserAsync(string userNameLastFm)
await using var db = await this._contextFactory.CreateDbContextAsync();
var userNameParameter = new NpgsqlParameter("userNameLastFm", userNameLastFm);

user = await db.Users
var users = await db.Users
.FromSql(
$"SELECT * FROM users WHERE UPPER(user_name_last_fm) = UPPER({userNameParameter}) AND last_used IS NOT NULL AND data_source != 1 ORDER BY last_used DESC LIMIT 1")
$"SELECT * FROM users WHERE UPPER(user_name_last_fm) = UPPER({userNameParameter}) AND last_used IS NOT NULL ORDER BY last_used DESC LIMIT 3")
.AsNoTracking()
.FirstOrDefaultAsync();
.ToListAsync();

var importUser = users.FirstOrDefault(a =>
a.DataSource != DataSource.LastFm && a.LastUsed >= DateTime.UtcNow.AddDays(-7));
user = importUser ?? users.OrderByDescending(o => o.LastUsed).FirstOrDefault();

if (user != null)
{
Expand Down Expand Up @@ -264,7 +268,8 @@ public async Task<Response<TopAlbumList>> GetTopAlbumsAsync(string lastFmUserNam
return topAlbums;
}

topAlbums = await this._lastfmRepository.GetTopAlbumsAsync(lastFmUserName, timeSettings, count, amountOfPages, useCache);
topAlbums = await this._lastfmRepository.GetTopAlbumsAsync(lastFmUserName, timeSettings, count, amountOfPages,
useCache);

await CorrectTopAlbumNamesInternally(topAlbums);
AddAlbumTopList(topAlbums, lastFmUserName);
Expand Down Expand Up @@ -336,7 +341,8 @@ await this._playDataSourceRepository.GetTopArtistsAsync(importUser, timeSettings
}

topArtists =
await this._lastfmRepository.GetTopArtistsAsync(lastFmUserName, timeSettings, count, amountOfPages, useCache);
await this._lastfmRepository.GetTopArtistsAsync(lastFmUserName, timeSettings, count, amountOfPages,
useCache);

await CorrectTopArtistNamesInternally(topArtists);
AddArtistTopList(topArtists, lastFmUserName);
Expand Down

0 comments on commit eba51bb

Please sign in to comment.