WIP reading news from app
This commit is contained in:
30
server/src/newsreader.rs
Normal file
30
server/src/newsreader.rs
Normal 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)
|
||||
}
|
||||
Reference in New Issue
Block a user