✨ Last seen at
👔 Update register account validation
This commit is contained in:
parent
0e78f7f7d2
commit
b1c12685c8
@ -50,10 +50,18 @@ public class AccountController(
|
|||||||
|
|
||||||
public class AccountCreateRequest
|
public class AccountCreateRequest
|
||||||
{
|
{
|
||||||
[Required] [MaxLength(256)] public string Name { get; set; } = string.Empty;
|
[Required]
|
||||||
|
[MinLength(2)]
|
||||||
|
[MaxLength(256)]
|
||||||
|
[RegularExpression(@"^[A-Za-z0-9_-]+$",
|
||||||
|
ErrorMessage = "Name can only contain letters, numbers, underscores, and hyphens.")
|
||||||
|
]
|
||||||
|
public string Name { get; set; } = string.Empty;
|
||||||
|
|
||||||
[Required] [MaxLength(256)] public string Nick { get; set; } = string.Empty;
|
[Required] [MaxLength(256)] public string Nick { get; set; } = string.Empty;
|
||||||
|
|
||||||
[EmailAddress]
|
[EmailAddress]
|
||||||
|
[RegularExpression(@"^[^+]+@[^@]+\.[^@]+$", ErrorMessage = "Email address cannot contain '+' symbol.")]
|
||||||
[Required]
|
[Required]
|
||||||
[MaxLength(1024)]
|
[MaxLength(1024)]
|
||||||
public string Email { get; set; } = string.Empty;
|
public string Email { get; set; } = string.Empty;
|
||||||
|
@ -3,6 +3,7 @@ using System.Security.Cryptography;
|
|||||||
using System.Text.Encodings.Web;
|
using System.Text.Encodings.Web;
|
||||||
using DysonNetwork.Sphere.Account;
|
using DysonNetwork.Sphere.Account;
|
||||||
using DysonNetwork.Sphere.Storage;
|
using DysonNetwork.Sphere.Storage;
|
||||||
|
using DysonNetwork.Sphere.Storage.Handlers;
|
||||||
using Microsoft.AspNetCore.Authentication;
|
using Microsoft.AspNetCore.Authentication;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
@ -37,7 +38,8 @@ public class DysonTokenAuthHandler(
|
|||||||
ILoggerFactory logger,
|
ILoggerFactory logger,
|
||||||
UrlEncoder encoder,
|
UrlEncoder encoder,
|
||||||
AppDatabase database,
|
AppDatabase database,
|
||||||
ICacheService cache
|
ICacheService cache,
|
||||||
|
FlushBufferService fbs
|
||||||
)
|
)
|
||||||
: AuthenticationHandler<DysonTokenAuthOptions>(options, logger, encoder)
|
: AuthenticationHandler<DysonTokenAuthOptions>(options, logger, encoder)
|
||||||
{
|
{
|
||||||
@ -116,6 +118,15 @@ public class DysonTokenAuthHandler(
|
|||||||
var principal = new ClaimsPrincipal(identity);
|
var principal = new ClaimsPrincipal(identity);
|
||||||
|
|
||||||
var ticket = new AuthenticationTicket(principal, AuthConstants.SchemeName);
|
var ticket = new AuthenticationTicket(principal, AuthConstants.SchemeName);
|
||||||
|
|
||||||
|
var lastInfo = new LastActiveInfo
|
||||||
|
{
|
||||||
|
Account = session.Account,
|
||||||
|
Session = session,
|
||||||
|
SeenAt = SystemClock.Instance.GetCurrentInstant(),
|
||||||
|
};
|
||||||
|
fbs.Enqueue(lastInfo);
|
||||||
|
|
||||||
return AuthenticateResult.Success(ticket);
|
return AuthenticateResult.Success(ticket);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
@ -16,7 +16,7 @@ public class Session : ModelBase
|
|||||||
public Guid AccountId { get; set; }
|
public Guid AccountId { get; set; }
|
||||||
[JsonIgnore] public Account.Account Account { get; set; } = null!;
|
[JsonIgnore] public Account.Account Account { get; set; } = null!;
|
||||||
public Guid ChallengeId { get; set; }
|
public Guid ChallengeId { get; set; }
|
||||||
[JsonIgnore] public Challenge Challenge { get; set; } = null!;
|
public Challenge Challenge { get; set; } = null!;
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum ChallengeType
|
public enum ChallengeType
|
||||||
|
@ -185,7 +185,7 @@ builder.Services.AddSingleton(tusDiskStore);
|
|||||||
builder.Services.AddSingleton<FlushBufferService>();
|
builder.Services.AddSingleton<FlushBufferService>();
|
||||||
builder.Services.AddScoped<ActionLogFlushHandler>();
|
builder.Services.AddScoped<ActionLogFlushHandler>();
|
||||||
builder.Services.AddScoped<MessageReadReceiptFlushHandler>();
|
builder.Services.AddScoped<MessageReadReceiptFlushHandler>();
|
||||||
builder.Services.AddScoped<ActionLogService>();
|
builder.Services.AddScoped<LastActiveFlushHandler>();
|
||||||
|
|
||||||
// The handlers for websocket
|
// The handlers for websocket
|
||||||
builder.Services.AddScoped<IWebSocketPacketHandler, MessageReadHandler>();
|
builder.Services.AddScoped<IWebSocketPacketHandler, MessageReadHandler>();
|
||||||
@ -199,6 +199,7 @@ builder.Services.AddScoped<GeoIpService>();
|
|||||||
builder.Services.AddScoped<WebSocketService>();
|
builder.Services.AddScoped<WebSocketService>();
|
||||||
builder.Services.AddScoped<EmailService>();
|
builder.Services.AddScoped<EmailService>();
|
||||||
builder.Services.AddScoped<PermissionService>();
|
builder.Services.AddScoped<PermissionService>();
|
||||||
|
builder.Services.AddScoped<ActionLogService>();
|
||||||
builder.Services.AddScoped<AccountService>();
|
builder.Services.AddScoped<AccountService>();
|
||||||
builder.Services.AddScoped<AccountEventService>();
|
builder.Services.AddScoped<AccountEventService>();
|
||||||
builder.Services.AddScoped<ActionLogService>();
|
builder.Services.AddScoped<ActionLogService>();
|
||||||
|
@ -0,0 +1,69 @@
|
|||||||
|
using EFCore.BulkExtensions;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using NodaTime;
|
||||||
|
using Quartz;
|
||||||
|
|
||||||
|
namespace DysonNetwork.Sphere.Storage.Handlers;
|
||||||
|
|
||||||
|
public class LastActiveInfo
|
||||||
|
{
|
||||||
|
public Auth.Session Session { get; set; }
|
||||||
|
public Account.Account Account { get; set; }
|
||||||
|
public Instant SeenAt { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class LastActiveFlushHandler(IServiceProvider serviceProvider) : IFlushHandler<LastActiveInfo>
|
||||||
|
{
|
||||||
|
public async Task FlushAsync(IReadOnlyList<LastActiveInfo> items)
|
||||||
|
{
|
||||||
|
using var scope = serviceProvider.CreateScope();
|
||||||
|
var db = scope.ServiceProvider.GetRequiredService<AppDatabase>();
|
||||||
|
|
||||||
|
// Remove duplicates by grouping on (sessionId, accountId), taking the most recent SeenAt
|
||||||
|
var distinctItems = items
|
||||||
|
.GroupBy(x => (SessionId: x.Session.Id, AccountId: x.Account.Id))
|
||||||
|
.Select(g => g.OrderByDescending(x => x.SeenAt).First())
|
||||||
|
.ToList();
|
||||||
|
|
||||||
|
// Build dictionaries so we can match session/account IDs to their new "last seen" timestamps
|
||||||
|
var sessionIdMap = distinctItems
|
||||||
|
.GroupBy(x => x.Session.Id)
|
||||||
|
.ToDictionary(g => g.Key, g => g.Last().SeenAt);
|
||||||
|
|
||||||
|
var accountIdMap = distinctItems
|
||||||
|
.GroupBy(x => x.Account.Id)
|
||||||
|
.ToDictionary(g => g.Key, g => g.Last().SeenAt);
|
||||||
|
|
||||||
|
// Load all sessions that need to be updated in one batch
|
||||||
|
var sessionsToUpdate = await db.AuthSessions
|
||||||
|
.Where(s => sessionIdMap.Keys.Contains(s.Id))
|
||||||
|
.ToListAsync();
|
||||||
|
|
||||||
|
// Update their LastGrantedAt
|
||||||
|
foreach (var session in sessionsToUpdate)
|
||||||
|
session.LastGrantedAt = sessionIdMap[session.Id];
|
||||||
|
|
||||||
|
// Bulk update sessions
|
||||||
|
await db.BulkUpdateAsync(sessionsToUpdate);
|
||||||
|
|
||||||
|
// Similarly, load account profiles in one batch
|
||||||
|
var accountProfilesToUpdate = await db.AccountProfiles
|
||||||
|
.Where(a => accountIdMap.Keys.Contains(a.AccountId))
|
||||||
|
.ToListAsync();
|
||||||
|
|
||||||
|
// Update their LastSeenAt
|
||||||
|
foreach (var profile in accountProfilesToUpdate)
|
||||||
|
profile.LastSeenAt = accountIdMap[profile.AccountId];
|
||||||
|
|
||||||
|
// Bulk update profiles
|
||||||
|
await db.BulkUpdateAsync(accountProfilesToUpdate);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class LastActiveFlushJob(FlushBufferService fbs, ActionLogFlushHandler hdl) : IJob
|
||||||
|
{
|
||||||
|
public async Task Execute(IJobExecutionContext context)
|
||||||
|
{
|
||||||
|
await fbs.FlushAsync(hdl);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user