1- import { isTimeType , resolveTimeZone } from "../../shared/content/time" ;
1+ import { isTimeType , resolveTimeZone , type TimePrecision } from "../../shared/content/time" ;
22import { reminderDue } from "../../shared/content/timers" ;
33import { LabloomError } from "../../shared/failure" ;
4- import { type Notice } from "../../shared/types/knowledge" ;
4+ import type { AttributeDefinition } from "../../shared/types/attributes" ;
5+ import { type Notice , type NoticeKey } from "../../shared/types/knowledge" ;
56import { reconcileFeedTimer } from "../feed/schedule" ;
67import * as workspace from "../workspace-state" ;
78import * as attributes from "./attributes" ;
@@ -42,27 +43,33 @@ export function completed(pageId: string): boolean {
4243 WHERE v.page_id=? AND a.type='status' AND o.value->>'$.id'=v.value AND o.value->>'$.group'='complete' LIMIT 1` ) . get ( pageId ) ;
4344}
4445
45- export function recalculate ( pageId : string , fields ?: string [ ] , leadOnly ?: number ) : void {
46+ export function recalculate ( pageId : string , fields ?: string [ ] , leadOnly ?: number ) : boolean {
4647 const all = ! fields || fields . includes ( "deleted" ) || fields . some ( id => data . statement ( "SELECT 1 FROM attrs_meta WHERE id=? AND type='status'" ) . get ( id ) ) ;
4748 const feedChange = fields ?. some ( id => [ "feed_url" , "feed_status" , "feed_refresh" , "deleted" ] . includes ( id ) ) ;
4849 const rows = data . statement ( `SELECT ${ columns } FROM timers WHERE page_id=?` + ( all ? "" : " AND attr_id IN (SELECT value FROM json_each(?))" ) ) . all ( ...( all ? [ pageId ] : [ pageId , JSON . stringify ( fields ) ] ) ) as TimerRow [ ] ;
49- if ( ! rows . length && ! feedChange ) return ;
50+ if ( ! rows . length && ! feedChange ) return false ;
5051 const page = { ...data . header ( pageId ) , attributes : data . valuesFor ( [ pageId ] ) . get ( pageId ) ! } ;
5152 const muted = page . attributes . deleted || completed ( pageId ) ;
53+ let noticesChanged = false ;
5254 for ( const row of rows ) {
5355 const definition = attributes . definition ( row . attrId ) ;
5456 if ( ! isTimeType ( definition . type ) ) {
55- if ( row . attrId !== "feed_refresh" || page . kind !== "feed" || definition . type !== "recurrence" ) data . statement ( "DELETE FROM timers WHERE page_id=? AND attr_id=?" ) . run ( pageId , row . attrId ) ;
57+ if ( row . attrId !== "feed_refresh" || page . kind !== "feed" || definition . type !== "recurrence" ) {
58+ data . statement ( "DELETE FROM timers WHERE page_id=? AND attr_id=?" ) . run ( pageId , row . attrId ) ;
59+ noticesChanged ||= row . firedAt !== null ;
60+ }
5661 continue ;
5762 }
5863 if ( leadOnly !== undefined && row . leadSeconds !== leadOnly ) continue ;
5964 const due = muted ? null
6065 : reminderDue ( definition . type , page . attributes [ row . attrId ] as number | null , row . leadSeconds , resolveTimeZone ( workspace . timeZone ( ) ) ) ;
6166 data . statement ( "UPDATE timers SET due_at=?,fired_at=NULL,seen=0 WHERE page_id=? AND attr_id=? AND lead_seconds=?" )
6267 . run ( due !== null && due > now ( ) ? due : null , pageId , row . attrId , row . leadSeconds ) ;
68+ noticesChanged ||= row . firedAt !== null ;
6369 }
6470 if ( page . kind === "feed" && fields && feedChange ) reconcileFeedTimer ( workspace . database ( ) , pageId , page . attributes , now ( ) ,
6571 fields . some ( id => id === "feed_url" || id === "feed_status" ) , fields . includes ( "deleted" ) && ! page . attributes . deleted ) ;
72+ return noticesChanged ;
6673}
6774
6875export function recalculateAll ( ) : void {
@@ -72,20 +79,40 @@ export function recalculateAll(): void {
7279}
7380
7481export function notices ( ) : Notice [ ] {
75-
76- return ( data . statement ( `SELECT t.${ columns . replaceAll ( "," , ",t." ) } FROM timers t JOIN page_attrs d ON d.page_id=t.page_id AND d.attr_id='deleted'
77- WHERE t.fired_at IS NOT NULL AND d.value=0 ORDER BY t.fired_at DESC,t.page_id,t.attr_id,t.lead_seconds` ) . all ( ) as TimerRow [ ] ) . map ( row => notice ( row ) ) ;
82+ const rows = data . statement ( `SELECT t.page_id AS pageId,t.attr_id AS attrId,t.lead_seconds AS leadSeconds,
83+ t.fired_at AS firedAt,t.seen,knowledge_text(b.type,b.content) AS title,
84+ coalesce(a.name,a.id) AS attrName,a.type AS attrType,v.value AS attrValue
85+ FROM timers t JOIN page_attrs d ON d.page_id=t.page_id AND d.attr_id='deleted' AND d.value=0
86+ JOIN pages p ON p.id=t.page_id JOIN blocks b ON b.id=p.title_block_id
87+ JOIN attrs_meta a ON a.id=t.attr_id JOIN page_attrs v ON v.page_id=t.page_id AND v.attr_id=t.attr_id
88+ WHERE t.fired_at IS NOT NULL ORDER BY t.fired_at DESC,t.page_id,t.attr_id,t.lead_seconds` ) . all ( ) as ( Omit < Notice , "seen" > & { seen : number } ) [ ] ;
89+ return rows . filter ( row => isTimeType ( row . attrType ) ) . map ( row => ( { ...row , seen : ! ! row . seen } ) ) ;
7890}
7991
80- export function notice ( row : TimerRow ) : Notice {
92+ export function notice ( row : TimerRow , definition : AttributeDefinition & { type : TimePrecision } , value : number | null ) : Notice {
8193 return {
82- pageId : row . pageId , attrId : row . attrId , leadSeconds : row . leadSeconds , title : data . title ( row . pageId ) ,
83- attrName : attributes . definition ( row . attrId ) . name , firedAt : row . firedAt ! , seen : ! ! row . seen
94+ pageId : row . pageId , attrId : row . attrId , leadSeconds : row . leadSeconds , title : data . title ( row . pageId , "" ) ,
95+ attrName : definition . name , attrType : definition . type , attrValue : value , firedAt : row . firedAt ! , seen : ! ! row . seen
8496 } ;
8597}
8698
87- export function read ( pageId : string , attrId : string , leadSeconds : number ) : void {
88- if ( ! Number . isSafeInteger ( leadSeconds ) || leadSeconds < 0 ) throw new LabloomError ( { kind : "timer-invalid-notice" } ) ;
99+ export function unreadCount ( ) : number {
100+ return Number ( data . statement ( `SELECT count(*) FROM timers t JOIN page_attrs d ON d.page_id=t.page_id AND d.attr_id='deleted' AND d.value=0
101+ WHERE t.fired_at IS NOT NULL AND t.seen=0` ) . pluck ( ) . get ( ) ) ;
102+ }
103+
104+ export function updateNotice ( action : "read" | "unread" | "clear" , { pageId, attrId, leadSeconds, firedAt } : Omit < NoticeKey , "firedAt" > & { firedAt ?: number } ) : number {
105+ if ( ! Number . isSafeInteger ( leadSeconds ) || leadSeconds < 0 || firedAt !== undefined && ! Number . isSafeInteger ( firedAt ) ) {
106+ throw new LabloomError ( { kind : "timer-invalid-notice" } ) ;
107+ }
89108 data . writable ( pageId ) ;
90- data . statement ( "UPDATE timers SET seen=1 WHERE page_id=? AND attr_id=? AND lead_seconds=?" ) . run ( pageId , attrId , leadSeconds ) ;
109+ const writes = { read : "seen=1" , unread : "seen=0" , clear : "fired_at=NULL,seen=0" } ;
110+ return data . statement ( `UPDATE timers SET ${ writes [ action ] } WHERE page_id=? AND attr_id=? AND lead_seconds=?
111+ AND fired_at IS NOT NULL AND (? IS NULL OR fired_at=?)` ) . run ( pageId , attrId , leadSeconds , firedAt ?? null , firedAt ?? null ) . changes ;
112+ }
113+
114+ export function updateAllNotices ( clear : boolean ) : number {
115+ return clear
116+ ? data . statement ( "UPDATE timers SET fired_at=NULL,seen=0 WHERE fired_at IS NOT NULL" ) . run ( ) . changes
117+ : data . statement ( "UPDATE timers SET seen=1 WHERE fired_at IS NOT NULL AND seen=0" ) . run ( ) . changes ;
91118}
0 commit comments