2018-03-09 08:45:12 -08:00
|
|
|
import { store } from '../_store/store'
|
2018-12-22 15:37:51 -08:00
|
|
|
import { toast } from '../_components/toast/toast'
|
2018-03-09 22:31:26 -08:00
|
|
|
import { postStatus as postStatusToServer } from '../_api/statuses'
|
2018-03-09 08:45:12 -08:00
|
|
|
import { addStatusOrNotification } from './addStatusOrNotification'
|
2018-08-29 19:03:12 -07:00
|
|
|
import { database } from '../_database/database'
|
2018-04-08 15:08:32 -07:00
|
|
|
import { emit } from '../_utils/eventBus'
|
2019-07-07 00:14:19 -07:00
|
|
|
import { putMediaMetadata } from '../_api/media'
|
2019-10-16 18:09:19 -07:00
|
|
|
import uniqBy from 'lodash-es/uniqBy'
|
2020-11-24 15:37:10 -08:00
|
|
|
import { deleteCachedMediaFile } from '../_utils/mediaUploadFileCache'
|
|
|
|
import { scheduleIdleTask } from '../_utils/scheduleIdleTask'
|
2020-11-29 14:13:27 -08:00
|
|
|
import { formatIntl } from '../_utils/formatIntl'
|
2018-03-09 08:45:12 -08:00
|
|
|
|
|
|
|
export async function insertHandleForReply (statusId) {
|
2019-08-03 13:49:37 -07:00
|
|
|
const { currentInstance } = store.get()
|
|
|
|
const status = await database.getStatus(currentInstance, statusId)
|
|
|
|
const { currentVerifyCredentials } = store.get()
|
|
|
|
const originalStatus = status.reblog || status
|
2019-10-16 18:09:19 -07:00
|
|
|
let accounts = [originalStatus.account].concat(originalStatus.mentions || [])
|
2018-04-19 09:37:05 -07:00
|
|
|
.filter(account => account.id !== currentVerifyCredentials.id)
|
2019-10-16 18:09:19 -07:00
|
|
|
// Pleroma includes account in mentions as well, so make uniq https://github.com/nolanlawson/pinafore/issues/1591
|
|
|
|
accounts = uniqBy(accounts, _ => _.id)
|
2018-03-09 08:45:12 -08: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-18 20:43:13 -07:00
|
|
|
sensitive, spoilerText, visibility,
|
2019-07-07 00:14:19 -07:00
|
|
|
mediaDescriptions, inReplyToUuid, poll, mediaFocalPoints) {
|
2019-08-03 13:49:37 -07:00
|
|
|
const { currentInstance, accessToken, online } = store.get()
|
2018-03-09 08:45:12 -08:00
|
|
|
|
|
|
|
if (!online) {
|
2020-11-29 14:13:27 -08:00
|
|
|
/* no await */ toast.say('intl.cannotPostOffline')
|
2018-03-09 08:45:12 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-26 18:54:59 -07:00
|
|
|
text = text || ''
|
|
|
|
|
2019-08-03 13:49:37 -07:00
|
|
|
const mediaMetadata = (mediaIds || []).map((mediaId, idx) => {
|
2019-07-07 00:14:19 -07:00
|
|
|
return {
|
|
|
|
description: mediaDescriptions && mediaDescriptions[idx],
|
|
|
|
focalPoint: mediaFocalPoints && mediaFocalPoints[idx]
|
|
|
|
}
|
2018-03-09 08:45:12 -08:00
|
|
|
})
|
2019-07-07 00:14:19 -07:00
|
|
|
|
|
|
|
store.set({ postingStatus: true })
|
2018-03-09 08:45:12 -08:00
|
|
|
try {
|
2019-07-07 00:14:19 -07:00
|
|
|
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)
|
|
|
|
}
|
2018-04-09 18:30:15 -07:00
|
|
|
}))
|
2019-08-03 13:49:37 -07:00
|
|
|
const status = await postStatusToServer(currentInstance, accessToken, text,
|
2019-07-07 00:14:19 -07:00
|
|
|
inReplyToId, mediaIds, sensitive, spoilerText, visibility, poll, mediaFocalPoints)
|
2018-04-19 09:37:05 -07:00
|
|
|
addStatusOrNotification(currentInstance, 'home', status)
|
2018-03-09 08:45:12 -08:00
|
|
|
store.clearComposeData(realm)
|
2018-04-12 21:18:14 -07:00
|
|
|
emit('postedStatus', realm, inReplyToUuid)
|
2020-11-24 15:37:10 -08:00
|
|
|
scheduleIdleTask(() => (mediaIds || []).forEach(mediaId => deleteCachedMediaFile(mediaId))) // clean up media cache
|
2018-03-09 08:45:12 -08:00
|
|
|
} catch (e) {
|
2018-04-04 18:33:31 -07:00
|
|
|
console.error(e)
|
2020-11-29 14:13:27 -08:00
|
|
|
/* no await */ toast.say(formatIntl('intl.unableToPost', { error: (e.message || '') }))
|
2018-03-09 08:45:12 -08:00
|
|
|
} finally {
|
2018-08-29 21:42:57 -07:00
|
|
|
store.set({ postingStatus: false })
|
2018-03-09 08:45:12 -08:00
|
|
|
}
|
|
|
|
}
|
2018-03-24 18:04:54 -07:00
|
|
|
|
2018-04-03 09:45:17 -07:00
|
|
|
export function setReplySpoiler (realm, spoiler) {
|
2019-08-03 13:49:37 -07:00
|
|
|
const contentWarning = store.getComposeData(realm, 'contentWarning')
|
|
|
|
const contentWarningShown = store.getComposeData(realm, 'contentWarningShown')
|
2018-04-08 13:42:31 -07:00
|
|
|
if (typeof contentWarningShown !== 'undefined' || contentWarning) {
|
|
|
|
return // user has already interacted with the CW
|
2018-04-03 09:45:17 -07:00
|
|
|
}
|
2018-04-08 13:42:31 -07:00
|
|
|
store.setComposeData(realm, {
|
|
|
|
contentWarning: spoiler,
|
|
|
|
contentWarningShown: true
|
|
|
|
})
|
2018-04-03 09:45:17 -07:00
|
|
|
}
|
2018-04-03 17:50:48 -07:00
|
|
|
|
|
|
|
const PRIVACY_LEVEL = {
|
2019-08-03 13:49:37 -07:00
|
|
|
direct: 1,
|
|
|
|
private: 2,
|
|
|
|
unlisted: 3,
|
|
|
|
public: 4
|
2018-04-03 17:50:48 -07: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 13:49:37 -07:00
|
|
|
const postPrivacy = store.getComposeData(realm, 'postPrivacy')
|
2018-04-08 13:42:31 -07:00
|
|
|
if (typeof postPrivacy !== 'undefined') {
|
|
|
|
return // user has already set the postPrivacy
|
|
|
|
}
|
2019-08-03 13:49:37 -07:00
|
|
|
const { currentVerifyCredentials } = store.get()
|
|
|
|
const defaultVisibility = currentVerifyCredentials.source.privacy
|
|
|
|
const visibility = PRIVACY_LEVEL[replyVisibility] < PRIVACY_LEVEL[defaultVisibility]
|
2018-04-03 17:50:48 -07:00
|
|
|
? replyVisibility
|
|
|
|
: defaultVisibility
|
2018-08-29 21:42:57 -07:00
|
|
|
store.setComposeData(realm, { postPrivacy: visibility })
|
2018-04-03 17:50:48 -07:00
|
|
|
}
|