From 14ef53f5771f2feb2d355070d4f001d4ea313caf Mon Sep 17 00:00:00 2001 From: Laura Hausmann Date: Mon, 19 Feb 2024 20:16:08 +0100 Subject: [PATCH] [backend/core] Update user LastActiveDate in background in AuthenticationMiddleware --- .../Core/Middleware/AuthenticationMiddleware.cs | 5 ++++- Iceshrimp.Backend/Core/Services/UserService.cs | 15 ++++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/Iceshrimp.Backend/Core/Middleware/AuthenticationMiddleware.cs b/Iceshrimp.Backend/Core/Middleware/AuthenticationMiddleware.cs index b41eea61..50ec56d6 100644 --- a/Iceshrimp.Backend/Core/Middleware/AuthenticationMiddleware.cs +++ b/Iceshrimp.Backend/Core/Middleware/AuthenticationMiddleware.cs @@ -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); } } diff --git a/Iceshrimp.Backend/Core/Services/UserService.cs b/Iceshrimp.Backend/Core/Services/UserService.cs index a748445c..e1abe799 100644 --- a/Iceshrimp.Backend/Core/Services/UserService.cs +++ b/Iceshrimp.Backend/Core/Services/UserService.cs @@ -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(); + await bgDb.Users.Where(p => p.Id == user.Id) + .ExecuteUpdateAsync(p => p.SetProperty(u => u.LastActiveDate, DateTime.UtcNow)); + }); + } } \ No newline at end of file