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
//! Profile query.

use crate::api::rcos::{prelude::*, send_query};
use crate::error::TelescopeError;
use chrono::Utc;

#[derive(GraphQLQuery)]
#[graphql(
    schema_path = "graphql/rcos/schema.json",
    query_path = "graphql/rcos/users/profile.graphql",
    response_derives = "Debug,Clone,Serialize"
)]
pub struct Profile;

// import generated types.
use profile::{ResponseData, Variables};

impl Profile {
    /// Get the profile data for a given user ID..
    pub async fn for_user(
        target: uuid,
        viewer: Option<uuid>,
    ) -> Result<ResponseData, TelescopeError> {
        // Convert viewer to a vec with one or zero user IDs in it.
        let viewer = viewer.map(|v| vec![v]).unwrap_or(Vec::new());

        // Send the query and await the response.
        send_query::<Self>(Variables {
            target,
            viewer,
            now: Utc::today().naive_utc(),
        })
        .await
    }
}

impl ResponseData {
    /// Get the target user's Discord ID if available.
    pub fn discord(&self) -> Option<&str> {
        self.target
            .as_ref()?
            .discord
            .get(0)
            .map(|disc| disc.account_id.as_str())
    }
}