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