Optimize the message notifications

This commit is contained in:
LittleSheep 2025-05-30 01:52:09 +08:00
parent fac9c3ae88
commit 8beeac09ef
3 changed files with 53 additions and 16 deletions

View File

@ -291,4 +291,42 @@ public class AccountCurrentController(
return Ok(logs);
}
[HttpGet("factors")]
public async Task<ActionResult<List<AccountAuthFactor>>> 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<ActionResult<List<Auth.Session>>> 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);
}
}

View File

@ -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;

View File

@ -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<AppDatabase>();
var scopedWs = scope.ServiceProvider.GetRequiredService<WebSocketService>();
var scopedNty = scope.ServiceProvider.GetRequiredService<NotificationService>();
var scopedCrs = scope.ServiceProvider.GetRequiredService<ChatRoomService>();
var roomSubject = room.Realm is not null ? $"{room.Name}, {room.Realm.Name}" : room.Name;
var tasks = new List<Task>();
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<Account.Account> 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);
}