From f0042c548660476f728af7f79eb74c890d45b787 Mon Sep 17 00:00:00 2001 From: dakkar Date: Fri, 31 Dec 2021 16:24:27 +0000 Subject: recent (broken) --- lib/App/MediaControl/DB.rakumod | 30 ++++++++++++++++++++++++++++++ lib/App/MediaControl/Web.rakumod | 4 ++++ resources/index.html | 33 ++++++++++++++++++++++++++++++++- 3 files changed, 66 insertions(+), 1 deletion(-) diff --git a/lib/App/MediaControl/DB.rakumod b/lib/App/MediaControl/DB.rakumod index 34716c5..96fc6cd 100644 --- a/lib/App/MediaControl/DB.rakumod +++ b/lib/App/MediaControl/DB.rakumod @@ -141,4 +141,34 @@ class App::MediaControl::DB { END } } + + method mark-entry-watched(Int:D() $id) { + self!db: { + .query(q:to/END/,:$id,:time(time)); + UPDATE files + SET watched_time=$time + WHERE id=$id + END + } + } + + method get-recently-watched-folders(Int:D() $limit=20) { + self!db: { + .query(qq:to/END/, :$limit).hashes; + WITH recent(id,watched_time) AS ( + SELECT parent_id AS id, MAX(watched_time) AS watched_time + FROM files + WHERE is_dir=false + AND watched_time IS NOT NULL + AND parent_id IS NOT NULL + GROUP BY parent_id + ORDER BY watched_time DESC + LIMIT $limit + ) + SELECT files.id, files.name, files.is_dir, recent.watched_time + FROM files + JOIN recent ON files.id=recent.id + END + } + } } diff --git a/lib/App/MediaControl/Web.rakumod b/lib/App/MediaControl/Web.rakumod index 076e2f2..b6fd85d 100644 --- a/lib/App/MediaControl/Web.rakumod +++ b/lib/App/MediaControl/Web.rakumod @@ -21,6 +21,7 @@ class App::MediaControl::Web { await self.vlc.play-file(|%( $file:p # no comma! )); + self.db.mark-entry-watched($id); } post -> 'pause' { await self.vlc.command('pl_pause') } post -> 'stop' { await self.vlc.command('pl_stop') } @@ -46,6 +47,9 @@ class App::MediaControl::Web { }; content 'application/json', %reply; } + get -> 'recent' { + content 'application/json', self.db.get-recently-watched-folders(); + } }; my $application = route { diff --git a/resources/index.html b/resources/index.html index 042601d..8fb57d2 100644 --- a/resources/index.html +++ b/resources/index.html @@ -103,6 +103,21 @@ pathList.replaceChildren(...pathItems); } + async function loadRecent() { + const recentList = document.querySelector('ul#recent-list'); + const itemTemplate = document.querySelector('#file-item'); + const recentData = await (await call('get','media/recent')).json(); + + const recentItems = recentData.map(f => { + return fillTemplate(itemTemplate, { + 'url': f.is_dir ? `javascript:browseTo(${f.id})` : `javascript:vlcCommand('play/${f.id}')`, + 'name': f.name, + 'dirclass': f.is_dir ? 'dir' : 'file', + }); + }); + recentList.replaceChildren(...recentItems); + } + document.addEventListener('readystatechange', (event) => { if (document.readyState == 'complete') { @@ -117,6 +132,17 @@ { once:true, passive:true }, ); + document.querySelector('#recent'). + addEventListener( + 'toggle', + async (event) => { + if (event.target.open) { + await loadRecent(); + } + }, + { passive:true }, + ); + let vlcUpdateIntervalId; document.querySelector('#vlc-controls'). addEventListener( @@ -134,7 +160,7 @@ { passive:true }, ); } - }) + });