diff options
author | TheArcaneBrony <myrainbowdash949@gmail.com> | 2023-10-26 13:09:05 +0000 |
---|---|---|
committer | TheArcaneBrony <myrainbowdash949@gmail.com> | 2023-10-26 13:09:05 +0000 |
commit | b75135d8cdb702423d693558ffaec3f025264b98 (patch) | |
tree | 1e7c0d746ac387a0d6b648d4e452169db17debc6 /LibMatrix/RoomTypes/GenericRoom.cs | |
parent | Minor cleanup (diff) | |
download | LibMatrix-b75135d8cdb702423d693558ffaec3f025264b98.tar.xz |
split client and server http client for homeservers
Diffstat (limited to 'LibMatrix/RoomTypes/GenericRoom.cs')
-rw-r--r-- | LibMatrix/RoomTypes/GenericRoom.cs | 35 |
1 files changed, 27 insertions, 8 deletions
diff --git a/LibMatrix/RoomTypes/GenericRoom.cs b/LibMatrix/RoomTypes/GenericRoom.cs index 1398f14..96bcefd 100644 --- a/LibMatrix/RoomTypes/GenericRoom.cs +++ b/LibMatrix/RoomTypes/GenericRoom.cs @@ -18,7 +18,7 @@ public class GenericRoom { if (string.IsNullOrWhiteSpace(roomId)) throw new ArgumentException("Room ID cannot be null or whitespace", nameof(roomId)); Homeserver = homeserver; - _httpClient = homeserver._httpClient; + _httpClient = homeserver.ClientHttpClient; RoomId = roomId; if (GetType() != typeof(SpaceRoom)) AsSpace = new SpaceRoom(homeserver, RoomId); @@ -83,11 +83,7 @@ public class GenericRoom { return res ?? new MessagesResponse(); } - // TODO: should we even error handle here? - public async Task<string?> GetNameAsync() { - var res = await GetStateAsync<RoomNameEventContent>("m.room.name"); - return res?.Name; - } + public async Task<string?> GetNameAsync() => (await GetStateAsync<RoomNameEventContent>("m.room.name"))?.Name; public async Task<RoomIdResponse> JoinAsync(string[]? homeservers = null, string? reason = null) { var join_url = $"/_matrix/client/v3/join/{HttpUtility.UrlEncode(RoomId)}"; @@ -100,7 +96,7 @@ public class GenericRoom { return await res.Content.ReadFromJsonAsync<RoomIdResponse>() ?? throw new Exception("Failed to join room?"); } - // TODO: rewrite (members endpoint?) + public async IAsyncEnumerable<StateEventResponse> GetMembersAsync(bool joinedOnly = true) { // var res = GetFullStateAsync(); // await foreach (var member in res) { @@ -108,7 +104,7 @@ public class GenericRoom { // if (joinedOnly && (member.TypedContent as RoomMemberEventContent)?.Membership is not "join") continue; // yield return member; // } - var res = await _httpClient.GetAsync($"/_matrix/client/v3/rooms/{RoomId}/members?limit=2"); + var res = await _httpClient.GetAsync($"/_matrix/client/v3/rooms/{RoomId}/members"); var resText = await res.Content.ReadAsStringAsync(); var result = await JsonSerializer.DeserializeAsync<ChunkedStateEventResponse>(await res.Content.ReadAsStreamAsync()); foreach (var resp in result.Chunk) { @@ -157,6 +153,29 @@ public class GenericRoom { public async Task<RoomPowerLevelEventContent?> GetPowerLevelsAsync() => await GetStateAsync<RoomPowerLevelEventContent>("m.room.power_levels"); + public async Task<string> GetNameOrFallbackAsync() { + try { + return await GetNameAsync(); + } + catch { + try { + var members = GetMembersAsync(); + var memberList = new List<string>(); + int memberCount = 0; + await foreach (var member in members) + memberList.Add((member.TypedContent is RoomMemberEventContent memberEvent ? memberEvent.DisplayName : "") ?? ""); + memberCount = memberList.Count; + memberList.RemoveAll(string.IsNullOrWhiteSpace); + if (memberList.Count >= 3) + return string.Join(", ", memberList.Take(2)) + " and " + (memberCount - 2) + " others."; + return string.Join(", ", memberList); + } + catch { + return RoomId; + } + } + } + #endregion public async Task ForgetAsync() => |