From edeb8e5e7f7db9a5048032205b57c5430b27653a Mon Sep 17 00:00:00 2001 From: cptrodgers Date: Thu, 15 Aug 2024 21:14:03 +0700 Subject: [PATCH] chore: increase magic link of assign assignment to 1 month --- graphql_server/src/db/notification.rs | 15 +++++++++------ graphql_server/src/helper/mod.rs | 7 +++++-- graphql_server/src/service/redis.rs | 9 +++++++-- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/graphql_server/src/db/notification.rs b/graphql_server/src/db/notification.rs index d1be3ffb..9a9514cf 100644 --- a/graphql_server/src/db/notification.rs +++ b/graphql_server/src/db/notification.rs @@ -6,7 +6,7 @@ use uuid::Uuid; use super::schema::{notification_receivers, notifications}; use crate::db::User; -use crate::helper::generate_document_magic_link; +use crate::helper::{generate_document_magic_link, ONE_MONTH_SECONDS}; use crate::impl_enum_for_db; use crate::util::get_now_as_secs; use crate::util::url_util::{format_document_url, format_space_url}; @@ -187,8 +187,12 @@ Hello there! You've been assigned to a assignment: {assignment_name}. If you hav } fn get_url_path(&self, receiver: &User) -> String { - generate_document_magic_link(receiver.id, self.assignment_document_id) - .unwrap_or(format_document_url(self.assignment_document_id)) + generate_document_magic_link( + receiver.id, + self.assignment_document_id, + Some(ONE_MONTH_SECONDS), + ) + .unwrap_or(format_document_url(self.assignment_document_id)) } } @@ -217,9 +221,8 @@ Attention! A student {student_name} has started their assignment: {assignment_na ) } - fn get_url_path(&self, receiver: &User) -> String { - generate_document_magic_link(receiver.id, self.submission_document_id) - .unwrap_or(format_document_url(self.submission_document_id)) + fn get_url_path(&self, _receiver: &User) -> String { + format_document_url(self.submission_document_id) } } diff --git a/graphql_server/src/helper/mod.rs b/graphql_server/src/helper/mod.rs index 43217679..701e2b21 100644 --- a/graphql_server/src/helper/mod.rs +++ b/graphql_server/src/helper/mod.rs @@ -111,18 +111,21 @@ pub fn add_space_member( Ok(new_member) } +pub const ONE_MONTH_SECONDS: i64 = 2_592_000; + pub fn generate_document_magic_link( user_id: i32, document_id: Uuid, + ttl_seconds: Option, ) -> Result { let otp = generate_otp(); - Redis::init().set_magic_token(user_id, &otp)?; + Redis::init().set_magic_token(user_id, &otp, ttl_seconds)?; Ok(format_document_magic_link(document_id, &otp, user_id)) } pub fn generate_start_space_magic_link(user_id: i32, space_id: i32) -> Result { let otp = generate_otp(); - Redis::init().set_magic_token(user_id, &otp)?; + Redis::init().set_magic_token(user_id, &otp, Some(600))?; Ok(format_start_space_magic_link(space_id, &otp, user_id)) } diff --git a/graphql_server/src/service/redis.rs b/graphql_server/src/service/redis.rs index ec80b9d9..d0d36c03 100644 --- a/graphql_server/src/service/redis.rs +++ b/graphql_server/src/service/redis.rs @@ -39,9 +39,14 @@ impl Redis { Ok(()) } - pub fn set_magic_token(&self, user_id: i32, otp: &str) -> RedisResult<()> { + pub fn set_magic_token( + &self, + user_id: i32, + otp: &str, + ttl_seconds: Option, + ) -> RedisResult<()> { let key = format_magic_token(user_id); - self.set_value(&key, otp, Some(600))?; + self.set_value(&key, otp, ttl_seconds)?; Ok(()) }