2018-06-10 05:25:39 +02:00
|
|
|
import Mastodon from 'megalodon'
|
2018-04-05 14:04:40 +02:00
|
|
|
import { ipcRenderer } from 'electron'
|
2018-03-14 13:27:26 +01:00
|
|
|
|
2018-03-29 04:25:35 +02:00
|
|
|
const NewToot = {
|
2018-03-14 13:27:26 +01:00
|
|
|
namespaced: true,
|
|
|
|
state: {
|
2018-03-14 14:19:17 +01:00
|
|
|
modalOpen: false,
|
|
|
|
status: '',
|
2018-03-29 06:05:19 +02:00
|
|
|
replyToMessage: null,
|
|
|
|
blockSubmit: false,
|
2018-04-11 16:12:51 +02:00
|
|
|
attachedMedias: [],
|
2018-05-24 16:38:24 +02:00
|
|
|
visibility: 'public',
|
2018-05-25 01:31:41 +02:00
|
|
|
sensitive: false,
|
2018-07-05 15:15:31 +02:00
|
|
|
spoiler: '',
|
|
|
|
attachedMediaId: 0
|
2018-03-14 13:27:26 +01:00
|
|
|
},
|
|
|
|
mutations: {
|
|
|
|
changeModal (state, value) {
|
|
|
|
state.modalOpen = value
|
2018-03-14 14:19:17 +01:00
|
|
|
},
|
|
|
|
setReplyTo (state, message) {
|
|
|
|
state.replyToMessage = message
|
|
|
|
},
|
|
|
|
updateStatus (state, status) {
|
|
|
|
state.status = status
|
2018-03-29 06:05:19 +02:00
|
|
|
},
|
|
|
|
changeBlockSubmit (state, value) {
|
|
|
|
state.blockSubmit = value
|
|
|
|
},
|
|
|
|
appendAttachedMedias (state, media) {
|
|
|
|
state.attachedMedias = state.attachedMedias.concat([media])
|
2018-03-29 06:16:15 +02:00
|
|
|
},
|
|
|
|
clearAttachedMedias (state) {
|
|
|
|
state.attachedMedias = []
|
2018-03-29 08:49:39 +02:00
|
|
|
},
|
|
|
|
removeMedia (state, media) {
|
|
|
|
state.attachedMedias = state.attachedMedias.filter(m => m.id !== media.id)
|
2018-04-11 16:12:51 +02:00
|
|
|
},
|
|
|
|
changeVisibility (state, value) {
|
|
|
|
state.visibility = value
|
2018-05-24 16:38:24 +02:00
|
|
|
},
|
|
|
|
changeSensitive (state, value) {
|
|
|
|
state.sensitive = value
|
2018-05-25 01:31:41 +02:00
|
|
|
},
|
|
|
|
updateSpoiler (state, value) {
|
|
|
|
state.spoiler = value
|
2018-07-05 15:15:31 +02:00
|
|
|
},
|
|
|
|
updateMediaId (state, value) {
|
|
|
|
state.attachedMediaId = value
|
2018-03-14 13:27:26 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
actions: {
|
|
|
|
postToot ({ state, commit, rootState }, form) {
|
2018-06-10 05:25:39 +02:00
|
|
|
if (rootState.TimelineSpace.account.accessToken === undefined || rootState.TimelineSpace.account.accessToken === null) {
|
|
|
|
throw new AuthenticationError()
|
|
|
|
}
|
|
|
|
const client = new Mastodon(
|
|
|
|
rootState.TimelineSpace.account.accessToken,
|
|
|
|
rootState.TimelineSpace.account.baseURL + '/api/v1'
|
|
|
|
)
|
|
|
|
return client.post('/statuses', form)
|
|
|
|
.then(data => {
|
2018-04-07 16:18:20 +02:00
|
|
|
ipcRenderer.send('toot-action-sound')
|
2018-06-10 05:25:39 +02:00
|
|
|
return data
|
2018-03-14 13:27:26 +01:00
|
|
|
})
|
2018-03-14 14:19:17 +01:00
|
|
|
},
|
2018-05-23 17:26:31 +02:00
|
|
|
openReply ({ commit, rootState }, message) {
|
2018-03-14 14:19:17 +01:00
|
|
|
commit('setReplyTo', message)
|
2018-05-23 17:26:31 +02:00
|
|
|
const mentionAccounts = [message.account.acct].concat(message.mentions.map(a => a.acct))
|
|
|
|
.filter((a, i, self) => self.indexOf(a) === i)
|
|
|
|
.filter((a) => a !== rootState.TimelineSpace.account.username)
|
2018-06-19 15:44:33 +02:00
|
|
|
commit('changeModal', true)
|
2018-05-29 16:15:48 +02:00
|
|
|
commit('updateStatus', `${mentionAccounts.map(m => `@${m}`).join(' ')} `)
|
2018-06-19 15:44:33 +02:00
|
|
|
commit('changeVisibility', message.visibility)
|
|
|
|
},
|
|
|
|
openModal ({ commit }) {
|
2018-03-14 14:19:17 +01:00
|
|
|
commit('changeModal', true)
|
2018-03-24 01:43:37 +01:00
|
|
|
},
|
2018-06-19 15:44:33 +02:00
|
|
|
closeModal ({ commit }) {
|
|
|
|
commit('changeModal', false)
|
|
|
|
commit('updateStatus', '')
|
|
|
|
commit('setReplyTo', null)
|
|
|
|
commit('changeBlockSubmit', false)
|
|
|
|
commit('clearAttachedMedias')
|
|
|
|
commit('changeSensitive', false)
|
|
|
|
commit('updateSpoiler', '')
|
|
|
|
commit('changeVisibility', 'public')
|
2018-03-29 06:05:19 +02:00
|
|
|
},
|
|
|
|
uploadImage ({ state, commit, rootState }, image) {
|
2018-06-10 05:25:39 +02:00
|
|
|
commit('changeBlockSubmit', true)
|
|
|
|
if (rootState.TimelineSpace.account.accessToken === undefined || rootState.TimelineSpace.account.accessToken === null) {
|
|
|
|
throw new AuthenticationError()
|
|
|
|
}
|
|
|
|
const client = new Mastodon(
|
|
|
|
rootState.TimelineSpace.account.accessToken,
|
|
|
|
rootState.TimelineSpace.account.baseURL + '/api/v1'
|
|
|
|
)
|
|
|
|
const formData = new FormData()
|
|
|
|
formData.append('file', image)
|
|
|
|
return client.post('/media', formData)
|
|
|
|
.then(data => {
|
2018-03-29 06:05:19 +02:00
|
|
|
commit('changeBlockSubmit', false)
|
2018-06-16 09:59:42 +02:00
|
|
|
if (data.type === 'unknown') throw new UnknownTypeError()
|
2018-03-29 06:05:19 +02:00
|
|
|
commit('appendAttachedMedias', data)
|
2018-06-10 05:25:39 +02:00
|
|
|
return data
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
commit('changeBlockSubmit', false)
|
|
|
|
console.error(err)
|
|
|
|
throw err
|
2018-03-29 06:05:19 +02:00
|
|
|
})
|
2018-07-05 15:15:31 +02:00
|
|
|
},
|
|
|
|
incrementMediaId ({ commit, state }) {
|
|
|
|
commit('updateMediaId', state.attachedMediaId + 1)
|
|
|
|
},
|
|
|
|
resetMediaId ({ commit }) {
|
|
|
|
commit('updateMediaId', 0)
|
2018-03-14 13:27:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-29 04:25:35 +02:00
|
|
|
export default NewToot
|
2018-03-14 13:27:26 +01:00
|
|
|
|
|
|
|
class AuthenticationError {}
|
2018-03-29 06:05:19 +02:00
|
|
|
class UnknownTypeError {}
|