feat: added super crude review command

This commit is contained in:
əlemi 2024-01-03 03:47:30 +01:00
parent 0afc79934d
commit 53c84839b8
Signed by: alemi
GPG key ID: A4895B84D311642C
4 changed files with 48 additions and 5 deletions

View file

@ -1,5 +1,6 @@
use std::net::SocketAddr; use std::{net::SocketAddr, io::Write};
use clap::{Parser, Subcommand}; use clap::{Parser, Subcommand};
use config::ConfigOverrides;
use crate::{storage::StorageProvider, routes::Context, notifications::console::ConsoleTracingNotifier, config::{Config, ConfigNotifierProvider}}; 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 /// print a sample configuration, redirect to file and customize
Default, 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] #[tokio::main]
@ -61,6 +69,31 @@ async fn main() {
cfg.notifiers.providers.push(ConfigNotifierProvider::TelegramNotifier { token: "asd".into(), chat_id: -1 }); cfg.notifiers.providers.push(ConfigNotifierProvider::TelegramNotifier { token: "asd".into(), chat_id: -1 });
println!("{}", toml::to_string(&cfg).unwrap()); 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 } => { CliAction::Serve { addr, config } => {
let addr : SocketAddr = addr.parse().expect("invalid host provided"); let addr : SocketAddr = addr.parse().expect("invalid host provided");

View file

@ -48,6 +48,7 @@ impl<'r> sqlx::FromRow<'r, sqlx::any::AnyRow> for Page {
#[derive(Debug, Clone, Default, Serialize)] #[derive(Debug, Clone, Default, Serialize)]
pub struct PageView { pub struct PageView {
pub id: i64,
pub author: String, pub author: String,
pub contact: Option<String>, pub contact: Option<String>,
pub url: Option<String>, pub url: Option<String>,
@ -76,6 +77,7 @@ impl From<&Page> for PageView {
}; };
PageView { PageView {
id: page.id,
url, avatar, url, avatar,
author: page.author.clone(), author: page.author.clone(),
contact: page.contact.clone(), contact: page.contact.clone(),

View file

@ -49,7 +49,7 @@ async fn get_suggestion(State(state): State<Arc<Context>>, Query(page): Query<Pa
let limit = std::cmp::min(page.limit.unwrap_or(20), 20); let limit = std::cmp::min(page.limit.unwrap_or(20), 20);
tracing::debug!("serving suggestions (offset {} limit {}", offset, limit); tracing::debug!("serving suggestions (offset {} limit {}", offset, limit);
match state.storage.extract(offset, limit).await { match state.storage.extract(offset, limit, true).await {
Ok(x) => Ok(Json(x)), Ok(x) => Ok(Json(x)),
Err(e) => Err(e.to_string()), Err(e) => Err(e.to_string()),
} }

View file

@ -73,9 +73,9 @@ impl StorageProvider {
) )
} }
pub async fn extract(&self, offset: i32, window: i32) -> sqlx::Result<Vec<PageView>> { pub async fn extract(&self, offset: i32, window: i32, public: bool) -> sqlx::Result<Vec<PageView>> {
// TODO since AnyPool won't handle booleans we compare with an integer let out = sqlx::query_as("SELECT * FROM pages WHERE public = $1 LIMIT $2 OFFSET $3")
let out = sqlx::query_as("SELECT * FROM pages WHERE public = 1 LIMIT $1 OFFSET $2") .bind(if public { 1 } else { 0 }) // TODO since AnyPool won't handle booleans we compare with an integer
.bind(window) .bind(window)
.bind(offset) .bind(offset)
.fetch_all(&self.db) .fetch_all(&self.db)
@ -85,4 +85,12 @@ impl StorageProvider {
.collect(); .collect();
Ok(out) 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(())
}
} }