[backend/core] Add lastActiveDate column to oauth_token and session tables

This commit is contained in:
Laura Hausmann 2024-02-24 22:43:14 +01:00
parent ba0e041bad
commit 2c33f7aafc
No known key found for this signature in database
GPG key ID: D044E84C5BE01605
7 changed files with 6224 additions and 4 deletions

View file

@ -0,0 +1,39 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Iceshrimp.Backend.Core.Database.Migrations
{
/// <inheritdoc />
public partial class AddOauthTokenAndSessionLastActiveDate : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<DateTime>(
name: "lastActiveDate",
table: "session",
type: "timestamp with time zone",
nullable: true);
migrationBuilder.AddColumn<DateTime>(
name: "lastActiveDate",
table: "oauth_token",
type: "timestamp with time zone",
nullable: true);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "lastActiveDate",
table: "session");
migrationBuilder.DropColumn(
name: "lastActiveDate",
table: "oauth_token");
}
}
}

View file

@ -3174,6 +3174,10 @@ namespace Iceshrimp.Backend.Core.Database.Migrations
.HasColumnName("createdAt")
.HasComment("The created date of the OAuth token");
b.Property<DateTime?>("LastActiveDate")
.HasColumnType("timestamp with time zone")
.HasColumnName("lastActiveDate");
b.Property<string>("RedirectUri")
.IsRequired()
.HasMaxLength(512)
@ -3738,6 +3742,10 @@ namespace Iceshrimp.Backend.Core.Database.Migrations
.HasColumnName("createdAt")
.HasComment("The created date of the OAuth token");
b.Property<DateTime?>("LastActiveDate")
.HasColumnType("timestamp with time zone")
.HasColumnName("lastActiveDate");
b.Property<string>("Token")
.IsRequired()
.HasMaxLength(64)

View file

@ -66,5 +66,7 @@ public class OauthToken
public virtual User User { get; set; } = null!;
[Column("supportsHtmlFormatting")] public bool SupportsHtmlFormatting { get; set; }
[Column("autoDetectQuotes")] public bool AutoDetectQuotes { get; set; }
[Column("autoDetectQuotes")] public bool AutoDetectQuotes { get; set; }
[Column("lastActiveDate")] public DateTime? LastActiveDate { get; set; }
}

View file

@ -37,4 +37,6 @@ public class Session
[ForeignKey("UserId")]
[InverseProperty(nameof(Tables.User.Sessions))]
public virtual User User { get; set; } = null!;
[Column("lastActiveDate")] public DateTime? LastActiveDate { get; set; }
}

View file

@ -56,7 +56,7 @@ public class AuthenticationMiddleware(DatabaseContext db, UserService userSvc) :
return;
}
userSvc.UpdateUserLastActive(oauthToken.User);
userSvc.UpdateOauthTokenMetadata(oauthToken);
ctx.SetOauthToken(oauthToken);
}
else
@ -80,7 +80,7 @@ public class AuthenticationMiddleware(DatabaseContext db, UserService userSvc) :
return;
}
userSvc.UpdateUserLastActive(session.User);
userSvc.UpdateSessionMetadata(session);
ctx.SetSession(session);
}
}

View file

@ -348,7 +348,33 @@ public class UserService(
await driveSvc.RemoveFile(user.Banner);
}
public void UpdateUserLastActive(User user)
public void UpdateOauthTokenMetadata(OauthToken token)
{
UpdateUserLastActive(token.User);
if (token.LastActiveDate != null && token.LastActiveDate > DateTime.UtcNow - TimeSpan.FromHours(1)) return;
_ = followupTaskSvc.ExecuteTask("UpdateOauthTokenMetadata", async provider =>
{
var bgDb = provider.GetRequiredService<DatabaseContext>();
await bgDb.OauthTokens.Where(p => p.Id == token.Id)
.ExecuteUpdateAsync(p => p.SetProperty(u => u.LastActiveDate, DateTime.UtcNow));
});
}
public void UpdateSessionMetadata(Session session)
{
UpdateUserLastActive(session.User);
_ = followupTaskSvc.ExecuteTask("UpdateSessionMetadata", async provider =>
{
var bgDb = provider.GetRequiredService<DatabaseContext>();
await bgDb.Sessions.Where(p => p.Id == session.Id)
.ExecuteUpdateAsync(p => p.SetProperty(u => u.LastActiveDate, DateTime.UtcNow));
});
}
private void UpdateUserLastActive(User user)
{
if (user.LastActiveDate != null && user.LastActiveDate > DateTime.UtcNow - TimeSpan.FromHours(1)) return;