about summary refs log tree commit diff
path: root/MatrixRoomUtils.Core/AuthenticatedHomeServer.cs
blob: 23e98ae97df2876625b6ea629eb22778101231b3 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using MatrixRoomUtils.Core.Extensions;
using MatrixRoomUtils.Core.Filters;
using MatrixRoomUtils.Core.Helpers;
using MatrixRoomUtils.Core.Interfaces;
using MatrixRoomUtils.Core.Responses;
using MatrixRoomUtils.Core.Responses.Admin;
using MatrixRoomUtils.Core.Services;

namespace MatrixRoomUtils.Core;

public class AuthenticatedHomeServer : IHomeServer {
    private readonly TieredStorageService _storage;
    public readonly HomeserverAdminApi Admin;
    public readonly SyncHelper SyncHelper;

    public AuthenticatedHomeServer(TieredStorageService storage, string canonicalHomeServerDomain, string accessToken) {
        _storage = storage;
        AccessToken = accessToken.Trim();
        HomeServerDomain = canonicalHomeServerDomain.Trim();
        Admin = new HomeserverAdminApi(this);
        SyncHelper = new SyncHelper(this, storage);
        _httpClient = new MatrixHttpClient();
    }

    public WhoAmIResponse WhoAmI { get; set; } = null!;
    public string UserId { get; }
    public string AccessToken { get; set; }

    public async Task<AuthenticatedHomeServer> Configure() {
        FullHomeServerDomain = await ResolveHomeserverFromWellKnown(HomeServerDomain);
        _httpClient.Dispose();
        _httpClient = new MatrixHttpClient { BaseAddress = new Uri(FullHomeServerDomain) };
        _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", AccessToken);
        Console.WriteLine("[AHS] Finished setting up http client");
        WhoAmI = (await _httpClient.GetFromJsonAsync<WhoAmIResponse>("/_matrix/client/v3/account/whoami"))!;

        return this;
    }

    public async Task<Room> GetRoom(string roomId) => new Room(_httpClient, roomId);

    public async Task<List<Room>> GetJoinedRooms() {
        var rooms = new List<Room>();
        var roomQuery = await _httpClient.GetAsync("/_matrix/client/v3/joined_rooms");

        var roomsJson = await roomQuery.Content.ReadFromJsonAsync<JsonElement>();
        foreach (var room in roomsJson.GetProperty("joined_rooms").EnumerateArray()) rooms.Add(new Room(_httpClient, room.GetString()));

        Console.WriteLine($"Fetched {rooms.Count} rooms");

        return rooms;
    }

    public async Task<string> 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<JsonElement>();
        return resJson.GetProperty("content_uri").GetString()!;
    }

    public async Task<Room> 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<JsonObject>())!["room_id"]!.ToString());
    }

    public class HomeserverAdminApi {
        private readonly AuthenticatedHomeServer _authenticatedHomeServer;

        public HomeserverAdminApi(AuthenticatedHomeServer authenticatedHomeServer) => _authenticatedHomeServer = authenticatedHomeServer;

        public async IAsyncEnumerable<AdminRoomListingResult.AdminRoomListingResultRoom> SearchRoomsAsync(int limit = int.MaxValue, string orderBy = "name", string dir = "f", string? searchTerm = null, LocalRoomQueryFilter? localFilter = null) {
            AdminRoomListingResult? res = null;
            var i = 0;
            int? totalRooms = null;
            do {
                var url = $"/_synapse/admin/v1/rooms?limit={Math.Min(limit, 100)}&dir={dir}&order_by={orderBy}";
                if (!string.IsNullOrEmpty(searchTerm)) url += $"&search_term={searchTerm}";

                if (res?.NextBatch != null) url += $"&from={res.NextBatch}";

                Console.WriteLine($"--- ADMIN Querying Room List with URL: {url} - Already have {i} items... ---");

                res = await _authenticatedHomeServer._httpClient.GetFromJsonAsync<AdminRoomListingResult>(url);
                totalRooms ??= res?.TotalRooms;
                Console.WriteLine(res.ToJson(false));
                foreach (var room in res.Rooms) {
                    if (localFilter != null) {
                        if (!room.RoomId.Contains(localFilter.RoomIdContains)) {
                            totalRooms--;
                            continue;
                        }
                        if (!room.Name?.Contains(localFilter.NameContains) == true) {
                            totalRooms--;
                            continue;
                        }
                        if (!room.CanonicalAlias?.Contains(localFilter.CanonicalAliasContains) == true) {
                            totalRooms--;
                            continue;
                        }
                        if (!room.Version.Contains(localFilter.VersionContains)) {
                            totalRooms--;
                            continue;
                        }
                        if (!room.Creator.Contains(localFilter.CreatorContains)) {
                            totalRooms--;
                            continue;
                        }
                        if (!room.Encryption?.Contains(localFilter.EncryptionContains) == true) {
                            totalRooms--;
                            continue;
                        }
                        if (!room.JoinRules?.Contains(localFilter.JoinRulesContains) == true) {
                            totalRooms--;
                            continue;
                        }
                        if(!room.GuestAccess?.Contains(localFilter.GuestAccessContains) == true) {
                            totalRooms--;
                            continue;
                        }
                        if(!room.HistoryVisibility?.Contains(localFilter.HistoryVisibilityContains) == true) {
                            totalRooms--;
                            continue;
                        }
                        
                        if(localFilter.CheckFederation && room.Federatable != localFilter.Federatable) {
                            totalRooms--;
                            continue;
                        }
                        if(localFilter.CheckPublic && room.Public != localFilter.Public) {
                            totalRooms--;
                            continue;
                        }
                        
                        if(room.JoinedMembers < localFilter.JoinedMembersGreaterThan || room.JoinedMembers > localFilter.JoinedMembersLessThan) {
                            totalRooms--;
                            continue;
                        }
                        if(room.JoinedLocalMembers < localFilter.JoinedLocalMembersGreaterThan || room.JoinedLocalMembers > localFilter.JoinedLocalMembersLessThan) {
                            totalRooms--;
                            continue;
                        }
                    }
                    // if (contentSearch != null && !string.IsNullOrEmpty(contentSearch) &&
                    //     !(
                    //         room.Name?.Contains(contentSearch, StringComparison.InvariantCultureIgnoreCase) == true ||
                    //         room.CanonicalAlias?.Contains(contentSearch, StringComparison.InvariantCultureIgnoreCase) == true ||
                    //         room.Creator?.Contains(contentSearch, StringComparison.InvariantCultureIgnoreCase) == true
                    //     )
                    //    ) {
                    //     totalRooms--;
                    //     continue;
                    // }

                    i++;
                    yield return room;
                }
            } while (i < Math.Min(limit, totalRooms ?? limit));
        }
    }
}

public class WhoAmIResponse {
    [JsonPropertyName("user_id")]
    public string UserId { get; set; } = null!;

    [JsonPropertyName("device_id")]
    public string? DeviceId { get; set; }
    [JsonPropertyName("is_guest")]
    public bool? IsGuest { get; set; }
}