pinafore/routes/_actions/compose.js

106 lines
3.9 KiB
JavaScript
Raw Normal View History

2018-03-09 17:45:12 +01:00
import { store } from '../_store/store'
import { toast } from '../_utils/toast'
2018-03-10 07:31:26 +01:00
import { postStatus as postStatusToServer } from '../_api/statuses'
2018-03-09 17:45:12 +01:00
import { addStatusOrNotification } from './addStatusOrNotification'
import { database } from '../_database/database'
2018-04-09 00:08:32 +02:00
import { emit } from '../_utils/eventBus'
import { putMediaDescription } from '../_api/media'
2018-03-09 17:45:12 +01:00
export async function insertHandleForReply (statusId) {
let instanceName = store.get('currentInstance')
let status = await database.getStatus(instanceName, statusId)
let verifyCredentials = store.get('currentVerifyCredentials')
let originalStatus = status.reblog || status
let accounts = [originalStatus.account].concat(originalStatus.mentions || [])
.filter(account => account.id !== verifyCredentials.id)
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,
sensitive, spoilerText, visibility,
mediaDescriptions = [], inReplyToUuid) {
2018-03-09 17:45:12 +01:00
let instanceName = store.get('currentInstance')
let accessToken = store.get('accessToken')
let online = store.get('online')
if (!online) {
toast.say('You cannot post while offline')
return
}
store.set({
2018-04-09 00:08:32 +02:00
postingStatus: true
2018-03-09 17:45:12 +01:00
})
try {
await Promise.all(mediaDescriptions.map(async (description, i) => {
return description && putMediaDescription(instanceName, accessToken, mediaIds[i], description)
}))
2018-03-09 17:45:12 +01:00
let status = await postStatusToServer(instanceName, accessToken, text,
inReplyToId, mediaIds, sensitive, spoilerText, visibility)
addStatusOrNotification(instanceName, 'home', status)
store.clearComposeData(realm)
emit('postedStatus', realm, inReplyToUuid)
2018-03-09 17:45:12 +01:00
} catch (e) {
2018-04-05 03:33:31 +02:00
console.error(e)
2018-03-09 17:45:12 +01:00
toast.say('Unable to post status: ' + (e.message || ''))
} finally {
store.set({postingStatus: false})
}
}
2018-03-25 03:04:54 +02:00
export async function insertUsername (realm, username, startIndex, endIndex) {
let oldText = store.getComposeData(realm, 'text')
let pre = oldText.substring(0, startIndex)
let post = oldText.substring(endIndex)
let newText = `${pre}@${username} ${post}`
store.setComposeData(realm, {text: newText})
}
export async function clickSelectedAutosuggestionUsername (realm) {
let selectionStart = store.get('composeSelectionStart')
let searchText = store.get('composeAutosuggestionSearchText')
let selection = store.get('composeAutosuggestionSelected') || 0
let account = store.get('composeAutosuggestionSearchResults')[selection]
let startIndex = selectionStart - searchText.length
let endIndex = selectionStart
await insertUsername(realm, account.acct, startIndex, endIndex)
}
export function setReplySpoiler (realm, spoiler) {
let contentWarning = store.getComposeData(realm, 'contentWarning')
let 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 = {
'direct': 1,
'private': 2,
'unlisted': 3,
'public': 4
}
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
2018-04-08 22:42:31 +02:00
let postPrivacy = store.getComposeData(realm, 'postPrivacy')
if (typeof postPrivacy !== 'undefined') {
return // user has already set the postPrivacy
}
2018-04-04 02:50:48 +02:00
let verifyCredentials = store.get('currentVerifyCredentials')
let defaultVisibility = verifyCredentials.source.privacy
let visibility = PRIVACY_LEVEL[replyVisibility] < PRIVACY_LEVEL[defaultVisibility]
? replyVisibility
: defaultVisibility
store.setComposeData(realm, {postPrivacy: visibility})
}