about summary refs log tree commit diff
path: root/MatrixRoomUtils.Core/Room.cs
diff options
context:
space:
mode:
authorTheArcaneBrony <myrainbowdash949@gmail.com>2023-06-19 02:36:32 +0200
committerTheArcaneBrony <myrainbowdash949@gmail.com>2023-06-19 02:36:32 +0200
commited2205972a7b7d6fdd4563c3775a83616920597a (patch)
tree54aec1cfdf861fd8a7739be131fbe79ae24d91e4 /MatrixRoomUtils.Core/Room.cs
parentPartial refactor (diff)
downloadMatrixUtils-ed2205972a7b7d6fdd4563c3775a83616920597a.tar.xz
Working sync
Diffstat (limited to 'MatrixRoomUtils.Core/Room.cs')
-rw-r--r--MatrixRoomUtils.Core/Room.cs49
1 files changed, 48 insertions, 1 deletions
diff --git a/MatrixRoomUtils.Core/Room.cs b/MatrixRoomUtils.Core/Room.cs
index 4f6bbca..1568746 100644
--- a/MatrixRoomUtils.Core/Room.cs
+++ b/MatrixRoomUtils.Core/Room.cs
@@ -1,4 +1,5 @@
 using System.Net.Http.Json;
+using System.Text;
 using System.Text.Json;
 using System.Text.Json.Serialization;
 using System.Web;
@@ -148,11 +149,57 @@ public class Room {
         if (res.Value.TryGetProperty("type", out var type)) return type.GetString();
         return null;
     }
+    
+    public async Task ForgetAsync() {
+        var res = await _httpClient.PostAsync($"/_matrix/client/r0/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}");
+        }
+    }
+    
+    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"));
+        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}");
+        }
+    }
+    
+    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 });
+        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}");
+        }
+    }
+    
+    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 });
+        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}");
+        }
+    }
+    
+    public async Task UnbanAsync(string userId) {
+        var res = await _httpClient.PostAsJsonAsync($"/_matrix/client/r0/rooms/{RoomId}/unban", new UserIdAndReason() { user_id = 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 SpaceRoom AsSpace;
+    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; }