fix: polls without expiry date

Fixes #2112
This commit is contained in:
Nolan Lawson 2022-05-14 10:38:21 -07:00
parent e3a7b783ed
commit 68c97b7522
3 changed files with 16 additions and 6 deletions

View File

@ -306,7 +306,8 @@
multiple: ({ poll }) => poll.multiple, multiple: ({ poll }) => poll.multiple,
expired: ({ poll }) => poll.expired, expired: ({ poll }) => poll.expired,
expiresAt: ({ poll }) => poll.expires_at, expiresAt: ({ poll }) => poll.expires_at,
expiresAtTS: ({ expiresAt }) => new Date(expiresAt).getTime(), // Misskey can have polls that never end. These give expiresAt as null
expiresAtTS: ({ expiresAt }) => typeof expiresAt === 'number' ? new Date(expiresAt).getTime() : null,
expiresAtTimeagoFormatted: ({ expiresAtTS, expired, $now }) => ( expiresAtTimeagoFormatted: ({ expiresAtTS, expired, $now }) => (
expired ? formatTimeagoDate(expiresAtTS, $now) : formatTimeagoFutureDate(expiresAtTS, $now) expired ? formatTimeagoDate(expiresAtTS, $now) : formatTimeagoFutureDate(expiresAtTS, $now)
), ),

View File

@ -3,6 +3,9 @@ import { mark, stop } from '../_utils/marks.js'
// Format a date in the past // Format a date in the past
export function formatTimeagoDate (date, now) { export function formatTimeagoDate (date, now) {
if (typeof date !== 'number') { // means "never" in Misskey
return 'intl.never'
}
mark('formatTimeagoDate') mark('formatTimeagoDate')
// use Math.min() to avoid things like "in 10 seconds" when the timestamps are slightly off // use Math.min() to avoid things like "in 10 seconds" when the timestamps are slightly off
const res = format(Math.min(0, date - now)) const res = format(Math.min(0, date - now))
@ -12,6 +15,9 @@ export function formatTimeagoDate (date, now) {
// Format a date in the future // Format a date in the future
export function formatTimeagoFutureDate (date, now) { export function formatTimeagoFutureDate (date, now) {
if (typeof date !== 'number') { // means "never" in Misskey
return 'intl.never'
}
mark('formatTimeagoFutureDate') mark('formatTimeagoFutureDate')
// use Math.max() for same reason as above // use Math.max() for same reason as above
const res = format(Math.max(0, date - now)) const res = format(Math.max(0, date - now))

View File

@ -2,15 +2,18 @@ import { LOCALE } from '../_static/intl.js'
import { thunk } from './thunk.js' import { thunk } from './thunk.js'
const safeFormatter = (formatter) => { const safeFormatter = (formatter) => {
// The fediverse is wild, so invalid dates may exist. Don't fail with a fatal error in that case.
// https://github.com/nolanlawson/pinafore/issues/2113
return { return {
format (str) { format (date) {
if (typeof date !== 'number') {
return 'intl.never' // null means "never" in Misskey
}
try { try {
return formatter.format(str) return formatter.format(date)
} catch (e) { } catch (e) {
if (e instanceof RangeError) { if (e instanceof RangeError) {
return 'N/A' // The fediverse is wild, so invalid dates may exist. Don't fail with a fatal error in that case.
// https://github.com/nolanlawson/pinafore/issues/2113
return 'intl.never'
} }
throw e throw e
} }