Handle polls in drafts

This commit is contained in:
tusooa 2023-03-10 20:16:03 -05:00
commit 2a8c7e8e7d
No known key found for this signature in database
GPG key ID: 42AEC43D48433C51
4 changed files with 71 additions and 28 deletions

View file

@ -0,0 +1,36 @@
import * as DateUtils from 'src/services/date_utils/date_utils.js'
import { uniq } from 'lodash'
const pollFallbackValues = {
pollType: 'single',
options: ['', ''],
expiryAmount: 10,
expiryUnit: 'minutes'
}
const pollFallback = (object, attr) => {
return object[attr] !== undefined ? object[attr] : pollFallbackValues[attr]
}
const pollFormToMasto = (poll) => {
const expiresIn = DateUtils.unitToSeconds(
pollFallback(poll, 'expiryUnit'),
pollFallback(poll, 'expiryAmount')
)
const options = uniq(pollFallback(poll, 'options').filter(option => option !== ''))
if (options.length < 2) {
return { errorKey: 'polls.not_enough_options' }
}
return {
options,
multiple: pollFallback(poll, 'pollType') === 'multiple',
expiresIn
}
}
export {
pollFallback,
pollFormToMasto
}