Line data Source code
1 : /* MIT License
2 : *
3 : * Copyright (C) 2019, 2020, 2021, 2022 Famedly GmbH
4 : *
5 : * Permission is hereby granted, free of charge, to any person obtaining a copy
6 : * of this software and associated documentation files (the "Software"), to deal
7 : * in the Software without restriction, including without limitation the rights
8 : * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 : * copies of the Software, and to permit persons to whom the Software is
10 : * furnished to do so, subject to the following conditions:
11 : *
12 : * The above copyright notice and this permission notice shall be included in all
13 : * copies or substantial portions of the Software.
14 : *
15 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 : * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 : * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 : * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 : * SOFTWARE.
22 : */
23 :
24 : import 'package:matrix/matrix_api_lite.dart';
25 : import 'package:matrix/msc_extensions/msc_3814_dehydrated_devices/model/dehydrated_device.dart';
26 : import 'package:matrix/msc_extensions/msc_3814_dehydrated_devices/model/dehydrated_device_events.dart';
27 :
28 : /// Endpoints related to MSC3814, dehydrated devices v2 aka shrivelled sessions
29 : /// https://github.com/matrix-org/matrix-spec-proposals/pull/3814
30 : extension DehydratedDeviceMatrixApi on MatrixApi {
31 : /// uploads a dehydrated device.
32 : /// https://github.com/matrix-org/matrix-spec-proposals/pull/3814
33 2 : Future<String> uploadDehydratedDevice({
34 : required String deviceId,
35 : String? initialDeviceDisplayName,
36 : Map<String, dynamic>? deviceData,
37 : MatrixDeviceKeys? deviceKeys,
38 : Map<String, dynamic>? oneTimeKeys,
39 : Map<String, dynamic>? fallbackKeys,
40 : }) async {
41 2 : final response = await request(
42 : RequestType.PUT,
43 : '/client/unstable/org.matrix.msc3814.v1/dehydrated_device',
44 2 : data: {
45 2 : 'device_id': deviceId,
46 : if (initialDeviceDisplayName != null)
47 2 : 'initial_device_display_name': initialDeviceDisplayName,
48 2 : if (deviceData != null) 'device_data': deviceData,
49 0 : if (deviceKeys != null) 'device_keys': deviceKeys.toJson(),
50 0 : if (oneTimeKeys != null) 'one_time_keys': oneTimeKeys,
51 0 : if (fallbackKeys != null) ...{
52 : 'fallback_keys': fallbackKeys,
53 : },
54 : },
55 : );
56 2 : return response['device_id'] as String;
57 : }
58 :
59 : /// fetch a dehydrated device.
60 : /// https://github.com/matrix-org/matrix-spec-proposals/pull/3814
61 2 : Future<DehydratedDevice> getDehydratedDevice() async {
62 2 : final response = await request(
63 : RequestType.GET,
64 : '/client/unstable/org.matrix.msc3814.v1/dehydrated_device',
65 : );
66 2 : return DehydratedDevice.fromJson(response);
67 : }
68 :
69 : /// fetch events sent to a dehydrated device.
70 : /// https://github.com/matrix-org/matrix-spec-proposals/pull/3814
71 2 : Future<DehydratedDeviceEvents> getDehydratedDeviceEvents(String deviceId,
72 : {String? nextBatch, int limit = 100}) async {
73 2 : final response = await request(RequestType.POST,
74 2 : '/client/unstable/org.matrix.msc3814.v1/dehydrated_device/$deviceId/events',
75 2 : query: {
76 2 : 'limit': limit.toString(),
77 : },
78 2 : data: {
79 0 : if (nextBatch != null) 'next_batch': nextBatch,
80 : });
81 2 : return DehydratedDeviceEvents.fromJson(response);
82 : }
83 : }
|