From 53c84839b807bc81580b7481a99ea63432ec84b9 Mon Sep 17 00:00:00 2001 From: alemi Date: Wed, 3 Jan 2024 03:47:30 +0100 Subject: [PATCH] feat: added super crude review command --- src/main.rs | 35 ++++++++++++++++++++++++++++++++++- src/model.rs | 2 ++ src/routes.rs | 2 +- src/storage.rs | 14 +++++++++++--- 4 files changed, 48 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index eca38d4..344057e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ -use std::net::SocketAddr; +use std::{net::SocketAddr, io::Write}; use clap::{Parser, Subcommand}; +use config::ConfigOverrides; use crate::{storage::StorageProvider, routes::Context, notifications::console::ConsoleTracingNotifier, config::{Config, ConfigNotifierProvider}}; @@ -43,6 +44,13 @@ enum CliAction { /// print a sample configuration, redirect to file and customize Default, + + /// review sent pages and approve for public view + Review { + #[arg(long, default_value_t = 20)] + /// how many pages to fetch per query + batch: i32, + }, } #[tokio::main] @@ -61,6 +69,31 @@ async fn main() { cfg.notifiers.providers.push(ConfigNotifierProvider::TelegramNotifier { token: "asd".into(), chat_id: -1 }); println!("{}", toml::to_string(&cfg).unwrap()); }, + CliAction::Review { batch } => { + sqlx::any::install_default_drivers(); // must install all available drivers before connecting + let storage = StorageProvider::connect(&args.db, ConfigOverrides::default()).await.unwrap(); + let mut offset = 0; + let mut buffer = String::new(); + let stdin = std::io::stdin(); + let mut stdout = std::io::stdout(); + loop { + let mut stop = true; + for page in storage.extract(offset, batch, false).await.unwrap() { + stop = false; // at least one page was returned + println!("{:?}", page); + print!("* approve? "); + stdout.flush().unwrap(); + stdin.read_line(&mut buffer).unwrap(); + if !buffer.trim().is_empty() { + println!("* OK published"); + storage.publish(page.id).await.unwrap(); + } + } + if stop { break } + offset += batch; + } + println!("* done"); + }, CliAction::Serve { addr, config } => { let addr : SocketAddr = addr.parse().expect("invalid host provided"); diff --git a/src/model.rs b/src/model.rs index 77cdf24..ddeb728 100644 --- a/src/model.rs +++ b/src/model.rs @@ -48,6 +48,7 @@ impl<'r> sqlx::FromRow<'r, sqlx::any::AnyRow> for Page { #[derive(Debug, Clone, Default, Serialize)] pub struct PageView { + pub id: i64, pub author: String, pub contact: Option, pub url: Option, @@ -76,6 +77,7 @@ impl From<&Page> for PageView { }; PageView { + id: page.id, url, avatar, author: page.author.clone(), contact: page.contact.clone(), diff --git a/src/routes.rs b/src/routes.rs index 87fc551..4174f3a 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -49,7 +49,7 @@ async fn get_suggestion(State(state): State>, Query(page): Query Ok(Json(x)), Err(e) => Err(e.to_string()), } diff --git a/src/storage.rs b/src/storage.rs index af1b684..6cf61d7 100644 --- a/src/storage.rs +++ b/src/storage.rs @@ -73,9 +73,9 @@ impl StorageProvider { ) } - pub async fn extract(&self, offset: i32, window: i32) -> sqlx::Result> { - // TODO since AnyPool won't handle booleans we compare with an integer - let out = sqlx::query_as("SELECT * FROM pages WHERE public = 1 LIMIT $1 OFFSET $2") + pub async fn extract(&self, offset: i32, window: i32, public: bool) -> sqlx::Result> { + let out = sqlx::query_as("SELECT * FROM pages WHERE public = $1 LIMIT $2 OFFSET $3") + .bind(if public { 1 } else { 0 }) // TODO since AnyPool won't handle booleans we compare with an integer .bind(window) .bind(offset) .fetch_all(&self.db) @@ -85,4 +85,12 @@ impl StorageProvider { .collect(); Ok(out) } + + pub async fn publish(&self, id: i64) -> sqlx::Result<()> { + sqlx::query("UPDATE pages SET public = 1 WHERE id = $1") + .bind(id) + .execute(&self.db) + .await?; + Ok(()) + } }