Skip to content

Commit

Permalink
Don't update tokens when it's not required
Browse files Browse the repository at this point in the history
  • Loading branch information
stanriders committed Jul 1, 2024
1 parent a2c13c7 commit bd2f34b
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 13 deletions.
2 changes: 1 addition & 1 deletion backend/Mutualify/Controllers/ApiController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public async Task RefreshFriends()
{
try
{
await _usersService.Update(_claim);
await _usersService.Update(_claim, true);
await _relationsService.UpdateRelations(_claim);
}
catch (HttpRequestException e)
Expand Down
2 changes: 1 addition & 1 deletion backend/Mutualify/Jobs/UserRelationsUpdateJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public async Task Run(PerformContext context, CancellationToken token)
_logger.LogInformation("[{JobId}] ({Current}/{Total}) Updating {Id}...", jobId, i+1,
userUpdateQueue.Count, userId);

await _usersService.Update(userId);
await _usersService.Update(userId, true);
await _relationsService.UpdateRelations(userId);
}
catch (AggregateException e)
Expand Down
3 changes: 2 additions & 1 deletion backend/Mutualify/Jobs/UserUpdateJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public async Task Run(PerformContext context, CancellationToken token)

var userUpdateQueue = await _databaseContext.Users.AsNoTracking()
.Where(x=> x.UpdatedAt == null || x.UpdatedAt < DateTime.UtcNow.AddDays(-1))
.OrderByDescending(x=> x.FollowerCount)
.Select(x => x.Id)
.ToListAsync(cancellationToken: token);

Expand All @@ -68,7 +69,7 @@ public async Task Run(PerformContext context, CancellationToken token)
}
#endif

await _usersService.Update(userId);
await _usersService.Update(userId, false);
}
catch (AggregateException e)
{
Expand Down
2 changes: 1 addition & 1 deletion backend/Mutualify/Services/Interfaces/IUsersService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public interface IUsersService
Task<StatsContract> GetStats();
Task<RankingsContract> GetFollowerLeaderboard(int offset);
Task<int> GetFollowerLeaderboardRanking(int userId);
Task Update(int userId);
Task Update(int userId, bool useTokens);
Task ToggleFriendlistAccess(int userId, bool allow);
}
}
39 changes: 30 additions & 9 deletions backend/Mutualify/Services/UsersService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Mutualify.Database;
using Mutualify.Database.Models;
using Mutualify.OsuApi.Interfaces;
using Mutualify.OsuApi.Models;
using Mutualify.Services.Interfaces;

namespace Mutualify.Services
Expand Down Expand Up @@ -95,7 +96,7 @@ public async Task<int> GetFollowerLeaderboardRanking(int userId)
.SingleOrDefaultAsync();
}

public async Task Update(int userId)
public async Task Update(int userId, bool useTokens)
{
var user = await _databaseContext.Users.FindAsync(userId);
if (user is null)
Expand All @@ -105,16 +106,36 @@ public async Task Update(int userId)
return;
}

var token = await _databaseContext.Tokens.FindAsync(userId);
if (token is not null && token.ExpiresOn <= DateTime.UtcNow.AddDays(1))
{
// refresh close-to-expiration tokens
_logger.LogInformation("User {UserId} tokens are close to expiration ({ExpiresOn} <= {Threshold}), updating...", token.UserId, token.ExpiresOn, DateTime.UtcNow.AddDays(1));
OsuUser? osuUser;

await RefreshToken(token);
// todo: refactor into a separate method?
if (useTokens)
{
var token = await _databaseContext.Tokens.FindAsync(userId);
if (token is not null)
{
if (token.ExpiresOn <= DateTime.UtcNow.AddDays(1))
{
// refresh close-to-expiration tokens
_logger.LogInformation(
"User {UserId} tokens are close to expiration ({ExpiresOn} <= {Threshold}), updating...",
token.UserId, token.ExpiresOn, DateTime.UtcNow.AddDays(1));

await RefreshToken(token);
}
osuUser = await _osuApiDataService.GetUser(token.AccessToken);
}
else
{
// no token - update using app's token
osuUser = await _osuApiDataService.GetUser(userId);
}
}
else
{
osuUser = await _osuApiDataService.GetUser(userId);
}

var osuUser = await _osuApiDataService.GetUser(userId);
if (osuUser is null)
{
return;
Expand Down Expand Up @@ -155,7 +176,7 @@ private async Task RefreshToken(Token token)
token.ExpiresOn = DateTime.UtcNow.AddSeconds(newToken.ExpiresIn);

_databaseContext.Tokens.Update(token);
_logger.LogInformation("Updated tokens for user {UserId}", token.UserId);
_logger.LogInformation("Updated tokens for user {UserId}, new token expiration: {ExpiresOn}", token.UserId, token.ExpiresOn);
}
else
{
Expand Down

0 comments on commit bd2f34b

Please sign in to comment.