diff --git a/src/api/rcos/meetings/creation/context.rs b/src/api/rcos/meetings/creation/context.rs index 2998fdf8..bf407e25 100644 --- a/src/api/rcos/meetings/creation/context.rs +++ b/src/api/rcos/meetings/creation/context.rs @@ -1,9 +1,9 @@ //! GraphQL query to get context for meeting creation. -use chrono::Utc; use crate::api::rcos::prelude::*; use crate::api::rcos::send_query; use crate::error::TelescopeError; +use chrono::Utc; #[derive(GraphQLQuery)] #[graphql( @@ -19,7 +19,10 @@ impl CreationContext { /// /// For meeting edits, semesters may be manually included by ID. otherwise, only ongoing and /// future semesters will be included. - pub async fn execute(host: Option, include_semesters: Vec) -> Result { + pub async fn execute( + host: Option, + include_semesters: Vec, + ) -> Result { send_query::(creation_context::Variables { host: host.map(|h| vec![h]).unwrap_or(vec![]), semester_filter: serde_json::from_value(json!({ @@ -27,7 +30,9 @@ impl CreationContext { { "end_date": { "_gte": Utc::today().naive_utc() }}, { "semester_id": {"_in": include_semesters }} ] - })).map_err(|_| TelescopeError::ise("Malformed semester filter in GraphQL query."))? - }).await + })) + .map_err(|_| TelescopeError::ise("Malformed semester filter in GraphQL query."))?, + }) + .await } } diff --git a/src/api/rcos/meetings/get_host.rs b/src/api/rcos/meetings/get_host.rs index 1e1be3d0..7a5704ec 100644 --- a/src/api/rcos/meetings/get_host.rs +++ b/src/api/rcos/meetings/get_host.rs @@ -1,9 +1,6 @@ //! GraphQL query to get the user ID of the host of a meeting by the meeting's ID. -use crate::api::rcos::{ - send_query, - prelude::* -}; +use crate::api::rcos::{prelude::*, send_query}; use crate::error::TelescopeError; #[derive(GraphQLQuery)] diff --git a/src/api/rcos/users/accounts/for_user.rs b/src/api/rcos/users/accounts/for_user.rs index 32e1f6f6..bfe72242 100644 --- a/src/api/rcos/users/accounts/for_user.rs +++ b/src/api/rcos/users/accounts/for_user.rs @@ -1,8 +1,8 @@ //! Lookup all the user accounts for a given user. // Namespacing -use crate::api::rcos::users::UserAccountType as user_account; use crate::api::rcos::prelude::*; +use crate::api::rcos::users::UserAccountType as user_account; #[derive(GraphQLQuery)] #[graphql( diff --git a/src/api/rcos/users/accounts/link.rs b/src/api/rcos/users/accounts/link.rs index b3fefd6a..02f59d7a 100644 --- a/src/api/rcos/users/accounts/link.rs +++ b/src/api/rcos/users/accounts/link.rs @@ -2,9 +2,7 @@ // Namespace items for generated code use crate::api::rcos::users::{UserAccountType as user_account, UserAccountType}; -use crate::api::rcos::{ - send_query, prelude::* -}; +use crate::api::rcos::{prelude::*, send_query}; use crate::error::TelescopeError; #[derive(GraphQLQuery)] @@ -18,11 +16,7 @@ use link_user_account::{ResponseData, Variables}; impl LinkUserAccount { /// Make the variables for a user account upsert mutation. - fn make_variables( - user_id: uuid, - platform: UserAccountType, - platform_id: String, - ) -> Variables { + fn make_variables(user_id: uuid, platform: UserAccountType, platform_id: String) -> Variables { Variables { user_id, platform, diff --git a/src/api/rcos/users/accounts/lookup.rs b/src/api/rcos/users/accounts/lookup.rs index 6cd5d2c1..8644d85c 100644 --- a/src/api/rcos/users/accounts/lookup.rs +++ b/src/api/rcos/users/accounts/lookup.rs @@ -16,10 +16,7 @@ use self::account_lookup::{ResponseData, Variables}; impl AccountLookup { /// Make the variables for an account lookup query. pub fn make_variables(user_id: uuid, platform: user_account) -> Variables { - Variables { - user_id, - platform, - } + Variables { user_id, platform } } /// Send the account lookup query. This return the user's ID on the given platform if there diff --git a/src/api/rcos/users/accounts/reverse_lookup.rs b/src/api/rcos/users/accounts/reverse_lookup.rs index 9d78349d..30840383 100644 --- a/src/api/rcos/users/accounts/reverse_lookup.rs +++ b/src/api/rcos/users/accounts/reverse_lookup.rs @@ -1,8 +1,8 @@ //! RCOS API query to get the user ID (if available) of a user by platform and account id. // Import and rename for GraphQL macro -use crate::api::rcos::users::UserAccountType as user_account; use crate::api::rcos::prelude::*; +use crate::api::rcos::users::UserAccountType as user_account; /// Type representing query for user ID given a platform and user id on that /// platform. @@ -13,10 +13,10 @@ use crate::api::rcos::prelude::*; )] pub struct ReverseLookup; -use reverse_lookup::ResponseData; -use reverse_lookup::Variables; use crate::api::rcos::send_query; use crate::error::TelescopeError; +use reverse_lookup::ResponseData; +use reverse_lookup::Variables; impl ReverseLookup { /// Make the variables for a reverse account lookup. @@ -28,7 +28,10 @@ impl ReverseLookup { } /// Get the user ID associated with an ID on a different platform if available. - pub async fn execute(platform: user_account, platform_id: String) -> Result, TelescopeError> { + pub async fn execute( + platform: user_account, + platform_id: String, + ) -> Result, TelescopeError> { send_query::(Self::make_vars(platform, platform_id)) .await .map(|response| response.user_id()) diff --git a/src/api/rcos/users/accounts/unlink.rs b/src/api/rcos/users/accounts/unlink.rs index ec8b32ee..93613a31 100644 --- a/src/api/rcos/users/accounts/unlink.rs +++ b/src/api/rcos/users/accounts/unlink.rs @@ -1,8 +1,8 @@ //! Mutation to unlink a user account. // Namespace items for generated module -use crate::api::rcos::users::UserAccountType as user_account; use crate::api::rcos::prelude::*; +use crate::api::rcos::users::UserAccountType as user_account; #[derive(GraphQLQuery)] #[graphql( diff --git a/src/api/rcos/users/create.rs b/src/api/rcos/users/create.rs index 6e306520..3641b9c2 100644 --- a/src/api/rcos/users/create.rs +++ b/src/api/rcos/users/create.rs @@ -28,7 +28,7 @@ impl CreateOneUser { platform, platform_id, }) - .await - .map(|response| response.insert_users_one.map(|obj| obj.id)) + .await + .map(|response| response.insert_users_one.map(|obj| obj.id)) } } diff --git a/src/api/rcos/users/delete.rs b/src/api/rcos/users/delete.rs index d059624b..447f5b77 100644 --- a/src/api/rcos/users/delete.rs +++ b/src/api/rcos/users/delete.rs @@ -1,6 +1,6 @@ //! RCOS API mutation to delete a user -use crate::api::rcos::{send_query, prelude::*}; +use crate::api::rcos::{prelude::*, send_query}; use crate::error::TelescopeError; #[derive(GraphQLQuery)] diff --git a/src/main.rs b/src/main.rs index 64dcb8e8..5f58a972 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,6 +19,11 @@ extern crate derive_more; #[macro_use] extern crate graphql_client; +use crate::discord_bot::DiscordBot; +use crate::templates::static_pages::sponsors::SponsorsPage; +use crate::templates::static_pages::StaticPage; +use crate::web::csrf::CsrfJanitor; +use crate::web::middlewares; use actix::prelude::*; use actix_files as afs; use actix_identity::{CookieIdentityPolicy, IdentityService}; @@ -27,11 +32,6 @@ use actix_web::{middleware, web as aweb, web::get, App, HttpServer}; use chrono::Offset; use rand::rngs::OsRng; use rand::Rng; -use crate::web::csrf::CsrfJanitor; -use crate::web::middlewares; -use crate::discord_bot::DiscordBot; -use crate::templates::static_pages::sponsors::SponsorsPage; -use crate::templates::static_pages::StaticPage; pub mod api; mod app_data; diff --git a/src/web/middlewares/authorization.rs b/src/web/middlewares/authorization.rs index 5991fa05..5a8ce5a2 100644 --- a/src/web/middlewares/authorization.rs +++ b/src/web/middlewares/authorization.rs @@ -108,8 +108,7 @@ where let user_id = user_id_result.unwrap(); // Call the authorization check. - let authorization_result: AuthorizationResult = - (check.as_ref())(user_id).await; + let authorization_result: AuthorizationResult = (check.as_ref())(user_id).await; // Check for an error. We have to explicitly convert to a response here otherwise // actix error handling will skip upstream middlewares. diff --git a/src/web/services/auth/identity.rs b/src/web/services/auth/identity.rs index bc370a8d..06672122 100644 --- a/src/web/services/auth/identity.rs +++ b/src/web/services/auth/identity.rs @@ -198,8 +198,7 @@ impl AuthenticationCookie { // Lookup the user's ID let user_id = self.get_user_id_or_error().await?; // Lookup the user's RCS id - let rcs_id: Option = - AccountLookup::send(user_id, UserAccountType::Rpi).await?; + let rcs_id: Option = AccountLookup::send(user_id, UserAccountType::Rpi).await?; // If there is an RCS id, replace the root. if let Some(rcs_id) = rcs_id { self.root = RootIdentity::RpiCas(RpiCasIdentity { rcs_id }); diff --git a/src/web/services/auth/mod.rs b/src/web/services/auth/mod.rs index e8196cfc..9e54c465 100644 --- a/src/web/services/auth/mod.rs +++ b/src/web/services/auth/mod.rs @@ -206,15 +206,14 @@ pub trait IdentityProvider: 'static { let user_id = cookie.get_user_id_or_error().await?; // Get all of the accounts linked to this user. Make sure at least one // can function for authentication. - let all_accounts: HashMap = - UserAccounts::send(user_id) - .await? - // Iterate - .into_iter() - // filter down to the authentication providers - .filter(|(u, _)| AUTHENTICATOR_ACCOUNT_TYPES.contains(u)) - // Collect into map. - .collect(); + let all_accounts: HashMap = UserAccounts::send(user_id) + .await? + // Iterate + .into_iter() + // filter down to the authentication providers + .filter(|(u, _)| AUTHENTICATOR_ACCOUNT_TYPES.contains(u)) + // Collect into map. + .collect(); // If there is not a secondary account for the user to authenticate with, // return an error. @@ -272,8 +271,7 @@ pub trait IdentityProvider: 'static { // There is a secondary authenticator linked, delete this user account record. // Log a message about the unlinked platform. - let platform_id = - UnlinkUserAccount::send(user_id, Self::USER_ACCOUNT_TY).await?; + let platform_id = UnlinkUserAccount::send(user_id, Self::USER_ACCOUNT_TY).await?; if let Some(platform_id) = platform_id { info!( diff --git a/src/web/services/auth/oauth2_providers/mod.rs b/src/web/services/auth/oauth2_providers/mod.rs index be8b0e0e..49bb4680 100644 --- a/src/web/services/auth/oauth2_providers/mod.rs +++ b/src/web/services/auth/oauth2_providers/mod.rs @@ -1,13 +1,14 @@ use super::{make_redirect_url, IdentityProvider}; use crate::api::rcos::users::accounts::for_user::UserAccounts; use crate::api::rcos::users::accounts::link::LinkUserAccount; +use crate::api::rcos::users::accounts::reverse_lookup::ReverseLookup; use crate::api::rcos::users::accounts::unlink::UnlinkUserAccount; use crate::api::rcos::users::UserAccountType; use crate::api::rcos::{send_query, users::accounts::reverse_lookup}; use crate::error::TelescopeError; +use crate::web::csrf; use crate::web::services::auth::identity::{AuthenticationCookie, Identity, RootIdentity}; use crate::web::services::auth::AUTHENTICATOR_ACCOUNT_TYPES; -use crate::web::csrf; use actix_web::http::header::LOCATION; use actix_web::web::Query; use actix_web::FromRequest; @@ -18,7 +19,6 @@ use oauth2::{AuthorizationCode, AuthorizationRequest, CsrfToken, RedirectUrl, Sc use std::borrow::Cow; use std::collections::HashMap; use std::sync::Arc; -use crate::api::rcos::users::accounts::reverse_lookup::ReverseLookup; pub mod discord; pub mod github; @@ -226,9 +226,12 @@ where .await? .ok_or(TelescopeError::resource_not_found( "Could not find associated user account.", - format!("Could not find user account associated with this {} account. \ + format!( + "Could not find user account associated with this {} account. \ Please create an account or sign in using another method.", - Self::SERVICE_NAME)))?; + Self::SERVICE_NAME + ), + ))?; // Otherwise, store the identity in the user's cookies and redirect to their profile. let identity: Identity = Identity::extract(&req).await?; diff --git a/src/web/services/auth/rpi_cas.rs b/src/web/services/auth/rpi_cas.rs index f7d54672..d10f7e64 100644 --- a/src/web/services/auth/rpi_cas.rs +++ b/src/web/services/auth/rpi_cas.rs @@ -299,12 +299,7 @@ impl IdentityProvider for RpiCas { // Add to database if needed. if add_new_to_db { // Link the account. - LinkUserAccount::send( - user_id, - Self::USER_ACCOUNT_TY, - new_rcs_id.clone(), - ) - .await?; + LinkUserAccount::send(user_id, Self::USER_ACCOUNT_TY, new_rcs_id.clone()).await?; } // Throw an error if the new RCS ID doesn't match the linked one. diff --git a/src/web/services/meetings/create.rs b/src/web/services/meetings/create.rs index 114a9f2a..59767b7c 100644 --- a/src/web/services/meetings/create.rs +++ b/src/web/services/meetings/create.rs @@ -7,6 +7,7 @@ use crate::api::rcos::meetings::authorization_for::UserMeetingAuthorization; use crate::api::rcos::meetings::creation; +use crate::api::rcos::meetings::creation::context::CreationContext; use crate::api::rcos::meetings::creation::create::CreateMeeting; use crate::api::rcos::meetings::creation::host_selection::HostSelection; use crate::api::rcos::meetings::{MeetingType, ALL_MEETING_TYPES}; @@ -22,7 +23,6 @@ use actix_web::HttpResponse; use chrono::{DateTime, Local, NaiveDate, NaiveDateTime, NaiveTime, TimeZone, Utc}; use serde_json::Value; use uuid::Uuid; -use crate::api::rcos::meetings::creation::context::CreationContext; /// The handlebars template for the user to select a host. const HOST_SELECTION_TEMPLATE: &'static str = "meetings/creation/host_selection"; diff --git a/src/web/services/meetings/edit.rs b/src/web/services/meetings/edit.rs index 9967b49c..f7d01626 100644 --- a/src/web/services/meetings/edit.rs +++ b/src/web/services/meetings/edit.rs @@ -74,10 +74,7 @@ async fn meeting_data_checked( ) -> Result { // Get meeting data. Extract host's user ID. let meeting_data = get_meeting_data(meeting_id).await?; - let meeting_host: Option<_> = meeting_data - .host - .as_ref() - .map(|host| host.id); + let meeting_host: Option<_> = meeting_data.host.as_ref().map(|host| host.id); // Get user's authorization object. let authorization = authorization_for_viewer(auth).await?; @@ -136,8 +133,8 @@ async fn edit_page( let host: Option = resolve_host_user_id(&meeting_data, set_host); // Get the creation context (based on the resolved host) // so we know what semesters are available. - let context = CreationContext::execute(host, vec![meeting_data.semester.semester_id.clone()]) - .await?; + let context = + CreationContext::execute(host, vec![meeting_data.semester.semester_id.clone()]).await?; // Create the meeting template. let mut form: FormTemplate = make_form(&meeting_data); @@ -177,8 +174,8 @@ async fn submit_meeting_edits( let host: Option = resolve_host_user_id(&meeting_data, set_host); // Get the creation context (based on the resolved host) // so we know what semesters are available. - let context = CreationContext::execute(host, vec![meeting_data.semester.semester_id.clone()]) - .await?; + let context = + CreationContext::execute(host, vec![meeting_data.semester.semester_id.clone()]).await?; // Create the meeting template. let mut form: FormTemplate = make_form(&meeting_data); @@ -334,7 +331,7 @@ async fn submit_meeting_edits( // Extract the host from context object. host: form.template["context"]["host"][0]["id"] .as_str() - .and_then(|host_id| host_id.parse::().ok()) + .and_then(|host_id| host_id.parse::().ok()), }; // The returned meeting ID should match the existing one but we don't check. diff --git a/src/web/services/user/delete.rs b/src/web/services/user/delete.rs index f0c7d925..d875b19c 100644 --- a/src/web/services/user/delete.rs +++ b/src/web/services/user/delete.rs @@ -32,10 +32,9 @@ pub async fn profile_delete( .ok_or(TelescopeError::NotAuthenticated)?; // Check if the viewer has a discord account linked. - let discord_id: Option = - AccountLookup::send(user_id, UserAccountType::Discord) - .await? - .and_then(|string| string.as_str().parse::().ok()); + let discord_id: Option = AccountLookup::send(user_id, UserAccountType::Discord) + .await? + .and_then(|string| string.as_str().parse::().ok()); // If there is one, kick it from the RCOS Discord. if let Some(discord_id) = discord_id { diff --git a/src/web/services/user/profile.rs b/src/web/services/user/profile.rs index e980fc66..cd9723b3 100644 --- a/src/web/services/user/profile.rs +++ b/src/web/services/user/profile.rs @@ -40,7 +40,7 @@ pub fn register(config: &mut ServiceConfig) { async fn profile( req: HttpRequest, identity: Identity, - Path(id): Path + Path(id): Path, ) -> Result { // Get the viewer's user ID. let viewer: Option = identity.get_user_id().await?; diff --git a/src/web/services/user/register.rs b/src/web/services/user/register.rs index dda6b15c..d0c0cbf1 100644 --- a/src/web/services/user/register.rs +++ b/src/web/services/user/register.rs @@ -185,38 +185,42 @@ pub async fn submit_registration( let platform_id: String = match &identity_cookie.root { RootIdentity::GitHub(gh) => gh.get_github_id().await?, RootIdentity::Discord(d) => d.get_discord_id().await?, - RootIdentity::RpiCas(RpiCasIdentity { rcs_id }) => rcs_id.clone() + RootIdentity::RpiCas(RpiCasIdentity { rcs_id }) => rcs_id.clone(), }; // Create the account let created_user_id: Uuid = CreateOneUser::execute( first_name, last_name, - (platform == UserAccountType::Rpi).then(|| UserRole::Student).unwrap_or(UserRole::External), + (platform == UserAccountType::Rpi) + .then(|| UserRole::Student) + .unwrap_or(UserRole::External), platform, - platform_id + platform_id, ) - .await - // If we cannot create an account, someone has probably already - // linked the identity provider to another account. Tell the user to - // cancel and try to login. - .map_err(|_| TelescopeError::BadRequest { - header: "Could Not Create Account".into(), - message: format!( - "We could not create an account. This likely (although not always) \ + .await + // If we cannot create an account, someone has probably already + // linked the identity provider to another account. Tell the user to + // cancel and try to login. + .map_err(|_| TelescopeError::BadRequest { + header: "Could Not Create Account".into(), + message: format!( + "We could not create an account. This likely (although not always) \ means that your {0} account is already linked to an existing user's account. Please \ try to login to that account. If you continue having issues or are sure that your {0} \ account is not already linked to an existing user, please contact a coordinator and \ file an issue on the Telescope GitHub.", - platform - ), - show_status_code: false, - })? - // If there is no user ID, throw an error - .ok_or(TelescopeError::ise( - "Create User mutation did not return user ID", - ))?; + platform + ), + show_status_code: false, + })? + // If there is no user ID, throw an error + .ok_or(TelescopeError::ise( + "Create User mutation did not return user ID", + ))?; // Redirect the user to the account we created for them - Ok(HttpResponse::Found().header(LOCATION, format!("/user/{}", created_user_id)).finish()) + Ok(HttpResponse::Found() + .header(LOCATION, format!("/user/{}", created_user_id)) + .finish()) }