diff options
author | Emma [it/its]@Rory& <root@rory.gay> | 2024-03-05 11:19:52 +0100 |
---|---|---|
committer | Emma [it/its]@Rory& <root@rory.gay> | 2024-03-05 11:19:52 +0100 |
commit | f41b6e5ec431c88bc1d94e4832d8ba49ddc42004 (patch) | |
tree | 503be94f5036f7cc221846c1eabf7c5edd107f1a /Tests/LibMatrix.HomeserverEmulator/Controllers/Users/FilterController.cs | |
parent | Unknown changes (diff) | |
download | LibMatrix-f41b6e5ec431c88bc1d94e4832d8ba49ddc42004.tar.xz |
HomeserverEmulator work
Diffstat (limited to '')
-rw-r--r-- | Tests/LibMatrix.HomeserverEmulator/Controllers/Users/FilterController.cs | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/Tests/LibMatrix.HomeserverEmulator/Controllers/Users/FilterController.cs b/Tests/LibMatrix.HomeserverEmulator/Controllers/Users/FilterController.cs new file mode 100644 index 0000000..ecbccd4 --- /dev/null +++ b/Tests/LibMatrix.HomeserverEmulator/Controllers/Users/FilterController.cs @@ -0,0 +1,58 @@ +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 FilterController(ILogger<FilterController> logger, TokenService tokenService, UserStore userStore, RoomStore roomStore) : ControllerBase { + [HttpPost("user/{mxid}/filter")] + public async Task<object> CreateFilter(string mxid, [FromBody] SyncFilter filter) { + var token = tokenService.GetAccessToken(HttpContext); + if (token is null) + throw new MatrixException() { + ErrorCode = "M_UNAUTHORIZED", + Error = "No token passed." + }; + + var user = await userStore.GetUserByToken(token, false); + if (user is null) + throw new MatrixException() { + ErrorCode = "M_UNAUTHORIZED", + Error = "Invalid token." + }; + var filterId = Guid.NewGuid().ToString(); + user.Filters[filterId] = filter; + return new { + filter_id = filterId + }; + } + + [HttpGet("user/{mxid}/filter/{filterId}")] + public async Task<SyncFilter> GetFilter(string mxid, string filterId) { + var token = tokenService.GetAccessToken(HttpContext); + if (token is null) + throw new MatrixException() { + ErrorCode = "M_UNAUTHORIZED", + Error = "No token passed." + }; + + var user = await userStore.GetUserByToken(token, false); + if (user is null) + throw new MatrixException() { + ErrorCode = "M_UNAUTHORIZED", + Error = "Invalid token." + }; + if (!user.Filters.ContainsKey(filterId)) + throw new MatrixException() { + ErrorCode = "M_NOT_FOUND", + Error = "Filter not found." + }; + return user.Filters[filterId]; + } +} \ No newline at end of file |