mirror of
https://git.alemi.dev/guestbook.rs.git
synced 2024-12-19 02:54:52 +01:00
feat: added super crude review command
This commit is contained in:
parent
0afc79934d
commit
53c84839b8
4 changed files with 48 additions and 5 deletions
35
src/main.rs
35
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");
|
||||
|
||||
|
|
|
@ -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<String>,
|
||||
pub url: Option<String>,
|
||||
|
@ -76,6 +77,7 @@ impl From<&Page> for PageView {
|
|||
};
|
||||
|
||||
PageView {
|
||||
id: page.id,
|
||||
url, avatar,
|
||||
author: page.author.clone(),
|
||||
contact: page.contact.clone(),
|
||||
|
|
|
@ -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);
|
||||
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)),
|
||||
Err(e) => Err(e.to_string()),
|
||||
}
|
||||
|
|
|
@ -73,9 +73,9 @@ impl StorageProvider {
|
|||
)
|
||||
}
|
||||
|
||||
pub async fn extract(&self, offset: i32, window: i32) -> 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 $1 OFFSET $2")
|
||||
pub async fn extract(&self, offset: i32, window: i32, public: bool) -> sqlx::Result<Vec<PageView>> {
|
||||
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(())
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue