diff options
author | TheArcaneBrony <myrainbowdash949@gmail.com> | 2023-09-19 00:16:36 +0200 |
---|---|---|
committer | TheArcaneBrony <myrainbowdash949@gmail.com> | 2023-09-19 00:16:36 +0200 |
commit | f5447484512d726f4403f0d7725777d0a95601fb (patch) | |
tree | 47fe88a4c021f62ff42663f6d8c5229e71d21813 /LibMatrix/RoomTypes | |
parent | Improve README (diff) | |
download | LibMatrix-f5447484512d726f4403f0d7725777d0a95601fb.tar.xz |
Add more stuff, add unit tests
Diffstat (limited to 'LibMatrix/RoomTypes')
-rw-r--r-- | LibMatrix/RoomTypes/GenericRoom.cs | 33 | ||||
-rw-r--r-- | LibMatrix/RoomTypes/SpaceRoom.cs | 6 |
2 files changed, 25 insertions, 14 deletions
diff --git a/LibMatrix/RoomTypes/GenericRoom.cs b/LibMatrix/RoomTypes/GenericRoom.cs index 146b5dd..ab748fe 100644 --- a/LibMatrix/RoomTypes/GenericRoom.cs +++ b/LibMatrix/RoomTypes/GenericRoom.cs @@ -2,10 +2,12 @@ using System.Net.Http.Json; using System.Text.Json; using System.Text.Json.Serialization; using System.Web; +using LibMatrix.EventTypes.Spec; +using LibMatrix.EventTypes.Spec.State; using LibMatrix.Extensions; using LibMatrix.Homeservers; +using LibMatrix.Interfaces; using LibMatrix.Responses; -using LibMatrix.StateEventTypes.Spec; using Microsoft.Extensions.Logging; namespace LibMatrix.RoomTypes; @@ -65,12 +67,12 @@ public class GenericRoom { #endif } catch (MatrixException e) { - if (e is not { ErrorCode: "M_NOT_FOUND" }) { + // if (e is not { ErrorCodode: "M_NOT_FOUND" }) { throw; - } + // } - Console.WriteLine(e); - return default; + // Console.WriteLine(e); + // return default; } } @@ -93,7 +95,7 @@ public class GenericRoom { } } - public async Task JoinAsync(string[]? homeservers = null, string? reason = null) { + public async Task<RoomIdResponse> JoinAsync(string[]? homeservers = null, string? reason = null) { var join_url = $"/_matrix/client/v3/join/{HttpUtility.UrlEncode(RoomId)}"; Console.WriteLine($"Calling {join_url} with {homeservers?.Length ?? 0} via's..."); if (homeservers == null || homeservers.Length == 0) homeservers = new[] { RoomId.Split(':')[1] }; @@ -101,6 +103,7 @@ public class GenericRoom { var res = await _httpClient.PostAsJsonAsync(fullJoinUrl, new { reason }); + return await res.Content.ReadFromJsonAsync<RoomIdResponse>() ?? throw new Exception("Failed to join room?"); } // TODO: rewrite (members endpoint?) @@ -111,10 +114,10 @@ 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"); - var result = - JsonSerializer.DeserializeAsyncEnumerable<StateEventResponse>(await res.Content.ReadAsStreamAsync()); - await foreach (var resp in result) { + var res = await _httpClient.GetAsync($"/_matrix/client/v3/rooms/{RoomId}/members?limit=2"); + var resText = await res.Content.ReadAsStringAsync(); + var result = await JsonSerializer.DeserializeAsync<ChunkedStateEventResponse>(await res.Content.ReadAsStreamAsync()); + foreach (var resp in result.Chunk) { if (resp?.Type != "m.room.member") continue; if (joinedOnly && (resp.TypedContent as RoomMemberEventContent)?.Membership is not "join") continue; yield return resp; @@ -123,6 +126,9 @@ public class GenericRoom { #region Utility shortcuts + public async Task<EventIdResponse> SendMessageEventAsync(RoomMessageEventContent content) => + await SendTimelineEventAsync("m.room.message", content); + public async Task<List<string>> GetAliasesAsync() { var res = await GetStateAsync<RoomAliasEventContent>("m.room.aliases"); return res.Aliases; @@ -187,7 +193,7 @@ public class GenericRoom { await (await _httpClient.PutAsJsonAsync($"/_matrix/client/v3/rooms/{RoomId}/state/{eventType}/{stateKey}", content)) .Content.ReadFromJsonAsync<EventIdResponse>(); - public async Task<EventIdResponse> SendMessageEventAsync(string eventType, RoomMessageEventContent content) { + public async Task<EventIdResponse> SendTimelineEventAsync(string eventType, EventContent content) { var res = await _httpClient.PutAsJsonAsync( $"/_matrix/client/v3/rooms/{RoomId}/send/{eventType}/" + Guid.NewGuid(), content, new JsonSerializerOptions { DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull @@ -244,3 +250,8 @@ public class GenericRoom { await _httpClient.PostAsJsonAsync($"/_matrix/client/v3/rooms/{RoomId}/invite", new UserIdAndReason(userId, reason)); } } + +public class RoomIdResponse { + [JsonPropertyName("room_id")] + public string RoomId { get; set; } = null!; +} diff --git a/LibMatrix/RoomTypes/SpaceRoom.cs b/LibMatrix/RoomTypes/SpaceRoom.cs index 0a4447a..a43ae82 100644 --- a/LibMatrix/RoomTypes/SpaceRoom.cs +++ b/LibMatrix/RoomTypes/SpaceRoom.cs @@ -13,14 +13,14 @@ public class SpaceRoom : GenericRoom { private static SemaphoreSlim _semaphore = new(1, 1); public async IAsyncEnumerable<GenericRoom> GetChildrenAsync(bool includeRemoved = false) { - await _semaphore.WaitAsync(); + // await _semaphore.WaitAsync(); var rooms = new List<GenericRoom>(); var state = GetFullStateAsync(); await foreach (var stateEvent in state) { if (stateEvent.Type != "m.space.child") continue; if (stateEvent.RawContent.ToJson() != "{}" || includeRemoved) - yield return await _homeserver.GetRoom(stateEvent.StateKey); + yield return _homeserver.GetRoom(stateEvent.StateKey); } - _semaphore.Release(); + // _semaphore.Release(); } } |