From 478fc1b0ef855530e1e93c5212d154280f9d7dd8 Mon Sep 17 00:00:00 2001 From: TheArcaneBrony Date: Tue, 24 Oct 2023 17:44:03 +0200 Subject: Minor cleanup --- .../EventTypes/Spec/State/ProfileResponseEventContent.cs | 12 ------------ .../Spec/State/RoomInfo/RoomMemberEventContent.cs | 4 +++- LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs | 5 +++-- LibMatrix/Homeservers/RemoteHomeServer.cs | 6 +++--- LibMatrix/Interfaces/EventContent.cs | 4 ++-- LibMatrix/Responses/UserProfileResponse.cs | 12 ++++++++++++ LibMatrix/RoomTypes/GenericRoom.cs | 13 ++++--------- LibMatrix/StateEvent.cs | 7 ++++++- Tests/LibMatrix.Tests/Tests/RoomTests.cs | 13 +++++++------ 9 files changed, 40 insertions(+), 36 deletions(-) delete mode 100644 LibMatrix/EventTypes/Spec/State/ProfileResponseEventContent.cs create mode 100644 LibMatrix/Responses/UserProfileResponse.cs diff --git a/LibMatrix/EventTypes/Spec/State/ProfileResponseEventContent.cs b/LibMatrix/EventTypes/Spec/State/ProfileResponseEventContent.cs deleted file mode 100644 index 893fce1..0000000 --- a/LibMatrix/EventTypes/Spec/State/ProfileResponseEventContent.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System.Text.Json.Serialization; -using LibMatrix.Interfaces; - -namespace LibMatrix.EventTypes.Spec.State; - -public class ProfileResponseEventContent : EventContent { - [JsonPropertyName("avatar_url")] - public string? AvatarUrl { get; set; } - - [JsonPropertyName("displayname")] - public string? DisplayName { get; set; } -} 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($"/_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 GetProfileAsync(string mxid) { + public async Task 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(); + var data = await resp.Content.ReadFromJsonAsync(); 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/Responses/UserProfileResponse.cs b/LibMatrix/Responses/UserProfileResponse.cs new file mode 100644 index 0000000..e56e87b --- /dev/null +++ b/LibMatrix/Responses/UserProfileResponse.cs @@ -0,0 +1,12 @@ +using System.Text.Json.Serialization; +using LibMatrix.Interfaces; + +namespace LibMatrix.Responses; + +public class UserProfileResponse { + [JsonPropertyName("avatar_url")] + public string? AvatarUrl { get; set; } + + [JsonPropertyName("displayname")] + public string? DisplayName { 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 GetNameAsync() { - try { - var res = await GetStateAsync("m.room.name"); - return res?.Name ?? RoomId; - } - catch (MatrixException e) { - return $"{RoomId} ({e.ErrorCode})"; - } + public async Task GetNameAsync() { + var res = await GetStateAsync("m.room.name"); + return res?.Name; } public async Task 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(JsonSerializer.Serialize(value, value.GetType())); + set { + if (value is null) { + RawContent = null; + } + else RawContent = JsonSerializer.Deserialize(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 { 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 { 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("gay.rory.libmatrix.unit_tests"); + var state1 = await room.GetStateAsync("gay.rory.libmatrix.unit_tests"); Assert.NotNull(state1); Assert.NotNull(state1.DisplayName); Assert.NotEmpty(state1.DisplayName); @@ -185,7 +186,7 @@ public class RoomTests : TestBed { Assert.Equal("wee_woo", state1.DisplayName); Assert.Equal("no", state1.AvatarUrl); - var state2 = await room.GetStateAsync("gay.rory.libmatrix.unit_tests", "state_key_maybe"); + var state2 = await room.GetStateAsync("gay.rory.libmatrix.unit_tests", "state_key_maybe"); Assert.NotNull(state2); Assert.NotNull(state2.DisplayName); Assert.NotEmpty(state2.DisplayName); -- cgit 1.4.1