Skip to content

Commit

Permalink
rustfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
vcfxb committed Nov 17, 2021
1 parent 94a6ebb commit a309668
Show file tree
Hide file tree
Showing 20 changed files with 84 additions and 94 deletions.
13 changes: 9 additions & 4 deletions src/api/rcos/meetings/creation/context.rs
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -19,15 +19,20 @@ 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<uuid>, include_semesters: Vec<String>) -> Result<creation_context::ResponseData, TelescopeError> {
pub async fn execute(
host: Option<uuid>,
include_semesters: Vec<String>,
) -> Result<creation_context::ResponseData, TelescopeError> {
send_query::<Self>(creation_context::Variables {
host: host.map(|h| vec![h]).unwrap_or(vec![]),
semester_filter: serde_json::from_value(json!({
"_or": [
{ "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
}
}
5 changes: 1 addition & 4 deletions src/api/rcos/meetings/get_host.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down
2 changes: 1 addition & 1 deletion src/api/rcos/users/accounts/for_user.rs
Original file line number Diff line number Diff line change
@@ -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(
Expand Down
10 changes: 2 additions & 8 deletions src/api/rcos/users/accounts/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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,
Expand Down
5 changes: 1 addition & 4 deletions src/api/rcos/users/accounts/lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 7 additions & 4 deletions src/api/rcos/users/accounts/reverse_lookup.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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.
Expand All @@ -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<Option<uuid>, TelescopeError> {
pub async fn execute(
platform: user_account,
platform_id: String,
) -> Result<Option<uuid>, TelescopeError> {
send_query::<Self>(Self::make_vars(platform, platform_id))
.await
.map(|response| response.user_id())
Expand Down
2 changes: 1 addition & 1 deletion src/api/rcos/users/accounts/unlink.rs
Original file line number Diff line number Diff line change
@@ -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(
Expand Down
4 changes: 2 additions & 2 deletions src/api/rcos/users/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
2 changes: 1 addition & 1 deletion src/api/rcos/users/delete.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down
10 changes: 5 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions src/web/middlewares/authorization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 1 addition & 2 deletions src/web/services/auth/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> =
AccountLookup::send(user_id, UserAccountType::Rpi).await?;
let rcs_id: Option<String> = 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 });
Expand Down
20 changes: 9 additions & 11 deletions src/web/services/auth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<UserAccountType, String> =
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<UserAccountType, String> = 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.
Expand Down Expand Up @@ -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!(
Expand Down
11 changes: 7 additions & 4 deletions src/web/services/auth/oauth2_providers/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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?;
Expand Down
7 changes: 1 addition & 6 deletions src/web/services/auth/rpi_cas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/web/services/meetings/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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";
Expand Down
15 changes: 6 additions & 9 deletions src/web/services/meetings/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,7 @@ async fn meeting_data_checked(
) -> Result<MeetingMeeting, TelescopeError> {
// 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?;
Expand Down Expand Up @@ -136,8 +133,8 @@ async fn edit_page(
let host: Option<Uuid> = 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);
Expand Down Expand Up @@ -177,8 +174,8 @@ async fn submit_meeting_edits(
let host: Option<Uuid> = 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);
Expand Down Expand Up @@ -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::<Uuid>().ok())
.and_then(|host_id| host_id.parse::<Uuid>().ok()),
};

// The returned meeting ID should match the existing one but we don't check.
Expand Down
7 changes: 3 additions & 4 deletions src/web/services/user/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u64> =
AccountLookup::send(user_id, UserAccountType::Discord)
.await?
.and_then(|string| string.as_str().parse::<u64>().ok());
let discord_id: Option<u64> = AccountLookup::send(user_id, UserAccountType::Discord)
.await?
.and_then(|string| string.as_str().parse::<u64>().ok());

// If there is one, kick it from the RCOS Discord.
if let Some(discord_id) = discord_id {
Expand Down
2 changes: 1 addition & 1 deletion src/web/services/user/profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub fn register(config: &mut ServiceConfig) {
async fn profile(
req: HttpRequest,
identity: Identity,
Path(id): Path<Uuid>
Path(id): Path<Uuid>,
) -> Result<Template, TelescopeError> {
// Get the viewer's user ID.
let viewer: Option<Uuid> = identity.get_user_id().await?;
Expand Down
Loading

0 comments on commit a309668

Please sign in to comment.