[backend/core] Update user LastActiveDate in background in AuthenticationMiddleware
This commit is contained in:
parent
0db4693f23
commit
14ef53f577
2 changed files with 18 additions and 2 deletions
|
@ -2,11 +2,12 @@ using Iceshrimp.Backend.Controllers.Mastodon.Attributes;
|
||||||
using Iceshrimp.Backend.Core.Database;
|
using Iceshrimp.Backend.Core.Database;
|
||||||
using Iceshrimp.Backend.Core.Database.Tables;
|
using Iceshrimp.Backend.Core.Database.Tables;
|
||||||
using Iceshrimp.Backend.Core.Helpers;
|
using Iceshrimp.Backend.Core.Helpers;
|
||||||
|
using Iceshrimp.Backend.Core.Services;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
namespace Iceshrimp.Backend.Core.Middleware;
|
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)
|
public async Task InvokeAsync(HttpContext ctx, RequestDelegate next)
|
||||||
{
|
{
|
||||||
|
@ -55,6 +56,7 @@ public class AuthenticationMiddleware(DatabaseContext db) : IMiddleware
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
userSvc.UpdateUserLastActive(oauthToken.User);
|
||||||
ctx.SetOauthToken(oauthToken);
|
ctx.SetOauthToken(oauthToken);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -78,6 +80,7 @@ public class AuthenticationMiddleware(DatabaseContext db) : IMiddleware
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
userSvc.UpdateUserLastActive(session.User);
|
||||||
ctx.SetSession(session);
|
ctx.SetSession(session);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,7 +23,8 @@ public class UserService(
|
||||||
DatabaseContext db,
|
DatabaseContext db,
|
||||||
ActivityPub.ActivityFetcherService fetchSvc,
|
ActivityPub.ActivityFetcherService fetchSvc,
|
||||||
DriveService driveSvc,
|
DriveService driveSvc,
|
||||||
MfmConverter mfmConverter
|
MfmConverter mfmConverter,
|
||||||
|
FollowupTaskService followupTaskSvc
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
private (string Username, string? Host) AcctToTuple(string acct)
|
private (string Username, string? Host) AcctToTuple(string acct)
|
||||||
|
@ -339,4 +340,16 @@ public class UserService(
|
||||||
if (user.Banner != null)
|
if (user.Banner != null)
|
||||||
await driveSvc.RemoveFile(user.Banner);
|
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));
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Add table
Reference in a new issue