[backend/core] Update user LastActiveDate in background in AuthenticationMiddleware

This commit is contained in:
Laura Hausmann 2024-02-19 20:16:08 +01:00
parent 0db4693f23
commit 14ef53f577
No known key found for this signature in database
GPG key ID: D044E84C5BE01605
2 changed files with 18 additions and 2 deletions

View file

@ -2,11 +2,12 @@ using Iceshrimp.Backend.Controllers.Mastodon.Attributes;
using Iceshrimp.Backend.Core.Database;
using Iceshrimp.Backend.Core.Database.Tables;
using Iceshrimp.Backend.Core.Helpers;
using Iceshrimp.Backend.Core.Services;
using Microsoft.EntityFrameworkCore;
namespace Iceshrimp.Backend.Core.Middleware;
public class AuthenticationMiddleware(DatabaseContext db) : IMiddleware
public class AuthenticationMiddleware(DatabaseContext db, UserService userSvc) : IMiddleware
{
public async Task InvokeAsync(HttpContext ctx, RequestDelegate next)
{
@ -55,6 +56,7 @@ public class AuthenticationMiddleware(DatabaseContext db) : IMiddleware
return;
}
userSvc.UpdateUserLastActive(oauthToken.User);
ctx.SetOauthToken(oauthToken);
}
else
@ -78,6 +80,7 @@ public class AuthenticationMiddleware(DatabaseContext db) : IMiddleware
return;
}
userSvc.UpdateUserLastActive(session.User);
ctx.SetSession(session);
}
}

View file

@ -23,7 +23,8 @@ public class UserService(
DatabaseContext db,
ActivityPub.ActivityFetcherService fetchSvc,
DriveService driveSvc,
MfmConverter mfmConverter
MfmConverter mfmConverter,
FollowupTaskService followupTaskSvc
)
{
private (string Username, string? Host) AcctToTuple(string acct)
@ -339,4 +340,16 @@ public class UserService(
if (user.Banner != null)
await driveSvc.RemoveFile(user.Banner);
}
public void UpdateUserLastActive(User user)
{
if (user.LastActiveDate != null && user.LastActiveDate > DateTime.UtcNow - TimeSpan.FromHours(1)) return;
_ = followupTaskSvc.ExecuteTask("UpdateUserLastActive", async provider =>
{
var bgDb = provider.GetRequiredService<DatabaseContext>();
await bgDb.Users.Where(p => p.Id == user.Id)
.ExecuteUpdateAsync(p => p.SetProperty(u => u.LastActiveDate, DateTime.UtcNow));
});
}
}