🐛 Bug fixes and improvements

This commit is contained in:
2025-05-01 00:47:26 +08:00
parent 758186f674
commit 84a88222bd
13 changed files with 111 additions and 59 deletions

View File

@ -4,7 +4,6 @@ using DysonNetwork.Sphere.Storage;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
using NodaTime;
namespace DysonNetwork.Sphere.Account;
@ -15,8 +14,8 @@ public class AccountController(
AppDatabase db,
FileService fs,
AuthService auth,
MagicSpellService spells,
IMemoryCache memCache
AccountService accounts,
MagicSpellService spells
) : ControllerBase
{
[HttpGet("{name}")]
@ -138,7 +137,7 @@ public class AccountController(
if (request.Nick is not null) account.Nick = request.Nick;
if (request.Language is not null) account.Language = request.Language;
memCache.Remove($"user_${account.Id}");
await accounts.PurgeAccountCache(account);
await db.SaveChangesAsync();
return account;
@ -199,7 +198,7 @@ public class AccountController(
db.Update(profile);
await db.SaveChangesAsync();
memCache.Remove($"user_${userId}");
await accounts.PurgeAccountCache(currentUser);
return profile;
}

View File

@ -1,12 +1,23 @@
using Casbin;
using DysonNetwork.Sphere.Permission;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
using NodaTime;
namespace DysonNetwork.Sphere.Account;
public class AccountService(AppDatabase db, PermissionService pm)
public class AccountService(AppDatabase db, PermissionService pm, IMemoryCache cache)
{
public async Task PurgeAccountCache(Account account)
{
var sessions = await db.AuthSessions.Where(e => e.Account.Id == account.Id).Select(e => e.Id)
.ToListAsync();
foreach (var session in sessions)
{
cache.Remove($"dyn_auth_{session}");
}
}
public async Task<Account?> LookupAccount(string probe)
{
var account = await db.Accounts.Where(a => a.Name == probe).FirstOrDefaultAsync();

View File

@ -45,6 +45,8 @@ public class MagicSpellService(AppDatabase db, EmailService email, ILogger<Magic
// TODO replace the baseurl
var link = $"https://api.sn.solsynth.dev/spells/{Uri.EscapeDataString(spell.Spell)}";
logger.LogError($"Sending magic spell... {link}");
try
{

View File

@ -1,4 +1,5 @@
using System.ComponentModel.DataAnnotations;
using DysonNetwork.Sphere.Auth;
using DysonNetwork.Sphere.Post;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
@ -36,7 +37,6 @@ public class NotificationController(AppDatabase db, NotificationService nty) : C
public class PushNotificationSubscribeRequest
{
[MaxLength(4096)] public string DeviceId { get; set; } = null!;
[MaxLength(4096)] public string DeviceToken { get; set; } = null!;
public NotificationPushProvider Provider { get; set; }
}
@ -47,12 +47,16 @@ public class NotificationController(AppDatabase db, NotificationService nty) : C
[FromBody] PushNotificationSubscribeRequest request
)
{
HttpContext.Items.TryGetValue("CurrentSession", out var currentSessionValue);
HttpContext.Items.TryGetValue("CurrentUser", out var currentUserValue);
var currentUser = currentUserValue as Account;
if (currentUser == null) return Unauthorized();
var currentSession = currentSessionValue as Session;
if (currentSession == null) return Unauthorized();
var result =
await nty.SubscribePushNotification(currentUser, request.Provider, request.DeviceId, request.DeviceToken);
await nty.SubscribePushNotification(currentUser, request.Provider, currentSession.Challenge.DeviceId!,
request.DeviceToken);
return Ok(result);
}

View File

@ -72,11 +72,10 @@ public class NotificationService
DeviceId = deviceId,
DeviceToken = deviceToken,
Provider = provider,
Account = account,
AccountId = account.Id,
};
_db.Add(subscription);
_db.NotificationPushSubscriptions.Add(subscription);
await _db.SaveChangesAsync();
return subscription;