about summary refs log tree commit diff
path: root/Utilities/LibMatrix.HomeserverEmulator/Controllers/Users/ProfileController.cs
blob: 98c41dafb9f6f4b153717a6915183ba0333bc72e (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
using System.Text.Json.Nodes;
using ArcaneLibs.Extensions;
using LibMatrix.EventTypes.Spec.State;
using LibMatrix.Filters;
using LibMatrix.HomeserverEmulator.Services;
using LibMatrix.Responses;
using Microsoft.AspNetCore.Mvc;

namespace LibMatrix.HomeserverEmulator.Controllers;

[ApiController]
[Route("/_matrix/client/{version}/")]
public class ProfileController(ILogger<ProfileController> logger, TokenService tokenService, UserStore userStore, RoomStore roomStore) : ControllerBase {
    [HttpGet("profile/{userId}")]
    public async Task<IDictionary<string, object>> GetProfile(string userId) {
        var user = await userStore.GetUserById(userId, false);
        if (user is null)
            throw new MatrixException() {
                ErrorCode = "M_NOT_FOUND",
                Error = "User not found."
            };
        return user.Profile;
    }

    [HttpGet("profile/{userId}/{key}")]
    public async Task<object> GetProfile(string userId, string key) {
        var user = await userStore.GetUserById(userId, false);
        if (user is null)
            throw new MatrixException() {
                ErrorCode = "M_NOT_FOUND",
                Error = "User not found."
            };
        if (!user.Profile.TryGetValue(key, out var value))
            throw new MatrixException() {
                ErrorCode = "M_NOT_FOUND",
                Error = "Key not found."
            };
        return value;
    }

    [HttpPut("profile/{userId}/{key}")]
    public async Task<object> SetProfile(string userId, string key, [FromBody] JsonNode value) {
        var user = await userStore.GetUserById(userId, false);
        if (user is null)
            throw new MatrixException() {
                ErrorCode = "M_NOT_FOUND",
                Error = "User not found."
            };
        user.Profile[key] = value[key]?.AsObject();
        return value;
    }
}