From 21da6cde79ccd0cb7f895a29e3d8cab959ef11ba Mon Sep 17 00:00:00 2001 From: TheArcaneBrony Date: Mon, 4 Sep 2023 02:17:10 +0200 Subject: Too many changes to name... --- .../Homeservers/AuthenticatedHomeserverGeneric.cs | 89 ++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs (limited to 'LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs') diff --git a/LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs b/LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs new file mode 100644 index 0000000..ecac4e4 --- /dev/null +++ b/LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs @@ -0,0 +1,89 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Net.Http; +using System.Net.Http.Json; +using System.Text.Json; +using System.Text.Json.Nodes; +using System.Threading.Tasks; +using LibMatrix.Extensions; +using LibMatrix.Helpers; +using LibMatrix.Responses; +using LibMatrix.RoomTypes; +using LibMatrix.Services; + +namespace LibMatrix.Homeservers; + +public class AuthenticatedHomeserverGeneric : RemoteHomeServer { + public AuthenticatedHomeserverGeneric(TieredStorageService storage, string canonicalHomeServerDomain, string accessToken) : base(canonicalHomeServerDomain) { + Storage = storage; + AccessToken = accessToken.Trim(); + HomeServerDomain = canonicalHomeServerDomain.Trim(); + SyncHelper = new SyncHelper(this, storage); + _httpClient = new MatrixHttpClient(); + } + + public TieredStorageService Storage { get; set; } + public SyncHelper SyncHelper { get; init; } + public WhoAmIResponse WhoAmI { get; set; } = null!; + public string UserId => WhoAmI.UserId; + public string AccessToken { get; set; } + + + public Task GetRoom(string roomId) => Task.FromResult(new(this, roomId)); + + public async Task> GetJoinedRooms() { + var roomQuery = await _httpClient.GetAsync("/_matrix/client/v3/joined_rooms"); + + var roomsJson = await roomQuery.Content.ReadFromJsonAsync(); + var rooms = roomsJson.GetProperty("joined_rooms").EnumerateArray().Select(room => new GenericRoom(this, room.GetString()!)).ToList(); + + Console.WriteLine($"Fetched {rooms.Count} rooms"); + + return rooms; + } + + public async Task UploadFile(string fileName, Stream fileStream, string contentType = "application/octet-stream") { + var res = await _httpClient.PostAsync($"/_matrix/media/v3/upload?filename={fileName}", new StreamContent(fileStream)); + if (!res.IsSuccessStatusCode) { + Console.WriteLine($"Failed to upload file: {await res.Content.ReadAsStringAsync()}"); + throw new InvalidDataException($"Failed to upload file: {await res.Content.ReadAsStringAsync()}"); + } + + var resJson = await res.Content.ReadFromJsonAsync(); + return resJson.GetProperty("content_uri").GetString()!; + } + + public async Task CreateRoom(CreateRoomRequest creationEvent) { + var res = await _httpClient.PostAsJsonAsync("/_matrix/client/v3/createRoom", creationEvent); + if (!res.IsSuccessStatusCode) { + Console.WriteLine($"Failed to create room: {await res.Content.ReadAsStringAsync()}"); + throw new InvalidDataException($"Failed to create room: {await res.Content.ReadAsStringAsync()}"); + } + + return await GetRoom((await res.Content.ReadFromJsonAsync())!["room_id"]!.ToString()); + } + +#region Account Data + + public async Task GetAccountData(string key) { + var res = await _httpClient.GetAsync($"/_matrix/client/v3/user/{UserId}/account_data/{key}"); + if (!res.IsSuccessStatusCode) { + Console.WriteLine($"Failed to get account data: {await res.Content.ReadAsStringAsync()}"); + throw new InvalidDataException($"Failed to get account data: {await res.Content.ReadAsStringAsync()}"); + } + + return await res.Content.ReadFromJsonAsync(); + } + + public async Task SetAccountData(string key, object data) { + var res = await _httpClient.PutAsJsonAsync($"/_matrix/client/v3/user/{UserId}/account_data/{key}", data); + if (!res.IsSuccessStatusCode) { + Console.WriteLine($"Failed to set account data: {await res.Content.ReadAsStringAsync()}"); + throw new InvalidDataException($"Failed to set account data: {await res.Content.ReadAsStringAsync()}"); + } + } + +#endregion +} -- cgit 1.4.1