feat: added max age support to newsfeed

Hardcoded to 91 days (~3 months).
This commit is contained in:
Bobby Wibowo 2021-01-28 00:18:30 +07:00
parent 220ec14d83
commit ce1e7bb21d
No known key found for this signature in database
GPG Key ID: 51C3A1E1E22D26CF

View File

@ -4,6 +4,7 @@ const newsfeed = {
lsKey: 'newsfeed', lsKey: 'newsfeed',
feedUrl: 'https://blog.fiery.me/rss-newsfeed.xml', feedUrl: 'https://blog.fiery.me/rss-newsfeed.xml',
maxItems: 3, maxItems: 3,
maxAge: 91 * 24 * 60 * 60, // 91 days (~3 months)
dismissed: {}, dismissed: {},
done: false done: false
} }
@ -58,10 +59,7 @@ newsfeed.formatRelativeDate = delta => {
} }
newsfeed.formatNotification = item => { newsfeed.formatNotification = item => {
const parsedDate = newsfeed.simpleParseDate(item.pubDate) const isRecentWeek = item.dateDelta <= (7 * 24 * 60 * 60) // 7 days (1 week)
const dateDelta = Math.round((+new Date() - parsedDate) / 1000)
const isRecentWeek = dateDelta <= 604800
const element = document.createElement('a') const element = document.createElement('a')
element.dataset.identifier = item.identifier element.dataset.identifier = item.identifier
element.className = 'notification is-info' element.className = 'notification is-info'
@ -79,7 +77,7 @@ newsfeed.formatNotification = item => {
: 'N/A'} : 'N/A'}
</div> </div>
<div class="news-date${isRecentWeek ? ' is-recent-week' : ''}"> <div class="news-date${isRecentWeek ? ' is-recent-week' : ''}">
<span title="${parsedDate.toLocaleString()}">${newsfeed.formatRelativeDate(dateDelta)}</span> <span title="${item.parsedDate.toLocaleString()}">${newsfeed.formatRelativeDate(item.dateDelta)}</span>
</div> </div>
<div> <div>
` `
@ -141,8 +139,12 @@ newsfeed.do = () => {
const identifier = title + '|' + description + '|' + pubDate + '|' + link const identifier = title + '|' + description + '|' + pubDate + '|' + link
if (!newsfeed.dismissed[identifier]) { if (!newsfeed.dismissed[identifier]) {
const parsedDate = newsfeed.simpleParseDate(pubDate)
const dateDelta = Math.round((+new Date() - parsedDate) / 1000)
if (typeof newsfeed.maxAge === 'number' && dateDelta <= newsfeed.maxAge) {
const notificationElement = newsfeed.formatNotification({ const notificationElement = newsfeed.formatNotification({
title, description, pubDate, link, identifier title, description, pubDate, link, identifier, parsedDate, dateDelta
}) })
const dismissTrigger = notificationElement.querySelector('.delete') const dismissTrigger = notificationElement.querySelector('.delete')
@ -156,6 +158,7 @@ newsfeed.do = () => {
column.appendChild(notificationElement) column.appendChild(notificationElement)
} }
} }
}
document.body.appendChild(element) document.body.appendChild(element)
} }