about summary refs log tree commit diff
path: root/Tests/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomAccountDataController.cs
blob: 64d73538373b3e6334553ad60b0d61134c8bc481 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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 LegacyMatrixEventResponse() {
            Type = "m.fully_read",
            StateKey = user.UserId,
            RawContent = new() {
                ["event_id"] = data.FullyRead
            }
        });

        room.AccountData[user.UserId].Add(new LegacyMatrixEventResponse() {
            Type = "m.read",
            StateKey = user.UserId,
            RawContent = new() {
                ["event_id"] = data.Read
            }
        });

        room.AccountData[user.UserId].Add(new LegacyMatrixEventResponse() {
            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; }
}