diff --git a/src/main.rs b/src/main.rs index 7b0ed7b..e7d5954 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ mod server; +mod store; mod tap; use sqlx::migrate::MigrateDatabase; diff --git a/src/tap.rs b/src/tap.rs index 8b95ace..a0604e9 100644 --- a/src/tap.rs +++ b/src/tap.rs @@ -1,9 +1,10 @@ use ::chrono::{DateTime, Utc}; use atproto_tap::{RecordAction, RecordEvent, TapClient, TapEvent, connect_to}; use serde::Deserialize; -use sqlx::{FromRow, Row, types::chrono}; use tokio_stream::StreamExt; +use crate::store; + #[derive(Deserialize)] struct FollowRecord { subject: String, @@ -97,7 +98,7 @@ async fn handle_user_follow_event(record: &RecordEvent, pool: &sqlx::SqlitePool) _ = add_repo(&follow.subject).await; } RecordAction::Delete => { - let follow_subject = get_follow_by_rkey(&record.rkey.to_string(), pool).await; + let follow_subject = store::get_follow_by_rkey(&record.rkey.to_string(), pool).await; if follow_subject != "" { _ = remove_repo(&follow_subject).await; } @@ -122,7 +123,7 @@ async fn handle_post_event(record: &RecordEvent, pool: &sqlx::SqlitePool) { Some(x) => x.to_string(), None => "".to_string(), }; - let mut post = PostRecord { + let mut post = store::PostRecord { created: Utc::now(), indexed: Utc::now(), author: record.did.clone().to_string(), @@ -142,10 +143,10 @@ async fn handle_post_event(record: &RecordEvent, pool: &sqlx::SqlitePool) { } } - insert_post(post, pool).await; + store::insert_post(post, pool).await; } RecordAction::Delete => { - delete_post(record.rkey.to_string(), pool).await; + store::delete_post(record.rkey.to_string(), pool).await; } RecordAction::Update => {} } @@ -165,7 +166,7 @@ async fn handle_repost_event(record: &RecordEvent, pool: &sqlx::SqlitePool) { None => "".to_string(), }; - let mut repost = RepostRecord { + let mut repost = store::RepostRecord { created: Utc::now(), indexed: Utc::now(), author: record.did.clone().to_string(), @@ -184,37 +185,15 @@ async fn handle_repost_event(record: &RecordEvent, pool: &sqlx::SqlitePool) { } } - insert_repost(repost, pool).await; + store::insert_repost(repost, pool).await; } RecordAction::Delete => { - delete_repost(record.rkey.to_string(), pool).await; + store::delete_repost(record.rkey.to_string(), pool).await; } RecordAction::Update => {} } } -async fn get_follow_by_rkey(rkey: &String, pool: &sqlx::SqlitePool) -> String { - let result = sqlx::query("SELECT subject FROM follows where rkey = ? LIMIT 1") - .bind(rkey) - .fetch_one(pool) - .await; - - match result { - Ok(row) => { - if row.len() == 0 { - println!("did not find subject in follows with rkey: {rkey}"); - return "".to_string(); - } - let subject = row.get::(0); - - return subject; - } - Err(e) => { - println!("error getting follow {e}"); - return "".to_string(); - } - } -} #[derive(Debug, Deserialize)] struct TapPost { #[serde(rename = "createdAt")] @@ -226,81 +205,3 @@ struct TapPost { struct Subject { uri: String, } - -#[derive(Debug, FromRow)] -struct PostRecord { - created: chrono::DateTime, - indexed: chrono::DateTime, - author: String, - rkey: String, - cid: String, - // TODO: other fields like reply to etc -} - -#[derive(Debug, FromRow)] -struct RepostRecord { - created: chrono::DateTime, - indexed: chrono::DateTime, - author: String, - rkey: String, - subject: String, - cid: String, - // TODO: other fields like reply to etc -} - -async fn insert_post(post: PostRecord, pool: &sqlx::SqlitePool) { - let result = sqlx::query( - "INSERT INTO posts (created, indexed, author, rkey, cid) VALUES ($1, $2, $3, $4, $5)", - ) - .bind(post.created) - .bind(post.indexed) - .bind(post.author) - .bind(post.rkey) - .bind(post.cid) - .execute(pool) - .await; - - if result.is_err() { - println!("Error inserting post into the database: {result:?}"); - } -} - -async fn insert_repost(repost: RepostRecord, pool: &sqlx::SqlitePool) { - let result = sqlx::query( - "INSERT INTO reposts (created, indexed, author, rkey, subject, cid) VALUES ($1, $2, $3, $4, $5, $6)", - ) - .bind(repost.created) - .bind(repost.indexed) - .bind(repost.author) - .bind(repost.rkey) - .bind(repost.subject) - .bind(repost.cid) - .execute(pool) - .await; - - if result.is_err() { - println!("Error inserting repost into the database: {result:?}"); - } -} - -async fn delete_post(rkey: String, pool: &sqlx::SqlitePool) { - let result = sqlx::query("DELETE FROM posts WHERE rkey = $1") - .bind(rkey) - .execute(pool) - .await; - - if result.is_err() { - println!("Error deleting post from the database: {result:?}"); - } -} - -async fn delete_repost(rkey: String, pool: &sqlx::SqlitePool) { - let result = sqlx::query("DELETE FROM reposts WHERE rkey = $1") - .bind(rkey) - .execute(pool) - .await; - - if result.is_err() { - println!("Error deleting repost from the database: {result:?}"); - } -}