Pinafore-Web-Client-Frontend/src/routes/_actions/compose.js

105 lines
4.1 KiB
JavaScript
Raw Normal View History

import { store } from '../_store/store.js'
import { toast } from '../_components/toast/toast.js'
import { postStatus as postStatusToServer } from '../_api/statuses.js'
import { addStatusOrNotification } from './addStatusOrNotification.js'
import { database } from '../_database/database.js'
import { emit } from '../_utils/eventBus.js'
import { putMediaMetadata } from '../_api/media.js'
import { uniqBy } from '../_thirdparty/lodash/objects.js'
import { scheduleIdleTask } from '../_utils/scheduleIdleTask.js'
import { formatIntl } from '../_utils/formatIntl.js'
2018-03-09 17:45:12 +01:00
export async function insertHandleForReply (statusId) {
2019-08-03 22:49:37 +02:00
const { currentInstance } = store.get()
const status = await database.getStatus(currentInstance, statusId)
const { currentVerifyCredentials } = store.get()
const originalStatus = status.reblog || status
let accounts = [originalStatus.account].concat(originalStatus.mentions || [])
.filter(account => account.id !== currentVerifyCredentials.id)
// Pleroma includes account in mentions as well, so make uniq https://github.com/nolanlawson/pinafore/issues/1591
accounts = uniqBy(accounts, _ => _.id)
2018-03-09 17:45:12 +01:00
if (!store.getComposeData(statusId, 'text') && accounts.length) {
store.setComposeData(statusId, {
text: accounts.map(account => `@${account.acct} `).join('')
})
}
}
export async function postStatus (realm, text, inReplyToId, mediaIds,
2018-04-19 05:43:13 +02:00
sensitive, spoilerText, visibility,
mediaDescriptions, inReplyToUuid, poll, mediaFocalPoints) {
2019-08-03 22:49:37 +02:00
const { currentInstance, accessToken, online } = store.get()
2018-03-09 17:45:12 +01:00
if (!online) {
/* no await */ toast.say('intl.cannotPostOffline')
2018-03-09 17:45:12 +01:00
return
}
text = text || ''
2019-08-03 22:49:37 +02:00
const mediaMetadata = (mediaIds || []).map((mediaId, idx) => {
return {
description: mediaDescriptions && mediaDescriptions[idx],
focalPoint: mediaFocalPoints && mediaFocalPoints[idx]
}
2018-03-09 17:45:12 +01:00
})
store.set({ postingStatus: true })
2018-03-09 17:45:12 +01:00
try {
await Promise.all(mediaMetadata.map(async ({ description, focalPoint }, i) => {
description = description || ''
focalPoint = focalPoint || [0, 0]
focalPoint[0] = focalPoint[0] || 0
focalPoint[1] = focalPoint[1] || 0
if (description || focalPoint[0] || focalPoint[1]) {
return putMediaMetadata(currentInstance, accessToken, mediaIds[i], description, focalPoint)
}
}))
2019-08-03 22:49:37 +02:00
const status = await postStatusToServer(currentInstance, accessToken, text,
inReplyToId, mediaIds, sensitive, spoilerText, visibility, poll, mediaFocalPoints)
addStatusOrNotification(currentInstance, 'home', status)
2018-03-09 17:45:12 +01:00
store.clearComposeData(realm)
emit('postedStatus', realm, inReplyToUuid)
scheduleIdleTask(() => (mediaIds || []).forEach(mediaId => database.deleteCachedMediaFile(mediaId))) // clean up media cache
2018-03-09 17:45:12 +01:00
} catch (e) {
2018-04-05 03:33:31 +02:00
console.error(e)
/* no await */ toast.say(formatIntl('intl.unableToPost', { error: (e.message || '') }))
2018-03-09 17:45:12 +01:00
} finally {
store.set({ postingStatus: false })
2018-03-09 17:45:12 +01:00
}
}
2018-03-25 03:04:54 +02:00
export function setReplySpoiler (realm, spoiler) {
2019-08-03 22:49:37 +02:00
const contentWarning = store.getComposeData(realm, 'contentWarning')
const contentWarningShown = store.getComposeData(realm, 'contentWarningShown')
2018-04-08 22:42:31 +02:00
if (typeof contentWarningShown !== 'undefined' || contentWarning) {
return // user has already interacted with the CW
}
2018-04-08 22:42:31 +02:00
store.setComposeData(realm, {
contentWarning: spoiler,
contentWarningShown: true
})
}
2018-04-04 02:50:48 +02:00
const PRIVACY_LEVEL = {
2019-08-03 22:49:37 +02:00
direct: 1,
private: 2,
unlisted: 3,
public: 4
2018-04-04 02:50:48 +02:00
}
export function setReplyVisibility (realm, replyVisibility) {
// return the most private between the user's preferred default privacy
// and the privacy of the status they're replying to
2019-08-03 22:49:37 +02:00
const postPrivacy = store.getComposeData(realm, 'postPrivacy')
2018-04-08 22:42:31 +02:00
if (typeof postPrivacy !== 'undefined') {
return // user has already set the postPrivacy
}
2019-08-03 22:49:37 +02:00
const { currentVerifyCredentials } = store.get()
const defaultVisibility = currentVerifyCredentials.source.privacy
const visibility = PRIVACY_LEVEL[replyVisibility] < PRIVACY_LEVEL[defaultVisibility]
2018-04-04 02:50:48 +02:00
? replyVisibility
: defaultVisibility
store.setComposeData(realm, { postPrivacy: visibility })
2018-04-04 02:50:48 +02:00
}