Skip to content

Commit

Permalink
Add support for application role connections metadata get/set operations
Browse files Browse the repository at this point in the history
  • Loading branch information
MOZGIII committed Dec 15, 2022
1 parent 425c82b commit e4494f4
Show file tree
Hide file tree
Showing 11 changed files with 271 additions and 0 deletions.
5 changes: 5 additions & 0 deletions twilight-http-ratelimiting/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ pub enum Path {
ApplicationGuildCommand(u64),
/// Operating on a specific command in a guild.
ApplicationGuildCommandId(u64),
/// Operating on application role connections metadata.
ApplicationRoleConnectionsMetadata(u64),
/// Operating on a channel.
ChannelsId(u64),
/// Operating on a channel's followers.
Expand Down Expand Up @@ -334,6 +336,9 @@ impl FromStr for Path {
| ["applications", id, "guilds", _, "commands", _, "permissions"] => {
ApplicationGuildCommandId(parse_id(id)?)
}
["applications", id, "role-connections", "metadata"] => {
ApplicationRoleConnectionsMetadata(parse_id(id)?)
}
["channels", id] => ChannelsId(parse_id(id)?),
["channels", id, "followers"] => ChannelsIdFollowers(parse_id(id)?),
["channels", id, "invites"] => ChannelsIdInvites(parse_id(id)?),
Expand Down
10 changes: 10 additions & 0 deletions twilight-http/src/client/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
mod builder;
mod connector;
mod interaction;
mod role_connections;

use self::role_connections::RoleConnectionsClient;
pub use self::{builder::ClientBuilder, interaction::InteractionClient};

#[allow(deprecated)]
Expand Down Expand Up @@ -276,6 +278,14 @@ impl Client {
InteractionClient::new(self, application_id)
}

/// Create an interface for using application role connections.
pub const fn role_connections(
&self,
application_id: Id<ApplicationMarker>,
) -> RoleConnectionsClient<'_> {
RoleConnectionsClient::new(self, application_id)
}

/// Get an immutable reference to the default [`AllowedMentions`] for sent
/// messages.
pub const fn default_allowed_mentions(&self) -> Option<&AllowedMentions> {
Expand Down
44 changes: 44 additions & 0 deletions twilight-http/src/client/role_connections.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use crate::{
request::application::role_connections::{GetMetadata, SetMetadata},
Client,
};
use twilight_model::{
application::role_connection::Metadata,
id::{marker::ApplicationMarker, Id},
};

/// Client interface for application role connections.
#[derive(Debug)]
pub struct RoleConnectionsClient<'a> {
application_id: Id<ApplicationMarker>,
client: &'a Client,
}

impl<'a> RoleConnectionsClient<'a> {
/// Create a new interface for using interactions.
pub(super) const fn new(client: &'a Client, application_id: Id<ApplicationMarker>) -> Self {
Self {
application_id,
client,
}
}

/// Get application role connections metadata.
pub const fn metadata(&'a self) -> GetMetadata<'a> {
GetMetadata::new(self.client, self.application_id)
}

/// Set the application role connections metadata.
pub const fn set_metadata(&'a self, records: &'a [Metadata]) -> SetMetadata<'a> {
SetMetadata::new(self.client, self.application_id, records)
}
}

#[cfg(test)]
mod tests {
use super::RoleConnectionsClient;
use static_assertions::assert_impl_all;
use std::fmt::Debug;

assert_impl_all!(RoleConnectionsClient<'_>: Debug, Send, Sync);
}
1 change: 1 addition & 0 deletions twilight-http/src/request/application/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod command;
pub mod interaction;
pub mod role_connections;
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use crate::{
client::Client,
error::Error,
request::{Request, TryIntoRequest},
response::{marker::ListBody, Response, ResponseFuture},
routing::Route,
};
use std::future::IntoFuture;
use twilight_model::{
application::role_connection::Metadata,
id::{marker::ApplicationMarker, Id},
};

/// Get Application Role Connection Metadata Records.
#[must_use = "requests must be configured and executed"]
pub struct GetMetadata<'a> {
application_id: Id<ApplicationMarker>,
http: &'a Client,
}

impl<'a> GetMetadata<'a> {
pub(crate) const fn new(http: &'a Client, application_id: Id<ApplicationMarker>) -> Self {
Self {
application_id,
http,
}
}

/// Execute the request, returning a future resolving to a [`Response`].
#[deprecated(since = "0.14.0", note = "use `.await` or `into_future` instead")]
pub fn exec(self) -> ResponseFuture<ListBody<Metadata>> {
self.into_future()
}
}

impl IntoFuture for GetMetadata<'_> {
type Output = Result<Response<ListBody<Metadata>>, Error>;

type IntoFuture = ResponseFuture<ListBody<Metadata>>;

fn into_future(self) -> Self::IntoFuture {
let http = self.http;

match self.try_into_request() {
Ok(request) => http.request(request),
Err(source) => ResponseFuture::error(source),
}
}
}

impl TryIntoRequest for GetMetadata<'_> {
fn try_into_request(self) -> Result<Request, Error> {
Ok(
Request::builder(&Route::GetApplicationRoleConnectionMetadataRecords {
application_id: self.application_id.get(),
})
.build(),
)
}
}
4 changes: 4 additions & 0 deletions twilight-http/src/request/application/role_connections/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
mod get_metadata;
mod set_metadata;

pub use self::{get_metadata::GetMetadata, set_metadata::SetMetadata};
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use crate::{
client::Client,
error::Error,
request::{Request, RequestBuilder, TryIntoRequest},
response::{marker::ListBody, Response, ResponseFuture},
routing::Route,
};
use std::future::IntoFuture;
use twilight_model::{
application::role_connection::Metadata,
id::{marker::ApplicationMarker, Id},
};

/// Set a user's linked roles metadata for the given application.
#[must_use = "requests must be configured and executed"]
pub struct SetMetadata<'a> {
application_id: Id<ApplicationMarker>,
http: &'a Client,
records: &'a [Metadata],
}

impl<'a> SetMetadata<'a> {
pub(crate) const fn new(
http: &'a Client,
application_id: Id<ApplicationMarker>,
records: &'a [Metadata],
) -> Self {
Self {
application_id,
http,
records,
}
}

/// Execute the request, returning a future resolving to a [`Response`].
#[deprecated(since = "0.14.0", note = "use `.await` or `into_future` instead")]
pub fn exec(self) -> ResponseFuture<ListBody<Metadata>> {
self.into_future()
}
}

impl IntoFuture for SetMetadata<'_> {
type Output = Result<Response<ListBody<Metadata>>, Error>;

type IntoFuture = ResponseFuture<ListBody<Metadata>>;

fn into_future(self) -> Self::IntoFuture {
let http = self.http;

match self.try_into_request() {
Ok(request) => http.request(request),
Err(source) => ResponseFuture::error(source),
}
}
}

impl TryIntoRequest for SetMetadata<'_> {
fn try_into_request(self) -> Result<Request, Error> {
Request::builder(&Route::SetApplicationRoleConnectionMetadataRecords {
application_id: self.application_id.get(),
})
.json(&self.records)
.map(RequestBuilder::build)
}
}
3 changes: 3 additions & 0 deletions twilight-http/src/request/try_into_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ mod private {
CreateFollowup, CreateResponse, DeleteFollowup, DeleteResponse, GetFollowup,
GetResponse, UpdateFollowup, UpdateResponse,
},
role_connections::{GetMetadata, SetMetadata},
},
channel::{
invite::{CreateInvite, DeleteInvite, GetChannelInvites, GetInvite},
Expand Down Expand Up @@ -206,6 +207,7 @@ mod private {
impl Sealed for GetJoinedPrivateArchivedThreads<'_> {}
impl Sealed for GetMember<'_> {}
impl Sealed for GetMessage<'_> {}
impl Sealed for GetMetadata<'_> {}
impl Sealed for GetNitroStickerPacks<'_> {}
impl Sealed for GetPins<'_> {}
impl Sealed for GetPrivateArchivedThreads<'_> {}
Expand All @@ -232,6 +234,7 @@ mod private {
impl Sealed for SearchGuildMembers<'_> {}
impl Sealed for SetGlobalCommands<'_> {}
impl Sealed for SetGuildCommands<'_> {}
impl Sealed for SetMetadata<'_> {}
impl Sealed for SyncTemplate<'_> {}
impl Sealed for UpdateAutoModerationRule<'_> {}
impl Sealed for UpdateChannel<'_> {}
Expand Down
24 changes: 24 additions & 0 deletions twilight-http/src/routing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,12 @@ pub enum Route<'a> {
/// ID of the guild.
guild_id: u64,
},
/// Returns a list of application role connection metadata objects for the
/// given application.
GetApplicationRoleConnectionMetadataRecords {
/// The ID of the owner application.
application_id: u64,
},
/// Route information to get a paginated list of audit logs in a guild.
GetAuditLogs {
/// The type of action to get audit logs for.
Expand Down Expand Up @@ -861,6 +867,12 @@ pub enum Route<'a> {
/// Query to search by.
query: &'a str,
},
/// Updates and returns a list of application role connection metadata
/// objects for the given application.
SetApplicationRoleConnectionMetadataRecords {
/// The ID of the owner application.
application_id: u64,
},
/// Route information to set global commands.
SetGlobalCommands {
/// The ID of the owner application.
Expand Down Expand Up @@ -1130,6 +1142,7 @@ impl<'a> Route<'a> {
| Self::RemoveThreadMember { .. }
| Self::UnpinMessage { .. } => Method::Delete,
Self::GetActiveThreads { .. }
| Self::GetApplicationRoleConnectionMetadataRecords { .. }
| Self::GetAuditLogs { .. }
| Self::GetAutoModerationRule { .. }
| Self::GetBan { .. }
Expand Down Expand Up @@ -1259,6 +1272,7 @@ impl<'a> Route<'a> {
| Self::CreateReaction { .. }
| Self::JoinThread { .. }
| Self::PinMessage { .. }
| Self::SetApplicationRoleConnectionMetadataRecords { .. }
| Self::SetGlobalCommands { .. }
| Self::SetGuildCommands { .. }
| Self::SyncTemplate { .. }
Expand Down Expand Up @@ -1329,6 +1343,10 @@ impl<'a> Route<'a> {
| Self::SetGlobalCommands { application_id } => {
Path::ApplicationCommand(application_id)
}
Self::GetApplicationRoleConnectionMetadataRecords { application_id }
| Self::SetApplicationRoleConnectionMetadataRecords { application_id } => {
Path::ApplicationRoleConnectionsMetadata(application_id)
}
Self::CreateGuild => Path::Guilds,
Self::CreateGuildFromTemplate { template_code, .. }
| Self::GetTemplate { template_code, .. } => {
Expand Down Expand Up @@ -1743,6 +1761,12 @@ impl Display for Route<'_> {

Ok(())
}
Route::GetApplicationRoleConnectionMetadataRecords { application_id }
| Route::SetApplicationRoleConnectionMetadataRecords { application_id } => {
f.write_str("applications/")?;
Display::fmt(application_id, f)?;
f.write_str("/role-connections/metadata")
}
Route::CreateGuild => f.write_str("guilds"),
Route::CreateGuildCommand {
application_id,
Expand Down
1 change: 1 addition & 0 deletions twilight-model/src/application/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod command;
pub mod interaction;
pub mod role_connection;
54 changes: 54 additions & 0 deletions twilight-model/src/application/role_connection/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//! Application role connections models.

use std::collections::HashMap;

use serde::{Deserialize, Serialize};
use serde_repr::{Deserialize_repr, Serialize_repr};

/// Application Role Connection Metadata Type.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Deserialize_repr, Serialize_repr)]
#[repr(u8)]
pub enum MetadataType {
/// the metadata value (integer) is less than or equal to the guild's
/// configured value (integer)
IntegerLessThanOrEqual = 1,
/// the metadata value (integer) is greater than or equal to the guild's
/// configured value (integer)
IntegerGreaterThanOrEqual = 2,
/// the metadata value (integer) is equal to the guild's configured value
/// (integer)
IntegerEqual = 3,
/// the metadata value (integer) is not equal to the guild's configured
/// value (integer)
IntegerNotEqual = 4,
/// the metadata value (ISO8601 string) is less than or equal to
/// the guild's configured value (integer; days before current date)
DatetimeLessThanOrEqual = 5,
/// the metadata value (ISO8601 string) is greater than or equal to
/// the guild's configured value (integer; days before current date)
DatetimeGreaterThanOrEqual = 6,
/// the metadata value (integer) is equal to the guild's configured value
/// (integer; 1)
BooleanEqual = 7,
/// the metadata value (integer) is not equal to the guild's configured
/// value (integer; 1)
BooleanNotEqual = 8,
}

/// Application Role Connection Metadata Structure.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
pub struct Metadata {
/// type of metadata value
pub r#type: MetadataType,
/// dictionary key for the metadata field
/// (must be a-z, 0-9, or _ characters; max 50 characters)
pub key: String,
/// name of the metadata field (max 100 characters)
pub name: String,
/// translations of the name
pub name_localizations: HashMap<String, String>,
/// description of the metadata field (max 200 characters)
pub description: String,
/// translations of the description
pub description_localizations: HashMap<String, String>,
}

0 comments on commit e4494f4

Please sign in to comment.