Background:
I’m creating a news area dedicated to the logged in users of a website. For the authentication I’m using Kirby’s native auth & user management.
My goal:
When there a unread ‘news’ – this is, when a logged-in user has not visited the news page – I want to display a hint in the form of a navigation icon, to make him aware of the news waiting for review.
My question:
A basic way to achieve the aforementioned functionality would be to store the viewing of specific pages per user, and then compare timestamps of when pages got updated vs. pages got viewed on a per user case.
Does Kirby already brings some functionality that I can build upon to implement it?
Is there a way to store ‘page views’ apart from storing this info in a cookie?
For any suggestion or hint I would be super thankful.
$cache = kirby()->caches('paulschwarz.news'); // this is the cache from the p
$viewsPages = $cache->get('views-pages-' . $user->id(), []);
// on visit of page
$viewPages[$page->id()] = $page->modified();
// then write
$expire = 0; // aka never
$cache->set('views-pages-' . $user->id(), $viewPages, $expire);
// check if a page has been visited with current timestamp
$timestamp = A::get($viewPages, $page->id(), null); // int or null
if (!$timestamp || $timestamp < $page->modified()) {
// this is a new news
} else {
// user has seen this already
}
(edited: if clause should have been with OR. fixed that)
if you do not like the file to be stored in kirbys cache folder consider creating your own cache driver (based on the file cache driver) and just overwrite the method where it stores the file to from cache to the users dir + filenname.