snooze: add UI elements and DB for snooze functionality

This commit is contained in:
Bill Thiede 2025-07-13 08:53:50 -07:00
parent 52b19365d7
commit 90ac9a1e43
11 changed files with 210 additions and 67 deletions

View File

@ -0,0 +1,2 @@
-- Add down migration script here
DROP TABLE IF EXISTS snooze;

View File

@ -0,0 +1,6 @@
-- Add up migration script here
CREATE TABLE IF NOT EXISTS snooze (
id integer NOT NULL GENERATED ALWAYS AS IDENTITY,
message_id text NOT NULL,
wake timestamptz NOT NULL
);

View File

@ -637,6 +637,17 @@ impl MutationRoot {
wake_time: DateTime<Utc>, wake_time: DateTime<Utc>,
) -> Result<bool, Error> { ) -> Result<bool, Error> {
info!("TODO snooze {query} until {wake_time})"); info!("TODO snooze {query} until {wake_time})");
let pool = ctx.data_unchecked::<PgPool>();
sqlx::query!(
r#"
INSERT INTO snooze (message_id, wake)
VALUES ($1, $2)
"#,
query,
wake_time
)
.execute(pool)
.await?;
Ok(true) Ok(true)
} }
/// Drop and recreate tantivy index. Warning this is slow /// Drop and recreate tantivy index. Warning this is slow

View File

@ -19,6 +19,7 @@ use std::{
use async_trait::async_trait; use async_trait::async_trait;
use cacher::{Cacher, FilesystemCacher}; use cacher::{Cacher, FilesystemCacher};
use chrono::NaiveDateTime;
use css_inline::{CSSInliner, InlineError, InlineOptions}; use css_inline::{CSSInliner, InlineError, InlineOptions};
pub use error::ServerError; pub use error::ServerError;
use linkify::{LinkFinder, LinkKind}; use linkify::{LinkFinder, LinkKind};
@ -30,7 +31,6 @@ use maplit::{hashmap, hashset};
use regex::Regex; use regex::Regex;
use reqwest::StatusCode; use reqwest::StatusCode;
use scraper::{Html, Selector}; use scraper::{Html, Selector};
use sqlx::types::time::PrimitiveDateTime;
use thiserror::Error; use thiserror::Error;
use tracing::{debug, error, info, warn}; use tracing::{debug, error, info, warn};
use url::Url; use url::Url;
@ -896,7 +896,7 @@ impl FromStr for Query {
} }
pub struct ThreadSummaryRecord { pub struct ThreadSummaryRecord {
pub site: Option<String>, pub site: Option<String>,
pub date: Option<PrimitiveDateTime>, pub date: Option<NaiveDateTime>,
pub is_read: Option<bool>, pub is_read: Option<bool>,
pub title: Option<String>, pub title: Option<String>,
pub uid: String, pub uid: String,
@ -914,11 +914,7 @@ async fn thread_summary_from_row(r: ThreadSummaryRecord) -> ThreadSummary {
title = clean_title(&title).await.expect("failed to clean title"); title = clean_title(&title).await.expect("failed to clean title");
ThreadSummary { ThreadSummary {
thread: format!("{NEWSREADER_THREAD_PREFIX}{}", r.uid), thread: format!("{NEWSREADER_THREAD_PREFIX}{}", r.uid),
timestamp: r timestamp: r.date.expect("post missing date").and_utc().timestamp() as isize,
.date
.expect("post missing date")
.assume_utc()
.unix_timestamp() as isize,
date_relative: format!("{:?}", r.date), date_relative: format!("{:?}", r.date),
//date_relative: "TODO date_relative".to_string(), //date_relative: "TODO date_relative".to_string(),
matched: 0, matched: 0,

View File

@ -211,11 +211,7 @@ pub async fn thread(
} }
let title = clean_title(&r.title.unwrap_or("NO TITLE".to_string())).await?; let title = clean_title(&r.title.unwrap_or("NO TITLE".to_string())).await?;
let is_read = r.is_read.unwrap_or(false); let is_read = r.is_read.unwrap_or(false);
let timestamp = r let timestamp = r.date.expect("post missing date").and_utc().timestamp();
.date
.expect("post missing date")
.assume_utc()
.unix_timestamp();
Ok(Thread::News(NewsPost { Ok(Thread::News(NewsPost {
thread_id, thread_id,
is_read, is_read,

View File

@ -51,7 +51,7 @@
}, },
{ {
"args": [], "args": [],
"description": "Indicates that an Input Object is a OneOf Input Object (and thus requires\n exactly one of its field be provided)", "description": "Indicates that an Input Object is a OneOf Input Object (and thus requires exactly one of its field be provided)",
"locations": [ "locations": [
"INPUT_OBJECT" "INPUT_OBJECT"
], ],
@ -107,12 +107,14 @@
} }
], ],
"mutationType": { "mutationType": {
"name": "Mutation" "name": "MutationRoot"
}, },
"queryType": { "queryType": {
"name": "QueryRoot" "name": "QueryRoot"
}, },
"subscriptionType": null, "subscriptionType": {
"name": "SubscriptionRoot"
},
"types": [ "types": [
{ {
"description": null, "description": null,
@ -314,6 +316,16 @@
"name": "Corpus", "name": "Corpus",
"possibleTypes": null "possibleTypes": null
}, },
{
"description": "Implement the DateTime<Utc> scalar\n\nThe input/output is a string in RFC3339 format.",
"enumValues": null,
"fields": null,
"inputFields": null,
"interfaces": null,
"kind": "SCALAR",
"name": "DateTime",
"possibleTypes": null
},
{ {
"description": null, "description": null,
"enumValues": [ "enumValues": [
@ -969,6 +981,51 @@
} }
} }
}, },
{
"args": [
{
"defaultValue": null,
"description": null,
"name": "query",
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "SCALAR",
"name": "String",
"ofType": null
}
}
},
{
"defaultValue": null,
"description": null,
"name": "wakeTime",
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "SCALAR",
"name": "DateTime",
"ofType": null
}
}
}
],
"deprecationReason": null,
"description": null,
"isDeprecated": false,
"name": "snooze",
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "SCALAR",
"name": "Boolean",
"ofType": null
}
}
},
{ {
"args": [], "args": [],
"deprecationReason": null, "deprecationReason": null,
@ -989,7 +1046,7 @@
"inputFields": null, "inputFields": null,
"interfaces": [], "interfaces": [],
"kind": "OBJECT", "kind": "OBJECT",
"name": "Mutation", "name": "MutationRoot",
"possibleTypes": null "possibleTypes": null
}, },
{ {
@ -1474,6 +1531,33 @@
"name": "String", "name": "String",
"possibleTypes": null "possibleTypes": null
}, },
{
"description": null,
"enumValues": null,
"fields": [
{
"args": [],
"deprecationReason": null,
"description": null,
"isDeprecated": false,
"name": "values",
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "SCALAR",
"name": "Int",
"ofType": null
}
}
}
],
"inputFields": null,
"interfaces": [],
"kind": "OBJECT",
"name": "SubscriptionRoot",
"possibleTypes": null
},
{ {
"description": null, "description": null,
"enumValues": null, "enumValues": null,

View File

@ -0,0 +1,4 @@
mutation SnoozeMutation($query: String!, $wakeTime: DateTime!) {
snooze(query: $query, wakeTime: $wakeTime)
}

View File

@ -1,4 +1,4 @@
DEV_HOST=localhost DEV_HOST=localhost
DEV_PORT=9345 DEV_PORT=9345
graphql-client introspect-schema http://${DEV_HOST:?}:${DEV_PORT:?}/api/graphql --output schema.json graphql-client introspect-schema http://${DEV_HOST:?}:${DEV_PORT:?}/api/graphql/ --output schema.json
git diff schema.json git diff schema.json

View File

@ -1,7 +1,9 @@
use chrono::Utc;
use gloo_net::{http::Request, Error}; use gloo_net::{http::Request, Error};
use graphql_client::GraphQLQuery; use graphql_client::GraphQLQuery;
use serde::{de::DeserializeOwned, Serialize}; use serde::{de::DeserializeOwned, Serialize};
type DateTime = chrono::DateTime<Utc>;
// The paths are relative to the directory where your `Cargo.toml` is located. // The paths are relative to the directory where your `Cargo.toml` is located.
// Both json and the GraphQL schema language are supported as sources for the schema // Both json and the GraphQL schema language are supported as sources for the schema
#[derive(GraphQLQuery)] #[derive(GraphQLQuery)]
@ -52,6 +54,14 @@ pub struct AddTagMutation;
)] )]
pub struct RemoveTagMutation; pub struct RemoveTagMutation;
#[derive(GraphQLQuery)]
#[graphql(
schema_path = "graphql/schema.json",
query_path = "graphql/snooze.graphql",
response_derives = "Debug"
)]
pub struct SnoozeMutation;
#[derive(GraphQLQuery)] #[derive(GraphQLQuery)]
#[graphql( #[graphql(
schema_path = "graphql/schema.json", schema_path = "graphql/schema.json",

View File

@ -260,8 +260,28 @@ pub fn update(msg: Msg, model: &mut Model, orders: &mut impl Orders<Msg>) {
Msg::GoToSearchResults Msg::GoToSearchResults
}); });
} }
Msg::Snooze(message_id, snooze_time) => { Msg::Snooze(query, wake_time) => {
info!("TODO: Snoozing {message_id} until {snooze_time}"); let is_catchup = model.catchup.is_some();
orders.skip().perform_cmd(async move {
let res: Result<
graphql_client::Response<graphql::snooze_mutation::ResponseData>,
gloo_net::Error,
> = send_graphql(graphql::SnoozeMutation::build_query(
graphql::snooze_mutation::Variables {
query: query.clone(),
wake_time,
},
))
.await;
if let Err(e) = res {
error!("Failed to snooze {query} until {wake_time}: {e}");
}
if is_catchup {
Msg::CatchupMarkAsRead
} else {
Msg::GoToSearchResults
}
});
} }
Msg::FrontPageRequest { Msg::FrontPageRequest {

View File

@ -727,9 +727,11 @@ fn render_open_header(msg: &ShowThreadQueryThreadOnEmailThreadMessages) -> Node<
C!["flex", "p-4", "bg-neutral-800"], C!["flex", "p-4", "bg-neutral-800"],
div![avatar], div![avatar],
div![ div![
C!["px-4", "mr-auto"], C!["px-4", "flex-1"],
span![ div![
C!["font-semibold", "text-sm"], C!["flex"],
div![
C!["font-semibold", "text-sm", "flex-1"],
from_detail.as_ref().map(|addr| attrs! { from_detail.as_ref().map(|addr| attrs! {
At::Title => addr At::Title => addr
}), }),
@ -737,6 +739,8 @@ fn render_open_header(msg: &ShowThreadQueryThreadOnEmailThreadMessages) -> Node<
" ", " ",
from_detail.as_ref().map(|text| copy_text_widget(&text)) from_detail.as_ref().map(|text| copy_text_widget(&text))
], ],
snooze_buttons(&id),
],
IF!(!msg.to.is_empty() =>div![ IF!(!msg.to.is_empty() =>div![
C!["text-xs"], C!["text-xs"],
span![ span![
@ -799,43 +803,6 @@ fn render_open_header(msg: &ShowThreadQueryThreadOnEmailThreadMessages) -> Node<
}) })
] ]
]), ]),
div![
C!["text-xs"],
span!["Snooze:"],
" ",
a![
"1d",
ev(Ev::Click, {
let id = id.clone();
move |e| {
e.stop_propagation();
Msg::Snooze(id, Utc::now() + chrono::Days::new(1))
}
})
],
" ",
a![
"7d",
ev(Ev::Click, {
let id = id.clone();
move |e| {
e.stop_propagation();
Msg::Snooze(id, Utc::now() + chrono::Days::new(7))
}
})
],
" ",
a![
"6m",
ev(Ev::Click, {
let id = id.clone();
move |e| {
e.stop_propagation();
Msg::Snooze(id, Utc::now() + chrono::Days::new(180))
}
})
],
]
], ],
span![ span![
C!["text-right"], C!["text-right"],
@ -1203,6 +1170,7 @@ fn thread(
let open = open_messages.contains(&msg.id); let open = open_messages.contains(&msg.id);
message_render(&msg, open) message_render(&msg, open)
}); });
let id = &thread.thread_id;
let read_thread_id = thread.thread_id.clone(); let read_thread_id = thread.thread_id.clone();
let unread_thread_id = thread.thread_id.clone(); let unread_thread_id = thread.thread_id.clone();
let spam_add_thread_id = thread.thread_id.clone(); let spam_add_thread_id = thread.thread_id.clone();
@ -1631,9 +1599,13 @@ fn render_news_post_header(post: &ShowThreadQueryThreadOnNewsPost) -> Node<Msg>
C!["flex", "p-4", "bg-neutral-800"], C!["flex", "p-4", "bg-neutral-800"],
div![favicon], div![favicon],
div![ div![
C!["px-4", "mr-auto"], C!["px-4", "mr-auto", "flex-1"],
div![ div![
div![C!["font-semibold", "text-sm"], from], div![
C!["flex"],
div![C!["font-semibold", "text-sm", "flex-1"], from],
snooze_buttons(&id),
],
div![ div![
C!["flex", "gap-2", "pt-2", "text-sm"], C!["flex", "gap-2", "pt-2", "text-sm"],
a![ a![
@ -1728,3 +1700,45 @@ fn click_to_top() -> Node<Msg> {
ev(Ev::Click, |_| Msg::ScrollToTop) ev(Ev::Click, |_| Msg::ScrollToTop)
] ]
} }
fn snooze_buttons(id: &str) -> Node<Msg> {
div![
span![C!["px-2"], ""],
button![
tw_classes::button(),
C!["rounded-r-none"],
"1d",
ev(Ev::Click, {
let id = id.to_string();
move |e| {
e.stop_propagation();
Msg::Snooze(id, Utc::now() + chrono::Days::new(1))
}
})
],
button![
tw_classes::button(),
C!["rounded-none"],
"7d",
ev(Ev::Click, {
let id = id.to_string();
move |e| {
e.stop_propagation();
Msg::Snooze(id, Utc::now() + chrono::Days::new(7))
}
})
],
button![
tw_classes::button(),
C!["rounded-l-none"],
"6m",
ev(Ev::Click, {
let id = id.to_string();
move |e| {
e.stop_propagation();
Msg::Snooze(id, Utc::now() + chrono::Days::new(180))
}
})
],
]
}