diff --git a/src/activitypub/actors/helpers.rs b/src/activitypub/actors/helpers.rs index e7547f9..b37b1f1 100644 --- a/src/activitypub/actors/helpers.rs +++ b/src/activitypub/actors/helpers.rs @@ -1,7 +1,6 @@ use std::path::Path; use serde_json::{Value as JsonValue}; -use tokio_postgres::GenericClient; use crate::activitypub::{ actors::types::Actor, @@ -9,6 +8,7 @@ use crate::activitypub::{ receiver::{parse_property_value, HandlerError}, }; use crate::config::Instance; +use crate::database::DatabaseClient; use crate::models::profiles::{ queries::{create_profile, update_profile}, types::{ @@ -90,7 +90,7 @@ fn parse_tags(actor: &Actor) -> () { } pub async fn create_remote_profile( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, instance: &Instance, media_dir: &Path, actor: Actor, @@ -128,7 +128,7 @@ pub async fn create_remote_profile( /// Updates remote actor's profile pub async fn update_remote_profile( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, instance: &Instance, media_dir: &Path, profile: DbActorProfile, diff --git a/src/activitypub/authentication.rs b/src/activitypub/authentication.rs index 444ec87..5aec0c3 100644 --- a/src/activitypub/authentication.rs +++ b/src/activitypub/authentication.rs @@ -1,9 +1,8 @@ use actix_web::HttpRequest; use serde_json::Value; -use tokio_postgres::GenericClient; use crate::config::Config; -use crate::database::DatabaseError; +use crate::database::{DatabaseClient, DatabaseError}; use crate::http_signatures::verify::{ parse_http_signature, verify_http_signature, @@ -71,7 +70,7 @@ fn key_id_to_actor_id(key_id: &str) -> Result { async fn get_signer( config: &Config, - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, signer_id: &str, no_fetch: bool, ) -> Result { @@ -98,7 +97,7 @@ async fn get_signer( /// Verifies HTTP signature and returns signer pub async fn verify_signed_request( config: &Config, - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, request: &HttpRequest, no_fetch: bool, ) -> Result { @@ -129,7 +128,7 @@ pub async fn verify_signed_request( /// Verifies JSON signature and returns signer pub async fn verify_signed_activity( config: &Config, - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, activity: &Value, no_fetch: bool, ) -> Result { diff --git a/src/activitypub/builders/announce.rs b/src/activitypub/builders/announce.rs index c1d4bea..83ffec1 100644 --- a/src/activitypub/builders/announce.rs +++ b/src/activitypub/builders/announce.rs @@ -1,6 +1,5 @@ use chrono::{DateTime, Utc}; use serde::Serialize; -use tokio_postgres::GenericClient; use crate::activitypub::{ actors::types::Actor, @@ -10,7 +9,7 @@ use crate::activitypub::{ vocabulary::ANNOUNCE, }; use crate::config::Instance; -use crate::database::DatabaseError; +use crate::database::{DatabaseClient, DatabaseError}; use crate::models::posts::types::Post; use crate::models::relationships::queries::get_followers; use crate::models::users::types::User; @@ -56,7 +55,7 @@ fn build_announce( } pub async fn get_announce_recipients( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, instance_url: &str, current_user: &User, post: &Post, @@ -76,7 +75,7 @@ pub async fn get_announce_recipients( } pub async fn prepare_announce( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, instance: &Instance, sender: &User, repost: &Post, diff --git a/src/activitypub/builders/create_note.rs b/src/activitypub/builders/create_note.rs index 9e9de94..2a7c166 100644 --- a/src/activitypub/builders/create_note.rs +++ b/src/activitypub/builders/create_note.rs @@ -1,6 +1,5 @@ use chrono::{DateTime, Utc}; use serde::Serialize; -use tokio_postgres::GenericClient; use crate::activitypub::{ actors::types::Actor, @@ -16,7 +15,7 @@ use crate::activitypub::{ vocabulary::{CREATE, DOCUMENT, HASHTAG, LINK, MENTION, NOTE}, }; use crate::config::Instance; -use crate::database::DatabaseError; +use crate::database::{DatabaseClient, DatabaseError}; use crate::models::posts::queries::get_post_author; use crate::models::posts::types::{Post, Visibility}; use crate::models::relationships::queries::{get_followers, get_subscribers}; @@ -206,7 +205,7 @@ pub fn build_create_note( } pub async fn get_note_recipients( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, current_user: &User, post: &Post, ) -> Result, DatabaseError> { @@ -239,7 +238,7 @@ pub async fn get_note_recipients( } pub async fn prepare_create_note( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, instance: &Instance, author: &User, post: &Post, diff --git a/src/activitypub/builders/delete_note.rs b/src/activitypub/builders/delete_note.rs index f4aad6d..8500c13 100644 --- a/src/activitypub/builders/delete_note.rs +++ b/src/activitypub/builders/delete_note.rs @@ -1,5 +1,4 @@ use serde::Serialize; -use tokio_postgres::GenericClient; use crate::activitypub::{ constants::AP_CONTEXT, @@ -8,7 +7,7 @@ use crate::activitypub::{ vocabulary::{DELETE, NOTE, TOMBSTONE}, }; use crate::config::Instance; -use crate::database::DatabaseError; +use crate::database::{DatabaseClient, DatabaseError}; use crate::models::posts::helpers::add_related_posts; use crate::models::posts::types::Post; use crate::models::users::types::User; @@ -74,7 +73,7 @@ fn build_delete_note( } pub async fn prepare_delete_note( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, instance: &Instance, author: &User, post: &Post, diff --git a/src/activitypub/builders/delete_person.rs b/src/activitypub/builders/delete_person.rs index 130cbb7..a2f7b40 100644 --- a/src/activitypub/builders/delete_person.rs +++ b/src/activitypub/builders/delete_person.rs @@ -1,5 +1,4 @@ use serde::Serialize; -use tokio_postgres::GenericClient; use uuid::Uuid; use crate::activitypub::{ @@ -9,7 +8,7 @@ use crate::activitypub::{ vocabulary::DELETE, }; use crate::config::Instance; -use crate::database::DatabaseError; +use crate::database::{DatabaseClient, DatabaseError}; use crate::models::relationships::queries::{get_followers, get_following}; use crate::models::users::types::User; @@ -45,7 +44,7 @@ fn build_delete_person( } async fn get_delete_person_recipients( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, user_id: &Uuid, ) -> Result, DatabaseError> { let followers = get_followers(db_client, user_id).await?; @@ -60,7 +59,7 @@ async fn get_delete_person_recipients( } pub async fn prepare_delete_person( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, instance: &Instance, user: &User, ) -> Result { diff --git a/src/activitypub/builders/like.rs b/src/activitypub/builders/like.rs index 9a7bcf2..cd1f179 100644 --- a/src/activitypub/builders/like.rs +++ b/src/activitypub/builders/like.rs @@ -1,5 +1,4 @@ use serde::Serialize; -use tokio_postgres::GenericClient; use uuid::Uuid; use crate::activitypub::{ @@ -10,7 +9,7 @@ use crate::activitypub::{ vocabulary::LIKE, }; use crate::config::Instance; -use crate::database::DatabaseError; +use crate::database::{DatabaseClient, DatabaseError}; use crate::models::posts::types::{Post, Visibility}; use crate::models::profiles::types::DbActorProfile; use crate::models::users::types::User; @@ -67,7 +66,7 @@ fn build_like( } pub async fn get_like_recipients( - _db_client: &impl GenericClient, + _db_client: &impl DatabaseClient, _instance_url: &str, post: &Post, ) -> Result, DatabaseError> { @@ -79,7 +78,7 @@ pub async fn get_like_recipients( } pub async fn prepare_like( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, instance: &Instance, sender: &User, post: &Post, diff --git a/src/activitypub/builders/undo_announce.rs b/src/activitypub/builders/undo_announce.rs index ddf67a4..0de1963 100644 --- a/src/activitypub/builders/undo_announce.rs +++ b/src/activitypub/builders/undo_announce.rs @@ -1,5 +1,4 @@ use serde::Serialize; -use tokio_postgres::GenericClient; use uuid::Uuid; use crate::activitypub::{ @@ -9,7 +8,7 @@ use crate::activitypub::{ vocabulary::UNDO, }; use crate::config::Instance; -use crate::database::DatabaseError; +use crate::database::{DatabaseClient, DatabaseError}; use crate::models::posts::types::Post; use crate::models::profiles::types::DbActorProfile; use crate::models::users::types::User; @@ -59,7 +58,7 @@ fn build_undo_announce( } pub async fn prepare_undo_announce( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, instance: &Instance, sender: &User, post: &Post, diff --git a/src/activitypub/builders/undo_like.rs b/src/activitypub/builders/undo_like.rs index 9a51196..8dca1fa 100644 --- a/src/activitypub/builders/undo_like.rs +++ b/src/activitypub/builders/undo_like.rs @@ -1,5 +1,4 @@ use serde::Serialize; -use tokio_postgres::GenericClient; use uuid::Uuid; use crate::activitypub::{ @@ -9,7 +8,7 @@ use crate::activitypub::{ vocabulary::UNDO, }; use crate::config::Instance; -use crate::database::DatabaseError; +use crate::database::{DatabaseClient, DatabaseError}; use crate::models::posts::types::{Post, Visibility}; use crate::models::profiles::types::DbActorProfile; use crate::models::users::types::User; @@ -58,7 +57,7 @@ fn build_undo_like( } pub async fn prepare_undo_like( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, instance: &Instance, sender: &User, post: &Post, diff --git a/src/activitypub/builders/update_person.rs b/src/activitypub/builders/update_person.rs index 2e97cee..e9b4ac3 100644 --- a/src/activitypub/builders/update_person.rs +++ b/src/activitypub/builders/update_person.rs @@ -1,5 +1,4 @@ use serde::Serialize; -use tokio_postgres::GenericClient; use uuid::Uuid; use crate::activitypub::{ @@ -10,7 +9,7 @@ use crate::activitypub::{ vocabulary::UPDATE, }; use crate::config::Instance; -use crate::database::{DatabaseError, DatabaseTypeError}; +use crate::database::{DatabaseClient, DatabaseError, DatabaseTypeError}; use crate::models::relationships::queries::get_followers; use crate::models::users::types::User; use crate::utils::id::new_uuid; @@ -55,7 +54,7 @@ pub fn build_update_person( } async fn get_update_person_recipients( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, user_id: &Uuid, ) -> Result, DatabaseError> { let followers = get_followers(db_client, user_id).await?; @@ -69,7 +68,7 @@ async fn get_update_person_recipients( } pub async fn prepare_update_person( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, instance: &Instance, user: &User, maybe_internal_activity_id: Option, diff --git a/src/activitypub/deliverer.rs b/src/activitypub/deliverer.rs index 391904a..2f0c763 100644 --- a/src/activitypub/deliverer.rs +++ b/src/activitypub/deliverer.rs @@ -7,10 +7,14 @@ use rsa::RsaPrivateKey; use serde::{Deserialize, Serialize}; use serde_json::Value; use tokio::time::sleep; -use tokio_postgres::GenericClient; use crate::config::Instance; -use crate::database::{get_database_client, DatabaseError, DbPool}; +use crate::database::{ + get_database_client, + DatabaseClient, + DatabaseError, + DbPool, +}; use crate::http_signatures::create::{ create_http_signature, HttpSignatureError, @@ -265,7 +269,7 @@ impl OutgoingActivity { pub async fn enqueue( self, - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, ) -> Result<(), DatabaseError> { let job_data = OutgoingActivityJobData { activity: self.activity, diff --git a/src/activitypub/fetcher/helpers.rs b/src/activitypub/fetcher/helpers.rs index b53abc3..051d0f5 100644 --- a/src/activitypub/fetcher/helpers.rs +++ b/src/activitypub/fetcher/helpers.rs @@ -1,8 +1,6 @@ use std::collections::HashMap; use std::path::Path; -use tokio_postgres::GenericClient; - use crate::activitypub::{ actors::helpers::{create_remote_profile, update_remote_profile}, handlers::create::handle_note, @@ -11,7 +9,7 @@ use crate::activitypub::{ types::Object, }; use crate::config::{Config, Instance}; -use crate::database::DatabaseError; +use crate::database::{DatabaseClient, DatabaseError}; use crate::errors::ValidationError; use crate::models::posts::helpers::get_local_post_by_id; use crate::models::posts::queries::get_post_by_remote_object_id; @@ -29,7 +27,7 @@ use super::fetchers::{ }; pub async fn get_or_import_profile_by_actor_id( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, instance: &Instance, media_dir: &Path, actor_id: &str, @@ -105,7 +103,7 @@ pub async fn get_or_import_profile_by_actor_id( /// Fetches actor profile and saves it into database pub async fn import_profile_by_actor_address( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, instance: &Instance, media_dir: &Path, actor_address: &ActorAddress, @@ -135,7 +133,7 @@ pub async fn import_profile_by_actor_address( } pub async fn get_or_import_profile_by_actor_address( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, instance: &Instance, media_dir: &Path, actor_address: &ActorAddress, @@ -164,7 +162,7 @@ pub async fn get_or_import_profile_by_actor_address( pub async fn import_post( config: &Config, - db_client: &mut impl GenericClient, + db_client: &mut impl DatabaseClient, object_id: String, object_received: Option, ) -> Result { diff --git a/src/activitypub/handlers/accept.rs b/src/activitypub/handlers/accept.rs index d1d4e77..0326019 100644 --- a/src/activitypub/handlers/accept.rs +++ b/src/activitypub/handlers/accept.rs @@ -1,6 +1,5 @@ use serde::Deserialize; use serde_json::Value; -use tokio_postgres::GenericClient; use crate::activitypub::{ identifiers::parse_local_object_id, @@ -8,6 +7,7 @@ use crate::activitypub::{ vocabulary::FOLLOW, }; use crate::config::Config; +use crate::database::DatabaseClient; use crate::errors::ValidationError; use crate::models::profiles::queries::get_profile_by_remote_actor_id; use crate::models::relationships::queries::{ @@ -26,7 +26,7 @@ struct Accept { pub async fn handle_accept( config: &Config, - db_client: &mut impl GenericClient, + db_client: &mut impl DatabaseClient, activity: Value, ) -> HandlerResult { // Accept(Follow) diff --git a/src/activitypub/handlers/add.rs b/src/activitypub/handlers/add.rs index 2f1119c..eb11ce1 100644 --- a/src/activitypub/handlers/add.rs +++ b/src/activitypub/handlers/add.rs @@ -1,12 +1,12 @@ use serde::Deserialize; use serde_json::Value; -use tokio_postgres::GenericClient; use crate::activitypub::{ identifiers::parse_local_actor_id, vocabulary::PERSON, }; use crate::config::Config; +use crate::database::DatabaseClient; use crate::errors::ValidationError; use crate::models::profiles::queries::get_profile_by_remote_actor_id; use crate::models::relationships::queries::subscribe_opt; @@ -22,7 +22,7 @@ struct Add { pub async fn handle_add( config: &Config, - db_client: &mut impl GenericClient, + db_client: &mut impl DatabaseClient, activity: Value, ) -> HandlerResult { let activity: Add = serde_json::from_value(activity) diff --git a/src/activitypub/handlers/announce.rs b/src/activitypub/handlers/announce.rs index 4cde153..15ed5dc 100644 --- a/src/activitypub/handlers/announce.rs +++ b/src/activitypub/handlers/announce.rs @@ -1,6 +1,5 @@ use serde::Deserialize; use serde_json::Value; -use tokio_postgres::GenericClient; use crate::activitypub::{ fetcher::helpers::{get_or_import_profile_by_actor_id, import_post}, @@ -9,7 +8,7 @@ use crate::activitypub::{ vocabulary::{CREATE, DISLIKE, LIKE, NOTE, UNDO, UPDATE}, }; use crate::config::Config; -use crate::database::DatabaseError; +use crate::database::{DatabaseClient, DatabaseError}; use crate::errors::ValidationError; use crate::models::posts::queries::{ create_post, @@ -28,7 +27,7 @@ struct Announce { pub async fn handle_announce( config: &Config, - db_client: &mut impl GenericClient, + db_client: &mut impl DatabaseClient, activity: Value, ) -> HandlerResult { if let Some(CREATE | DISLIKE | LIKE | UNDO | UPDATE) = diff --git a/src/activitypub/handlers/create.rs b/src/activitypub/handlers/create.rs index 6de5b01..77bb8a8 100644 --- a/src/activitypub/handlers/create.rs +++ b/src/activitypub/handlers/create.rs @@ -3,7 +3,6 @@ use std::path::Path; use chrono::Utc; use serde_json::{Value as JsonValue}; -use tokio_postgres::GenericClient; use uuid::Uuid; use crate::activitypub::{ @@ -20,7 +19,7 @@ use crate::activitypub::{ vocabulary::*, }; use crate::config::{Config, Instance}; -use crate::database::DatabaseError; +use crate::database::{DatabaseClient, DatabaseError}; use crate::errors::{ConversionError, ValidationError}; use crate::models::attachments::queries::create_attachment; use crate::models::emojis::queries::{ @@ -158,7 +157,7 @@ fn is_gnu_social_link(author_id: &str, attachment: &Attachment) -> bool { } pub async fn handle_note( - db_client: &mut impl GenericClient, + db_client: &mut impl DatabaseClient, instance: &Instance, media_dir: &Path, object: Object, @@ -524,7 +523,7 @@ pub async fn handle_note( pub async fn handle_create( config: &Config, - db_client: &mut impl GenericClient, + db_client: &mut impl DatabaseClient, activity: JsonValue, is_authenticated: bool, ) -> HandlerResult { diff --git a/src/activitypub/handlers/delete.rs b/src/activitypub/handlers/delete.rs index 03ce585..cb27a63 100644 --- a/src/activitypub/handlers/delete.rs +++ b/src/activitypub/handlers/delete.rs @@ -1,13 +1,12 @@ use serde::Deserialize; use serde_json::Value; -use tokio_postgres::GenericClient; use crate::activitypub::{ receiver::deserialize_into_object_id, vocabulary::{NOTE, PERSON}, }; use crate::config::Config; -use crate::database::DatabaseError; +use crate::database::{DatabaseClient, DatabaseError}; use crate::errors::ValidationError; use crate::models::posts::queries::{ delete_post, @@ -28,7 +27,7 @@ struct Delete { pub async fn handle_delete( config: &Config, - db_client: &mut impl GenericClient, + db_client: &mut impl DatabaseClient, activity: Value, ) -> HandlerResult { let activity: Delete = serde_json::from_value(activity) diff --git a/src/activitypub/handlers/follow.rs b/src/activitypub/handlers/follow.rs index 9b281e3..c15f4b5 100644 --- a/src/activitypub/handlers/follow.rs +++ b/src/activitypub/handlers/follow.rs @@ -1,6 +1,5 @@ use serde::Deserialize; use serde_json::Value; -use tokio_postgres::GenericClient; use crate::activitypub::{ builders::accept_follow::prepare_accept_follow, @@ -10,7 +9,7 @@ use crate::activitypub::{ vocabulary::PERSON, }; use crate::config::Config; -use crate::database::DatabaseError; +use crate::database::{DatabaseClient, DatabaseError}; use crate::errors::ValidationError; use crate::models::{ relationships::queries::{ @@ -31,7 +30,7 @@ struct Follow { pub async fn handle_follow( config: &Config, - db_client: &mut impl GenericClient, + db_client: &mut impl DatabaseClient, activity: Value, ) -> HandlerResult { // Follow(Person) diff --git a/src/activitypub/handlers/like.rs b/src/activitypub/handlers/like.rs index 5f9c4bd..d6e22b0 100644 --- a/src/activitypub/handlers/like.rs +++ b/src/activitypub/handlers/like.rs @@ -1,6 +1,5 @@ use serde::Deserialize; use serde_json::Value; -use tokio_postgres::GenericClient; use crate::activitypub::{ fetcher::helpers::get_or_import_profile_by_actor_id, @@ -9,7 +8,7 @@ use crate::activitypub::{ vocabulary::NOTE, }; use crate::config::Config; -use crate::database::DatabaseError; +use crate::database::{DatabaseClient, DatabaseError}; use crate::errors::ValidationError; use crate::models::reactions::queries::create_reaction; use crate::models::posts::queries::get_post_by_remote_object_id; @@ -25,7 +24,7 @@ struct Like { pub async fn handle_like( config: &Config, - db_client: &mut impl GenericClient, + db_client: &mut impl DatabaseClient, activity: Value, ) -> HandlerResult { let activity: Like = serde_json::from_value(activity) diff --git a/src/activitypub/handlers/move.rs b/src/activitypub/handlers/move.rs index 74f87be..528e1b6 100644 --- a/src/activitypub/handlers/move.rs +++ b/src/activitypub/handlers/move.rs @@ -1,6 +1,5 @@ use serde::Deserialize; use serde_json::Value; -use tokio_postgres::GenericClient; use crate::activitypub::{ builders::{ @@ -13,7 +12,7 @@ use crate::activitypub::{ vocabulary::PERSON, }; use crate::config::Config; -use crate::database::DatabaseError; +use crate::database::{DatabaseClient, DatabaseError}; use crate::errors::ValidationError; use crate::models::{ notifications::queries::create_move_notification, @@ -36,7 +35,7 @@ struct Move { pub async fn handle_move( config: &Config, - db_client: &mut impl GenericClient, + db_client: &mut impl DatabaseClient, activity: Value, ) -> HandlerResult { // Move(Person) diff --git a/src/activitypub/handlers/reject.rs b/src/activitypub/handlers/reject.rs index b48c56d..cc41dd6 100644 --- a/src/activitypub/handlers/reject.rs +++ b/src/activitypub/handlers/reject.rs @@ -1,6 +1,5 @@ use serde::Deserialize; use serde_json::Value; -use tokio_postgres::GenericClient; use crate::activitypub::{ identifiers::parse_local_object_id, @@ -8,6 +7,7 @@ use crate::activitypub::{ vocabulary::FOLLOW, }; use crate::config::Config; +use crate::database::DatabaseClient; use crate::errors::ValidationError; use crate::models::profiles::queries::get_profile_by_remote_actor_id; use crate::models::relationships::queries::{ @@ -26,7 +26,7 @@ struct Reject { pub async fn handle_reject( config: &Config, - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, activity: Value, ) -> HandlerResult { // Reject(Follow) diff --git a/src/activitypub/handlers/remove.rs b/src/activitypub/handlers/remove.rs index 39e7ca6..f712525 100644 --- a/src/activitypub/handlers/remove.rs +++ b/src/activitypub/handlers/remove.rs @@ -1,13 +1,12 @@ use serde::Deserialize; use serde_json::Value; -use tokio_postgres::GenericClient; use crate::activitypub::{ identifiers::parse_local_actor_id, vocabulary::PERSON, }; use crate::config::Config; -use crate::database::DatabaseError; +use crate::database::{DatabaseClient, DatabaseError}; use crate::errors::ValidationError; use crate::models::notifications::queries::{ create_subscription_expiration_notification, @@ -26,7 +25,7 @@ struct Remove { pub async fn handle_remove( config: &Config, - db_client: &mut impl GenericClient, + db_client: &mut impl DatabaseClient, activity: Value, ) -> HandlerResult { let activity: Remove = serde_json::from_value(activity) diff --git a/src/activitypub/handlers/undo.rs b/src/activitypub/handlers/undo.rs index 56dd553..a97d056 100644 --- a/src/activitypub/handlers/undo.rs +++ b/src/activitypub/handlers/undo.rs @@ -1,6 +1,5 @@ use serde::Deserialize; use serde_json::Value; -use tokio_postgres::GenericClient; use crate::activitypub::{ identifiers::parse_local_actor_id, @@ -8,7 +7,7 @@ use crate::activitypub::{ vocabulary::{ANNOUNCE, FOLLOW, LIKE}, }; use crate::config::Config; -use crate::database::DatabaseError; +use crate::database::{DatabaseClient, DatabaseError}; use crate::errors::ValidationError; use crate::models::{ posts::queries::{ @@ -38,7 +37,7 @@ struct UndoFollow { async fn handle_undo_follow( config: &Config, - db_client: &mut impl GenericClient, + db_client: &mut impl DatabaseClient, activity: Value, ) -> HandlerResult { let activity: UndoFollow = serde_json::from_value(activity) @@ -72,7 +71,7 @@ struct Undo { pub async fn handle_undo( config: &Config, - db_client: &mut impl GenericClient, + db_client: &mut impl DatabaseClient, activity: Value, ) -> HandlerResult { if let Some(FOLLOW) = activity["object"]["type"].as_str() { diff --git a/src/activitypub/handlers/update.rs b/src/activitypub/handlers/update.rs index e877ca2..c7d8ed0 100644 --- a/src/activitypub/handlers/update.rs +++ b/src/activitypub/handlers/update.rs @@ -1,7 +1,6 @@ use chrono::Utc; use serde::Deserialize; use serde_json::Value; -use tokio_postgres::GenericClient; use crate::activitypub::{ actors::{ @@ -13,7 +12,7 @@ use crate::activitypub::{ vocabulary::{NOTE, PERSON}, }; use crate::config::Config; -use crate::database::DatabaseError; +use crate::database::{DatabaseClient, DatabaseError}; use crate::errors::ValidationError; use crate::models::{ posts::queries::{ @@ -26,7 +25,7 @@ use crate::models::{ use super::HandlerResult; async fn handle_update_note( - db_client: &mut impl GenericClient, + db_client: &mut impl DatabaseClient, activity: Value, ) -> HandlerResult { let object: Object = serde_json::from_value(activity["object"].to_owned()) @@ -55,7 +54,7 @@ struct UpdatePerson { async fn handle_update_person( config: &Config, - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, activity: Value, ) -> HandlerResult { let activity: UpdatePerson = serde_json::from_value(activity) @@ -79,7 +78,7 @@ async fn handle_update_person( pub async fn handle_update( config: &Config, - db_client: &mut impl GenericClient, + db_client: &mut impl DatabaseClient, activity: Value, ) -> HandlerResult { let object_type = activity["object"]["type"].as_str() diff --git a/src/activitypub/queues.rs b/src/activitypub/queues.rs index 9a5aa25..bd60886 100644 --- a/src/activitypub/queues.rs +++ b/src/activitypub/queues.rs @@ -1,12 +1,12 @@ use chrono::{Duration, Utc}; use serde::{Deserialize, Serialize}; use serde_json::Value; -use tokio_postgres::GenericClient; use uuid::Uuid; use crate::config::Config; use crate::database::{ get_database_client, + DatabaseClient, DatabaseError, DatabaseTypeError, DbPool, @@ -41,7 +41,7 @@ impl IncomingActivityJobData { pub async fn into_job( self, - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, delay: i64, ) -> Result<(), DatabaseError> { let job_data = serde_json::to_value(self) @@ -58,7 +58,7 @@ impl IncomingActivityJobData { pub async fn process_queued_incoming_activities( config: &Config, - db_client: &mut impl GenericClient, + db_client: &mut impl DatabaseClient, ) -> Result<(), DatabaseError> { let batch_size = 10; let max_retries = 2; @@ -111,7 +111,7 @@ pub struct OutgoingActivityJobData { impl OutgoingActivityJobData { pub async fn into_job( self, - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, ) -> Result<(), DatabaseError> { let job_data = serde_json::to_value(self) .expect("activity should be serializable"); diff --git a/src/activitypub/receiver.rs b/src/activitypub/receiver.rs index 2135d3e..3c05c8b 100644 --- a/src/activitypub/receiver.rs +++ b/src/activitypub/receiver.rs @@ -6,10 +6,9 @@ use serde::{ de::Error as DeserializerError, }; use serde_json::Value; -use tokio_postgres::GenericClient; use crate::config::Config; -use crate::database::DatabaseError; +use crate::database::{DatabaseClient, DatabaseError}; use crate::errors::{ ConversionError, HttpError, @@ -145,7 +144,7 @@ pub fn deserialize_into_object_id<'de, D>( pub async fn handle_activity( config: &Config, - db_client: &mut impl GenericClient, + db_client: &mut impl DatabaseClient, activity: &Value, is_authenticated: bool, ) -> Result<(), HandlerError> { @@ -211,7 +210,7 @@ pub async fn handle_activity( pub async fn receive_activity( config: &Config, - db_client: &mut impl GenericClient, + db_client: &mut impl DatabaseClient, request: &HttpRequest, activity: &Value, ) -> Result<(), HandlerError> { diff --git a/src/cli.rs b/src/cli.rs index 09f2064..545bd77 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,7 +1,6 @@ use anyhow::{anyhow, Error}; use chrono::{Duration, Utc}; use clap::Parser; -use tokio_postgres::GenericClient; use uuid::Uuid; use crate::activitypub::{ @@ -11,6 +10,7 @@ use crate::activitypub::{ fetcher::fetchers::fetch_actor, }; use crate::config::Config; +use crate::database::DatabaseClient; use crate::ethereum::signatures::generate_ecdsa_key; use crate::ethereum::sync::save_current_block_number; use crate::ethereum::utils::key_to_ethereum_address; @@ -108,7 +108,7 @@ pub struct GenerateInviteCode; impl GenerateInviteCode { pub async fn execute( &self, - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, ) -> Result<(), Error> { let invite_code = create_invite_code(db_client).await?; println!("generated invite code: {}", invite_code); @@ -123,7 +123,7 @@ pub struct ListInviteCodes; impl ListInviteCodes { pub async fn execute( &self, - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, ) -> Result<(), Error> { let invite_codes = get_invite_codes(db_client).await?; if invite_codes.is_empty() { @@ -147,7 +147,7 @@ pub struct SetPassword { impl SetPassword { pub async fn execute( &self, - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, ) -> Result<(), Error> { let password_hash = hash_password(&self.password)?; set_user_password(db_client, &self.id, password_hash).await?; @@ -168,7 +168,7 @@ impl RefetchActor { pub async fn execute( &self, config: &Config, - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, ) -> Result<(), Error> { let profile = get_profile_by_remote_actor_id( db_client, @@ -197,7 +197,7 @@ impl DeleteProfile { pub async fn execute( &self, config: &Config, - db_client: &mut impl GenericClient, + db_client: &mut impl DatabaseClient, ) -> Result<(), Error> { let profile = get_profile_by_id(db_client, &self.id).await?; let mut maybe_delete_person = None; @@ -228,7 +228,7 @@ impl DeletePost { pub async fn execute( &self, config: &Config, - db_client: &mut impl GenericClient, + db_client: &mut impl DatabaseClient, ) -> Result<(), Error> { let post = get_post_by_id(db_client, &self.id).await?; let mut maybe_delete_note = None; @@ -263,7 +263,7 @@ impl DeleteEmoji { pub async fn execute( &self, config: &Config, - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, ) -> Result<(), Error> { let deletion_queue = delete_emoji(db_client, &self.id).await?; deletion_queue.process(config).await; @@ -282,7 +282,7 @@ impl DeleteExtraneousPosts { pub async fn execute( &self, config: &Config, - db_client: &mut impl GenericClient, + db_client: &mut impl DatabaseClient, ) -> Result<(), Error> { let updated_before = Utc::now() - Duration::days(self.days); let posts = find_extraneous_posts(db_client, &updated_before).await?; @@ -305,7 +305,7 @@ impl DeleteUnusedAttachments { pub async fn execute( &self, config: &Config, - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, ) -> Result<(), Error> { let created_before = Utc::now() - Duration::days(self.days); let deletion_queue = delete_unused_attachments( @@ -326,7 +326,7 @@ impl DeleteOrphanedFiles { pub async fn execute( &self, config: &Config, - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, ) -> Result<(), Error> { let media_dir = config.media_dir(); let mut files = vec![]; @@ -355,7 +355,7 @@ impl DeleteEmptyProfiles { pub async fn execute( &self, config: &Config, - db_client: &mut impl GenericClient, + db_client: &mut impl DatabaseClient, ) -> Result<(), Error> { let updated_before = Utc::now() - Duration::days(self.days); let profiles = find_empty_profiles(db_client, &updated_before).await?; @@ -379,7 +379,7 @@ impl UpdateCurrentBlock { pub async fn execute( &self, config: &Config, - _db_client: &impl GenericClient, + _db_client: &impl DatabaseClient, ) -> Result<(), Error> { save_current_block_number(&config.storage_dir, self.number)?; println!("current block updated"); @@ -399,7 +399,7 @@ impl ResetSubscriptions { pub async fn execute( &self, _config: &Config, - db_client: &mut impl GenericClient, + db_client: &mut impl DatabaseClient, ) -> Result<(), Error> { reset_subscriptions(db_client, self.ethereum_contract_replaced).await?; println!("subscriptions deleted"); @@ -443,7 +443,7 @@ impl CheckExpiredInvoice { pub async fn execute( &self, config: &Config, - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, ) -> Result<(), Error> { let monero_config = config.blockchain() .and_then(|conf| conf.monero_config()) diff --git a/src/database/mod.rs b/src/database/mod.rs index e848d57..fb38080 100644 --- a/src/database/mod.rs +++ b/src/database/mod.rs @@ -1,4 +1,4 @@ -use tokio_postgres::config::{Config as DbConfig}; +use tokio_postgres::config::{Config as DatabaseConfig}; use tokio_postgres::error::{Error as PgError, SqlState}; pub mod int_enum; @@ -10,6 +10,7 @@ pub mod query_macro; pub mod test_utils; pub type DbPool = deadpool_postgres::Pool; +pub use tokio_postgres::{GenericClient as DatabaseClient}; #[derive(thiserror::Error, Debug)] #[error("database type error")] @@ -36,7 +37,9 @@ pub enum DatabaseError { AlreadyExists(&'static str), // object type } -pub async fn create_database_client(db_config: &DbConfig) -> tokio_postgres::Client { +pub async fn create_database_client(db_config: &DatabaseConfig) + -> tokio_postgres::Client +{ let (client, connection) = db_config.connect(tokio_postgres::NoTls) .await.unwrap(); tokio::spawn(async move { @@ -73,8 +76,8 @@ pub fn catch_unique_violation( if let Some(code) = err.code() { if code == &SqlState::UNIQUE_VIOLATION { return DatabaseError::AlreadyExists(object_type); - } - } + }; + }; err.into() } } diff --git a/src/ethereum/subscriptions.rs b/src/ethereum/subscriptions.rs index 973d59a..cbf9183 100644 --- a/src/ethereum/subscriptions.rs +++ b/src/ethereum/subscriptions.rs @@ -1,7 +1,6 @@ use std::convert::TryInto; use chrono::{DateTime, TimeZone, Utc}; -use tokio_postgres::GenericClient; use web3::{ api::Web3, @@ -17,7 +16,12 @@ use crate::activitypub::builders::{ }; use crate::activitypub::identifiers::LocalActorCollection; use crate::config::{EthereumConfig, Instance}; -use crate::database::{get_database_client, DatabaseError, DbPool}; +use crate::database::{ + get_database_client, + DatabaseClient, + DatabaseError, + DbPool, +}; use crate::errors::ConversionError; use crate::models::notifications::queries::{ create_subscription_notification, @@ -64,7 +68,7 @@ fn u256_to_date(value: U256) -> Result, ConversionError> { } pub async fn send_subscription_notifications( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, instance: &Instance, sender: &DbActorProfile, recipient: &User, diff --git a/src/mastodon_api/accounts/helpers.rs b/src/mastodon_api/accounts/helpers.rs index f4c3d60..cdb152c 100644 --- a/src/mastodon_api/accounts/helpers.rs +++ b/src/mastodon_api/accounts/helpers.rs @@ -1,9 +1,8 @@ -use tokio_postgres::GenericClient; use uuid::Uuid; use crate::activitypub::builders::follow::prepare_follow; use crate::config::Instance; -use crate::database::DatabaseError; +use crate::database::{DatabaseClient, DatabaseError}; use crate::models::{ profiles::types::DbActorProfile, relationships::queries::{ @@ -17,7 +16,7 @@ use crate::models::{ use super::types::RelationshipMap; pub async fn follow_or_create_request( - db_client: &mut impl GenericClient, + db_client: &mut impl DatabaseClient, instance: &Instance, current_user: &User, target_profile: &DbActorProfile, @@ -51,7 +50,7 @@ pub async fn follow_or_create_request( } pub async fn get_relationship( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, source_id: &Uuid, target_id: &Uuid, ) -> Result { @@ -112,7 +111,7 @@ mod tests { use crate::models::users::types::{User, UserCreateData}; use super::*; - async fn create_users(db_client: &mut impl GenericClient) + async fn create_users(db_client: &mut impl DatabaseClient) -> Result<(User, User), DatabaseError> { let user_data_1 = UserCreateData { diff --git a/src/mastodon_api/oauth/auth.rs b/src/mastodon_api/oauth/auth.rs index 0a82220..be2611f 100644 --- a/src/mastodon_api/oauth/auth.rs +++ b/src/mastodon_api/oauth/auth.rs @@ -5,15 +5,14 @@ use actix_web::{ middleware::{ErrorHandlerResponse, ErrorHandlers}, }; use serde_json::json; -use tokio_postgres::GenericClient; -use crate::database::DatabaseError; +use crate::database::{DatabaseClient, DatabaseError}; use crate::errors::HttpError; use crate::models::oauth::queries::get_user_by_oauth_token; use crate::models::users::types::User; pub async fn get_current_user( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, token: &str, ) -> Result { let user = get_user_by_oauth_token(db_client, token).await.map_err(|err| { diff --git a/src/mastodon_api/search/helpers.rs b/src/mastodon_api/search/helpers.rs index 884e1aa..a07a926 100644 --- a/src/mastodon_api/search/helpers.rs +++ b/src/mastodon_api/search/helpers.rs @@ -1,7 +1,6 @@ use std::str::FromStr; use regex::Regex; -use tokio_postgres::GenericClient; use url::Url; use crate::activitypub::{ @@ -14,7 +13,7 @@ use crate::activitypub::{ HandlerError, }; use crate::config::Config; -use crate::database::DatabaseError; +use crate::database::{DatabaseClient, DatabaseError}; use crate::errors::{HttpError, ValidationError}; use crate::identity::did::Did; use crate::mastodon_api::accounts::types::Account; @@ -101,7 +100,7 @@ fn parse_search_query(search_query: &str) -> SearchQuery { async fn search_profiles_or_import( config: &Config, - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, username: String, mut maybe_hostname: Option, limit: u16, @@ -150,7 +149,7 @@ async fn search_profiles_or_import( /// Finds post by its object ID async fn find_post_by_url( config: &Config, - db_client: &mut impl GenericClient, + db_client: &mut impl DatabaseClient, url: &str, ) -> Result, DatabaseError> { let maybe_post = match parse_local_object_id( @@ -185,7 +184,7 @@ async fn find_post_by_url( async fn find_profile_by_url( config: &Config, - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, url: &str, ) -> Result, DatabaseError> { let profile = match parse_local_actor_id( @@ -217,7 +216,7 @@ async fn find_profile_by_url( pub async fn search( config: &Config, current_user: &User, - db_client: &mut impl GenericClient, + db_client: &mut impl DatabaseClient, search_query: &str, limit: u16, ) -> Result { @@ -293,7 +292,7 @@ pub async fn search( pub async fn search_profiles_only( config: &Config, - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, search_query: &str, limit: u16, ) -> Result, HttpError> { diff --git a/src/mastodon_api/settings/helpers.rs b/src/mastodon_api/settings/helpers.rs index 2180143..a79f2e7 100644 --- a/src/mastodon_api/settings/helpers.rs +++ b/src/mastodon_api/settings/helpers.rs @@ -1,4 +1,3 @@ -use tokio_postgres::GenericClient; use uuid::Uuid; use crate::activitypub::{ @@ -6,7 +5,12 @@ use crate::activitypub::{ HandlerError, }; use crate::config::Config; -use crate::database::{get_database_client, DatabaseError, DbPool}; +use crate::database::{ + get_database_client, + DatabaseClient, + DatabaseError, + DbPool, +}; use crate::errors::ValidationError; use crate::mastodon_api::accounts::helpers::follow_or_create_request; use crate::models::{ @@ -30,7 +34,7 @@ fn export_profiles_to_csv( } pub async fn export_followers( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, local_hostname: &str, user_id: &Uuid, ) -> Result { @@ -40,7 +44,7 @@ pub async fn export_followers( } pub async fn export_follows( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, local_hostname: &str, user_id: &Uuid, ) -> Result { diff --git a/src/mastodon_api/statuses/helpers.rs b/src/mastodon_api/statuses/helpers.rs index 599cd7a..9e7a21d 100644 --- a/src/mastodon_api/statuses/helpers.rs +++ b/src/mastodon_api/statuses/helpers.rs @@ -1,8 +1,7 @@ -use tokio_postgres::GenericClient; use uuid::Uuid; use crate::config::Instance; -use crate::database::DatabaseError; +use crate::database::{DatabaseClient, DatabaseError}; use crate::models::{ posts::{ hashtags::{find_hashtags, replace_hashtags}, @@ -24,7 +23,7 @@ pub struct PostContent { } pub async fn parse_microsyntaxes( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, instance: &Instance, mut content: String, ) -> Result { @@ -65,7 +64,7 @@ pub async fn parse_microsyntaxes( /// Load related objects and build status for API response pub async fn build_status( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, instance_url: &str, user: Option<&User>, mut post: Post, @@ -79,7 +78,7 @@ pub async fn build_status( } pub async fn build_status_list( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, instance_url: &str, user: Option<&User>, mut posts: Vec, diff --git a/src/models/attachments/queries.rs b/src/models/attachments/queries.rs index 610290a..c3f66df 100644 --- a/src/models/attachments/queries.rs +++ b/src/models/attachments/queries.rs @@ -1,8 +1,7 @@ use chrono::{DateTime, Utc}; -use tokio_postgres::GenericClient; use uuid::Uuid; -use crate::database::DatabaseError; +use crate::database::{DatabaseClient, DatabaseError}; use crate::models::cleanup::{ find_orphaned_files, find_orphaned_ipfs_objects, @@ -12,7 +11,7 @@ use crate::utils::id::new_uuid; use super::types::DbMediaAttachment; pub async fn create_attachment( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, owner_id: &Uuid, file_name: String, media_type: Option, @@ -31,7 +30,7 @@ pub async fn create_attachment( } pub async fn set_attachment_ipfs_cid( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, attachment_id: &Uuid, ipfs_cid: &str, ) -> Result { @@ -50,7 +49,7 @@ pub async fn set_attachment_ipfs_cid( } pub async fn delete_unused_attachments( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, created_before: &DateTime, ) -> Result { let rows = db_client.query( diff --git a/src/models/background_jobs/queries.rs b/src/models/background_jobs/queries.rs index 9d58002..63ebe7f 100644 --- a/src/models/background_jobs/queries.rs +++ b/src/models/background_jobs/queries.rs @@ -1,13 +1,12 @@ use chrono::{DateTime, Utc}; use serde_json::Value; -use tokio_postgres::GenericClient; use uuid::Uuid; -use crate::database::DatabaseError; +use crate::database::{DatabaseClient, DatabaseError}; use super::types::{DbBackgroundJob, JobStatus, JobType}; pub async fn enqueue_job( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, job_type: &JobType, job_data: &Value, scheduled_for: &DateTime, @@ -29,7 +28,7 @@ pub async fn enqueue_job( } pub async fn get_job_batch( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, job_type: &JobType, batch_size: i64, ) -> Result, DatabaseError> { @@ -65,7 +64,7 @@ pub async fn get_job_batch( } pub async fn delete_job_from_queue( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, job_id: &Uuid, ) -> Result<(), DatabaseError> { let deleted_count = db_client.execute( diff --git a/src/models/cleanup.rs b/src/models/cleanup.rs index 8ffdb63..11b80c7 100644 --- a/src/models/cleanup.rs +++ b/src/models/cleanup.rs @@ -1,7 +1,5 @@ -use tokio_postgres::GenericClient; - use crate::config::Config; -use crate::database::DatabaseError; +use crate::database::{DatabaseClient, DatabaseError}; use crate::ipfs::store as ipfs_store; use crate::utils::files::remove_files; @@ -31,7 +29,7 @@ impl DeletionQueue { } pub async fn find_orphaned_files( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, files: Vec, ) -> Result, DatabaseError> { let rows = db_client.query( @@ -61,7 +59,7 @@ pub async fn find_orphaned_files( } pub async fn find_orphaned_ipfs_objects( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, ipfs_objects: Vec, ) -> Result, DatabaseError> { let rows = db_client.query( diff --git a/src/models/emojis/queries.rs b/src/models/emojis/queries.rs index 22d28f6..3c9e608 100644 --- a/src/models/emojis/queries.rs +++ b/src/models/emojis/queries.rs @@ -1,8 +1,11 @@ use chrono::{DateTime, Utc}; -use tokio_postgres::GenericClient; use uuid::Uuid; -use crate::database::{catch_unique_violation, DatabaseError}; +use crate::database::{ + catch_unique_violation, + DatabaseClient, + DatabaseError, +}; use crate::models::{ cleanup::{find_orphaned_files, DeletionQueue}, instances::queries::create_instance, @@ -12,7 +15,7 @@ use crate::utils::id::new_uuid; use super::types::DbEmoji; pub async fn create_emoji( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, emoji_name: &str, hostname: Option<&str>, file_name: &str, @@ -55,7 +58,7 @@ pub async fn create_emoji( } pub async fn update_emoji( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, emoji_id: &Uuid, file_name: &str, media_type: &str, @@ -85,7 +88,7 @@ pub async fn update_emoji( } pub async fn get_emoji_by_remote_object_id( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, object_id: &str, ) -> Result { let maybe_row = db_client.query_opt( @@ -101,7 +104,7 @@ pub async fn get_emoji_by_remote_object_id( } pub async fn delete_emoji( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, emoji_id: &Uuid, ) -> Result { let maybe_row = db_client.query_opt( diff --git a/src/models/instances/queries.rs b/src/models/instances/queries.rs index da73abc..1a02883 100644 --- a/src/models/instances/queries.rs +++ b/src/models/instances/queries.rs @@ -1,9 +1,7 @@ -use tokio_postgres::GenericClient; - -use crate::database::DatabaseError; +use crate::database::{DatabaseClient, DatabaseError}; pub async fn create_instance( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, hostname: &str, ) -> Result<(), DatabaseError> { db_client.execute( @@ -17,7 +15,7 @@ pub async fn create_instance( } pub async fn get_peer_count( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, ) -> Result { let row = db_client.query_one( "SELECT count(instance) FROM instance", diff --git a/src/models/invoices/queries.rs b/src/models/invoices/queries.rs index 251ea5b..d0a74c2 100644 --- a/src/models/invoices/queries.rs +++ b/src/models/invoices/queries.rs @@ -1,13 +1,16 @@ -use tokio_postgres::GenericClient; use uuid::Uuid; -use crate::database::{catch_unique_violation, DatabaseError}; +use crate::database::{ + catch_unique_violation, + DatabaseClient, + DatabaseError, +}; use crate::utils::caip2::ChainId; use crate::utils::id::new_uuid; use super::types::{DbInvoice, InvoiceStatus}; pub async fn create_invoice( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, sender_id: &Uuid, recipient_id: &Uuid, chain_id: &ChainId, @@ -42,7 +45,7 @@ pub async fn create_invoice( } pub async fn get_invoice_by_id( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, invoice_id: &Uuid, ) -> Result { let maybe_row = db_client.query_opt( @@ -58,7 +61,7 @@ pub async fn get_invoice_by_id( } pub async fn get_invoice_by_address( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, chain_id: &ChainId, payment_address: &str, ) -> Result { @@ -75,7 +78,7 @@ pub async fn get_invoice_by_address( } pub async fn get_invoices_by_status( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, chain_id: &ChainId, status: InvoiceStatus, ) -> Result, DatabaseError> { @@ -93,7 +96,7 @@ pub async fn get_invoices_by_status( } pub async fn set_invoice_status( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, invoice_id: &Uuid, status: InvoiceStatus, ) -> Result<(), DatabaseError> { diff --git a/src/models/markers/queries.rs b/src/models/markers/queries.rs index 6e69594..a95bc03 100644 --- a/src/models/markers/queries.rs +++ b/src/models/markers/queries.rs @@ -1,11 +1,10 @@ -use tokio_postgres::GenericClient; use uuid::Uuid; -use crate::database::DatabaseError; +use crate::database::{DatabaseClient, DatabaseError}; use super::types::{DbTimelineMarker, Timeline}; pub async fn create_or_update_marker( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, user_id: &Uuid, timeline: Timeline, last_read_id: String, @@ -25,7 +24,7 @@ pub async fn create_or_update_marker( } pub async fn get_marker_opt( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, user_id: &Uuid, timeline: Timeline, ) -> Result, DatabaseError> { diff --git a/src/models/notifications/queries.rs b/src/models/notifications/queries.rs index 860ee15..beb085a 100644 --- a/src/models/notifications/queries.rs +++ b/src/models/notifications/queries.rs @@ -1,7 +1,6 @@ -use tokio_postgres::GenericClient; use uuid::Uuid; -use crate::database::DatabaseError; +use crate::database::{DatabaseClient, DatabaseError}; use crate::models::posts::helpers::{add_related_posts, add_user_actions}; use crate::models::posts::queries::{ RELATED_ATTACHMENTS, @@ -13,7 +12,7 @@ use crate::models::posts::queries::{ use super::types::{EventType, Notification}; async fn create_notification( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, sender_id: &Uuid, recipient_id: &Uuid, post_id: Option<&Uuid>, @@ -35,7 +34,7 @@ async fn create_notification( } pub async fn create_follow_notification( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, sender_id: &Uuid, recipient_id: &Uuid, ) -> Result<(), DatabaseError> { @@ -46,7 +45,7 @@ pub async fn create_follow_notification( } pub async fn create_reply_notification( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, sender_id: &Uuid, recipient_id: &Uuid, post_id: &Uuid, @@ -58,7 +57,7 @@ pub async fn create_reply_notification( } pub async fn create_reaction_notification( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, sender_id: &Uuid, recipient_id: &Uuid, post_id: &Uuid, @@ -70,7 +69,7 @@ pub async fn create_reaction_notification( } pub async fn create_mention_notification( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, sender_id: &Uuid, recipient_id: &Uuid, post_id: &Uuid, @@ -82,7 +81,7 @@ pub async fn create_mention_notification( } pub async fn create_repost_notification( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, sender_id: &Uuid, recipient_id: &Uuid, post_id: &Uuid, @@ -94,7 +93,7 @@ pub async fn create_repost_notification( } pub async fn create_subscription_notification( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, sender_id: &Uuid, recipient_id: &Uuid, ) -> Result<(), DatabaseError> { @@ -105,7 +104,7 @@ pub async fn create_subscription_notification( } pub async fn create_subscription_expiration_notification( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, sender_id: &Uuid, recipient_id: &Uuid, ) -> Result<(), DatabaseError> { @@ -116,7 +115,7 @@ pub async fn create_subscription_expiration_notification( } pub async fn create_move_notification( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, sender_id: &Uuid, recipient_id: &Uuid, ) -> Result<(), DatabaseError> { @@ -127,7 +126,7 @@ pub async fn create_move_notification( } pub async fn get_notifications( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, recipient_id: &Uuid, max_id: Option, limit: u16, diff --git a/src/models/oauth/queries.rs b/src/models/oauth/queries.rs index 685a8db..c591770 100644 --- a/src/models/oauth/queries.rs +++ b/src/models/oauth/queries.rs @@ -1,13 +1,12 @@ use chrono::{DateTime, Utc}; -use tokio_postgres::GenericClient; use uuid::Uuid; -use crate::database::DatabaseError; +use crate::database::{DatabaseClient, DatabaseError}; use crate::models::profiles::types::DbActorProfile; use crate::models::users::types::{DbUser, User}; pub async fn save_oauth_token( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, owner_id: &Uuid, token: &str, created_at: &DateTime, @@ -24,7 +23,7 @@ pub async fn save_oauth_token( } pub async fn delete_oauth_token( - db_client: &mut impl GenericClient, + db_client: &mut impl DatabaseClient, current_user_id: &Uuid, token: &str, ) -> Result<(), DatabaseError> { @@ -54,7 +53,7 @@ pub async fn delete_oauth_token( } pub async fn delete_oauth_tokens( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, owner_id: &Uuid, ) -> Result<(), DatabaseError> { db_client.execute( @@ -65,7 +64,7 @@ pub async fn delete_oauth_tokens( } pub async fn get_user_by_oauth_token( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, access_token: &str, ) -> Result { let maybe_row = db_client.query_opt( diff --git a/src/models/posts/helpers.rs b/src/models/posts/helpers.rs index fcdb7ec..5dedf37 100644 --- a/src/models/posts/helpers.rs +++ b/src/models/posts/helpers.rs @@ -1,8 +1,7 @@ -use tokio_postgres::GenericClient; use uuid::Uuid; use crate::activitypub::identifiers::parse_local_object_id; -use crate::database::DatabaseError; +use crate::database::{DatabaseClient, DatabaseError}; use crate::models::reactions::queries::find_favourited_by_user; use crate::models::relationships::queries::has_relationship; use crate::models::relationships::types::RelationshipType; @@ -16,7 +15,7 @@ use super::queries::{ use super::types::{Post, PostActions, Visibility}; pub async fn add_related_posts( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, posts: Vec<&mut Post>, ) -> Result<(), DatabaseError> { let posts_ids = posts.iter().map(|post| post.id).collect(); @@ -50,7 +49,7 @@ pub async fn add_related_posts( } pub async fn add_user_actions( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, user_id: &Uuid, posts: Vec<&mut Post>, ) -> Result<(), DatabaseError> { @@ -82,7 +81,7 @@ pub async fn add_user_actions( } pub async fn can_view_post( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, user: Option<&User>, post: &Post, ) -> Result { @@ -125,7 +124,7 @@ pub async fn can_view_post( } pub async fn get_local_post_by_id( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, post_id: &Uuid, ) -> Result { let post = get_post_by_id(db_client, post_id).await?; @@ -136,7 +135,7 @@ pub async fn get_local_post_by_id( } pub async fn get_post_by_object_id( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, instance_url: &str, object_id: &str, ) -> Result { diff --git a/src/models/posts/links.rs b/src/models/posts/links.rs index c07a1cd..cbc0444 100644 --- a/src/models/posts/links.rs +++ b/src/models/posts/links.rs @@ -1,9 +1,8 @@ use std::collections::HashMap; use regex::{Captures, Match, Regex}; -use tokio_postgres::GenericClient; -use crate::database::DatabaseError; +use crate::database::{DatabaseClient, DatabaseError}; use super::helpers::get_post_by_object_id; use super::types::{Post, Visibility}; @@ -38,7 +37,7 @@ fn find_object_links(text: &str) -> Vec { } pub async fn find_linked_posts( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, instance_url: &str, text: &str, ) -> Result, DatabaseError> { diff --git a/src/models/posts/mentions.rs b/src/models/posts/mentions.rs index 4fde4a3..30b39d8 100644 --- a/src/models/posts/mentions.rs +++ b/src/models/posts/mentions.rs @@ -1,9 +1,8 @@ use std::collections::HashMap; use regex::{Captures, Regex}; -use tokio_postgres::GenericClient; -use crate::database::DatabaseError; +use crate::database::{DatabaseClient, DatabaseError}; use crate::errors::ValidationError; use crate::models::profiles::queries::get_profiles_by_accts; use crate::models::profiles::types::DbActorProfile; @@ -45,7 +44,7 @@ fn find_mentions( } pub async fn find_mentioned_profiles( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, instance_hostname: &str, text: &str, ) -> Result, DatabaseError> { diff --git a/src/models/posts/queries.rs b/src/models/posts/queries.rs index 77981a5..a352d82 100644 --- a/src/models/posts/queries.rs +++ b/src/models/posts/queries.rs @@ -1,10 +1,10 @@ use chrono::{DateTime, Utc}; -use tokio_postgres::GenericClient; use uuid::Uuid; use crate::database::{ catch_unique_violation, query_macro::query, + DatabaseClient, DatabaseError, }; use crate::models::attachments::queries::set_attachment_ipfs_cid; @@ -33,7 +33,7 @@ use super::types::{ }; pub async fn create_post( - db_client: &mut impl GenericClient, + db_client: &mut impl DatabaseClient, author_id: &Uuid, data: PostCreateData, ) -> Result { @@ -300,7 +300,7 @@ fn build_visibility_filter() -> String { } pub async fn get_home_timeline( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, current_user_id: &Uuid, max_post_id: Option, limit: u16, @@ -404,7 +404,7 @@ pub async fn get_home_timeline( } pub async fn get_local_timeline( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, current_user_id: &Uuid, max_post_id: Option, limit: u16, @@ -449,7 +449,7 @@ pub async fn get_local_timeline( } pub async fn get_related_posts( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, posts_ids: Vec, ) -> Result, DatabaseError> { let statement = format!( @@ -495,7 +495,7 @@ pub async fn get_related_posts( } pub async fn get_posts_by_author( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, profile_id: &Uuid, current_user_id: Option<&Uuid>, include_replies: bool, @@ -553,7 +553,7 @@ pub async fn get_posts_by_author( } pub async fn get_posts_by_tag( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, tag_name: &str, current_user_id: Option<&Uuid>, max_post_id: Option, @@ -604,7 +604,7 @@ pub async fn get_posts_by_tag( } pub async fn get_post_by_id( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, post_id: &Uuid, ) -> Result { let statement = format!( @@ -640,7 +640,7 @@ pub async fn get_post_by_id( /// Given a post ID, finds all items in thread. /// Results are sorted by tree path. pub async fn get_thread( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, post_id: &Uuid, current_user_id: Option<&Uuid>, ) -> Result, DatabaseError> { @@ -700,7 +700,7 @@ pub async fn get_thread( } pub async fn get_post_by_remote_object_id( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, object_id: &str, ) -> Result { let statement = format!( @@ -732,7 +732,7 @@ pub async fn get_post_by_remote_object_id( } pub async fn get_post_by_ipfs_cid( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, ipfs_cid: &str, ) -> Result { let statement = format!( @@ -766,7 +766,7 @@ pub async fn get_post_by_ipfs_cid( } pub async fn update_post( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, post_id: &Uuid, post_data: PostUpdateData, ) -> Result<(), DatabaseError> { @@ -794,7 +794,7 @@ pub async fn update_post( } pub async fn update_reply_count( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, post_id: &Uuid, change: i32, ) -> Result<(), DatabaseError> { @@ -813,7 +813,7 @@ pub async fn update_reply_count( } pub async fn update_reaction_count( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, post_id: &Uuid, change: i32, ) -> Result<(), DatabaseError> { @@ -832,7 +832,7 @@ pub async fn update_reaction_count( } pub async fn update_repost_count( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, post_id: &Uuid, change: i32, ) -> Result<(), DatabaseError> { @@ -851,7 +851,7 @@ pub async fn update_repost_count( } pub async fn set_post_ipfs_cid( - db_client: &mut impl GenericClient, + db_client: &mut impl DatabaseClient, post_id: &Uuid, ipfs_cid: &str, attachments: Vec<(Uuid, String)>, @@ -878,7 +878,7 @@ pub async fn set_post_ipfs_cid( } pub async fn set_post_token_id( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, post_id: &Uuid, token_id: i32, ) -> Result<(), DatabaseError> { @@ -899,7 +899,7 @@ pub async fn set_post_token_id( } pub async fn set_post_token_tx_id( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, post_id: &Uuid, token_tx_id: &str, ) -> Result<(), DatabaseError> { @@ -919,7 +919,7 @@ pub async fn set_post_token_tx_id( } pub async fn get_post_author( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, post_id: &Uuid, ) -> Result { let maybe_row = db_client.query_opt( @@ -938,7 +938,7 @@ pub async fn get_post_author( /// Finds reposts of given posts and returns their IDs pub async fn find_reposts_by_user( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, user_id: &Uuid, posts_ids: &[Uuid], ) -> Result, DatabaseError> { @@ -958,7 +958,7 @@ pub async fn find_reposts_by_user( /// Finds items reposted by user among given posts pub async fn find_reposted_by_user( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, user_id: &Uuid, posts_ids: &[Uuid], ) -> Result, DatabaseError> { @@ -980,7 +980,7 @@ pub async fn find_reposted_by_user( } pub async fn get_token_waitlist( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, ) -> Result, DatabaseError> { let rows = db_client.query( " @@ -1000,7 +1000,7 @@ pub async fn get_token_waitlist( /// updated before the specified date /// that do not contain local posts, reposts, mentions, links or reactions. pub async fn find_extraneous_posts( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, updated_before: &DateTime, ) -> Result, DatabaseError> { let rows = db_client.query( @@ -1075,7 +1075,7 @@ pub async fn find_extraneous_posts( /// Deletes post from database and returns collection of orphaned objects. pub async fn delete_post( - db_client: &mut impl GenericClient, + db_client: &mut impl DatabaseClient, post_id: &Uuid, ) -> Result { let transaction = db_client.transaction().await?; @@ -1168,7 +1168,7 @@ pub async fn delete_post( } pub async fn get_local_post_count( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, ) -> Result { let row = db_client.query_one( " diff --git a/src/models/profiles/helpers.rs b/src/models/profiles/helpers.rs index 6619288..b695ac4 100644 --- a/src/models/profiles/helpers.rs +++ b/src/models/profiles/helpers.rs @@ -1,11 +1,9 @@ -use tokio_postgres::GenericClient; - -use crate::database::DatabaseError; +use crate::database::{DatabaseClient, DatabaseError}; use super::queries::search_profiles_by_did_only; use super::types::DbActorProfile; pub async fn find_aliases( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, profile: &DbActorProfile, ) -> Result, DatabaseError> { let mut results = vec![]; diff --git a/src/models/profiles/queries.rs b/src/models/profiles/queries.rs index 97acac2..4f5be8c 100644 --- a/src/models/profiles/queries.rs +++ b/src/models/profiles/queries.rs @@ -1,10 +1,10 @@ use chrono::{DateTime, Utc}; -use tokio_postgres::GenericClient; use uuid::Uuid; use crate::database::{ catch_unique_violation, query_macro::query, + DatabaseClient, DatabaseError, }; use crate::identity::{did::Did, did_pkh::DidPkh}; @@ -28,7 +28,7 @@ use super::types::{ /// Create new profile using given Client or Transaction. pub async fn create_profile( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, profile_data: ProfileCreateData, ) -> Result { let profile_id = new_uuid(); @@ -66,7 +66,7 @@ pub async fn create_profile( } pub async fn update_profile( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, profile_id: &Uuid, data: ProfileUpdateData, ) -> Result { @@ -108,7 +108,7 @@ pub async fn update_profile( } pub async fn get_profile_by_id( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, profile_id: &Uuid, ) -> Result { let result = db_client.query_opt( @@ -127,7 +127,7 @@ pub async fn get_profile_by_id( } pub async fn get_profile_by_remote_actor_id( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, actor_id: &str, ) -> Result { let maybe_row = db_client.query_opt( @@ -145,7 +145,7 @@ pub async fn get_profile_by_remote_actor_id( } pub async fn get_profile_by_acct( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, acct: &str, ) -> Result { let result = db_client.query_opt( @@ -164,7 +164,7 @@ pub async fn get_profile_by_acct( } pub async fn get_profiles( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, only_local: bool, offset: u16, limit: u16, @@ -191,7 +191,7 @@ pub async fn get_profiles( } pub async fn get_profiles_by_accts( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, accts: Vec, ) -> Result, DatabaseError> { let rows = db_client.query( @@ -210,7 +210,7 @@ pub async fn get_profiles_by_accts( /// Deletes profile from database and returns collection of orphaned objects. pub async fn delete_profile( - db_client: &mut impl GenericClient, + db_client: &mut impl DatabaseClient, profile_id: &Uuid, ) -> Result { let transaction = db_client.transaction().await?; @@ -378,7 +378,7 @@ pub async fn delete_profile( } pub async fn search_profiles( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, username: &str, maybe_hostname: Option<&String>, limit: u16, @@ -409,7 +409,7 @@ pub async fn search_profiles( } pub async fn search_profiles_by_did_only( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, did: &Did, ) -> Result, DatabaseError> { let rows = db_client.query( @@ -432,7 +432,7 @@ pub async fn search_profiles_by_did_only( } pub async fn search_profiles_by_did( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, did: &Did, prefer_verified: bool, ) -> Result, DatabaseError> { @@ -495,7 +495,7 @@ pub async fn search_profiles_by_did( } pub async fn search_profiles_by_wallet_address( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, currency: &Currency, wallet_address: &str, prefer_verified: bool, @@ -506,7 +506,7 @@ pub async fn search_profiles_by_wallet_address( } pub async fn update_follower_count( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, profile_id: &Uuid, change: i32, ) -> Result { @@ -525,7 +525,7 @@ pub async fn update_follower_count( } pub async fn update_following_count( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, profile_id: &Uuid, change: i32, ) -> Result { @@ -544,7 +544,7 @@ pub async fn update_following_count( } pub async fn update_subscriber_count( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, profile_id: &Uuid, change: i32, ) -> Result { @@ -563,7 +563,7 @@ pub async fn update_subscriber_count( } pub async fn update_post_count( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, profile_id: &Uuid, change: i32, ) -> Result { @@ -583,7 +583,7 @@ pub async fn update_post_count( // Doesn't return error if profile doesn't exist pub async fn set_reachability_status( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, actor_id: &str, is_reachable: bool, ) -> Result<(), DatabaseError> { @@ -615,7 +615,7 @@ pub async fn set_reachability_status( /// (without any posts, reactions, relationships) /// updated before the specified date pub async fn find_empty_profiles( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, updated_before: &DateTime, ) -> Result, DatabaseError> { let rows = db_client.query( diff --git a/src/models/reactions/queries.rs b/src/models/reactions/queries.rs index 2f67ab3..4bac9d9 100644 --- a/src/models/reactions/queries.rs +++ b/src/models/reactions/queries.rs @@ -1,7 +1,10 @@ -use tokio_postgres::GenericClient; use uuid::Uuid; -use crate::database::{catch_unique_violation, DatabaseError}; +use crate::database::{ + catch_unique_violation, + DatabaseClient, + DatabaseError, +}; use crate::models::notifications::queries::create_reaction_notification; use crate::models::posts::queries::{ update_reaction_count, @@ -11,7 +14,7 @@ use crate::utils::id::new_uuid; use super::types::DbReaction; pub async fn create_reaction( - db_client: &mut impl GenericClient, + db_client: &mut impl DatabaseClient, author_id: &Uuid, post_id: &Uuid, activity_id: Option<&String>, @@ -48,7 +51,7 @@ pub async fn create_reaction( } pub async fn get_reaction_by_remote_activity_id( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, activity_id: &str, ) -> Result { let maybe_row = db_client.query_opt( @@ -65,7 +68,7 @@ pub async fn get_reaction_by_remote_activity_id( } pub async fn delete_reaction( - db_client: &mut impl GenericClient, + db_client: &mut impl DatabaseClient, author_id: &Uuid, post_id: &Uuid, ) -> Result { @@ -87,7 +90,7 @@ pub async fn delete_reaction( /// Finds favourites among given posts and returns their IDs pub async fn find_favourited_by_user( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, user_id: &Uuid, posts_ids: &[Uuid], ) -> Result, DatabaseError> { diff --git a/src/models/relationships/queries.rs b/src/models/relationships/queries.rs index e7cefb2..70a114b 100644 --- a/src/models/relationships/queries.rs +++ b/src/models/relationships/queries.rs @@ -1,7 +1,10 @@ -use tokio_postgres::GenericClient; use uuid::Uuid; -use crate::database::{catch_unique_violation, DatabaseError}; +use crate::database::{ + catch_unique_violation, + DatabaseClient, + DatabaseError, +}; use crate::models::notifications::queries::create_follow_notification; use crate::models::profiles::queries::{ update_follower_count, @@ -19,7 +22,7 @@ use super::types::{ }; pub async fn get_relationships( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, source_id: &Uuid, target_id: &Uuid, ) -> Result, DatabaseError> { @@ -52,7 +55,7 @@ pub async fn get_relationships( } pub async fn has_relationship( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, source_id: &Uuid, target_id: &Uuid, relationship_type: RelationshipType, @@ -75,7 +78,7 @@ pub async fn has_relationship( } pub async fn follow( - db_client: &mut impl GenericClient, + db_client: &mut impl DatabaseClient, source_id: &Uuid, target_id: &Uuid, ) -> Result<(), DatabaseError> { @@ -97,7 +100,7 @@ pub async fn follow( } pub async fn unfollow( - db_client: &mut impl GenericClient, + db_client: &mut impl DatabaseClient, source_id: &Uuid, target_id: &Uuid, ) -> Result, DatabaseError> { @@ -135,7 +138,7 @@ pub async fn unfollow( // Follow remote actor pub async fn create_follow_request( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, source_id: &Uuid, target_id: &Uuid, ) -> Result { @@ -161,7 +164,7 @@ pub async fn create_follow_request( // Save follow request from remote actor pub async fn create_remote_follow_request_opt( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, source_id: &Uuid, target_id: &Uuid, activity_id: &str, @@ -194,7 +197,7 @@ pub async fn create_remote_follow_request_opt( } pub async fn follow_request_accepted( - db_client: &mut impl GenericClient, + db_client: &mut impl DatabaseClient, request_id: &Uuid, ) -> Result<(), DatabaseError> { let mut transaction = db_client.transaction().await?; @@ -216,7 +219,7 @@ pub async fn follow_request_accepted( } pub async fn follow_request_rejected( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, request_id: &Uuid, ) -> Result<(), DatabaseError> { let updated_count = db_client.execute( @@ -234,7 +237,7 @@ pub async fn follow_request_rejected( } async fn delete_follow_request_opt( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, source_id: &Uuid, target_id: &Uuid, ) -> Result, DatabaseError> { @@ -254,7 +257,7 @@ async fn delete_follow_request_opt( } pub async fn get_follow_request_by_id( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, request_id: &Uuid, ) -> Result { let maybe_row = db_client.query_opt( @@ -271,7 +274,7 @@ pub async fn get_follow_request_by_id( } pub async fn get_follow_request_by_activity_id( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, activity_id: &str, ) -> Result { let maybe_row = db_client.query_opt( @@ -288,7 +291,7 @@ pub async fn get_follow_request_by_activity_id( } pub async fn get_followers( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, profile_id: &Uuid, ) -> Result, DatabaseError> { let rows = db_client.query( @@ -310,7 +313,7 @@ pub async fn get_followers( } pub async fn get_followers_paginated( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, profile_id: &Uuid, max_relationship_id: Option, limit: u16, @@ -342,7 +345,7 @@ pub async fn get_followers_paginated( } pub async fn get_following( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, profile_id: &Uuid, ) -> Result, DatabaseError> { let rows = db_client.query( @@ -364,7 +367,7 @@ pub async fn get_following( } pub async fn get_following_paginated( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, profile_id: &Uuid, max_relationship_id: Option, limit: u16, @@ -396,7 +399,7 @@ pub async fn get_following_paginated( } pub async fn subscribe( - db_client: &mut impl GenericClient, + db_client: &mut impl DatabaseClient, source_id: &Uuid, target_id: &Uuid, ) -> Result<(), DatabaseError> { @@ -414,7 +417,7 @@ pub async fn subscribe( } pub async fn subscribe_opt( - db_client: &mut impl GenericClient, + db_client: &mut impl DatabaseClient, source_id: &Uuid, target_id: &Uuid, ) -> Result<(), DatabaseError> { @@ -435,7 +438,7 @@ pub async fn subscribe_opt( } pub async fn unsubscribe( - db_client: &mut impl GenericClient, + db_client: &mut impl DatabaseClient, source_id: &Uuid, target_id: &Uuid, ) -> Result<(), DatabaseError> { @@ -458,7 +461,7 @@ pub async fn unsubscribe( } pub async fn get_subscribers( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, profile_id: &Uuid, ) -> Result, DatabaseError> { let rows = db_client.query( @@ -481,7 +484,7 @@ pub async fn get_subscribers( } pub async fn hide_reposts( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, source_id: &Uuid, target_id: &Uuid, ) -> Result<(), DatabaseError> { @@ -497,7 +500,7 @@ pub async fn hide_reposts( } pub async fn show_reposts( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, source_id: &Uuid, target_id: &Uuid, ) -> Result<(), DatabaseError> { @@ -515,7 +518,7 @@ pub async fn show_reposts( } pub async fn hide_replies( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, source_id: &Uuid, target_id: &Uuid, ) -> Result<(), DatabaseError> { @@ -531,7 +534,7 @@ pub async fn hide_replies( } pub async fn show_replies( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, source_id: &Uuid, target_id: &Uuid, ) -> Result<(), DatabaseError> { diff --git a/src/models/subscriptions/queries.rs b/src/models/subscriptions/queries.rs index a7ba264..6ab3d52 100644 --- a/src/models/subscriptions/queries.rs +++ b/src/models/subscriptions/queries.rs @@ -1,8 +1,11 @@ use chrono::{DateTime, Utc}; -use tokio_postgres::GenericClient; use uuid::Uuid; -use crate::database::{catch_unique_violation, DatabaseError}; +use crate::database::{ + catch_unique_violation, + DatabaseClient, + DatabaseError, +}; use crate::models::profiles::types::PaymentType; use crate::models::relationships::queries::{subscribe, subscribe_opt}; use crate::models::relationships::types::RelationshipType; @@ -10,7 +13,7 @@ use crate::utils::caip2::ChainId; use super::types::{DbSubscription, Subscription}; pub async fn create_subscription( - db_client: &mut impl GenericClient, + db_client: &mut impl DatabaseClient, sender_id: &Uuid, sender_address: Option<&str>, recipient_id: &Uuid, @@ -47,7 +50,7 @@ pub async fn create_subscription( } pub async fn update_subscription( - db_client: &mut impl GenericClient, + db_client: &mut impl DatabaseClient, subscription_id: i32, chain_id: &ChainId, expires_at: &DateTime, @@ -82,7 +85,7 @@ pub async fn update_subscription( } pub async fn get_subscription_by_participants( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, sender_id: &Uuid, recipient_id: &Uuid, ) -> Result { @@ -100,7 +103,7 @@ pub async fn get_subscription_by_participants( } pub async fn get_expired_subscriptions( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, ) -> Result, DatabaseError> { let rows = db_client.query( " @@ -123,7 +126,7 @@ pub async fn get_expired_subscriptions( } pub async fn get_incoming_subscriptions( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, recipient_id: &Uuid, max_subscription_id: Option, limit: u16, @@ -149,7 +152,7 @@ pub async fn get_incoming_subscriptions( } pub async fn reset_subscriptions( - db_client: &mut impl GenericClient, + db_client: &mut impl DatabaseClient, ethereum_contract_replaced: bool, ) -> Result<(), DatabaseError> { let transaction = db_client.transaction().await?; diff --git a/src/models/tags/queries.rs b/src/models/tags/queries.rs index b5d5728..685a5ff 100644 --- a/src/models/tags/queries.rs +++ b/src/models/tags/queries.rs @@ -1,9 +1,7 @@ -use tokio_postgres::GenericClient; - -use crate::database::DatabaseError; +use crate::database::{DatabaseClient, DatabaseError}; pub async fn search_tags( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, search_query: &str, limit: u16, ) -> Result, DatabaseError> { diff --git a/src/models/users/queries.rs b/src/models/users/queries.rs index 3b8f3e8..0f7e185 100644 --- a/src/models/users/queries.rs +++ b/src/models/users/queries.rs @@ -1,7 +1,10 @@ -use tokio_postgres::GenericClient; use uuid::Uuid; -use crate::database::{catch_unique_violation, DatabaseError}; +use crate::database::{ + catch_unique_violation, + DatabaseClient, + DatabaseError, +}; use crate::identity::{did::Did, did_pkh::DidPkh}; use crate::models::profiles::queries::create_profile; use crate::models::profiles::types::{DbActorProfile, ProfileCreateData}; @@ -10,7 +13,7 @@ use super::types::{DbUser, User, UserCreateData}; use super::utils::generate_invite_code; pub async fn create_invite_code( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, ) -> Result { let invite_code = generate_invite_code(); db_client.execute( @@ -24,7 +27,7 @@ pub async fn create_invite_code( } pub async fn get_invite_codes( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, ) -> Result, DatabaseError> { let rows = db_client.query( " @@ -41,7 +44,7 @@ pub async fn get_invite_codes( } pub async fn is_valid_invite_code( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, invite_code: &str, ) -> Result { let maybe_row = db_client.query_opt( @@ -55,7 +58,7 @@ pub async fn is_valid_invite_code( } pub async fn create_user( - db_client: &mut impl GenericClient, + db_client: &mut impl DatabaseClient, user_data: UserCreateData, ) -> Result { let transaction = db_client.transaction().await?; @@ -128,7 +131,7 @@ pub async fn create_user( } pub async fn set_user_password( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, user_id: &Uuid, password_hash: String, ) -> Result<(), DatabaseError> { @@ -146,7 +149,7 @@ pub async fn set_user_password( } pub async fn get_user_by_id( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, user_id: &Uuid, ) -> Result { let maybe_row = db_client.query_opt( @@ -165,7 +168,7 @@ pub async fn get_user_by_id( } pub async fn get_user_by_name( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, username: &str, ) -> Result { let maybe_row = db_client.query_opt( @@ -184,7 +187,7 @@ pub async fn get_user_by_name( } pub async fn is_registered_user( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, username: &str, ) -> Result { let maybe_row = db_client.query_opt( @@ -198,7 +201,7 @@ pub async fn is_registered_user( } pub async fn get_user_by_login_address( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, wallet_address: &str, ) -> Result { let maybe_row = db_client.query_opt( @@ -217,7 +220,7 @@ pub async fn get_user_by_login_address( } pub async fn get_user_by_did( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, did: &Did, ) -> Result { // DIDs must be locally unique @@ -242,7 +245,7 @@ pub async fn get_user_by_did( } pub async fn get_user_by_public_wallet_address( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, currency: &Currency, wallet_address: &str, ) -> Result { @@ -252,7 +255,7 @@ pub async fn get_user_by_public_wallet_address( } pub async fn get_user_count( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, ) -> Result { let row = db_client.query_one( "SELECT count(user_account) FROM user_account", diff --git a/src/monero/helpers.rs b/src/monero/helpers.rs index 0e466b5..f557dd7 100644 --- a/src/monero/helpers.rs +++ b/src/monero/helpers.rs @@ -2,10 +2,10 @@ use std::str::FromStr; use monero_rpc::TransferType; use monero_rpc::monero::Address; -use tokio_postgres::GenericClient; use uuid::Uuid; use crate::config::MoneroConfig; +use crate::database::DatabaseClient; use crate::errors::ValidationError; use crate::models::{ invoices::queries::{ @@ -30,7 +30,7 @@ pub fn validate_monero_address(address: &str) pub async fn check_expired_invoice( config: &MoneroConfig, - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, invoice_id: &Uuid, ) -> Result<(), MoneroError> { let wallet_client = open_monero_wallet(config).await?; diff --git a/src/nodeinfo/helpers.rs b/src/nodeinfo/helpers.rs index 6564a7d..c207429 100644 --- a/src/nodeinfo/helpers.rs +++ b/src/nodeinfo/helpers.rs @@ -1,11 +1,11 @@ -use tokio_postgres::GenericClient; - -use crate::database::DatabaseError; +use crate::database::{DatabaseClient, DatabaseError}; use crate::models::posts::queries::get_local_post_count; use crate::models::users::queries::get_user_count; use super::types::{Usage, Users}; -pub async fn get_usage(db_client: &impl GenericClient) -> Result { +pub async fn get_usage(db_client: &impl DatabaseClient) + -> Result +{ let user_count = get_user_count(db_client).await?; let post_count = get_local_post_count(db_client).await?; let usage = Usage { diff --git a/src/webfinger/views.rs b/src/webfinger/views.rs index 625d0b5..31b420f 100644 --- a/src/webfinger/views.rs +++ b/src/webfinger/views.rs @@ -1,5 +1,4 @@ use actix_web::{get, web, HttpResponse}; -use tokio_postgres::GenericClient; use crate::activitypub::constants::AP_MEDIA_TYPE; use crate::activitypub::identifiers::{ @@ -8,7 +7,7 @@ use crate::activitypub::identifiers::{ parse_local_actor_id, }; use crate::config::{Config, Instance}; -use crate::database::{get_database_client, DbPool}; +use crate::database::{get_database_client, DatabaseClient, DbPool}; use crate::errors::{HttpError, ValidationError}; use crate::models::users::queries::is_registered_user; use super::types::{ @@ -28,7 +27,7 @@ fn parse_acct_uri(uri: &str) -> Result { } async fn get_jrd( - db_client: &impl GenericClient, + db_client: &impl DatabaseClient, instance: Instance, resource: &str, ) -> Result {