diff options
author | TheArcaneBrony <myrainbowdash949@gmail.com> | 2023-10-24 17:44:03 +0200 |
---|---|---|
committer | TheArcaneBrony <myrainbowdash949@gmail.com> | 2023-10-24 17:44:03 +0200 |
commit | 478fc1b0ef855530e1e93c5212d154280f9d7dd8 (patch) | |
tree | 3bb348b939f8d78e85337d49d3163ac5dbc8e8cc | |
parent | fix synchelper null check (diff) | |
download | LibMatrix-478fc1b0ef855530e1e93c5212d154280f9d7dd8.tar.xz |
Minor cleanup
-rw-r--r-- | LibMatrix/EventTypes/Spec/State/RoomInfo/RoomMemberEventContent.cs | 4 | ||||
-rw-r--r-- | LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs | 5 | ||||
-rw-r--r-- | LibMatrix/Homeservers/RemoteHomeServer.cs | 6 | ||||
-rw-r--r-- | LibMatrix/Interfaces/EventContent.cs | 4 | ||||
-rw-r--r-- | LibMatrix/Responses/UserProfileResponse.cs (renamed from LibMatrix/EventTypes/Spec/State/ProfileResponseEventContent.cs) | 4 | ||||
-rw-r--r-- | LibMatrix/RoomTypes/GenericRoom.cs | 13 | ||||
-rw-r--r-- | LibMatrix/StateEvent.cs | 7 | ||||
-rw-r--r-- | Tests/LibMatrix.Tests/Tests/RoomTests.cs | 13 |
8 files changed, 30 insertions, 26 deletions
diff --git a/LibMatrix/EventTypes/Spec/State/RoomInfo/RoomMemberEventContent.cs b/LibMatrix/EventTypes/Spec/State/RoomInfo/RoomMemberEventContent.cs index bda84a4..da9ab0a 100644 --- a/LibMatrix/EventTypes/Spec/State/RoomInfo/RoomMemberEventContent.cs +++ b/LibMatrix/EventTypes/Spec/State/RoomInfo/RoomMemberEventContent.cs @@ -3,8 +3,10 @@ using LibMatrix.Interfaces; namespace LibMatrix.EventTypes.Spec.State; -[MatrixEvent(EventName = "m.room.member")] +[MatrixEvent(EventName = EventId)] public class RoomMemberEventContent : EventContent { + public const string EventId = "m.room.member"; + [JsonPropertyName("reason")] public string? Reason { get; set; } diff --git a/LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs b/LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs index ef5d01f..e5e4274 100644 --- a/LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs +++ b/LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs @@ -156,7 +156,7 @@ public class AuthenticatedHomeserverGeneric(string baseUrl, string accessToken) return await _httpClient.GetFromJsonAsync<T>($"/_matrix/client/v3/user/{WhoAmI.UserId}/account_data/{key}"); } - public virtual async Task SetAccountData(string key, object data) { + public virtual async Task SetAccountDataAsync(string key, object data) { var res = await _httpClient.PutAsJsonAsync($"/_matrix/client/v3/user/{WhoAmI.UserId}/account_data/{key}", data); if (!res.IsSuccessStatusCode) { Console.WriteLine($"Failed to set account data: {await res.Content.ReadAsStringAsync()}"); @@ -168,10 +168,11 @@ public class AuthenticatedHomeserverGeneric(string baseUrl, string accessToken) public string? ResolveMediaUri(string? mxcUri) { if (mxcUri is null) return null; + if (mxcUri.StartsWith("https://")) return mxcUri; return $"{_httpClient.BaseAddress}/_matrix/media/v3/download/{mxcUri.Replace("mxc://", "")}".Replace("//_matrix", "/_matrix"); } - public async Task UpdateProfileAsync(ProfileResponseEventContent? newProfile, bool preserveCustomRoomProfile = true) { + public async Task UpdateProfileAsync(UserProfileResponse? newProfile, bool preserveCustomRoomProfile = true) { if (newProfile is null) return; Console.WriteLine($"Updating profile for {WhoAmI.UserId} to {newProfile.ToJson(ignoreNull: true)} (preserving room profiles: {preserveCustomRoomProfile})"); var oldProfile = await GetProfileAsync(WhoAmI.UserId!); diff --git a/LibMatrix/Homeservers/RemoteHomeServer.cs b/LibMatrix/Homeservers/RemoteHomeServer.cs index e8b0cc8..a8d0326 100644 --- a/LibMatrix/Homeservers/RemoteHomeServer.cs +++ b/LibMatrix/Homeservers/RemoteHomeServer.cs @@ -23,17 +23,17 @@ public class RemoteHomeServer(string baseUrl) { public string BaseUrl { get; } = baseUrl; public MatrixHttpClient _httpClient { get; set; } - public async Task<ProfileResponseEventContent> GetProfileAsync(string mxid) { + public async Task<UserProfileResponse> GetProfileAsync(string mxid) { if (mxid is null) throw new ArgumentNullException(nameof(mxid)); if (_profileCache.TryGetValue(mxid, out var value)) { if (value is SemaphoreSlim s) await s.WaitAsync(); - if (value is ProfileResponseEventContent p) return p; + if (value is UserProfileResponse p) return p; } _profileCache[mxid] = new SemaphoreSlim(1); var resp = await _httpClient.GetAsync($"/_matrix/client/v3/profile/{mxid}"); - var data = await resp.Content.ReadFromJsonAsync<ProfileResponseEventContent>(); + var data = await resp.Content.ReadFromJsonAsync<UserProfileResponse>(); if (!resp.IsSuccessStatusCode) Console.WriteLine("Profile: " + data); _profileCache[mxid] = data; diff --git a/LibMatrix/Interfaces/EventContent.cs b/LibMatrix/Interfaces/EventContent.cs index 37d316b..5cf0503 100644 --- a/LibMatrix/Interfaces/EventContent.cs +++ b/LibMatrix/Interfaces/EventContent.cs @@ -6,8 +6,8 @@ public abstract class EventContent { [JsonPropertyName("m.relates_to")] public MessageRelatesTo? RelatesTo { get; set; } - [JsonPropertyName("m.new_content")] - public EventContent? NewContent { get; set; } + // [JsonPropertyName("m.new_content")] + // public EventContent? NewContent { get; set; } public class MessageRelatesTo { [JsonPropertyName("m.in_reply_to")] diff --git a/LibMatrix/EventTypes/Spec/State/ProfileResponseEventContent.cs b/LibMatrix/Responses/UserProfileResponse.cs index 893fce1..e56e87b 100644 --- a/LibMatrix/EventTypes/Spec/State/ProfileResponseEventContent.cs +++ b/LibMatrix/Responses/UserProfileResponse.cs @@ -1,9 +1,9 @@ using System.Text.Json.Serialization; using LibMatrix.Interfaces; -namespace LibMatrix.EventTypes.Spec.State; +namespace LibMatrix.Responses; -public class ProfileResponseEventContent : EventContent { +public class UserProfileResponse { [JsonPropertyName("avatar_url")] public string? AvatarUrl { get; set; } diff --git a/LibMatrix/RoomTypes/GenericRoom.cs b/LibMatrix/RoomTypes/GenericRoom.cs index d11b28d..1398f14 100644 --- a/LibMatrix/RoomTypes/GenericRoom.cs +++ b/LibMatrix/RoomTypes/GenericRoom.cs @@ -84,14 +84,9 @@ public class GenericRoom { } // TODO: should we even error handle here? - public async Task<string> GetNameAsync() { - try { - var res = await GetStateAsync<RoomNameEventContent>("m.room.name"); - return res?.Name ?? RoomId; - } - catch (MatrixException e) { - return $"{RoomId} ({e.ErrorCode})"; - } + public async Task<string?> GetNameAsync() { + var res = await GetStateAsync<RoomNameEventContent>("m.room.name"); + return res?.Name; } public async Task<RoomIdResponse> JoinAsync(string[]? homeservers = null, string? reason = null) { @@ -285,4 +280,4 @@ public class GenericRoom { public class RoomIdResponse { [JsonPropertyName("room_id")] public string RoomId { get; set; } = null!; -} +} \ No newline at end of file diff --git a/LibMatrix/StateEvent.cs b/LibMatrix/StateEvent.cs index fb4dd71..dbb3401 100644 --- a/LibMatrix/StateEvent.cs +++ b/LibMatrix/StateEvent.cs @@ -56,7 +56,12 @@ public class StateEvent { return null; } - set => RawContent = JsonSerializer.Deserialize<JsonObject>(JsonSerializer.Serialize(value, value.GetType())); + set { + if (value is null) { + RawContent = null; + } + else RawContent = JsonSerializer.Deserialize<JsonObject>(JsonSerializer.Serialize(value, value.GetType())); + } } [JsonPropertyName("state_key")] diff --git a/Tests/LibMatrix.Tests/Tests/RoomTests.cs b/Tests/LibMatrix.Tests/Tests/RoomTests.cs index 72a2775..65f1cca 100644 --- a/Tests/LibMatrix.Tests/Tests/RoomTests.cs +++ b/Tests/LibMatrix.Tests/Tests/RoomTests.cs @@ -2,6 +2,7 @@ using System.Text; using ArcaneLibs.Extensions; using LibMatrix.EventTypes.Spec.State; using LibMatrix.Homeservers; +using LibMatrix.Responses; using LibMatrix.Services; using LibMatrix.Tests.Abstractions; using LibMatrix.Tests.Fixtures; @@ -150,11 +151,11 @@ public class RoomTests : TestBed<TestFixture> { var room = await RoomAbstraction.GetTestRoom(hs); Assert.NotNull(room); - await room.SendStateEventAsync("gay.rory.libmatrix.unit_tests", new ProfileResponseEventContent() { + await room.SendStateEventAsync("gay.rory.libmatrix.unit_tests", new UserProfileResponse() { DisplayName = "wee_woo", AvatarUrl = "no" }); - await room.SendStateEventAsync("gay.rory.libmatrix.unit_tests", "state_key_maybe", new ProfileResponseEventContent() { + await room.SendStateEventAsync("gay.rory.libmatrix.unit_tests", "state_key_maybe", new UserProfileResponse() { DisplayName = "wee_woo", AvatarUrl = "yes" }); @@ -167,16 +168,16 @@ public class RoomTests : TestBed<TestFixture> { var room = await RoomAbstraction.GetTestRoom(hs); Assert.NotNull(room); - await room.SendStateEventAsync("gay.rory.libmatrix.unit_tests", new ProfileResponseEventContent() { + await room.SendStateEventAsync("gay.rory.libmatrix.unit_tests", new UserProfileResponse() { DisplayName = "wee_woo", AvatarUrl = "no" }); - await room.SendStateEventAsync("gay.rory.libmatrix.unit_tests", "state_key_maybe", new ProfileResponseEventContent() { + await room.SendStateEventAsync("gay.rory.libmatrix.unit_tests", "state_key_maybe", new UserProfileResponse() { DisplayName = "wee_woo", AvatarUrl = "yes" }); - var state1 = await room.GetStateAsync<ProfileResponseEventContent>("gay.rory.libmatrix.unit_tests"); + var state1 = await room.GetStateAsync<UserProfileResponse>("gay.rory.libmatrix.unit_tests"); Assert.NotNull(state1); Assert.NotNull(state1.DisplayName); Assert.NotEmpty(state1.DisplayName); @@ -185,7 +186,7 @@ public class RoomTests : TestBed<TestFixture> { Assert.Equal("wee_woo", state1.DisplayName); Assert.Equal("no", state1.AvatarUrl); - var state2 = await room.GetStateAsync<ProfileResponseEventContent>("gay.rory.libmatrix.unit_tests", "state_key_maybe"); + var state2 = await room.GetStateAsync<UserProfileResponse>("gay.rory.libmatrix.unit_tests", "state_key_maybe"); Assert.NotNull(state2); Assert.NotNull(state2.DisplayName); Assert.NotEmpty(state2.DisplayName); |