2016-10-31 02:53:58 +11:00
|
|
|
import { map } from 'lodash'
|
|
|
|
import apiService from '../api/api.service.js'
|
|
|
|
|
|
|
|
const postStatus = ({ store, status, media = [], inReplyToStatusId = undefined }) => {
|
|
|
|
const mediaIds = map(media, 'id')
|
|
|
|
|
|
|
|
return apiService.postStatus({credentials: store.state.users.currentUser.credentials, status, mediaIds, inReplyToStatusId})
|
|
|
|
.then((data) => data.json())
|
|
|
|
.then((data) => {
|
2017-08-20 20:16:41 +10:00
|
|
|
if (!data.error) {
|
|
|
|
store.dispatch('addNewStatuses', {
|
|
|
|
statuses: [data],
|
|
|
|
timeline: 'friends',
|
|
|
|
showImmediately: true,
|
|
|
|
noIdUpdate: true // To prevent missing notices on next pull.
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return data
|
2016-10-31 02:53:58 +11:00
|
|
|
})
|
2017-08-21 22:35:14 +10:00
|
|
|
.catch((err) => {
|
|
|
|
return {
|
|
|
|
error: err.message
|
|
|
|
}
|
|
|
|
})
|
2016-10-31 02:53:58 +11:00
|
|
|
}
|
|
|
|
|
2016-11-07 05:30:20 +11:00
|
|
|
const uploadMedia = ({ store, formData }) => {
|
|
|
|
const credentials = store.state.users.currentUser.credentials
|
|
|
|
|
|
|
|
return apiService.uploadMedia({ credentials, formData }).then((xml) => {
|
2017-01-21 09:58:58 +11:00
|
|
|
// Firefox and Chrome treat method differently...
|
|
|
|
let link = xml.getElementsByTagName('link')
|
|
|
|
|
|
|
|
if (link.length === 0) {
|
|
|
|
link = xml.getElementsByTagName('atom:link')
|
|
|
|
}
|
|
|
|
|
|
|
|
link = link[0]
|
|
|
|
|
|
|
|
const mediaData = {
|
2016-11-07 05:30:20 +11:00
|
|
|
id: xml.getElementsByTagName('media_id')[0].textContent,
|
|
|
|
url: xml.getElementsByTagName('media_url')[0].textContent,
|
2017-01-21 09:58:58 +11:00
|
|
|
image: link.getAttribute('href'),
|
|
|
|
mimetype: link.getAttribute('type')
|
2016-11-07 05:30:20 +11:00
|
|
|
}
|
2017-01-21 09:58:58 +11:00
|
|
|
|
|
|
|
return mediaData
|
2016-11-07 05:30:20 +11:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-10-31 02:53:58 +11:00
|
|
|
const statusPosterService = {
|
2016-11-07 05:30:20 +11:00
|
|
|
postStatus,
|
|
|
|
uploadMedia
|
2016-10-31 02:53:58 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
export default statusPosterService
|