From 314f7044f62b92c49abe2d5c7422c6cf3430b021 Mon Sep 17 00:00:00 2001 From: TheArcaneBrony Date: Sat, 23 Dec 2023 12:00:48 +0100 Subject: SetImpersonate optimisation (dont execute if already set), cleanup --- .../Homeservers/AuthenticatedHomeserverGeneric.cs | 1 + LibMatrix/RoomTypes/GenericRoom.cs | 74 ++++++++++++---------- 2 files changed, 43 insertions(+), 32 deletions(-) diff --git a/LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs b/LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs index 73cd641..0f2c1ea 100644 --- a/LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs +++ b/LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs @@ -309,6 +309,7 @@ public class AuthenticatedHomeserverGeneric(string serverName, string accessToke #endregion public async Task SetImpersonate(string mxid) { + if(ClientHttpClient.AdditionalQueryParameters.TryGetValue("user_id", out var existingMxid) && existingMxid == mxid && WhoAmI.UserId == mxid) return; ClientHttpClient.AdditionalQueryParameters["user_id"] = mxid; WhoAmI = await ClientHttpClient.GetFromJsonAsync("/_matrix/client/v3/account/whoami"); } diff --git a/LibMatrix/RoomTypes/GenericRoom.cs b/LibMatrix/RoomTypes/GenericRoom.cs index 786dd01..551d780 100644 --- a/LibMatrix/RoomTypes/GenericRoom.cs +++ b/LibMatrix/RoomTypes/GenericRoom.cs @@ -36,8 +36,8 @@ public class GenericRoom { } } - public async Task> GetFullStateAsListAsync() { - return await Homeserver.ClientHttpClient.GetFromJsonAsync>($"/_matrix/client/v3/rooms/{RoomId}/state"); + public Task> GetFullStateAsListAsync() { + return Homeserver.ClientHttpClient.GetFromJsonAsync>($"/_matrix/client/v3/rooms/{RoomId}/state"); } public async Task GetStateAsync(string type, string stateKey = "") { @@ -171,32 +171,41 @@ public class GenericRoom { public async IAsyncEnumerable GetMembersEnumerableAsync(bool joinedOnly = true) { var sw = Stopwatch.StartNew(); var res = await Homeserver.ClientHttpClient.GetAsync($"/_matrix/client/v3/rooms/{RoomId}/members"); - Console.WriteLine($"Members call responded in {sw.GetElapsedAndRestart()}"); + if(sw.ElapsedMilliseconds > 1000) + Console.WriteLine($"Members call responded in {sw.GetElapsedAndRestart()}"); + else sw.Restart(); // var resText = await res.Content.ReadAsStringAsync(); - Console.WriteLine($"Members call response read in {sw.GetElapsedAndRestart()}"); + // Console.WriteLine($"Members call response read in {sw.GetElapsedAndRestart()}"); var result = await JsonSerializer.DeserializeAsync(await res.Content.ReadAsStreamAsync(), new JsonSerializerOptions() { TypeInfoResolver = ChunkedStateEventResponseSerializerContext.Default, }); - Console.WriteLine($"Members call deserialised in {sw.GetElapsedAndRestart()}"); + if(sw.ElapsedMilliseconds > 100) + Console.WriteLine($"Members call deserialised in {sw.GetElapsedAndRestart()}"); + else sw.Restart(); 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; } - Console.WriteLine($"Members call iterated in {sw.GetElapsedAndRestart()}"); + if(sw.ElapsedMilliseconds > 100) + Console.WriteLine($"Members call iterated in {sw.GetElapsedAndRestart()}"); } public async Task> GetMembersListAsync(bool joinedOnly = true) { var sw = Stopwatch.StartNew(); var res = await Homeserver.ClientHttpClient.GetAsync($"/_matrix/client/v3/rooms/{RoomId}/members"); - Console.WriteLine($"Members call responded in {sw.GetElapsedAndRestart()}"); + if(sw.ElapsedMilliseconds > 1000) + Console.WriteLine($"Members call responded in {sw.GetElapsedAndRestart()}"); + else sw.Restart(); // var resText = await res.Content.ReadAsStringAsync(); - Console.WriteLine($"Members call response read in {sw.GetElapsedAndRestart()}"); + // Console.WriteLine($"Members call response read in {sw.GetElapsedAndRestart()}"); var result = await JsonSerializer.DeserializeAsync(await res.Content.ReadAsStreamAsync(), new JsonSerializerOptions() { TypeInfoResolver = ChunkedStateEventResponseSerializerContext.Default, }); - Console.WriteLine($"Members call deserialised in {sw.GetElapsedAndRestart()}"); + if(sw.ElapsedMilliseconds > 100) + Console.WriteLine($"Members call deserialised in {sw.GetElapsedAndRestart()}"); + else sw.Restart(); var members = new List(); foreach (var resp in result.Chunk) { if (resp?.Type != "m.room.member") continue; @@ -204,48 +213,49 @@ public class GenericRoom { members.Add(resp); } - Console.WriteLine($"Members call iterated in {sw.GetElapsedAndRestart()}"); + if(sw.ElapsedMilliseconds > 100) + Console.WriteLine($"Members call iterated in {sw.GetElapsedAndRestart()}"); return members; } #region Utility shortcuts - public async Task SendMessageEventAsync(RoomMessageEventContent content) => - await SendTimelineEventAsync("m.room.message", content); + public Task SendMessageEventAsync(RoomMessageEventContent content) => + SendTimelineEventAsync("m.room.message", content); public async Task?> GetAliasesAsync() { var res = await GetStateAsync("m.room.aliases"); return res.Aliases; } - public async Task GetCanonicalAliasAsync() => - await GetStateAsync("m.room.canonical_alias"); + public Task GetCanonicalAliasAsync() => + GetStateAsync("m.room.canonical_alias"); - public async Task GetTopicAsync() => - await GetStateAsync("m.room.topic"); + public Task GetTopicAsync() => + GetStateAsync("m.room.topic"); - public async Task GetAvatarUrlAsync() => - await GetStateAsync("m.room.avatar"); + public Task GetAvatarUrlAsync() => + GetStateAsync("m.room.avatar"); - public async Task GetJoinRuleAsync() => - await GetStateAsync("m.room.join_rules"); + public Task GetJoinRuleAsync() => + GetStateAsync("m.room.join_rules"); - public async Task GetHistoryVisibilityAsync() => - await GetStateAsync("m.room.history_visibility"); + public Task GetHistoryVisibilityAsync() => + GetStateAsync("m.room.history_visibility"); - public async Task GetGuestAccessAsync() => - await GetStateAsync("m.room.guest_access"); + public Task GetGuestAccessAsync() => + GetStateAsync("m.room.guest_access"); - public async Task GetCreateEventAsync() => - await GetStateAsync("m.room.create"); + public Task GetCreateEventAsync() => + GetStateAsync("m.room.create"); public async Task GetRoomType() { var res = await GetStateAsync("m.room.create"); return res.Type; } - public async Task GetPowerLevelsAsync() => - await GetStateAsync("m.room.power_levels"); + public Task GetPowerLevelsAsync() => + GetStateAsync("m.room.power_levels"); public async Task GetNameOrFallbackAsync(int maxMemberNames = 2) { try { @@ -271,9 +281,9 @@ public class GenericRoom { } } - public async Task InviteUsersAsync(IEnumerable users, string? reason = null, bool skipExisting = true) { + public Task InviteUsersAsync(IEnumerable users, string? reason = null, bool skipExisting = true) { var tasks = users.Select(x => InviteUserAsync(x, reason, skipExisting)).ToList(); - await Task.WhenAll(tasks); + return Task.WhenAll(tasks); } public async Task GetResolvedRoomAvatarUrlAsync(bool useOriginHomeserver = false) { @@ -375,8 +385,8 @@ public class GenericRoom { } } - public async Task GetEventAsync(string eventId) { - return await Homeserver.ClientHttpClient.GetFromJsonAsync($"/_matrix/client/v3/rooms/{RoomId}/event/{eventId}"); + public Task GetEventAsync(string eventId) { + return Homeserver.ClientHttpClient.GetFromJsonAsync($"/_matrix/client/v3/rooms/{RoomId}/event/{eventId}"); } public async Task RedactEventAsync(string eventToRedact, string reason) { -- cgit 1.4.1