Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ddboline committed Nov 23, 2024
1 parent 6b69d42 commit 4a4f0f2
Show file tree
Hide file tree
Showing 15 changed files with 211 additions and 196 deletions.
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "movie_collection_rust"
version = "0.10.52"
version = "0.11.0"
authors = ["Daniel Boline <[email protected]>"]
edition = "2018"

Expand All @@ -14,7 +14,7 @@ Utilities for maintaining a collection of videos."""

[dependencies]
anyhow = "1.0"
clap = {version="4.0", features=["derive"]}
clap = {version="4.5", features=["derive"]}
derive_more = {version="1.0", features=["full"]}
env_logger = "0.11"
futures = "0.3"
Expand All @@ -23,11 +23,11 @@ movie_collection_lib = {path = "movie_collection_lib"}
movie_collection_http = {path = "movie_collection_http"}
refinery = {version="0.8", features=["tokio-postgres"]}
serde_json = "1.0"
stack-string = { git = "https://github.com/ddboline/stack-string-rs.git", features=["postgres_types"], tag="0.9.3" }
stack-string = { git = "https://github.com/ddboline/stack-string-rs.git", features=["postgres_types"], tag="1.0.2" }
stdout-channel = "0.6"
time = {version="0.3", features=["serde-human-readable", "macros", "formatting"]}
time-tz = {version="2.0", features=["system"]}
tokio = {version="1.40", features=["rt", "macros", "rt-multi-thread"]}
tokio = {version="1.41", features=["rt", "macros", "rt-multi-thread"]}
transcode_lib = {path = "transcode_lib"}

[workspace]
Expand Down
2 changes: 2 additions & 0 deletions migrations/V34__authorized_users_created_deleted.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE authorized_users ADD COLUMN created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now();
ALTER TABLE authorized_users ADD COLUMN deleted_at TIMESTAMP WITH TIME ZONE;
10 changes: 5 additions & 5 deletions movie_collection_http/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "movie_collection_http"
version = "0.10.52"
version = "0.11.0"
authors = ["Daniel Boline <[email protected]>"]
edition = "2018"

Expand All @@ -9,7 +9,7 @@ anyhow = "1.0"
async-graphql = {version="7.0", features=["dataloader", "time"]}
async-graphql-warp = "7.0"
async-trait = "0.1"
authorized_users = { git = "https://github.com/ddboline/auth_server_rust.git", tag="0.11.15"}
authorized_users = { git = "https://github.com/ddboline/auth_server_rust.git", tag="0.12.0"}
bytes = "1.0"
derive_more = {version="1.0", features=["full"]}
dioxus = "0.5"
Expand All @@ -33,12 +33,12 @@ rweb = {git = "https://github.com/ddboline/rweb.git", features=["openapi"], defa
rweb-helper = { git = "https://github.com/ddboline/rweb_helper.git", tag="0.5.3" }
serde_yaml = "0.9"
smallvec = {version="1.6", features=["serde", "write"]}
stack-string = { git = "https://github.com/ddboline/stack-string-rs.git", features=["postgres_types", "rweb-openapi", "async_graphql"], tag="0.9.3" }
stack-string = { git = "https://github.com/ddboline/stack-string-rs.git", features=["postgres_types", "rweb-openapi", "async_graphql"], tag="1.0.2" }
stdout-channel = "0.6"
thiserror = "1.0"
thiserror = "2.0"
time = {version="0.3", features=["serde-human-readable", "macros", "formatting"]}
time-tz = {version="2.0", features=["system"]}
tokio = {version="1.40", features=["rt", "macros", "rt-multi-thread"]}
tokio = {version="1.41", features=["rt", "macros", "rt-multi-thread"]}
tokio-stream = "0.1"
uuid = "1.0"
url = "2.3"
2 changes: 1 addition & 1 deletion movie_collection_http/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ derive_rweb_schema!(PlexEventWrapper, _PlexEventWrapper);
#[schema(component = "PlexEvent")]
struct _PlexEventWrapper {
#[schema(description = "ID")]
id: StackString,
id: UuidWrapper,
#[schema(description = "Event")]
event: StackString,
#[schema(description = "Account")]
Expand Down
66 changes: 49 additions & 17 deletions movie_collection_http/src/logged_user.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
pub use authorized_users::{
get_random_key, get_secrets, token::Token, AuthorizedUser, AUTHORIZED_USERS, JWT_SECRET,
KEY_LENGTH, LOGIN_HTML, SECRET_KEY, TRIGGER_DB_UPDATE,
get_random_key, get_secrets, token::Token, AuthorizedUser as ExternalUser, AUTHORIZED_USERS,
JWT_SECRET, KEY_LENGTH, LOGIN_HTML, SECRET_KEY, TRIGGER_DB_UPDATE,
};
use futures::TryStreamExt;
use log::debug;
use maplit::hashset;
use maplit::hashmap;
use reqwest::Client;
use rweb::{filters::cookie::cookie, Filter, Rejection, Schema};
use rweb_helper::UuidWrapper;
use serde::{Deserialize, Serialize};
use stack_string::{format_sstr, StackString};
use std::{
collections::HashMap,
convert::{TryFrom, TryInto},
env::var,
str::FromStr,
};
use time::OffsetDateTime;
use tokio::task::{spawn, JoinHandle};
use url::Url;
use uuid::Uuid;

use movie_collection_lib::{config::Config, pgpool::PgPool, utils::get_authorized_users};
use movie_collection_lib::{config::Config, pgpool::PgPool, utils::AuthorizedUsers};

use crate::errors::ServiceError as Error;

Expand Down Expand Up @@ -68,7 +71,7 @@ impl LoggedUser {
config: &Config,
) -> Result<Option<StackString>, anyhow::Error> {
let base_url: Url = format_sstr!("https://{}", config.domain).parse()?;
let session: Option<SessionData> = AuthorizedUser::get_session_data(
let session: Option<SessionData> = ExternalUser::get_session_data(
&base_url,
self.session.into(),
&self.secret_key,
Expand All @@ -91,7 +94,7 @@ impl LoggedUser {
let session = SessionData {
movie_last_url: set_url.into(),
};
AuthorizedUser::set_session_data(
ExternalUser::set_session_data(
&base_url,
self.session.into(),
&self.secret_key,
Expand All @@ -118,8 +121,8 @@ impl LoggedUser {
}
}

impl From<AuthorizedUser> for LoggedUser {
fn from(user: AuthorizedUser) -> Self {
impl From<ExternalUser> for LoggedUser {
fn from(user: ExternalUser) -> Self {
Self {
email: user.email,
session: user.session.into(),
Expand Down Expand Up @@ -154,17 +157,46 @@ impl FromStr for LoggedUser {
/// # Errors
/// Returns error if `get_authorized_users` fails
pub async fn fill_from_db(pool: &PgPool) -> Result<(), Error> {
debug!("{:?}", *TRIGGER_DB_UPDATE);
let users = if TRIGGER_DB_UPDATE.check() {
get_authorized_users(pool).await?
} else {
AUTHORIZED_USERS.get_users()
};
if let Ok("true") = var("TESTENV").as_ref().map(String::as_str) {
AUTHORIZED_USERS.update_users(hashset! {"user@test".into()});
AUTHORIZED_USERS.update_users(hashmap! {
"user@test".into() => ExternalUser {
email: "user@test".into(),
session: Uuid::new_v4(),
secret_key: StackString::default(),
created_at: Some(OffsetDateTime::now_utc())
}
});
return Ok(());
}
let (created_at, deleted_at) = AuthorizedUsers::get_most_recent(pool).await?;
let most_recent_user_db = created_at.max(deleted_at);
let existing_users = AUTHORIZED_USERS.get_users();
let most_recent_user = existing_users.values().map(|i| i.created_at).max();
debug!("most_recent_user_db {most_recent_user_db:?} most_recent_user {most_recent_user:?}");
if most_recent_user_db.is_some()
&& most_recent_user.is_some()
&& most_recent_user_db <= most_recent_user
{
return Ok(());
}
AUTHORIZED_USERS.update_users(users);

debug!("{:?}", *AUTHORIZED_USERS);
let result: Result<HashMap<StackString, _>, _> = AuthorizedUsers::get_authorized_users(pool)
.await?
.map_ok(|u| {
(
u.email.clone(),
ExternalUser {
email: u.email,
session: Uuid::new_v4(),
secret_key: StackString::default(),
created_at: Some(u.created_at),
},
)
})
.try_collect()
.await;
let users = result?;
AUTHORIZED_USERS.update_users(users);
debug!("AUTHORIZED_USERS {:?}", *AUTHORIZED_USERS);
Ok(())
}
Loading

0 comments on commit 4a4f0f2

Please sign in to comment.