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
//! [POST /_matrix/identity/v2/lookup](https://matrix.org/docs/spec/identity_service/r0.3.0#post-matrix-identity-v2-lookup)

use std::collections::BTreeMap;

use ruma_api::ruma_api;
use ruma_identifiers::UserId;

use crate::lookup::IdentifierHashingAlgorithm;

ruma_api! {
    metadata: {
        description: "Looks up the set of Matrix User IDs which have bound the 3PIDs given, if bindings are available.",
        method: POST,
        name: "lookup_3pid",
        path: "/_matrix/identity/v2/lookup",
        authentication: AccessToken,
        rate_limited: false,
    }

    request: {
        /// The algorithm the client is using to encode the `addresses`. This should be one of the
        /// available options from `/hash_details`.
        pub algorithm: &'a IdentifierHashingAlgorithm,

        /// The pepper from `/hash_details`. This is required even when the `algorithm` does not
        /// make use of it.
        pub pepper: &'a str,

        /// The addresses to look up.
        ///
        /// The format of the entries here depend on the `algorithm` used. Note that queries which
        /// have been incorrectly hashed or formatted will lead to no matches.
        pub addresses: &'a [String],
    }

    response: {
        /// Any applicable mappings of `addresses` to Matrix User IDs.
        ///
        /// Addresses which do not have associations will not be included, which can make this
        /// property be an empty object.
        pub mappings: BTreeMap<String, Box<UserId>>,
    }
}

impl<'a> Request<'a> {
    /// Create a `Request` with algorithm, pepper and addresses to loop up.
    pub fn new(
        algorithm: &'a IdentifierHashingAlgorithm,
        pepper: &'a str,
        addresses: &'a [String],
    ) -> Self {
        Self { algorithm, pepper, addresses }
    }
}

impl Response {
    /// Create a `Response` with the BTreeMap which map addresses from the request which were
    /// found to their corresponding User IDs.
    pub fn new(mappings: BTreeMap<String, Box<UserId>>) -> Self {
        Self { mappings }
    }
}