djs_rest/endpoints/application_identity_profile.rs
1//! [Application identity profile endpoints](https://docs.discord.com/developers/resources/application-identity-profile):
2//! game stats shown in Game Stats Widgets on Discord profiles.
3//!
4//! Every endpoint takes the bot token of the application, and the user must have authorized
5//! it with the `application_identities.write` scope.
6
7use djs_model::{
8 application_identity_profile::{ApplicationIdentity, ApplicationIdentityProfile},
9 id::{ApplicationId, UserId},
10 rest::{
11 DeleteApplicationIdentity, GetApplicationIdentitiesByExternalId,
12 UpdateApplicationIdentityProfile,
13 },
14};
15use serde::Deserialize;
16
17use crate::client::boxed;
18use crate::{Error, Request, Rest, Route};
19
20/// The response of the identity lists, unwrapped by the methods (C-065).
21#[derive(Deserialize)]
22struct Identities {
23 identities: Vec<ApplicationIdentity>,
24}
25
26impl Rest {
27 /// [Update Application Identity Profile](https://docs.discord.com/developers/resources/application-identity-profile#update-application-identity-profile):
28 /// stores a user's game stats for their account `provider_issued_user_id` in the app's
29 /// system. The first write creates the identity (provider type `NONE`) and returns its
30 /// profile; later writes return `None` (D-135).
31 ///
32 /// discord.js counterpart: none.
33 ///
34 /// # Examples
35 ///
36 /// ```no_run
37 /// # async fn example(rest: djs_rest::Rest, application_id: djs_model::id::ApplicationId, user_id: djs_model::id::UserId) -> Result<(), djs_rest::Error> {
38 /// use djs_model::{application_identity_profile::ProfileData, rest::UpdateApplicationIdentityProfile};
39 ///
40 /// let mut body = UpdateApplicationIdentityProfile::default();
41 /// body.username = Some("ferris".into());
42 /// body.data = Some(ProfileData::default());
43 /// rest.update_application_identity_profile(application_id, user_id, "player-42", &body)
44 /// .await?;
45 /// # Ok(())
46 /// # }
47 /// ```
48 ///
49 /// # Errors
50 ///
51 /// See [`Rest::request`]; `403` when the application may not store game stats; `400` for
52 /// data over 10 KB or an external ID that belongs to another user or does not match the
53 /// user's identity.
54 pub fn update_application_identity_profile<'a>(
55 &'a self,
56 application_id: ApplicationId,
57 user_id: UserId,
58 provider_issued_user_id: &'a str,
59 body: &'a UpdateApplicationIdentityProfile,
60 ) -> impl Future<Output = Result<Option<ApplicationIdentityProfile>, Error>> + Send + 'a {
61 boxed(async move {
62 self.call(
63 Request::new(Route::UpdateApplicationIdentityProfile {
64 application_id,
65 user_id,
66 provider_issued_user_id,
67 })
68 .json(body)?,
69 )
70 .await
71 })
72 }
73
74 /// [Get Application Identity Profile](https://docs.discord.com/developers/resources/application-identity-profile#get-application-identity-profile):
75 /// returns the game stats stored for a user's account in the app's system.
76 ///
77 /// discord.js counterpart: none.
78 ///
79 /// # Errors
80 ///
81 /// See [`Rest::request`].
82 pub fn get_application_identity_profile<'a>(
83 &'a self,
84 application_id: ApplicationId,
85 user_id: UserId,
86 provider_issued_user_id: &'a str,
87 ) -> impl Future<Output = Result<ApplicationIdentityProfile, Error>> + Send + 'a {
88 boxed(async move {
89 self.call(Request::new(Route::GetApplicationIdentityProfile {
90 application_id,
91 user_id,
92 provider_issued_user_id,
93 }))
94 .await
95 })
96 }
97
98 /// [Get Application Identities by User ID](https://docs.discord.com/developers/resources/application-identity-profile#get-application-identities-by-user-id):
99 /// returns a user's identities in the application (without profiles), e.g. to find the
100 /// values [`delete_application_identity`](Self::delete_application_identity) needs.
101 ///
102 /// discord.js counterpart: none.
103 ///
104 /// # Errors
105 ///
106 /// See [`Rest::request`].
107 pub fn get_application_identities_by_user_id(
108 &self,
109 user_id: UserId,
110 application_id: ApplicationId,
111 ) -> impl Future<Output = Result<Vec<ApplicationIdentity>, Error>> + Send + '_ {
112 boxed(async move {
113 let identities: Identities = self
114 .request(Request::new(Route::GetApplicationIdentitiesByUserId {
115 user_id,
116 application_id,
117 }))
118 .await?;
119 Ok(identities.identities)
120 })
121 }
122
123 /// [Get Application Identities by External ID](https://docs.discord.com/developers/resources/application-identity-profile#get-application-identities-by-external-id):
124 /// returns the identities of the Discord user that an external account belongs to (the
125 /// identities carry the [`user_id`](ApplicationIdentity::user_id)).
126 ///
127 /// discord.js counterpart: none.
128 ///
129 /// # Errors
130 ///
131 /// See [`Rest::request`].
132 pub fn get_application_identities_by_external_id<'a>(
133 &'a self,
134 application_id: ApplicationId,
135 provider_type: &'a str,
136 provider_issued_user_id: &'a str,
137 query: &'a GetApplicationIdentitiesByExternalId,
138 ) -> impl Future<Output = Result<Vec<ApplicationIdentity>, Error>> + Send + 'a {
139 boxed(async move {
140 let identities: Identities = self
141 .request(
142 Request::new(Route::GetApplicationIdentitiesByExternalId {
143 application_id,
144 provider_type,
145 provider_issued_user_id,
146 })
147 .query(query),
148 )
149 .await?;
150 Ok(identities.identities)
151 })
152 }
153
154 /// [Delete Application Identity](https://docs.discord.com/developers/resources/application-identity-profile#delete-application-identity):
155 /// deletes one of a user's identities and its game stats — e.g. a stale external ID that
156 /// blocks writing the current one. The user's last account-linking identity cannot be
157 /// deleted.
158 ///
159 /// discord.js counterpart: none.
160 ///
161 /// # Errors
162 ///
163 /// See [`Rest::request`].
164 pub fn delete_application_identity<'a>(
165 &'a self,
166 user_id: UserId,
167 application_id: ApplicationId,
168 provider_type: &'a str,
169 provider_issued_user_id: &'a str,
170 body: &'a DeleteApplicationIdentity,
171 ) -> impl Future<Output = Result<(), Error>> + Send + 'a {
172 boxed(async move {
173 self.request_ignoring_body(
174 Request::new(Route::DeleteApplicationIdentity {
175 user_id,
176 application_id,
177 provider_type,
178 provider_issued_user_id,
179 })
180 .json(body)?,
181 )
182 .await
183 })
184 }
185}