diff options
author | TheArcaneBrony <myrainbowdash949@gmail.com> | 2023-06-25 03:07:05 +0200 |
---|---|---|
committer | TheArcaneBrony <myrainbowdash949@gmail.com> | 2023-06-25 03:07:05 +0200 |
commit | f866946fbbe962c712049327ade9dcbd43900295 (patch) | |
tree | 3400fcce20f68a6ef3eb130f4ef57733e346d0e9 /MatrixRoomUtils.Core/Room.cs | |
parent | Working sync (diff) | |
download | MatrixUtils-f866946fbbe962c712049327ade9dcbd43900295.tar.xz |
Working state, refactored Rory&::LibMatrix
Diffstat (limited to 'MatrixRoomUtils.Core/Room.cs')
-rw-r--r-- | MatrixRoomUtils.Core/Room.cs | 90 |
1 files changed, 31 insertions, 59 deletions
diff --git a/MatrixRoomUtils.Core/Room.cs b/MatrixRoomUtils.Core/Room.cs index 1568746..59c56ed 100644 --- a/MatrixRoomUtils.Core/Room.cs +++ b/MatrixRoomUtils.Core/Room.cs @@ -1,10 +1,8 @@ using System.Net.Http.Json; using System.Text; using System.Text.Json; -using System.Text.Json.Serialization; using System.Web; using MatrixRoomUtils.Core.Extensions; -using MatrixRoomUtils.Core.Responses; using MatrixRoomUtils.Core.RoomTypes; namespace MatrixRoomUtils.Core; @@ -43,7 +41,7 @@ public class Room { } public async Task<MessagesResponse> GetMessagesAsync(string from = "", int limit = 10, string dir = "b", string filter = "") { - var url = $"/_matrix/client/r0/rooms/{RoomId}/messages?from={from}&limit={limit}&dir={dir}"; + var url = $"/_matrix/client/v3/rooms/{RoomId}/messages?from={from}&limit={limit}&dir={dir}"; if (!string.IsNullOrEmpty(filter)) url += $"&filter={filter}"; var res = await _httpClient.GetAsync(url); if (!res.IsSuccessStatusCode) { @@ -67,12 +65,14 @@ public class Room { return resn; } - public async Task JoinAsync(string[]? homeservers = null) { - var join_url = $"/_matrix/client/r0/join/{HttpUtility.UrlEncode(RoomId)}"; + public async Task JoinAsync(string[]? homeservers = null, string? reason = null) { + var join_url = $"/_matrix/client/v3/join/{HttpUtility.UrlEncode(RoomId)}"; Console.WriteLine($"Calling {join_url} with {homeservers?.Length ?? 0} via's..."); if (homeservers == null || homeservers.Length == 0) homeservers = new[] { RoomId.Split(':')[1] }; var fullJoinUrl = $"{join_url}?server_name=" + string.Join("&server_name=", homeservers); - var res = await _httpClient.PostAsync(fullJoinUrl, null); + var res = await _httpClient.PostAsJsonAsync(fullJoinUrl, new { + reason + }); } public async Task<List<string>> GetMembersAsync(bool joinedOnly = true) { @@ -138,7 +138,7 @@ public class Room { var res = await GetStateAsync("m.room.create"); if (!res.HasValue) return new CreateEvent(); - res.FindExtraJsonFields(typeof(CreateEvent)); + res.FindExtraJsonElementFields(typeof(CreateEvent)); return res.Value.Deserialize<CreateEvent>() ?? new CreateEvent(); } @@ -151,7 +151,7 @@ public class Room { } public async Task ForgetAsync() { - var res = await _httpClient.PostAsync($"/_matrix/client/r0/rooms/{RoomId}/forget", null); + var res = await _httpClient.PostAsync($"/_matrix/client/v3/rooms/{RoomId}/forget", null); if (!res.IsSuccessStatusCode) { Console.WriteLine($"Failed to forget room {RoomId} - got status: {res.StatusCode}"); throw new Exception($"Failed to forget room {RoomId} - got status: {res.StatusCode}"); @@ -159,7 +159,9 @@ public class Room { } public async Task LeaveAsync(string? reason = null) { - var res = await _httpClient.PostAsync($"/_matrix/client/r0/rooms/{RoomId}/leave", string.IsNullOrWhiteSpace(reason) ? null : new StringContent($"{{\"reason\":\"{reason}\"}}", Encoding.UTF8, "application/json")); + var res = await _httpClient.PostAsJsonAsync($"/_matrix/client/v3/rooms/{RoomId}/leave", new { + reason + }); if (!res.IsSuccessStatusCode) { Console.WriteLine($"Failed to leave room {RoomId} - got status: {res.StatusCode}"); throw new Exception($"Failed to leave room {RoomId} - got status: {res.StatusCode}"); @@ -168,7 +170,7 @@ public class Room { public async Task KickAsync(string userId, string? reason = null) { - var res = await _httpClient.PostAsJsonAsync($"/_matrix/client/r0/rooms/{RoomId}/kick", new UserIdAndReason() { user_id = userId, reason = reason }); + var res = await _httpClient.PostAsJsonAsync($"/_matrix/client/v3/rooms/{RoomId}/kick", new UserIdAndReason() { UserId = userId, Reason = reason }); if (!res.IsSuccessStatusCode) { Console.WriteLine($"Failed to kick {userId} from room {RoomId} - got status: {res.StatusCode}"); throw new Exception($"Failed to kick {userId} from room {RoomId} - got status: {res.StatusCode}"); @@ -176,7 +178,7 @@ public class Room { } public async Task BanAsync(string userId, string? reason = null) { - var res = await _httpClient.PostAsJsonAsync($"/_matrix/client/r0/rooms/{RoomId}/ban", new UserIdAndReason() { user_id = userId, reason = reason }); + var res = await _httpClient.PostAsJsonAsync($"/_matrix/client/v3/rooms/{RoomId}/ban", new UserIdAndReason() { UserId = userId, Reason = reason }); if (!res.IsSuccessStatusCode) { Console.WriteLine($"Failed to ban {userId} from room {RoomId} - got status: {res.StatusCode}"); throw new Exception($"Failed to ban {userId} from room {RoomId} - got status: {res.StatusCode}"); @@ -184,61 +186,31 @@ public class Room { } public async Task UnbanAsync(string userId) { - var res = await _httpClient.PostAsJsonAsync($"/_matrix/client/r0/rooms/{RoomId}/unban", new UserIdAndReason() { user_id = userId }); + var res = await _httpClient.PostAsJsonAsync($"/_matrix/client/v3/rooms/{RoomId}/unban", new UserIdAndReason() { UserId = userId }); if (!res.IsSuccessStatusCode) { Console.WriteLine($"Failed to unban {userId} from room {RoomId} - got status: {res.StatusCode}"); throw new Exception($"Failed to unban {userId} from room {RoomId} - got status: {res.StatusCode}"); } } + public async Task<EventIdResponse> SendStateEventAsync(string eventType, object content) { + var res = await _httpClient.PostAsJsonAsync($"/_matrix/client/v3/rooms/{RoomId}/state/{eventType}", content); + if (!res.IsSuccessStatusCode) { + Console.WriteLine($"Failed to send state event {eventType} to room {RoomId} - got status: {res.StatusCode}"); + throw new Exception($"Failed to send state event {eventType} to room {RoomId} - got status: {res.StatusCode}"); + } + return await res.Content.ReadFromJsonAsync<EventIdResponse>(); + } + + public async Task<EventIdResponse> SendMessageEventAsync(string eventType, object content) { + var res = await _httpClient.PutAsJsonAsync($"/_matrix/client/v3/rooms/{RoomId}/send/{eventType}/"+new Guid(), content); + if (!res.IsSuccessStatusCode) { + Console.WriteLine($"Failed to send event {eventType} to room {RoomId} - got status: {res.StatusCode}"); + throw new Exception($"Failed to send event {eventType} to room {RoomId} - got status: {res.StatusCode}"); + } + return await res.Content.ReadFromJsonAsync<EventIdResponse>(); + } public readonly SpaceRoom AsSpace; -} - -internal class UserIdAndReason { - public string user_id { get; set; } - public string? reason { get; set; } -} -public class MessagesResponse { - [JsonPropertyName("start")] - public string Start { get; set; } - - [JsonPropertyName("end")] - public string? End { get; set; } - - [JsonPropertyName("chunk")] - public List<StateEventResponse> Chunk { get; set; } = new(); - - [JsonPropertyName("state")] - public List<StateEventResponse> State { get; set; } = new(); -} - -public class CreateEvent { - [JsonPropertyName("creator")] - public string Creator { get; set; } - - [JsonPropertyName("room_version")] - public string RoomVersion { get; set; } - - [JsonPropertyName("type")] - public string? Type { get; set; } - - [JsonPropertyName("predecessor")] - public object? Predecessor { get; set; } - - [JsonPropertyName("m.federate")] - public bool Federate { get; set; } -} - -public class JoinRules { - private const string Public = "public"; - private const string Invite = "invite"; - private const string Knock = "knock"; - - [JsonPropertyName("join_rule")] - public string JoinRule { get; set; } - - [JsonPropertyName("allow")] - public List<string> Allow { get; set; } } \ No newline at end of file |