diff --git a/DysonNetwork.Sphere/Account/AccountCurrentController.cs b/DysonNetwork.Sphere/Account/AccountCurrentController.cs index 112a6fe..85b5d97 100644 --- a/DysonNetwork.Sphere/Account/AccountCurrentController.cs +++ b/DysonNetwork.Sphere/Account/AccountCurrentController.cs @@ -291,4 +291,42 @@ public class AccountCurrentController( return Ok(logs); } + + [HttpGet("factors")] + public async Task>> GetAuthFactors() + { + if (HttpContext.Items["CurrentUser"] is not Account currentUser) return Unauthorized(); + + var factors = await db.AccountAuthFactors + .Include(f => f.Account) + .Where(f => f.Account.Id == currentUser.Id) + .ToListAsync(); + + return Ok(factors); + } + + [HttpGet("sessions")] + public async Task>> GetSessions( + [FromQuery] int take = 20, + [FromQuery] int offset = 0 + ) + { + if (HttpContext.Items["CurrentUser"] is not Account currentUser) return Unauthorized(); + + var query = db.AuthSessions + .Include(session => session.Account) + .Include(session => session.Challenge) + .Where(session => session.Account.Id == currentUser.Id) + .OrderByDescending(session => session.CreatedAt); + + var total = await query.CountAsync(); + Response.Headers.Append("X-Total", total.ToString()); + + var sessions = await query + .Skip(offset) + .Take(take) + .ToListAsync(); + + return Ok(sessions); + } } \ No newline at end of file diff --git a/DysonNetwork.Sphere/Chat/ChatRoomService.cs b/DysonNetwork.Sphere/Chat/ChatRoomService.cs index b232990..fb4fe39 100644 --- a/DysonNetwork.Sphere/Chat/ChatRoomService.cs +++ b/DysonNetwork.Sphere/Chat/ChatRoomService.cs @@ -25,8 +25,8 @@ public class ChatRoomService(AppDatabase db, ICacheService cache) .ToListAsync(); var chatRoomGroup = ChatRoomGroupPrefix + roomId; - await cache.SetWithGroupsAsync(cacheKey, members, - new[] { chatRoomGroup }, + await cache.SetWithGroupsAsync(cacheKey, members, + [chatRoomGroup], TimeSpan.FromMinutes(5)); return members; diff --git a/DysonNetwork.Sphere/Chat/ChatService.cs b/DysonNetwork.Sphere/Chat/ChatService.cs index ad68038..8dcee64 100644 --- a/DysonNetwork.Sphere/Chat/ChatService.cs +++ b/DysonNetwork.Sphere/Chat/ChatService.cs @@ -40,7 +40,7 @@ public class ChatService( return message; } - public async Task DeliverMessageAsync( + private async Task DeliverMessageAsync( Message message, ChatMember sender, ChatRoom room, @@ -48,20 +48,22 @@ public class ChatService( ) { using var scope = scopeFactory.CreateScope(); - var scopedDb = scope.ServiceProvider.GetRequiredService(); var scopedWs = scope.ServiceProvider.GetRequiredService(); var scopedNty = scope.ServiceProvider.GetRequiredService(); + var scopedCrs = scope.ServiceProvider.GetRequiredService(); var roomSubject = room.Realm is not null ? $"{room.Name}, {room.Realm.Name}" : room.Name; var tasks = new List(); - var members = await scopedDb.ChatMembers - .Where(m => m.ChatRoomId == message.ChatRoomId && m.AccountId != sender.AccountId) - .Where(m => m.Notify != ChatMemberNotify.None) - .Where(m => m.Notify != ChatMemberNotify.Mentions || - (message.MembersMentioned != null && message.MembersMentioned.Contains(m.Id))) - .ToListAsync(); + var members = await scopedCrs.ListRoomMembers(room.Id); + var notification = new Notification + { + Topic = "messages.new", + Title = $"{sender.Nick ?? sender.Account.Nick} ({roomSubject})", + }; + + List accountsToNotify = []; foreach (var member in members) { scopedWs.SendPacketToAccount(member.AccountId, new WebSocketPacket @@ -69,14 +71,11 @@ public class ChatService( Type = type, Data = message }); - tasks.Add(scopedNty.DeliveryNotification(new Notification - { - AccountId = member.AccountId, - Topic = "messages.new", - Title = $"{sender.Nick ?? sender.Account.Nick} ({roomSubject})", - })); + accountsToNotify.Add(member.Account); } + tasks.Add(scopedNty.SendNotificationBatch(notification, accountsToNotify, save: false)); + await Task.WhenAll(tasks); }