about summary refs log tree commit diff
path: root/Utilities/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomAccountDataController.cs
diff options
context:
space:
mode:
authorRory& <root@rory.gay>2024-09-17 04:26:25 +0200
committerRory& <root@rory.gay>2024-09-17 04:26:25 +0200
commit37a8ac420278fd53ab8218956f1ba13692feb48e (patch)
tree83afec51130a254f6ed0ba11c45975f3f2ba8b9f /Utilities/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomAccountDataController.cs
parentDrop example bots (diff)
parentMove around some projects, further cleanup pending (diff)
downloadLibMatrix-37a8ac420278fd53ab8218956f1ba13692feb48e.tar.xz
Merge remote-tracking branch 'origin/dev/project-cleanup'
Diffstat (limited to 'Utilities/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomAccountDataController.cs')
-rw-r--r--Utilities/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomAccountDataController.cs78
1 files changed, 78 insertions, 0 deletions
diff --git a/Utilities/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomAccountDataController.cs b/Utilities/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomAccountDataController.cs
new file mode 100644
index 0000000..bac803f
--- /dev/null
+++ b/Utilities/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomAccountDataController.cs
@@ -0,0 +1,78 @@
+using System.Text.Json.Serialization;
+using LibMatrix.HomeserverEmulator.Services;
+using Microsoft.AspNetCore.Mvc;
+
+namespace LibMatrix.HomeserverEmulator.Controllers.Rooms;
+
+[ApiController]
+[Route("/_matrix/client/{version}/rooms/{roomId}")]
+public class RoomAccountDataController(ILogger<RoomAccountDataController> logger, TokenService tokenService, UserStore userStore, RoomStore roomStore) : ControllerBase {
+    [HttpPost("read_markers")]
+    public async Task<object> SetReadMarkers(string roomId, [FromBody] ReadMarkersData data) {
+        var token = tokenService.GetAccessToken(HttpContext);
+        var user = await userStore.GetUserByToken(token);
+
+        var room = roomStore.GetRoomById(roomId);
+        if (room == null)
+            throw new MatrixException() {
+                ErrorCode = "M_NOT_FOUND",
+                Error = "Room not found"
+            };
+
+        if (!room.JoinedMembers.Any(x => x.StateKey == user.UserId))
+            throw new MatrixException() {
+                ErrorCode = "M_FORBIDDEN",
+                Error = "User is not in the room"
+            };
+
+        if (!room.ReadMarkers.ContainsKey(user.UserId))
+            room.ReadMarkers[user.UserId] = new();
+
+        if (data.FullyRead != null)
+            room.ReadMarkers[user.UserId].FullyRead = data.FullyRead;
+        if (data.Read != null)
+            room.ReadMarkers[user.UserId].Read = data.Read;
+        if (data.ReadPrivate != null)
+            room.ReadMarkers[user.UserId].ReadPrivate = data.ReadPrivate;
+
+        if (!room.AccountData.ContainsKey(user.UserId))
+            room.AccountData[user.UserId] = new();
+
+        room.AccountData[user.UserId].Add(new StateEventResponse() {
+            Type = "m.fully_read",
+            StateKey = user.UserId,
+            RawContent = new() {
+                ["event_id"] = data.FullyRead
+            }
+        });
+
+        room.AccountData[user.UserId].Add(new StateEventResponse() {
+            Type = "m.read",
+            StateKey = user.UserId,
+            RawContent = new() {
+                ["event_id"] = data.Read
+            }
+        });
+
+        room.AccountData[user.UserId].Add(new StateEventResponse() {
+            Type = "m.read.private",
+            StateKey = user.UserId,
+            RawContent = new() {
+                ["event_id"] = data.ReadPrivate
+            }
+        });
+
+        return data;
+    }
+}
+
+public class ReadMarkersData {
+    [JsonPropertyName("m.fully_read")]
+    public string? FullyRead { get; set; }
+
+    [JsonPropertyName("m.read")]
+    public string? Read { get; set; }
+
+    [JsonPropertyName("m.read.private")]
+    public string? ReadPrivate { get; set; }
+}
\ No newline at end of file