Implement more mutations

Implement mutations:
- create language
- delete language
- user follows a language
- user unfollows a language
- new word

Context's user_auth now contains either the user's ID or nothing if
not authentificated.
This commit is contained in:
2023-01-24 01:05:18 +01:00
parent 51f0fc3108
commit a624636939
4 changed files with 423 additions and 16 deletions

View File

@@ -27,35 +27,42 @@ impl Default for OtherEnvVar {
pub struct Context {
pub db: Database,
pub appwrite: APVariables,
pub user_auth: bool,
pub user_auth: Option<String>,
pub other_vars: OtherEnvVar,
}
impl Context {
/// HTTP header for a user's session
/// Check if a request is performed by an autentificated user.
///
/// This header `Authorization` must be a single string in the
/// The HTTP header `Authorization` must be a single string in the
/// form `userId;userSessionId` with `userId` and `userSessionId`
/// being variables given by Appwrite to users that are logged in.
pub async fn user_auth<'r>(&self, auth_token: Option<&'r str>) -> bool {
///
/// The function returns either the user's ID if the user is
/// authentified or `None`.
pub async fn user_auth<'r>(
&self,
auth_token: Option<&'r str>,
) -> Option<String> {
if let Some(token) = auth_token {
let key = token.split(';').collect::<Vec<_>>();
if key.len() == 2 {
let user_id = key[0];
let session_id = key[1];
match self.appwrite.check_session(session_id, user_id).await {
Ok(val) => val,
Ok(true) => Some(key[0].to_string()),
Ok(false) => None,
Err(e) => {
info!("Error checking user session: {:?}", e);
false
None
}
}
} else {
info!("Invalid session key: {}", token);
false
None
}
} else {
false
None
}
}

View File

@@ -1,6 +1,15 @@
use juniper::FieldResult;
use std::str::FromStr;
use crate::db::{models::users::User, DatabaseError};
use juniper::FieldResult;
use uuid::Uuid;
use crate::db::{
models::{
languages::{Language, NewLanguage, UserFollowLanguage},
users::User,
},
DatabaseError,
};
use super::Context;
@@ -9,7 +18,7 @@ pub struct Mutation;
#[juniper::graphql_object(Context = Context)]
impl Mutation {
fn api_version(context: &Context) -> String {
if context.user_auth {
if context.user_auth.is_some() {
"0.1 (authentified)"
} else {
"0.1 (not authentified)"
@@ -49,4 +58,103 @@ impl Mutation {
.into())
}
}
pub fn user_follow_language(
context: &Context,
language: String,
) -> FieldResult<Language> {
if let Some(userid) = &context.user_auth {
match Uuid::from_str(&language) {
Err(e) => Err(DatabaseError::new(
format!(
"Could not parse {language} as a valid UUID: {e:?}"
),
"Bad Request",
)
.into()),
Ok(lang) => UserFollowLanguage::user_follow_language(
context,
&userid.to_string(),
lang,
)
.map_err(Into::into),
}
} else {
Err(DatabaseError::new(
"User not authentificated, cannot proceed",
"Unauthorized",
)
.into())
}
}
pub fn user_unfollow_language(
context: &Context,
language: String,
) -> FieldResult<Language> {
if let Some(userid) = &context.user_auth {
match Uuid::from_str(&language) {
Err(e) => Err(DatabaseError::new(
format!(
"Could not parse {language} as a valid UUID: {e:?}"
),
"Bad Request",
)
.into()),
Ok(lang) => UserFollowLanguage::user_unfollow_language(
context,
&userid.to_string(),
lang,
)
.map_err(Into::into),
}
} else {
Err(DatabaseError::new(
"User not authentificated, cannot proceed",
"Unauthorized",
)
.into())
}
}
pub fn new_language(
context: &Context,
language: NewLanguage,
) -> FieldResult<Language> {
if let Some(owner) = &context.user_auth {
language.insert_db(&context.db, owner).map_err(Into::into)
} else {
Err(DatabaseError::new(
"User not authentificated, cannot create new language",
"Unauthorized",
)
.into())
}
}
pub fn delete_language(
context: &Context,
language: String,
) -> FieldResult<Option<Language>> {
if context.user_auth.is_some() {
match Uuid::from_str(&language) {
Ok(uuid) => Language::delete(context, uuid)
.map(|_| None)
.map_err(Into::into),
Err(e) => Err(DatabaseError::new(
format!(
"Could not parse {language} as a valid UUID: {e:?}"
),
"Bad Request",
)
.into()),
}
} else {
Err(DatabaseError::new(
"User not authentificated, cannot create new language",
"Unauthorized",
)
.into())
}
}
}