Join/Leave Guilds, (Group) DMs and minor improvements (#157)

## Summary:

**Added:**
- Schemas `PrivateChannelCreateSchema` `ChannelInviteCreateSchema`, `AddChannelRecipientSchema` recursively (including schemas which were needed to create these schemas)
- Methods `create_private_channel`, `leave_guild`, `accept_invite`, `create_user_invite`, `create_guild_invite`, `add_channel_recipient`, `remove_channel_recipient`
- Integration tests for the functionality covered by implementing #45
- Documentation in some places where I noticed it would be useful to have some
- `create_user` method in `/src/tests`: Cuts down on test boilerplate needed to create an addition test user

**Changed:**
- `.gitignore`
  - Added `.DS_store` files to gitignore (some weird macos files), removed Cargo.lock, as Cargo.lock should be included for libraries
- Added a lot of default trait derives like Clone, Copy, PartialEq, Eq, Ord, PartialOrd to structs and enums where I saw them missing
- Added missing `teardown()` calls to the integration tests
- Renamed integration test files in `/src/tests` dir to all be plural: `channel.rs` -> `channels.rs`
- Made more fields on `User` type `Option<>`
- All instances in `/src/tests` where a second test user was created using a RegistrationSchema and the register_user method were replaced with the new `create_user` method
- README.md: Marked roadmap fields covered by #45 as implemented
- Changed visibility of `/src/tests/common/mod.rs` methods from `pub` to `pub(crate)`. In hindsight, this was probably not required, haha

**Removed:**
- Unneeded import in`src/types/config/types/guild_configuration.rs`


## Commit log:

* Add .DS_store, remove Cargo.lock

* Create PrivateChannelCreateSchema

* pub use users

* add channels.rs

* create channels.rs

* Add Deserialize/Serialize derives

* Implement create_private_channel

* Add create_dm() test

* Make optional fields on `User` `Option<>`

* Check boxes for implemented features

* Create users/guilds.rs

* Remove unneeded import

* Add UserMeta::leave_guild()

* Create api/invites/mod.rs

* Add debug print to send_request

* Rename tests files

* Create invites.rs

* create invite.rs

* Add documentation

* Implement accept_invite

* Sort fields on Channel alphabetically

* Add invite mod

* Add forgotten teardown() at test end

* change visiblities, add create_user()

* Implement `create_user_invite()`

* start working on invite tests

* Add allow flags

* Fix bad url

* Create CreateChannelInviteSchema and friends

* remove non-implemented test code

* add body to requests

* Add Clone to UserMeta

* More comprehensive error message when running into a deserialization error

* Add arguments documentation to accept_invite

* Add Eq derive to GuildFeaturesList

* Add Eq derive to Emoji

* Add Eq derive to GuildBan

* Add create_accept_invite() test

* Add Default derive to ChannelCreateSchema

* Change create_guild_invite to return GuildInvite

* Dates as chrono::Date(Utc); sort alphabetically

* Add default derives wherever possible

* Implement add_- and remove_channel_recipient

* Create AddChannelRecipientSchema

* replace otheruser regs with bundle.creeate_user()

* Add (disabled) test remove_add_person_from_to_dm()
This commit is contained in:
Flori 2023-07-17 19:36:28 +02:00 committed by GitHub
parent 5f5e56d1ca
commit 7df6cb183f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
28 changed files with 3096 additions and 111 deletions

13
.gitignore vendored
View File

@ -2,19 +2,18 @@
# will have compiled files and executables
/target/
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock
# These are backup files generated by rustfmt
**/*.rs.bk
# Added by cargo
/target
###
# IDE specific folders and configs
.vscode/**
.idea/**
.idea/**
# macOS
**/.DS_Store

2552
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -56,9 +56,9 @@ accepted, if it violates these guidelines or [our Code of Conduct](https://githu
- [x] Channel creation
- [x] Channel deletion
- [x] [Channel management (name, description, icon, etc.)](https://github.com/polyphony-chat/chorus/issues/48)
- [ ] [Join and Leave Guilds](https://github.com/polyphony-chat/chorus/issues/45)
- [ ] [Start DMs](https://github.com/polyphony-chat/chorus/issues/45)
- [ ] [Group DM creation, deletion and member management](https://github.com/polyphony-chat/chorus/issues/89)
- [x] [Join and Leave Guilds](https://github.com/polyphony-chat/chorus/issues/45)
- [x] [Start DMs](https://github.com/polyphony-chat/chorus/issues/45)
- [x] [Group DM creation, deletion and member management](https://github.com/polyphony-chat/chorus/issues/89)
- [ ] [Deleting messages](https://github.com/polyphony-chat/chorus/issues/91)
- [ ] [Message threads](https://github.com/polyphony-chat/chorus/issues/90)
- [x] [Reactions](https://github.com/polyphony-chat/chorus/issues/85)

View File

@ -1,6 +1,7 @@
use reqwest::Client;
use serde_json::to_string;
use crate::types::AddChannelRecipientSchema;
use crate::{
api::LimitType,
errors::{ChorusError, ChorusResult},
@ -105,4 +106,54 @@ impl Channel {
.deserialize_response::<Vec<Message>>(user)
.await
}
/// # Reference:
/// Read: <https://discord-userdoccers.vercel.app/resources/channel#add-channel-recipient>
pub async fn add_channel_recipient(
&self,
recipient_id: Snowflake,
user: &mut UserMeta,
add_channel_recipient_schema: Option<AddChannelRecipientSchema>,
) -> ChorusResult<()> {
let mut request = Client::new()
.put(format!(
"{}/channels/{}/recipients/{}/",
user.belongs_to.borrow().urls.api,
self.id,
recipient_id
))
.bearer_auth(user.token());
if let Some(schema) = add_channel_recipient_schema {
request = request.body(to_string(&schema).unwrap());
}
ChorusRequest {
request,
limit_type: LimitType::Channel(self.id),
}
.handle_request_as_result(user)
.await
}
/// # Reference:
/// Read: <https://discord-userdoccers.vercel.app/resources/channel#remove-channel-recipient>
pub async fn remove_channel_recipient(
&self,
recipient_id: Snowflake,
user: &mut UserMeta,
) -> ChorusResult<()> {
let request = Client::new()
.delete(format!(
"{}/channels/{}/recipients/{}/",
user.belongs_to.borrow().urls.api,
self.id,
recipient_id
))
.bearer_auth(user.token());
ChorusRequest {
request,
limit_type: LimitType::Channel(self.id),
}
.handle_request_as_result(user)
.await
}
}

73
src/api/invites/mod.rs Normal file
View File

@ -0,0 +1,73 @@
use reqwest::Client;
use serde_json::to_string;
use crate::errors::ChorusResult;
use crate::instance::UserMeta;
use crate::ratelimiter::ChorusRequest;
use crate::types::{CreateChannelInviteSchema, GuildInvite, Invite, Snowflake};
impl UserMeta {
/// # Arguments
/// - invite_code: The invite code to accept the invite for.
/// - session_id: The session ID that is accepting the invite, required for guest invites.
///
/// # Reference:
/// Read <https://discord-userdoccers.vercel.app/resources/invite#accept-invite>
pub async fn accept_invite(
&mut self,
invite_code: &str,
session_id: Option<&str>,
) -> ChorusResult<Invite> {
let mut request = ChorusRequest {
request: Client::new()
.post(format!(
"{}/invites/{}/",
self.belongs_to.borrow().urls.api,
invite_code
))
.bearer_auth(self.token()),
limit_type: super::LimitType::Global,
};
if session_id.is_some() {
request.request = request
.request
.body(to_string(session_id.unwrap()).unwrap());
}
request.deserialize_response::<Invite>(self).await
}
/// Note: Spacebar does not yet implement this endpoint.
pub async fn create_user_invite(&mut self, code: Option<&str>) -> ChorusResult<Invite> {
ChorusRequest {
request: Client::new()
.post(format!(
"{}/users/@me/invites/",
self.belongs_to.borrow().urls.api
))
.body(to_string(&code).unwrap())
.bearer_auth(self.token()),
limit_type: super::LimitType::Global,
}
.deserialize_response::<Invite>(self)
.await
}
pub async fn create_guild_invite(
&mut self,
create_channel_invite_schema: CreateChannelInviteSchema,
channel_id: Snowflake,
) -> ChorusResult<GuildInvite> {
ChorusRequest {
request: Client::new()
.post(format!(
"{}/channels/{}/invites/",
self.belongs_to.borrow().urls.api,
channel_id
))
.bearer_auth(self.token())
.body(to_string(&create_channel_invite_schema).unwrap()),
limit_type: super::LimitType::Channel(channel_id),
}
.deserialize_response::<GuildInvite>(self)
.await
}
}

View File

@ -1,10 +1,13 @@
pub use channels::messages::*;
pub use guilds::*;
pub use invites::*;
pub use policies::instance::instance::*;
pub use policies::instance::ratelimits::*;
pub use users::*;
pub mod auth;
pub mod channels;
pub mod guilds;
pub mod invites;
pub mod policies;
pub mod users;

32
src/api/users/channels.rs Normal file
View File

@ -0,0 +1,32 @@
use reqwest::Client;
use serde_json::to_string;
use crate::{
api::LimitType,
errors::ChorusResult,
instance::UserMeta,
ratelimiter::ChorusRequest,
types::{Channel, PrivateChannelCreateSchema},
};
impl UserMeta {
/// Creates a DM channel or group DM channel.
///
/// # Reference:
/// Read <https://discord-userdoccers.vercel.app/resources/channel#create-private-channel>
pub async fn create_private_channel(
&mut self,
create_private_channel_schema: PrivateChannelCreateSchema,
) -> ChorusResult<Channel> {
let url = format!("{}/users/@me/channels", self.belongs_to.borrow().urls.api);
ChorusRequest {
request: Client::new()
.post(url)
.bearer_auth(self.token())
.body(to_string(&create_private_channel_schema).unwrap()),
limit_type: LimitType::Global,
}
.deserialize_response::<Channel>(self)
.await
}
}

30
src/api/users/guilds.rs Normal file
View File

@ -0,0 +1,30 @@
use reqwest::Client;
use serde_json::to_string;
use crate::errors::ChorusResult;
use crate::instance::UserMeta;
use crate::ratelimiter::ChorusRequest;
use crate::types::Snowflake;
impl UserMeta {
/// # Arguments:
/// - lurking: Whether the user is lurking in the guild
///
/// # Reference:
/// Read <https://discord-userdoccers.vercel.app/resources/guild#leave-guild>
pub async fn leave_guild(&mut self, guild_id: &Snowflake, lurking: bool) -> ChorusResult<()> {
ChorusRequest {
request: Client::new()
.delete(format!(
"{}/users/@me/guilds/{}/",
self.belongs_to.borrow().urls.api,
guild_id
))
.bearer_auth(self.token())
.body(to_string(&lurking).unwrap()),
limit_type: crate::api::LimitType::Guild(*guild_id),
}
.handle_request_as_result(self)
.await
}
}

View File

@ -1,5 +1,9 @@
pub use channels::*;
pub use guilds::*;
pub use relationships::*;
pub use users::*;
pub mod channels;
pub mod guilds;
pub mod relationships;
pub mod users;

View File

@ -88,7 +88,7 @@ impl fmt::Display for Token {
}
}
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct UserMeta {
pub belongs_to: Rc<RefCell<Instance>>,
pub token: String,

View File

@ -1,6 +1,6 @@
use std::collections::HashMap;
use log;
use log::{self, debug};
use reqwest::{Client, RequestBuilder, Response};
use serde::Deserialize;
use serde_json::from_str;
@ -37,7 +37,10 @@ impl ChorusRequest {
.execute(self.request.build().unwrap())
.await
{
Ok(result) => result,
Ok(result) => {
debug!("Request successful: {:?}", result);
result
}
Err(error) => {
log::warn!("Request failed: {:?}", error);
return Err(ChorusError::RequestFailed {
@ -430,6 +433,7 @@ impl ChorusRequest {
user: &mut UserMeta,
) -> ChorusResult<T> {
let response = self.send_request(user).await?;
debug!("Got response: {:?}", response);
let response_text = match response.text().await {
Ok(string) => string,
Err(e) => {
@ -446,8 +450,8 @@ impl ChorusRequest {
Err(e) => {
return Err(ChorusError::InvalidResponse {
error: format!(
"Error while trying to deserialize the JSON response into T: {}",
e
"Error while trying to deserialize the JSON response into requested type T: {}. JSON Response: {}",
e, response_text
),
})
}

View File

@ -10,7 +10,7 @@ use sqlx::{
database::{HasArguments, HasValueRef},
encode::IsNull,
error::BoxDynError,
Decode, Encode, MySql,
Decode, MySql,
};
use crate::types::config::types::subconfigs::guild::{
@ -139,7 +139,7 @@ pub enum GuildFeatures {
InvitesClosed,
}
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize, Eq)]
pub struct GuildFeaturesList(Vec<GuildFeatures>);
impl Deref for GuildFeaturesList {

View File

@ -11,58 +11,58 @@ use crate::types::{
#[derive(Default, Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "sqlx", derive(sqlx::FromRow))]
pub struct Channel {
pub id: Snowflake,
pub created_at: Option<chrono::DateTime<Utc>>,
#[serde(rename = "type")]
pub channel_type: ChannelType,
pub guild_id: Option<Snowflake>,
pub position: Option<i32>,
#[cfg(feature = "sqlx")]
pub permission_overwrites: Option<sqlx::types::Json<Vec<PermissionOverwrite>>>,
#[cfg(not(feature = "sqlx"))]
pub permission_overwrites: Option<Vec<PermissionOverwrite>>,
pub name: Option<String>,
pub topic: Option<String>,
pub nsfw: Option<bool>,
pub last_message_id: Option<Snowflake>,
pub bitrate: Option<i32>,
pub user_limit: Option<i32>,
pub rate_limit_per_user: Option<i32>,
#[cfg_attr(feature = "sqlx", sqlx(skip))]
pub recipients: Option<Vec<User>>,
pub icon: Option<String>,
pub owner_id: Option<Snowflake>,
pub application_id: Option<Snowflake>,
pub managed: Option<bool>,
pub parent_id: Option<Snowflake>,
pub last_pin_timestamp: Option<String>,
pub rtc_region: Option<String>,
pub video_quality_mode: Option<i32>,
pub message_count: Option<i32>,
pub member_count: Option<i32>,
#[cfg_attr(feature = "sqlx", sqlx(skip))]
pub thread_metadata: Option<ThreadMetadata>,
#[cfg_attr(feature = "sqlx", sqlx(skip))]
pub member: Option<ThreadMember>,
pub default_auto_archive_duration: Option<i32>,
pub permissions: Option<String>,
pub flags: Option<i32>,
pub total_message_sent: Option<i32>,
#[cfg(feature = "sqlx")]
pub available_tags: Option<sqlx::types::Json<Vec<Tag>>>,
#[cfg(not(feature = "sqlx"))]
pub available_tags: Option<Vec<Tag>>,
#[cfg(feature = "sqlx")]
pub applied_tags: Option<sqlx::types::Json<Vec<String>>>,
#[cfg(not(feature = "sqlx"))]
pub applied_tags: Option<Vec<String>>,
#[cfg(feature = "sqlx")]
pub available_tags: Option<sqlx::types::Json<Vec<Tag>>>,
#[cfg(not(feature = "sqlx"))]
pub available_tags: Option<Vec<Tag>>,
pub bitrate: Option<i32>,
#[serde(rename = "type")]
pub channel_type: ChannelType,
pub created_at: Option<chrono::DateTime<Utc>>,
pub default_auto_archive_duration: Option<i32>,
pub default_forum_layout: Option<i32>,
#[cfg(feature = "sqlx")]
pub default_reaction_emoji: Option<sqlx::types::Json<DefaultReaction>>,
#[cfg(not(feature = "sqlx"))]
pub default_reaction_emoji: Option<DefaultReaction>,
pub default_thread_rate_limit_per_user: Option<i32>,
pub default_sort_order: Option<i32>,
pub default_forum_layout: Option<i32>,
pub default_thread_rate_limit_per_user: Option<i32>,
pub flags: Option<i32>,
pub guild_id: Option<Snowflake>,
pub icon: Option<String>,
pub id: Snowflake,
pub last_message_id: Option<Snowflake>,
pub last_pin_timestamp: Option<String>,
pub managed: Option<bool>,
#[cfg_attr(feature = "sqlx", sqlx(skip))]
pub member: Option<ThreadMember>,
pub member_count: Option<i32>,
pub message_count: Option<i32>,
pub name: Option<String>,
pub nsfw: Option<bool>,
pub owner_id: Option<Snowflake>,
pub parent_id: Option<Snowflake>,
#[cfg(feature = "sqlx")]
pub permission_overwrites: Option<sqlx::types::Json<Vec<PermissionOverwrite>>>,
#[cfg(not(feature = "sqlx"))]
pub permission_overwrites: Option<Vec<PermissionOverwrite>>,
pub permissions: Option<String>,
pub position: Option<i32>,
pub rate_limit_per_user: Option<i32>,
#[cfg_attr(feature = "sqlx", sqlx(skip))]
pub recipients: Option<Vec<User>>,
pub rtc_region: Option<String>,
#[cfg_attr(feature = "sqlx", sqlx(skip))]
pub thread_metadata: Option<ThreadMetadata>,
pub topic: Option<String>,
pub total_message_sent: Option<i32>,
pub user_limit: Option<i32>,
pub video_quality_mode: Option<i32>,
}
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq)]
@ -74,7 +74,7 @@ pub struct Tag {
pub emoji_name: Option<String>,
}
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, PartialOrd)]
pub struct PermissionOverwrite {
pub id: Snowflake,
#[serde(rename = "type")]

View File

@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize};
use crate::types::entities::User;
use crate::types::Snowflake;
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize, Default)]
#[derive(Debug, PartialEq, Eq, Clone, Deserialize, Serialize, Default)]
#[cfg_attr(feature = "sqlx", derive(sqlx::FromRow))]
pub struct Emoji {
pub id: Option<Snowflake>,

View File

@ -91,7 +91,7 @@ pub struct Guild {
}
/// See https://docs.spacebar.chat/routes/#get-/guilds/-guild_id-/bans/-user-
#[derive(Serialize, Deserialize, Debug, Default, Clone, PartialEq)]
#[derive(Serialize, Deserialize, Debug, Default, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "sqlx", derive(sqlx::FromRow))]
pub struct GuildBan {
pub user_id: Snowflake,

View File

@ -0,0 +1,75 @@
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use crate::types::{Snowflake, WelcomeScreenObject};
use super::guild::GuildScheduledEvent;
use super::{Application, Channel, GuildMember, User};
/// Represents a code that when used, adds a user to a guild or group DM channel, or creates a relationship between two users.
/// See <https://discord-userdoccers.vercel.app/resources/invite#invite-object>
#[derive(Debug, Serialize, Deserialize)]
pub struct Invite {
pub approximate_member_count: Option<i32>,
pub approximate_presence_count: Option<i32>,
pub channel: Option<Channel>,
pub code: String,
pub created_at: Option<DateTime<Utc>>,
pub expires_at: Option<DateTime<Utc>>,
pub flags: Option<i32>,
pub guild: Option<InviteGuild>,
pub guild_id: Option<Snowflake>,
pub guild_scheduled_event: Option<GuildScheduledEvent>,
#[serde(rename = "type")]
pub invite_type: Option<i32>,
pub inviter: Option<User>,
pub max_age: Option<i32>,
pub max_uses: Option<i32>,
pub stage_instance: Option<InviteStageInstance>,
pub target_application: Option<Application>,
pub target_type: Option<i32>,
pub target_user: Option<User>,
pub temporary: Option<bool>,
pub uses: Option<i32>,
}
/// The guild an invite is for.
/// See <https://discord-userdoccers.vercel.app/resources/invite#invite-guild-object>
#[derive(Debug, Serialize, Deserialize)]
pub struct InviteGuild {
pub id: Snowflake,
pub name: String,
pub icon: Option<String>,
pub splash: Option<String>,
pub verification_level: i32,
pub features: Vec<String>,
pub vanity_url_code: Option<String>,
pub description: Option<String>,
pub banner: Option<String>,
pub premium_subscription_count: Option<i32>,
#[serde(rename = "nsfw")]
#[serde(skip_serializing_if = "Option::is_none")]
pub nsfw_deprecated: Option<bool>,
pub nsfw_level: NSFWLevel,
pub welcome_screen: Option<WelcomeScreenObject>,
}
/// See <https://discord-userdoccers.vercel.app/resources/guild#nsfw-level> for an explanation on what
/// the levels mean.
#[derive(Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum NSFWLevel {
Default = 0,
Explicit = 1,
Safe = 2,
AgeRestricted = 3,
}
/// See <https://discord-userdoccers.vercel.app/resources/invite#invite-stage-instance-object>
#[derive(Debug, Serialize, Deserialize)]
pub struct InviteStageInstance {
pub members: Vec<GuildMember>,
pub participant_count: i32,
pub speaker_count: i32,
pub topic: String,
}

View File

@ -8,6 +8,7 @@ pub use emoji::*;
pub use guild::*;
pub use guild_member::*;
pub use integration::*;
pub use invite::*;
pub use message::*;
pub use relationship::*;
pub use role::*;
@ -31,6 +32,7 @@ mod emoji;
mod guild;
mod guild_member;
mod integration;
mod invite;
mod message;
mod relationship;
mod role;

View File

@ -43,9 +43,9 @@ pub struct User {
pub bio: Option<String>,
pub theme_colors: Option<Vec<u8>>,
pub phone: Option<String>,
pub nsfw_allowed: bool,
pub premium: bool,
pub purchased_flags: i32,
pub nsfw_allowed: Option<bool>,
pub premium: Option<bool>,
pub purchased_flags: Option<i32>,
pub premium_usage_flags: Option<i32>,
pub disabled: Option<bool>,
}

View File

@ -1,8 +1,9 @@
use bitflags::bitflags;
use serde::{Deserialize, Serialize};
use crate::types::{entities::PermissionOverwrite, Snowflake};
#[derive(Debug, Deserialize, Serialize)]
#[derive(Debug, Deserialize, Serialize, Default, PartialEq, PartialOrd)]
#[serde(rename_all = "snake_case")]
pub struct ChannelCreateSchema {
pub name: String,
@ -26,7 +27,7 @@ pub struct ChannelCreateSchema {
pub video_quality_mode: Option<i32>,
}
#[derive(Debug, Deserialize, Serialize, Clone, Default)]
#[derive(Debug, Deserialize, Serialize, Clone, Default, PartialEq, PartialOrd)]
#[serde(rename_all = "snake_case")]
pub struct ChannelModifySchema {
pub name: Option<String>,
@ -48,7 +49,7 @@ pub struct ChannelModifySchema {
pub video_quality_mode: Option<i32>,
}
#[derive(Debug, Deserialize, Serialize, Clone)]
#[derive(Debug, Deserialize, Serialize, Clone, Copy, PartialEq, PartialOrd, Eq, Ord)]
pub struct GetChannelMessagesSchema {
/// Between 1 and 100, defaults to 50.
pub limit: Option<i32>,
@ -56,7 +57,7 @@ pub struct GetChannelMessagesSchema {
pub anchor: ChannelMessagesAnchor,
}
#[derive(Debug, Deserialize, Serialize, Clone)]
#[derive(Debug, Deserialize, Serialize, Clone, Copy, PartialEq, PartialOrd, Eq, Ord)]
#[serde(rename_all = "snake_case")]
pub enum ChannelMessagesAnchor {
Before(Snowflake),
@ -94,3 +95,56 @@ impl GetChannelMessagesSchema {
}
}
}
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, PartialOrd)]
pub struct CreateChannelInviteSchema {
pub flags: Option<InviteFlags>,
pub max_age: Option<u32>,
pub max_uses: Option<u8>,
pub temporary: Option<bool>,
pub unique: Option<bool>,
pub validate: Option<String>,
pub target_type: Option<InviteType>,
pub target_user_id: Option<Snowflake>,
pub target_application_id: Option<Snowflake>,
}
impl Default for CreateChannelInviteSchema {
fn default() -> Self {
Self {
flags: None,
max_age: Some(86400),
max_uses: Some(0),
temporary: Some(false),
unique: Some(false),
validate: None,
target_type: None,
target_user_id: None,
target_application_id: None,
}
}
}
bitflags! {
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, PartialOrd, Ord)]
pub struct InviteFlags: u64 {
const GUEST = 1 << 0;
}
}
#[derive(Debug, Deserialize, Serialize, Clone, Copy, Default, PartialOrd, Ord, PartialEq, Eq)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum InviteType {
#[default]
Stream = 1,
EmbeddedApplication = 2,
RoleSubscriptions = 3,
CreatorPage = 4,
}
/// See <https://discord-userdoccers.vercel.app/resources/channel#add-channel-recipient>
#[derive(Debug, Deserialize, Serialize, Clone, Default, PartialOrd, Ord, PartialEq, Eq)]
pub struct AddChannelRecipientSchema {
pub access_token: Option<String>,
pub nick: Option<String>,
}

View File

@ -1,5 +1,9 @@
use std::collections::HashMap;
use serde::{Deserialize, Serialize};
use crate::types::Snowflake;
#[derive(Debug, Deserialize, Serialize)]
#[serde(rename_all = "snake_case")]
pub struct UserModifySchema {
@ -14,3 +18,17 @@ pub struct UserModifySchema {
pub email: Option<String>,
pub discriminator: Option<i16>,
}
/// # Attributes:
/// - recipients: The users to include in the private channel
/// - access_tokens: The access tokens of users that have granted your app the `gdm.join` scope. Only usable for OAuth2 requests (which can only create group DMs).
/// - nicks: A mapping of user IDs to their respective nicknames. Only usable for OAuth2 requests (which can only create group DMs).
///
/// # Reference:
/// Read: <https://discord-userdoccers.vercel.app/resources/channel#json-params>
#[derive(Debug, Deserialize, Serialize)]
pub struct PrivateChannelCreateSchema {
pub recipients: Option<Vec<Snowflake>>,
pub access_tokens: Option<Vec<String>>,
pub nicks: Option<HashMap<Snowflake, String>>,
}

View File

@ -12,7 +12,7 @@ const EPOCH: i64 = 1420070400000;
/// Unique identifier including a timestamp.
/// See https://discord.com/developers/docs/reference#snowflakes
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[cfg_attr(feature = "sqlx", derive(Type))]
#[cfg_attr(feature = "sqlx", sqlx(transparent))]
pub struct Snowflake(u64);

View File

@ -1,6 +1,6 @@
use chorus::types::{
self, Channel, GetChannelMessagesSchema, MessageSendSchema, PermissionFlags,
PermissionOverwrite, Snowflake,
PermissionOverwrite, PrivateChannelCreateSchema, RelationshipType, Snowflake,
};
mod common;
@ -136,3 +136,81 @@ async fn get_channel_messages() {
common::teardown(bundle).await
}
#[tokio::test]
async fn create_dm() {
let mut bundle = common::setup().await;
let other_user = bundle.create_user("integrationtestuser2").await;
let user = &mut bundle.user;
let private_channel_create_schema = PrivateChannelCreateSchema {
recipients: Some(Vec::from([other_user.object.id])),
access_tokens: None,
nicks: None,
};
let dm_channel = user
.create_private_channel(private_channel_create_schema)
.await
.unwrap();
assert!(dm_channel.recipients.is_some());
assert_eq!(
dm_channel.recipients.as_ref().unwrap().get(0).unwrap().id,
other_user.object.id
);
assert_eq!(
dm_channel.recipients.as_ref().unwrap().get(1).unwrap().id,
user.object.id
);
common::teardown(bundle).await;
}
// #[tokio::test]
// This test currently is broken due to an issue with the Spacebar Server.
#[allow(dead_code)]
async fn remove_add_person_from_to_dm() {
let mut bundle = common::setup().await;
let mut other_user = bundle.create_user("integrationtestuser2").await;
let mut third_user = bundle.create_user("integrationtestuser3").await;
let user = &mut bundle.user;
let private_channel_create_schema = PrivateChannelCreateSchema {
recipients: Some(Vec::from([other_user.object.id, third_user.object.id])),
access_tokens: None,
nicks: None,
};
let dm_channel = user
.create_private_channel(private_channel_create_schema)
.await
.unwrap(); // Creates the Channel and stores the response Channel object
dm_channel
.remove_channel_recipient(other_user.object.id, user)
.await
.unwrap();
assert!(dm_channel.recipients.as_ref().unwrap().get(1).is_none());
other_user
.modify_user_relationship(user.object.id, RelationshipType::Friends)
.await
.unwrap();
user.modify_user_relationship(other_user.object.id, RelationshipType::Friends)
.await
.unwrap();
third_user
.modify_user_relationship(user.object.id, RelationshipType::Friends)
.await
.unwrap();
user.modify_user_relationship(third_user.object.id, RelationshipType::Friends)
.await
.unwrap();
// Users 1-2 and 1-3 are now friends
dm_channel
.add_channel_recipient(other_user.object.id, user, None)
.await
.unwrap();
assert!(dm_channel.recipients.is_some());
assert_eq!(
dm_channel.recipients.as_ref().unwrap().get(0).unwrap().id,
other_user.object.id
);
assert_eq!(
dm_channel.recipients.as_ref().unwrap().get(1).unwrap().id,
user.object.id
);
}

View File

@ -7,8 +7,9 @@ use chorus::{
UrlBundle,
};
#[allow(dead_code)]
#[derive(Debug)]
pub struct TestBundle {
pub(crate) struct TestBundle {
pub urls: UrlBundle,
pub user: UserMeta,
pub instance: Instance,
@ -17,8 +18,24 @@ pub struct TestBundle {
pub channel: Channel,
}
impl TestBundle {
#[allow(unused)]
pub(crate) async fn create_user(&mut self, username: &str) -> UserMeta {
let register_schema = RegisterSchema {
username: username.to_string(),
consent: true,
date_of_birth: Some("2000-01-01".to_string()),
..Default::default()
};
self.instance
.register_account(&register_schema)
.await
.unwrap()
}
}
// Set up a test by creating an Instance and a User. Reduces Test boilerplate.
pub async fn setup() -> TestBundle {
pub(crate) async fn setup() -> TestBundle {
let urls = UrlBundle::new(
"http://localhost:3001/api".to_string(),
"ws://localhost:3001".to_string(),
@ -93,7 +110,7 @@ pub async fn setup() -> TestBundle {
// Teardown method to clean up after a test.
#[allow(dead_code)]
pub async fn teardown(mut bundle: TestBundle) {
pub(crate) async fn teardown(mut bundle: TestBundle) {
Guild::delete(&mut bundle.user, bundle.guild.id)
.await
.unwrap();

25
tests/invites.rs Normal file
View File

@ -0,0 +1,25 @@
use chorus::types::CreateChannelInviteSchema;
mod common;
#[tokio::test]
async fn create_accept_invite() {
let mut bundle = common::setup().await;
let channel = bundle.channel.clone();
let mut user = bundle.user.clone();
let create_channel_invite_schema = CreateChannelInviteSchema::default();
let mut other_user = bundle.create_user("testuser1312").await;
assert!(chorus::types::Guild::get(bundle.guild.id, &mut other_user)
.await
.is_err());
let invite = user
.create_guild_invite(create_channel_invite_schema, channel.id)
.await
.unwrap();
other_user.accept_invite(&invite.code, None).await.unwrap();
assert!(chorus::types::Guild::get(bundle.guild.id, &mut other_user)
.await
.is_ok());
common::teardown(bundle).await;
}

View File

@ -1,20 +1,12 @@
use chorus::types::{self, RegisterSchema, Relationship, RelationshipType};
use chorus::types::{self, Relationship, RelationshipType};
mod common;
#[tokio::test]
async fn test_get_mutual_relationships() {
let register_schema = RegisterSchema {
username: "integrationtestuser2".to_string(),
consent: true,
date_of_birth: Some("2000-01-01".to_string()),
..Default::default()
};
let mut bundle = common::setup().await;
let belongs_to = &mut bundle.instance;
let mut other_user = bundle.create_user("integrationtestuser2").await;
let user = &mut bundle.user;
let mut other_user = belongs_to.register_account(&register_schema).await.unwrap();
let friend_request_schema = types::FriendRequestSendSchema {
username: user.object.username.clone(),
discriminator: Some(user.object.discriminator.clone()),
@ -30,17 +22,9 @@ async fn test_get_mutual_relationships() {
#[tokio::test]
async fn test_get_relationships() {
let register_schema = RegisterSchema {
username: "integrationtestuser2".to_string(),
consent: true,
date_of_birth: Some("2000-01-01".to_string()),
..Default::default()
};
let mut bundle = common::setup().await;
let belongs_to = &mut bundle.instance;
let mut other_user = bundle.create_user("integrationtestuser2").await;
let user = &mut bundle.user;
let mut other_user = belongs_to.register_account(&register_schema).await.unwrap();
let friend_request_schema = types::FriendRequestSendSchema {
username: user.object.username.clone(),
discriminator: Some(user.object.discriminator.clone()),
@ -56,17 +40,9 @@ async fn test_get_relationships() {
#[tokio::test]
async fn test_modify_relationship_friends() {
let register_schema = RegisterSchema {
username: "integrationtestuser2".to_string(),
consent: true,
date_of_birth: Some("2000-01-01".to_string()),
..Default::default()
};
let mut bundle = common::setup().await;
let belongs_to = &mut bundle.instance;
let mut other_user = bundle.create_user("integrationtestuser2").await;
let user = &mut bundle.user;
let mut other_user = belongs_to.register_account(&register_schema).await.unwrap();
let _ = other_user
.modify_user_relationship(user.object.id, types::RelationshipType::Friends)
.await;
@ -105,17 +81,9 @@ async fn test_modify_relationship_friends() {
#[tokio::test]
async fn test_modify_relationship_block() {
let register_schema = RegisterSchema {
username: "integrationtestuser2".to_string(),
consent: true,
date_of_birth: Some("2000-01-01".to_string()),
..Default::default()
};
let mut bundle = common::setup().await;
let belongs_to = &mut bundle.instance;
let mut other_user = bundle.create_user("integrationtestuser2").await;
let user = &mut bundle.user;
let mut other_user = belongs_to.register_account(&register_schema).await.unwrap();
let _ = other_user
.modify_user_relationship(user.object.id, types::RelationshipType::Blocked)
.await;