DM groups

This commit is contained in:
LittleSheep 2025-05-17 22:36:46 +08:00
parent 3d197b667a
commit 27f934c634
2 changed files with 56 additions and 40 deletions

View File

@ -16,7 +16,8 @@ public class ChatRoomController(
FileService fs, FileService fs,
ChatRoomService crs, ChatRoomService crs,
RealmService rs, RealmService rs,
ActionLogService als) : ControllerBase ActionLogService als
) : ControllerBase
{ {
[HttpGet("{id:guid}")] [HttpGet("{id:guid}")]
public async Task<ActionResult<ChatRoom>> GetChatRoom(Guid id) public async Task<ActionResult<ChatRoom>> GetChatRoom(Guid id)
@ -28,23 +29,18 @@ public class ChatRoomController(
if (chatRoom is null) return NotFound(); if (chatRoom is null) return NotFound();
if (chatRoom.Type != ChatRoomType.DirectMessage) return Ok(chatRoom); if (chatRoom.Type != ChatRoomType.DirectMessage) return Ok(chatRoom);
// Preload members for direct messages if (HttpContext.Items["CurrentUser"] is Account.Account currentUser)
var currentUser = HttpContext.Items["CurrentUser"] as Account.Account; chatRoom = await crs.LoadDirectMessageMembers(chatRoom, currentUser.Id);
var directMembers = await db.ChatMembers
.Where(m => (currentUser != null && m.AccountId != currentUser!.Id))
.Include(m => m.Account)
.Include(m => m.Account.Profile)
.ToListAsync();
chatRoom.DirectMembers = directMembers.Select(ChatMemberTransmissionObject.FromEntity).ToList();
return Ok(chatRoom); return Ok(chatRoom);
} }
[HttpGet] [HttpGet]
[Authorize]
public async Task<ActionResult<List<ChatRoom>>> ListJoinedChatRooms() public async Task<ActionResult<List<ChatRoom>>> ListJoinedChatRooms()
{ {
if (HttpContext.Items["CurrentUser"] is not Account.Account currentUser) if (HttpContext.Items["CurrentUser"] is not Account.Account currentUser)
return Unauthorized(); return Unauthorized();
var userId = currentUser.Id; var userId = currentUser.Id;
var chatRooms = await db.ChatMembers var chatRooms = await db.ChatMembers
@ -53,30 +49,9 @@ public class ChatRoomController(
.Include(m => m.ChatRoom) .Include(m => m.ChatRoom)
.Select(m => m.ChatRoom) .Select(m => m.ChatRoom)
.ToListAsync(); .ToListAsync();
chatRooms = await crs.LoadDirectMessageMembers(chatRooms, userId);
var directRoomsId = chatRooms return Ok(chatRooms);
.Where(r => r.Type == ChatRoomType.DirectMessage)
.Select(r => r.Id)
.ToList();
var directMembers = directRoomsId.Count != 0
? await db.ChatMembers
.Where(m => directRoomsId.Contains(m.ChatRoomId))
.Where(m => m.AccountId != userId)
.Include(m => m.Account)
.Include(m => m.Account.Profile)
.ToDictionaryAsync(m => m.ChatRoomId, m => m)
: new Dictionary<Guid, ChatMember>();
// Map the results
var result = chatRooms.Select(r =>
{
if (r.Type == ChatRoomType.DirectMessage && directMembers.TryGetValue(r.Id, out var otherMember))
r.DirectMembers = new List<ChatMemberTransmissionObject>
{ ChatMemberTransmissionObject.FromEntity(otherMember) };
return r;
}).ToList();
return Ok(result);
} }
public class DirectMessageRequest public class DirectMessageRequest
@ -454,7 +429,7 @@ public class ChatRoomController(
.Where(m => m.ChatRoom.Type == ChatRoomType.DirectMessage) .Where(m => m.ChatRoom.Type == ChatRoomType.DirectMessage)
.Select(m => m.ChatRoom.Id) .Select(m => m.ChatRoom.Id)
.ToList(); .ToList();
var directMembers = directRoomsId.Count != 0 var directMembers = directRoomsId.Count != 0
? await db.ChatMembers ? await db.ChatMembers
.Where(m => directRoomsId.Contains(m.ChatRoomId)) .Where(m => directRoomsId.Contains(m.ChatRoomId))
@ -463,15 +438,16 @@ public class ChatRoomController(
.Include(m => m.Account.Profile) .Include(m => m.Account.Profile)
.ToDictionaryAsync(m => m.ChatRoomId, m => m) .ToDictionaryAsync(m => m.ChatRoomId, m => m)
: new Dictionary<Guid, ChatMember>(); : new Dictionary<Guid, ChatMember>();
// Map the results // Map the results
members.ForEach(m => members.ForEach(m =>
{ {
if (m.ChatRoom.Type == ChatRoomType.DirectMessage && directMembers.TryGetValue(m.ChatRoomId, out var otherMember)) if (m.ChatRoom.Type == ChatRoomType.DirectMessage &&
directMembers.TryGetValue(m.ChatRoomId, out var otherMember))
m.ChatRoom.DirectMembers = new List<ChatMemberTransmissionObject> m.ChatRoom.DirectMembers = new List<ChatMemberTransmissionObject>
{ ChatMemberTransmissionObject.FromEntity(otherMember) }; { ChatMemberTransmissionObject.FromEntity(otherMember) };
}); });
return members.ToList(); return members.ToList();
} }
@ -497,7 +473,7 @@ public class ChatRoomController(
ActionLogType.ChatroomJoin, ActionLogType.ChatroomJoin,
new Dictionary<string, object> { { "chatroom_id", roomId } }, Request new Dictionary<string, object> { { "chatroom_id", roomId } }, Request
); );
return Ok(member); return Ok(member);
} }
@ -621,7 +597,7 @@ public class ChatRoomController(
ActionLogType.ChatroomKick, ActionLogType.ChatroomKick,
new Dictionary<string, object> { { "chatroom_id", roomId }, { "account_id", memberId } }, Request new Dictionary<string, object> { { "chatroom_id", roomId }, { "account_id", memberId } }, Request
); );
return NoContent(); return NoContent();
} }
@ -660,7 +636,7 @@ public class ChatRoomController(
ActionLogType.ChatroomLeave, ActionLogType.ChatroomLeave,
new Dictionary<string, object> { { "chatroom_id", roomId } }, Request new Dictionary<string, object> { { "chatroom_id", roomId } }, Request
); );
return NoContent(); return NoContent();
} }
} }

View File

@ -11,6 +11,46 @@ public class ChatRoomService(AppDatabase db, NotificationService nty)
$"You just got invited to join {member.ChatRoom.Name}"); $"You just got invited to join {member.ChatRoom.Name}");
} }
public async Task<List<ChatRoom>> LoadDirectMessageMembers(List<ChatRoom> rooms, Guid userId)
{
var directRoomsId = rooms
.Where(r => r.Type == ChatRoomType.DirectMessage)
.Select(r => r.Id)
.ToList();
if (directRoomsId.Count == 0) return rooms;
var directMembers = directRoomsId.Count != 0
? await db.ChatMembers
.Where(m => directRoomsId.Contains(m.ChatRoomId))
.Where(m => m.AccountId != userId)
.Include(m => m.Account)
.Include(m => m.Account.Profile)
.GroupBy(m => m.ChatRoomId)
.ToDictionaryAsync(g => g.Key, g => g.ToList())
: new Dictionary<Guid, List<ChatMember>>();
return rooms.Select(r =>
{
if (r.Type == ChatRoomType.DirectMessage && directMembers.TryGetValue(r.Id, out var otherMembers))
r.DirectMembers = otherMembers.Select(ChatMemberTransmissionObject.FromEntity).ToList();
return r;
}).ToList();
}
public async Task<ChatRoom> LoadDirectMessageMembers(ChatRoom room, Guid userId)
{
if (room.Type != ChatRoomType.DirectMessage) return room;
var members = await db.ChatMembers
.Where(m => m.ChatRoomId == room.Id && m.AccountId != userId)
.Include(m => m.Account)
.Include(m => m.Account.Profile)
.ToListAsync();
if (members.Count > 0)
room.DirectMembers = members.Select(ChatMemberTransmissionObject.FromEntity).ToList();
return room;
}
public async Task<bool> IsMemberWithRole(Guid roomId, Guid accountId, ChatMemberRole requiredRole) public async Task<bool> IsMemberWithRole(Guid roomId, Guid accountId, ChatMemberRole requiredRole)
{ {
var member = await db.ChatMembers var member = await db.ChatMembers