diff options
author | TheArcaneBrony <myrainbowdash949@gmail.com> | 2023-09-05 06:28:52 +0200 |
---|---|---|
committer | TheArcaneBrony <myrainbowdash949@gmail.com> | 2023-09-05 06:28:52 +0200 |
commit | cf455ed8de20bbee011289223e7d8d5775dfd69e (patch) | |
tree | cbdfdbc207af64a105b4d21941a6f0e71ca65e9d /LibMatrix/RoomTypes/GenericRoom.cs | |
parent | Add start of Media Moderator PoC bot (diff) | |
download | LibMatrix-cf455ed8de20bbee011289223e7d8d5775dfd69e.tar.xz |
Media moderator PoC works, abstract command handling to library
Diffstat (limited to 'LibMatrix/RoomTypes/GenericRoom.cs')
-rw-r--r-- | LibMatrix/RoomTypes/GenericRoom.cs | 46 |
1 files changed, 29 insertions, 17 deletions
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<StateEventResponse> 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<StateEventResponse>(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<List<string>> GetAliasesAsync() { @@ -150,13 +154,11 @@ public class GenericRoom { return res.Type; } - public async Task<RoomPowerLevelEventData?> GetPowerLevelAsync() => + public async Task<RoomPowerLevelEventData?> GetPowerLevelsAsync() => await GetStateAsync<RoomPowerLevelEventData>("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<EventIdResponse> 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<EventIdResponse>(); + + public async Task<EventIdResponse> SendStateEventAsync(string eventType, string stateKey, object content) => + await (await _httpClient.PutAsJsonAsync($"/_matrix/client/v3/rooms/{RoomId}/state/{eventType}/{stateKey}", content)) .Content.ReadFromJsonAsync<EventIdResponse>(); public async Task<EventIdResponse> 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<EventIdResponse>(); @@ -227,4 +233,10 @@ public class GenericRoom { public async Task<T> GetEvent<T>(string eventId) { return await _httpClient.GetFromJsonAsync<T>($"/_matrix/client/v3/rooms/{RoomId}/event/{eventId}"); } + + public async Task<EventIdResponse> 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<EventIdResponse>())!; + } } |