🐛 Fix chat room missing realms info
This commit is contained in:
@@ -42,6 +42,8 @@ public class ChatRoomController(
|
||||
|
||||
if (chatRoom.Type != ChatRoomType.DirectMessage) return Ok(chatRoom);
|
||||
|
||||
chatRoom = await crs.LoadChatRealms(chatRoom);
|
||||
|
||||
if (HttpContext.Items["CurrentUser"] is Account currentUser)
|
||||
chatRoom = await crs.LoadDirectMessageMembers(chatRoom, Guid.Parse(currentUser.Id));
|
||||
|
||||
@@ -62,6 +64,7 @@ public class ChatRoomController(
|
||||
.Include(m => m.ChatRoom)
|
||||
.Select(m => m.ChatRoom)
|
||||
.ToListAsync();
|
||||
chatRooms = await crs.LoadChatRealms(chatRooms);
|
||||
chatRooms = await crs.LoadDirectMessageMembers(chatRooms, accountId);
|
||||
chatRooms = await crs.SortChatRoomByLastMessage(chatRooms);
|
||||
|
||||
@@ -715,8 +718,10 @@ public class ChatRoomController(
|
||||
.ToListAsync();
|
||||
|
||||
var chatRooms = members.Select(m => m.ChatRoom).ToList();
|
||||
chatRooms = await crs.LoadDirectMessageMembers(chatRooms, accountId);
|
||||
chatRooms = await crs.LoadChatRealms(chatRooms);
|
||||
var directMembers =
|
||||
(await crs.LoadDirectMessageMembers(chatRooms, accountId)).ToDictionary(c => c.Id, c => c.Members);
|
||||
chatRooms.ToDictionary(c => c.Id, c => c.Members);
|
||||
|
||||
foreach (var member in members.Where(member => member.ChatRoom.Type == ChatRoomType.DirectMessage))
|
||||
member.ChatRoom.Members = directMembers[member.ChatRoom.Id];
|
||||
|
||||
@@ -9,7 +9,8 @@ namespace DysonNetwork.Sphere.Chat;
|
||||
public class ChatRoomService(
|
||||
AppDatabase db,
|
||||
ICacheService cache,
|
||||
RemoteAccountService remoteAccountsHelper
|
||||
RemoteAccountService remoteAccounts,
|
||||
RemoteRealmService remoteRealms
|
||||
)
|
||||
{
|
||||
private const string ChatRoomGroupPrefix = "chatroom:";
|
||||
@@ -84,6 +85,26 @@ public class ChatRoomService(
|
||||
return sortedRooms;
|
||||
}
|
||||
|
||||
public async Task<List<SnChatRoom>> LoadChatRealms(List<SnChatRoom> rooms)
|
||||
{
|
||||
var realmIds = rooms.Where(r => r.RealmId.HasValue).Select(r => r.RealmId!.Value.ToString()).Distinct().ToList();
|
||||
|
||||
var realms = await remoteRealms.GetRealmBatch(realmIds);
|
||||
var realmDict = realms.ToDictionary(r => r.Id, r => r);
|
||||
|
||||
foreach (var room in rooms)
|
||||
if (room.RealmId.HasValue && realmDict.TryGetValue(room.RealmId.Value, out var realm))
|
||||
room.Realm = realm;
|
||||
|
||||
return rooms;
|
||||
}
|
||||
|
||||
public async Task<SnChatRoom> LoadChatRealms(SnChatRoom room)
|
||||
{
|
||||
var result = await LoadChatRealms(new List<SnChatRoom> { room });
|
||||
return result[0];
|
||||
}
|
||||
|
||||
public async Task<List<SnChatRoom>> LoadDirectMessageMembers(List<SnChatRoom> rooms, Guid userId)
|
||||
{
|
||||
var directRoomsId = rooms
|
||||
@@ -140,7 +161,7 @@ public class ChatRoomService(
|
||||
|
||||
public async Task<SnChatMember> LoadMemberAccount(SnChatMember member)
|
||||
{
|
||||
var account = await remoteAccountsHelper.GetAccount(member.AccountId);
|
||||
var account = await remoteAccounts.GetAccount(member.AccountId);
|
||||
member.Account = SnAccount.FromProtoValue(account);
|
||||
return member;
|
||||
}
|
||||
@@ -148,7 +169,7 @@ public class ChatRoomService(
|
||||
public async Task<List<SnChatMember>> LoadMemberAccounts(ICollection<SnChatMember> members)
|
||||
{
|
||||
var accountIds = members.Select(m => m.AccountId).ToList();
|
||||
var accounts = (await remoteAccountsHelper.GetAccountBatch(accountIds)).ToDictionary(a => Guid.Parse(a.Id), a => a);
|
||||
var accounts = (await remoteAccounts.GetAccountBatch(accountIds)).ToDictionary(a => Guid.Parse(a.Id), a => a);
|
||||
|
||||
return
|
||||
[
|
||||
|
||||
@@ -8,7 +8,7 @@ namespace DysonNetwork.Sphere.Chat;
|
||||
|
||||
[ApiController]
|
||||
[Route("/api/realms/{slug}")]
|
||||
public class RealmChatController(AppDatabase db, RemoteRealmService rs) : ControllerBase
|
||||
public class RealmChatController(AppDatabase db, ChatRoomService crs, RemoteRealmService rs) : ControllerBase
|
||||
{
|
||||
[HttpGet("chat")]
|
||||
[Authorize]
|
||||
@@ -28,6 +28,8 @@ public class RealmChatController(AppDatabase db, RemoteRealmService rs) : Contro
|
||||
var chatRooms = await db.ChatRooms
|
||||
.Where(c => c.RealmId == realm.Id)
|
||||
.ToListAsync();
|
||||
|
||||
chatRooms = await crs.LoadChatRealms(chatRooms);
|
||||
|
||||
return Ok(chatRooms);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user