This commit is contained in:
2024-12-19 10:40:18 -08:00
parent 06c5cb6cbf
commit 51154044cc
8 changed files with 509 additions and 2 deletions

114
server/src/bin/email2db.rs Normal file
View File

@@ -0,0 +1,114 @@
use chrono::NaiveDateTime;
use clap::Parser;
use mailparse::{addrparse_header, dateparse, parse_mail, MailHeaderMap, ParsedMail};
use server::mail::read_mail_to_db;
use sqlx::postgres::PgPool;
/// Add certain emails as posts in newsfeed app.
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Args {
/// DB URL, something like postgres://newsreader@nixos-07.h.xinu.tv/newsreader
#[arg(short, long)]
db_url: String,
/// path to parse
path: String,
}
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let args = Args::parse();
let pool = PgPool::connect(&args.db_url).await?;
let mut buffer = Vec::new();
read_mail_to_db(&pool, &args.path)?;
Ok(())
}
async fn add_post(
pool: &PgPool,
site: &str,
title: &str,
summary: &str,
link: &str,
date: i64,
uid: &str,
feed_url: &str,
) -> Result<(), MailError> {
todo!("add_post")
/*
// site | text
// title | text
// summary | text
// link | text
// date | timestamp without time zone
// is_read | boolean
// uid | text
// clean_site | text
// clean_title | text
// clean_summary | text
// clean_summary_text | text
// feed_url | text
let date = NaiveDateTime::from_timestamp_millis(date * 1000);
sqlx::query!(
r#"
INSERT INTO post (
site,
title,
summary,
link,
date,
is_read,
uid,
clean_title,
clean_summary,
clean_summary_text,
feed_url
)
VALUES ( $1, $2, $3, $4, $5, false, $6, $2, $3, '', $7 )
"#,
site,
title,
summary,
link,
date,
uid,
feed_url
)
.execute(pool)
.await?;
Ok(())
*/
}
async fn find_feed(pool: &PgPool, name: &str, slug: &str, url: &str) -> Result<i32, MailError> {
match sqlx::query!(
r#"
SELECT id
FROM feed
WHERE slug = $1
"#,
slug
)
.fetch_one(pool)
.await
{
Err(sqlx::Error::RowNotFound) => {
let rec = sqlx::query!(
r#"
INSERT INTO feed ( name, slug, url, homepage, selector )
VALUES ( $1, $2, $3, '', '' )
RETURNING id
"#,
name,
slug,
url
)
.fetch_one(pool)
.await?;
return Ok(rec.id);
}
Ok(rec) => return Ok(rec.id),
Err(e) => return Err(e.into()),
};
}