WIP reading news from app

This commit is contained in:
2024-07-21 07:53:02 -07:00
parent 5c0c45b99f
commit 0bf865fdef
11 changed files with 734 additions and 62 deletions

30
server/src/newsreader.rs Normal file
View File

@@ -0,0 +1,30 @@
use std::hash::{DefaultHasher, Hash, Hasher};
use log::info;
use sqlx::postgres::PgPool;
const TAG_PREFIX: &'static str = "News";
use crate::{error, graphql::Tag};
pub async fn tags(pool: &PgPool, needs_unread: bool) -> Result<Vec<Tag>, error::ServerError> {
// TODO: write separate query for needs_unread.
let tags = sqlx::query_file!("sql/tags.sql").fetch_all(pool).await?;
info!("sqlx tags {tags:#?}");
let tags = tags
.into_iter()
.map(|tag| {
let mut hasher = DefaultHasher::new();
tag.site.hash(&mut hasher);
let hex = format!("#{:06x}", hasher.finish() % (1 << 24));
let unread = tag.unread.unwrap_or(0).try_into().unwrap_or(0);
let name = format!("{TAG_PREFIX}/{}", tag.site.expect("tag must have site"));
Tag {
name,
fg_color: "white".to_string(),
bg_color: hex,
unread,
}
})
.collect();
Ok(tags)
}