From 6bd02248ccfbcb46960a6f39eaad23888d190eb5 Mon Sep 17 00:00:00 2001 From: TheArcaneBrony Date: Fri, 15 Sep 2023 09:50:45 +0200 Subject: Some refactoring --- LibMatrix/RoomTypes/GenericRoom.cs | 48 +++++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 22 deletions(-) (limited to 'LibMatrix/RoomTypes/GenericRoom.cs') diff --git a/LibMatrix/RoomTypes/GenericRoom.cs b/LibMatrix/RoomTypes/GenericRoom.cs index 4c784ce..146b5dd 100644 --- a/LibMatrix/RoomTypes/GenericRoom.cs +++ b/LibMatrix/RoomTypes/GenericRoom.cs @@ -85,7 +85,7 @@ public class GenericRoom { // TODO: should we even error handle here? public async Task GetNameAsync() { try { - var res = await GetStateAsync("m.room.name"); + var res = await GetStateAsync("m.room.name"); return res?.Name ?? RoomId; } catch (MatrixException e) { @@ -108,7 +108,7 @@ public class GenericRoom { // 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; + // if (joinedOnly && (member.TypedContent as RoomMemberEventContent)?.Membership is not "join") continue; // yield return member; // } var res = await _httpClient.GetAsync($"/_matrix/client/v3/rooms/{RoomId}/members"); @@ -116,7 +116,7 @@ public class GenericRoom { 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; + if (joinedOnly && (resp.TypedContent as RoomMemberEventContent)?.Membership is not "join") continue; yield return resp; } } @@ -124,38 +124,38 @@ public class GenericRoom { #region Utility shortcuts public async Task> GetAliasesAsync() { - var res = await GetStateAsync("m.room.aliases"); + var res = await GetStateAsync("m.room.aliases"); return res.Aliases; } - public async Task GetCanonicalAliasAsync() => - await GetStateAsync("m.room.canonical_alias"); + public async Task GetCanonicalAliasAsync() => + await GetStateAsync("m.room.canonical_alias"); - public async Task GetTopicAsync() => - await GetStateAsync("m.room.topic"); + public async Task GetTopicAsync() => + await GetStateAsync("m.room.topic"); - public async Task GetAvatarUrlAsync() => - await GetStateAsync("m.room.avatar"); + public async Task GetAvatarUrlAsync() => + await GetStateAsync("m.room.avatar"); - public async Task GetJoinRuleAsync() => - await GetStateAsync("m.room.join_rules"); + public async Task GetJoinRuleAsync() => + await GetStateAsync("m.room.join_rules"); - public async Task GetHistoryVisibilityAsync() => - await GetStateAsync("m.room.history_visibility"); + public async Task GetHistoryVisibilityAsync() => + await GetStateAsync("m.room.history_visibility"); - public async Task GetGuestAccessAsync() => - await GetStateAsync("m.room.guest_access"); + public async Task GetGuestAccessAsync() => + await GetStateAsync("m.room.guest_access"); - public async Task GetCreateEventAsync() => - await GetStateAsync("m.room.create"); + public async Task GetCreateEventAsync() => + await GetStateAsync("m.room.create"); public async Task GetRoomType() { - var res = await GetStateAsync("m.room.create"); + var res = await GetStateAsync("m.room.create"); return res.Type; } - public async Task GetPowerLevelsAsync() => - await GetStateAsync("m.room.power_levels"); + public async Task GetPowerLevelsAsync() => + await GetStateAsync("m.room.power_levels"); #endregion @@ -187,7 +187,7 @@ public class GenericRoom { await (await _httpClient.PutAsJsonAsync($"/_matrix/client/v3/rooms/{RoomId}/state/{eventType}/{stateKey}", content)) .Content.ReadFromJsonAsync(); - public async Task SendMessageEventAsync(string eventType, RoomMessageEventData content) { + public async Task SendMessageEventAsync(string eventType, RoomMessageEventContent content) { var res = await _httpClient.PutAsJsonAsync( $"/_matrix/client/v3/rooms/{RoomId}/send/{eventType}/" + Guid.NewGuid(), content, new JsonSerializerOptions { DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull @@ -239,4 +239,8 @@ public class GenericRoom { return (await (await _httpClient.PutAsJsonAsync( $"/_matrix/client/v3/rooms/{RoomId}/redact/{eventToRedact}/{Guid.NewGuid()}", data)).Content.ReadFromJsonAsync())!; } + + public async Task InviteUser(string userId, string? reason = null) { + await _httpClient.PostAsJsonAsync($"/_matrix/client/v3/rooms/{RoomId}/invite", new UserIdAndReason(userId, reason)); + } } -- cgit 1.4.1