Skip to content

Commit e4494f4

Browse files
committed
Add support for application role connections metadata get/set operations
1 parent 425c82b commit e4494f4

File tree

11 files changed

+271
-0
lines changed

11 files changed

+271
-0
lines changed

twilight-http-ratelimiting/src/request.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ pub enum Path {
123123
ApplicationGuildCommand(u64),
124124
/// Operating on a specific command in a guild.
125125
ApplicationGuildCommandId(u64),
126+
/// Operating on application role connections metadata.
127+
ApplicationRoleConnectionsMetadata(u64),
126128
/// Operating on a channel.
127129
ChannelsId(u64),
128130
/// Operating on a channel's followers.
@@ -334,6 +336,9 @@ impl FromStr for Path {
334336
| ["applications", id, "guilds", _, "commands", _, "permissions"] => {
335337
ApplicationGuildCommandId(parse_id(id)?)
336338
}
339+
["applications", id, "role-connections", "metadata"] => {
340+
ApplicationRoleConnectionsMetadata(parse_id(id)?)
341+
}
337342
["channels", id] => ChannelsId(parse_id(id)?),
338343
["channels", id, "followers"] => ChannelsIdFollowers(parse_id(id)?),
339344
["channels", id, "invites"] => ChannelsIdInvites(parse_id(id)?),

twilight-http/src/client/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
mod builder;
22
mod connector;
33
mod interaction;
4+
mod role_connections;
45

6+
use self::role_connections::RoleConnectionsClient;
57
pub use self::{builder::ClientBuilder, interaction::InteractionClient};
68

79
#[allow(deprecated)]
@@ -276,6 +278,14 @@ impl Client {
276278
InteractionClient::new(self, application_id)
277279
}
278280

281+
/// Create an interface for using application role connections.
282+
pub const fn role_connections(
283+
&self,
284+
application_id: Id<ApplicationMarker>,
285+
) -> RoleConnectionsClient<'_> {
286+
RoleConnectionsClient::new(self, application_id)
287+
}
288+
279289
/// Get an immutable reference to the default [`AllowedMentions`] for sent
280290
/// messages.
281291
pub const fn default_allowed_mentions(&self) -> Option<&AllowedMentions> {
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
use crate::{
2+
request::application::role_connections::{GetMetadata, SetMetadata},
3+
Client,
4+
};
5+
use twilight_model::{
6+
application::role_connection::Metadata,
7+
id::{marker::ApplicationMarker, Id},
8+
};
9+
10+
/// Client interface for application role connections.
11+
#[derive(Debug)]
12+
pub struct RoleConnectionsClient<'a> {
13+
application_id: Id<ApplicationMarker>,
14+
client: &'a Client,
15+
}
16+
17+
impl<'a> RoleConnectionsClient<'a> {
18+
/// Create a new interface for using interactions.
19+
pub(super) const fn new(client: &'a Client, application_id: Id<ApplicationMarker>) -> Self {
20+
Self {
21+
application_id,
22+
client,
23+
}
24+
}
25+
26+
/// Get application role connections metadata.
27+
pub const fn metadata(&'a self) -> GetMetadata<'a> {
28+
GetMetadata::new(self.client, self.application_id)
29+
}
30+
31+
/// Set the application role connections metadata.
32+
pub const fn set_metadata(&'a self, records: &'a [Metadata]) -> SetMetadata<'a> {
33+
SetMetadata::new(self.client, self.application_id, records)
34+
}
35+
}
36+
37+
#[cfg(test)]
38+
mod tests {
39+
use super::RoleConnectionsClient;
40+
use static_assertions::assert_impl_all;
41+
use std::fmt::Debug;
42+
43+
assert_impl_all!(RoleConnectionsClient<'_>: Debug, Send, Sync);
44+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
pub mod command;
22
pub mod interaction;
3+
pub mod role_connections;
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
use crate::{
2+
client::Client,
3+
error::Error,
4+
request::{Request, TryIntoRequest},
5+
response::{marker::ListBody, Response, ResponseFuture},
6+
routing::Route,
7+
};
8+
use std::future::IntoFuture;
9+
use twilight_model::{
10+
application::role_connection::Metadata,
11+
id::{marker::ApplicationMarker, Id},
12+
};
13+
14+
/// Get Application Role Connection Metadata Records.
15+
#[must_use = "requests must be configured and executed"]
16+
pub struct GetMetadata<'a> {
17+
application_id: Id<ApplicationMarker>,
18+
http: &'a Client,
19+
}
20+
21+
impl<'a> GetMetadata<'a> {
22+
pub(crate) const fn new(http: &'a Client, application_id: Id<ApplicationMarker>) -> Self {
23+
Self {
24+
application_id,
25+
http,
26+
}
27+
}
28+
29+
/// Execute the request, returning a future resolving to a [`Response`].
30+
#[deprecated(since = "0.14.0", note = "use `.await` or `into_future` instead")]
31+
pub fn exec(self) -> ResponseFuture<ListBody<Metadata>> {
32+
self.into_future()
33+
}
34+
}
35+
36+
impl IntoFuture for GetMetadata<'_> {
37+
type Output = Result<Response<ListBody<Metadata>>, Error>;
38+
39+
type IntoFuture = ResponseFuture<ListBody<Metadata>>;
40+
41+
fn into_future(self) -> Self::IntoFuture {
42+
let http = self.http;
43+
44+
match self.try_into_request() {
45+
Ok(request) => http.request(request),
46+
Err(source) => ResponseFuture::error(source),
47+
}
48+
}
49+
}
50+
51+
impl TryIntoRequest for GetMetadata<'_> {
52+
fn try_into_request(self) -> Result<Request, Error> {
53+
Ok(
54+
Request::builder(&Route::GetApplicationRoleConnectionMetadataRecords {
55+
application_id: self.application_id.get(),
56+
})
57+
.build(),
58+
)
59+
}
60+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
mod get_metadata;
2+
mod set_metadata;
3+
4+
pub use self::{get_metadata::GetMetadata, set_metadata::SetMetadata};
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
use crate::{
2+
client::Client,
3+
error::Error,
4+
request::{Request, RequestBuilder, TryIntoRequest},
5+
response::{marker::ListBody, Response, ResponseFuture},
6+
routing::Route,
7+
};
8+
use std::future::IntoFuture;
9+
use twilight_model::{
10+
application::role_connection::Metadata,
11+
id::{marker::ApplicationMarker, Id},
12+
};
13+
14+
/// Set a user's linked roles metadata for the given application.
15+
#[must_use = "requests must be configured and executed"]
16+
pub struct SetMetadata<'a> {
17+
application_id: Id<ApplicationMarker>,
18+
http: &'a Client,
19+
records: &'a [Metadata],
20+
}
21+
22+
impl<'a> SetMetadata<'a> {
23+
pub(crate) const fn new(
24+
http: &'a Client,
25+
application_id: Id<ApplicationMarker>,
26+
records: &'a [Metadata],
27+
) -> Self {
28+
Self {
29+
application_id,
30+
http,
31+
records,
32+
}
33+
}
34+
35+
/// Execute the request, returning a future resolving to a [`Response`].
36+
#[deprecated(since = "0.14.0", note = "use `.await` or `into_future` instead")]
37+
pub fn exec(self) -> ResponseFuture<ListBody<Metadata>> {
38+
self.into_future()
39+
}
40+
}
41+
42+
impl IntoFuture for SetMetadata<'_> {
43+
type Output = Result<Response<ListBody<Metadata>>, Error>;
44+
45+
type IntoFuture = ResponseFuture<ListBody<Metadata>>;
46+
47+
fn into_future(self) -> Self::IntoFuture {
48+
let http = self.http;
49+
50+
match self.try_into_request() {
51+
Ok(request) => http.request(request),
52+
Err(source) => ResponseFuture::error(source),
53+
}
54+
}
55+
}
56+
57+
impl TryIntoRequest for SetMetadata<'_> {
58+
fn try_into_request(self) -> Result<Request, Error> {
59+
Request::builder(&Route::SetApplicationRoleConnectionMetadataRecords {
60+
application_id: self.application_id.get(),
61+
})
62+
.json(&self.records)
63+
.map(RequestBuilder::build)
64+
}
65+
}

twilight-http/src/request/try_into_request.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ mod private {
1818
CreateFollowup, CreateResponse, DeleteFollowup, DeleteResponse, GetFollowup,
1919
GetResponse, UpdateFollowup, UpdateResponse,
2020
},
21+
role_connections::{GetMetadata, SetMetadata},
2122
},
2223
channel::{
2324
invite::{CreateInvite, DeleteInvite, GetChannelInvites, GetInvite},
@@ -206,6 +207,7 @@ mod private {
206207
impl Sealed for GetJoinedPrivateArchivedThreads<'_> {}
207208
impl Sealed for GetMember<'_> {}
208209
impl Sealed for GetMessage<'_> {}
210+
impl Sealed for GetMetadata<'_> {}
209211
impl Sealed for GetNitroStickerPacks<'_> {}
210212
impl Sealed for GetPins<'_> {}
211213
impl Sealed for GetPrivateArchivedThreads<'_> {}
@@ -232,6 +234,7 @@ mod private {
232234
impl Sealed for SearchGuildMembers<'_> {}
233235
impl Sealed for SetGlobalCommands<'_> {}
234236
impl Sealed for SetGuildCommands<'_> {}
237+
impl Sealed for SetMetadata<'_> {}
235238
impl Sealed for SyncTemplate<'_> {}
236239
impl Sealed for UpdateAutoModerationRule<'_> {}
237240
impl Sealed for UpdateChannel<'_> {}

twilight-http/src/routing.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,12 @@ pub enum Route<'a> {
359359
/// ID of the guild.
360360
guild_id: u64,
361361
},
362+
/// Returns a list of application role connection metadata objects for the
363+
/// given application.
364+
GetApplicationRoleConnectionMetadataRecords {
365+
/// The ID of the owner application.
366+
application_id: u64,
367+
},
362368
/// Route information to get a paginated list of audit logs in a guild.
363369
GetAuditLogs {
364370
/// The type of action to get audit logs for.
@@ -861,6 +867,12 @@ pub enum Route<'a> {
861867
/// Query to search by.
862868
query: &'a str,
863869
},
870+
/// Updates and returns a list of application role connection metadata
871+
/// objects for the given application.
872+
SetApplicationRoleConnectionMetadataRecords {
873+
/// The ID of the owner application.
874+
application_id: u64,
875+
},
864876
/// Route information to set global commands.
865877
SetGlobalCommands {
866878
/// The ID of the owner application.
@@ -1130,6 +1142,7 @@ impl<'a> Route<'a> {
11301142
| Self::RemoveThreadMember { .. }
11311143
| Self::UnpinMessage { .. } => Method::Delete,
11321144
Self::GetActiveThreads { .. }
1145+
| Self::GetApplicationRoleConnectionMetadataRecords { .. }
11331146
| Self::GetAuditLogs { .. }
11341147
| Self::GetAutoModerationRule { .. }
11351148
| Self::GetBan { .. }
@@ -1259,6 +1272,7 @@ impl<'a> Route<'a> {
12591272
| Self::CreateReaction { .. }
12601273
| Self::JoinThread { .. }
12611274
| Self::PinMessage { .. }
1275+
| Self::SetApplicationRoleConnectionMetadataRecords { .. }
12621276
| Self::SetGlobalCommands { .. }
12631277
| Self::SetGuildCommands { .. }
12641278
| Self::SyncTemplate { .. }
@@ -1329,6 +1343,10 @@ impl<'a> Route<'a> {
13291343
| Self::SetGlobalCommands { application_id } => {
13301344
Path::ApplicationCommand(application_id)
13311345
}
1346+
Self::GetApplicationRoleConnectionMetadataRecords { application_id }
1347+
| Self::SetApplicationRoleConnectionMetadataRecords { application_id } => {
1348+
Path::ApplicationRoleConnectionsMetadata(application_id)
1349+
}
13321350
Self::CreateGuild => Path::Guilds,
13331351
Self::CreateGuildFromTemplate { template_code, .. }
13341352
| Self::GetTemplate { template_code, .. } => {
@@ -1743,6 +1761,12 @@ impl Display for Route<'_> {
17431761

17441762
Ok(())
17451763
}
1764+
Route::GetApplicationRoleConnectionMetadataRecords { application_id }
1765+
| Route::SetApplicationRoleConnectionMetadataRecords { application_id } => {
1766+
f.write_str("applications/")?;
1767+
Display::fmt(application_id, f)?;
1768+
f.write_str("/role-connections/metadata")
1769+
}
17461770
Route::CreateGuild => f.write_str("guilds"),
17471771
Route::CreateGuildCommand {
17481772
application_id,

twilight-model/src/application/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
pub mod command;
22
pub mod interaction;
3+
pub mod role_connection;
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//! Application role connections models.
2+
3+
use std::collections::HashMap;
4+
5+
use serde::{Deserialize, Serialize};
6+
use serde_repr::{Deserialize_repr, Serialize_repr};
7+
8+
/// Application Role Connection Metadata Type.
9+
#[derive(Debug, Copy, Clone, PartialEq, Eq, Deserialize_repr, Serialize_repr)]
10+
#[repr(u8)]
11+
pub enum MetadataType {
12+
/// the metadata value (integer) is less than or equal to the guild's
13+
/// configured value (integer)
14+
IntegerLessThanOrEqual = 1,
15+
/// the metadata value (integer) is greater than or equal to the guild's
16+
/// configured value (integer)
17+
IntegerGreaterThanOrEqual = 2,
18+
/// the metadata value (integer) is equal to the guild's configured value
19+
/// (integer)
20+
IntegerEqual = 3,
21+
/// the metadata value (integer) is not equal to the guild's configured
22+
/// value (integer)
23+
IntegerNotEqual = 4,
24+
/// the metadata value (ISO8601 string) is less than or equal to
25+
/// the guild's configured value (integer; days before current date)
26+
DatetimeLessThanOrEqual = 5,
27+
/// the metadata value (ISO8601 string) is greater than or equal to
28+
/// the guild's configured value (integer; days before current date)
29+
DatetimeGreaterThanOrEqual = 6,
30+
/// the metadata value (integer) is equal to the guild's configured value
31+
/// (integer; 1)
32+
BooleanEqual = 7,
33+
/// the metadata value (integer) is not equal to the guild's configured
34+
/// value (integer; 1)
35+
BooleanNotEqual = 8,
36+
}
37+
38+
/// Application Role Connection Metadata Structure.
39+
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
40+
pub struct Metadata {
41+
/// type of metadata value
42+
pub r#type: MetadataType,
43+
/// dictionary key for the metadata field
44+
/// (must be a-z, 0-9, or _ characters; max 50 characters)
45+
pub key: String,
46+
/// name of the metadata field (max 100 characters)
47+
pub name: String,
48+
/// translations of the name
49+
pub name_localizations: HashMap<String, String>,
50+
/// description of the metadata field (max 200 characters)
51+
pub description: String,
52+
/// translations of the description
53+
pub description_localizations: HashMap<String, String>,
54+
}

0 commit comments

Comments
 (0)