From cf455ed8de20bbee011289223e7d8d5775dfd69e Mon Sep 17 00:00:00 2001 From: TheArcaneBrony Date: Tue, 5 Sep 2023 06:28:52 +0200 Subject: Media moderator PoC works, abstract command handling to library --- LibMatrix/RoomTypes/GenericRoom.cs | 46 ++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 17 deletions(-) (limited to 'LibMatrix/RoomTypes/GenericRoom.cs') diff --git a/LibMatrix/RoomTypes/GenericRoom.cs b/LibMatrix/RoomTypes/GenericRoom.cs index 8ba9a4b..4c784ce 100644 --- a/LibMatrix/RoomTypes/GenericRoom.cs +++ b/LibMatrix/RoomTypes/GenericRoom.cs @@ -1,16 +1,12 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Net.Http; using System.Net.Http.Json; using System.Text.Json; using System.Text.Json.Serialization; -using System.Threading.Tasks; using System.Web; using LibMatrix.Extensions; using LibMatrix.Homeservers; using LibMatrix.Responses; using LibMatrix.StateEventTypes.Spec; +using Microsoft.Extensions.Logging; namespace LibMatrix.RoomTypes; @@ -19,6 +15,8 @@ public class GenericRoom { internal readonly MatrixHttpClient _httpClient; public GenericRoom(AuthenticatedHomeserverGeneric homeserver, string roomId) { + if (string.IsNullOrWhiteSpace(roomId)) + throw new ArgumentException("Room ID cannot be null or whitespace", nameof(roomId)); Homeserver = homeserver; _httpClient = homeserver._httpClient; RoomId = roomId; @@ -105,18 +103,24 @@ public class GenericRoom { }); } - // TODO: rewrite (members endpoint?) public async IAsyncEnumerable GetMembersAsync(bool joinedOnly = true) { - var res = GetFullStateAsync(); - await foreach (var member in res) { - if (member?.Type != "m.room.member") continue; - if (joinedOnly && (member.TypedContent as RoomMemberEventData)?.Membership is not "join") continue; - yield return member; + // var res = GetFullStateAsync(); + // await foreach (var member in res) { + // if (member?.Type != "m.room.member") continue; + // if (joinedOnly && (member.TypedContent as RoomMemberEventData)?.Membership is not "join") continue; + // yield return member; + // } + var res = await _httpClient.GetAsync($"/_matrix/client/v3/rooms/{RoomId}/members"); + var result = + JsonSerializer.DeserializeAsyncEnumerable(await res.Content.ReadAsStreamAsync()); + await foreach (var resp in result) { + if (resp?.Type != "m.room.member") continue; + if (joinedOnly && (resp.TypedContent as RoomMemberEventData)?.Membership is not "join") continue; + yield return resp; } } - #region Utility shortcuts public async Task> GetAliasesAsync() { @@ -150,13 +154,11 @@ public class GenericRoom { return res.Type; } - public async Task GetPowerLevelAsync() => + public async Task GetPowerLevelsAsync() => await GetStateAsync("m.room.power_levels"); #endregion - - public async Task ForgetAsync() => await _httpClient.PostAsync($"/_matrix/client/v3/rooms/{RoomId}/forget", null); @@ -178,12 +180,16 @@ public class GenericRoom { new UserIdAndReason { UserId = userId }); public async Task SendStateEventAsync(string eventType, object content) => - await (await _httpClient.PostAsJsonAsync($"/_matrix/client/v3/rooms/{RoomId}/state/{eventType}", content)) + await (await _httpClient.PutAsJsonAsync($"/_matrix/client/v3/rooms/{RoomId}/state/{eventType}", content)) + .Content.ReadFromJsonAsync(); + + public async Task SendStateEventAsync(string eventType, string stateKey, object content) => + await (await _httpClient.PutAsJsonAsync($"/_matrix/client/v3/rooms/{RoomId}/state/{eventType}/{stateKey}", content)) .Content.ReadFromJsonAsync(); public async Task SendMessageEventAsync(string eventType, RoomMessageEventData content) { var res = await _httpClient.PutAsJsonAsync( - $"/_matrix/client/v3/rooms/{RoomId}/send/{eventType}/" + Guid.NewGuid(), content, new JsonSerializerOptions() { + $"/_matrix/client/v3/rooms/{RoomId}/send/{eventType}/" + Guid.NewGuid(), content, new JsonSerializerOptions { DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull }); var resu = await res.Content.ReadFromJsonAsync(); @@ -227,4 +233,10 @@ public class GenericRoom { public async Task GetEvent(string eventId) { return await _httpClient.GetFromJsonAsync($"/_matrix/client/v3/rooms/{RoomId}/event/{eventId}"); } + + public async Task RedactEventAsync(string eventToRedact, string reason) { + var data = new { reason }; + return (await (await _httpClient.PutAsJsonAsync( + $"/_matrix/client/v3/rooms/{RoomId}/redact/{eventToRedact}/{Guid.NewGuid()}", data)).Content.ReadFromJsonAsync())!; + } } -- cgit 1.4.1