add basic idempotency support

This commit is contained in:
Shpuld Shpuldson 2020-07-15 16:19:57 +03:00
parent 297a0c1f7f
commit 89a677f5e8
3 changed files with 52 additions and 28 deletions

View file

@ -61,6 +61,7 @@ const PostStatusForm = {
StatusContent StatusContent
}, },
mounted () { mounted () {
this.updateIdempotencyKey()
this.resize(this.$refs.textarea) this.resize(this.$refs.textarea)
if (this.replyTo) { if (this.replyTo) {
@ -111,7 +112,8 @@ const PostStatusForm = {
dropStopTimeout: null, dropStopTimeout: null,
preview: null, preview: null,
previewLoading: false, previewLoading: false,
emojiInputShown: false emojiInputShown: false,
idempotencyKey: ''
} }
}, },
computed: { computed: {
@ -214,6 +216,32 @@ const PostStatusForm = {
} }
}, },
methods: { methods: {
clearStatus () {
const newStatus = this.newStatus
this.newStatus = {
status: '',
spoilerText: '',
files: [],
visibility: newStatus.visibility,
contentType: newStatus.contentType,
poll: {},
mediaDescriptions: {}
}
this.pollFormVisible = false
this.$refs.mediaUpload && this.$refs.mediaUpload.clearFile()
this.clearPollForm()
if (this.preserveFocus) {
this.$nextTick(() => {
this.$refs.textarea.focus()
})
}
let el = this.$el.querySelector('textarea')
el.style.height = 'auto'
el.style.height = undefined
this.error = null
this.updateIdempotencyKey()
if (this.preview) this.previewStatus()
},
async postStatus (event, newStatus, opts = {}) { async postStatus (event, newStatus, opts = {}) {
if (this.posting) { return } if (this.posting) { return }
if (this.disableSubmit) { return } if (this.disableSubmit) { return }
@ -253,36 +281,16 @@ const PostStatusForm = {
store: this.$store, store: this.$store,
inReplyToStatusId: this.replyTo, inReplyToStatusId: this.replyTo,
contentType: newStatus.contentType, contentType: newStatus.contentType,
poll poll,
idempotencyKey: this.idempotencyKey
} }
const postHandler = this.postHandler ? this.postHandler : statusPoster.postStatus const postHandler = this.postHandler ? this.postHandler : statusPoster.postStatus
postHandler(postingOptions).then((data) => { postHandler(postingOptions).then((data) => {
if (!data.error) { if (!data.error) {
this.newStatus = { this.clearStatus()
status: '',
spoilerText: '',
files: [],
visibility: newStatus.visibility,
contentType: newStatus.contentType,
poll: {},
mediaDescriptions: {}
}
this.pollFormVisible = false
this.$refs.mediaUpload && this.$refs.mediaUpload.clearFile()
this.clearPollForm()
this.$emit('posted', data) this.$emit('posted', data)
if (this.preserveFocus) {
this.$nextTick(() => {
this.$refs.textarea.focus()
})
}
let el = this.$el.querySelector('textarea')
el.style.height = 'auto'
el.style.height = undefined
this.error = null
if (this.preview) this.previewStatus()
} else { } else {
this.error = data.error this.error = data.error
} }
@ -530,6 +538,9 @@ const PostStatusForm = {
}, },
handleEmojiInputShow (value) { handleEmojiInputShow (value) {
this.emojiInputShown = value this.emojiInputShown = value
},
updateIdempotencyKey () {
this.idempotencyKey = Date.now().toString()
} }
} }
} }

View file

@ -631,7 +631,8 @@ const postStatus = ({
mediaIds = [], mediaIds = [],
inReplyToStatusId, inReplyToStatusId,
contentType, contentType,
preview preview,
idempotencyKey
}) => { }) => {
const form = new FormData() const form = new FormData()
const pollOptions = poll.options || [] const pollOptions = poll.options || []
@ -665,10 +666,15 @@ const postStatus = ({
form.append('preview', 'true') form.append('preview', 'true')
} }
let postHeaders = authHeaders(credentials)
if (idempotencyKey) {
postHeaders['idempotency-key'] = idempotencyKey
}
return fetch(MASTODON_POST_STATUS_URL, { return fetch(MASTODON_POST_STATUS_URL, {
body: form, body: form,
method: 'POST', method: 'POST',
headers: authHeaders(credentials) headers: postHeaders
}) })
.then((response) => { .then((response) => {
return response.json() return response.json()

View file

@ -11,7 +11,8 @@ const postStatus = ({
media = [], media = [],
inReplyToStatusId = undefined, inReplyToStatusId = undefined,
contentType = 'text/plain', contentType = 'text/plain',
preview = false preview = false,
idempotencyKey = ''
}) => { }) => {
const mediaIds = map(media, 'id') const mediaIds = map(media, 'id')
@ -25,9 +26,14 @@ const postStatus = ({
inReplyToStatusId, inReplyToStatusId,
contentType, contentType,
poll, poll,
preview preview,
idempotencyKey
}) })
.then((data) => { .then((data) => {
return {
error: 'test'
}
/*
if (!data.error && !preview) { if (!data.error && !preview) {
store.dispatch('addNewStatuses', { store.dispatch('addNewStatuses', {
statuses: [data], statuses: [data],
@ -37,6 +43,7 @@ const postStatus = ({
}) })
} }
return data return data
*/
}) })
.catch((err) => { .catch((err) => {
return { return {