[backend/core] Add lastActiveDate column to oauth_token and session tables
This commit is contained in:
parent
ba0e041bad
commit
2c33f7aafc
7 changed files with 6224 additions and 4 deletions
6143
Iceshrimp.Backend/Core/Database/Migrations/20240224214251_AddOauthTokenAndSessionLastActiveDate.Designer.cs
generated
Normal file
6143
Iceshrimp.Backend/Core/Database/Migrations/20240224214251_AddOauthTokenAndSessionLastActiveDate.Designer.cs
generated
Normal file
File diff suppressed because it is too large
Load diff
|
@ -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");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -3174,6 +3174,10 @@ namespace Iceshrimp.Backend.Core.Database.Migrations
|
||||||
.HasColumnName("createdAt")
|
.HasColumnName("createdAt")
|
||||||
.HasComment("The created date of the OAuth token");
|
.HasComment("The created date of the OAuth token");
|
||||||
|
|
||||||
|
b.Property<DateTime?>("LastActiveDate")
|
||||||
|
.HasColumnType("timestamp with time zone")
|
||||||
|
.HasColumnName("lastActiveDate");
|
||||||
|
|
||||||
b.Property<string>("RedirectUri")
|
b.Property<string>("RedirectUri")
|
||||||
.IsRequired()
|
.IsRequired()
|
||||||
.HasMaxLength(512)
|
.HasMaxLength(512)
|
||||||
|
@ -3738,6 +3742,10 @@ namespace Iceshrimp.Backend.Core.Database.Migrations
|
||||||
.HasColumnName("createdAt")
|
.HasColumnName("createdAt")
|
||||||
.HasComment("The created date of the OAuth token");
|
.HasComment("The created date of the OAuth token");
|
||||||
|
|
||||||
|
b.Property<DateTime?>("LastActiveDate")
|
||||||
|
.HasColumnType("timestamp with time zone")
|
||||||
|
.HasColumnName("lastActiveDate");
|
||||||
|
|
||||||
b.Property<string>("Token")
|
b.Property<string>("Token")
|
||||||
.IsRequired()
|
.IsRequired()
|
||||||
.HasMaxLength(64)
|
.HasMaxLength(64)
|
||||||
|
|
|
@ -67,4 +67,6 @@ public class OauthToken
|
||||||
|
|
||||||
[Column("supportsHtmlFormatting")] public bool SupportsHtmlFormatting { get; set; }
|
[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; }
|
||||||
}
|
}
|
|
@ -37,4 +37,6 @@ public class Session
|
||||||
[ForeignKey("UserId")]
|
[ForeignKey("UserId")]
|
||||||
[InverseProperty(nameof(Tables.User.Sessions))]
|
[InverseProperty(nameof(Tables.User.Sessions))]
|
||||||
public virtual User User { get; set; } = null!;
|
public virtual User User { get; set; } = null!;
|
||||||
|
|
||||||
|
[Column("lastActiveDate")] public DateTime? LastActiveDate { get; set; }
|
||||||
}
|
}
|
|
@ -56,7 +56,7 @@ public class AuthenticationMiddleware(DatabaseContext db, UserService userSvc) :
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
userSvc.UpdateUserLastActive(oauthToken.User);
|
userSvc.UpdateOauthTokenMetadata(oauthToken);
|
||||||
ctx.SetOauthToken(oauthToken);
|
ctx.SetOauthToken(oauthToken);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -80,7 +80,7 @@ public class AuthenticationMiddleware(DatabaseContext db, UserService userSvc) :
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
userSvc.UpdateUserLastActive(session.User);
|
userSvc.UpdateSessionMetadata(session);
|
||||||
ctx.SetSession(session);
|
ctx.SetSession(session);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -348,7 +348,33 @@ public class UserService(
|
||||||
await driveSvc.RemoveFile(user.Banner);
|
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;
|
if (user.LastActiveDate != null && user.LastActiveDate > DateTime.UtcNow - TimeSpan.FromHours(1)) return;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue