tooot/src/screens/Compose/utils/post.ts

69 lines
2.1 KiB
TypeScript
Raw Normal View History

2020-12-30 00:56:25 +01:00
import client from '@root/api/client'
2021-01-30 01:29:15 +01:00
import { ComposeState } from '@screens/Compose/utils/types'
import { SharedComposeProp } from '@screens/Tabs/Shared/sharedScreens'
2020-12-30 00:56:25 +01:00
import * as Crypto from 'expo-crypto'
2021-01-01 23:10:47 +01:00
const composePost = async (
2021-01-14 22:53:01 +01:00
params: SharedComposeProp['route']['params'],
2020-12-30 00:56:25 +01:00
composeState: ComposeState
) => {
const formData = new FormData()
2021-01-22 01:34:20 +01:00
if (params?.type === 'reply') {
2020-12-30 00:56:25 +01:00
formData.append('in_reply_to_id', composeState.replyToStatus!.id)
}
if (composeState.spoiler.active) {
formData.append('spoiler_text', composeState.spoiler.raw)
}
formData.append('status', composeState.text.raw)
if (composeState.poll.active) {
2021-01-22 01:34:20 +01:00
Object.values(composeState.poll.options).forEach(
e => e && e.length && formData.append('poll[options][]', e)
)
2020-12-30 00:56:25 +01:00
formData.append('poll[expires_in]', composeState.poll.expire)
formData.append('poll[multiple]', composeState.poll.multiple.toString())
}
if (
composeState.attachments.uploads.filter(
upload => upload.remote && upload.remote.id
).length
) {
formData.append('sensitive', composeState.attachments.sensitive.toString())
composeState.attachments.uploads.forEach(e =>
formData.append('media_ids[]', e.remote!.id!)
)
}
formData.append('visibility', composeState.visibility)
2021-01-07 19:13:09 +01:00
return client<Mastodon.Status>({
2020-12-30 00:56:25 +01:00
method: 'post',
instance: 'local',
2021-01-16 00:00:31 +01:00
url: 'statuses',
2020-12-30 00:56:25 +01:00
headers: {
'Idempotency-Key': await Crypto.digestStringAsync(
Crypto.CryptoDigestAlgorithm.SHA256,
composeState.spoiler.raw +
composeState.text.raw +
composeState.poll.options['0'] +
composeState.poll.options['1'] +
composeState.poll.options['2'] +
composeState.poll.options['3'] +
composeState.poll.multiple +
composeState.poll.expire +
composeState.attachments.sensitive +
composeState.attachments.uploads.map(upload => upload.remote?.id) +
composeState.visibility +
(params?.type === 'edit' ? Math.random() : '')
)
},
body: formData
})
}
2021-01-01 23:10:47 +01:00
export default composePost