aboutsummaryrefslogtreecommitdiff
path: root/src/services/status_poster/status_poster.service.js
blob: ee8d160a484e7b96160d7660c24349fecb37685e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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) => {
      if (!data.error) {
        store.dispatch('addNewStatuses', {
          statuses: [data],
          timeline: 'friends',
          showImmediately: true,
          noIdUpdate: true // To prevent missing notices on next pull.
        })
      }
      return data
    })
}

const uploadMedia = ({ store, formData }) => {
  const credentials = store.state.users.currentUser.credentials

  return apiService.uploadMedia({ credentials, formData }).then((xml) => {
    // 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 = {
      id: xml.getElementsByTagName('media_id')[0].textContent,
      url: xml.getElementsByTagName('media_url')[0].textContent,
      image: link.getAttribute('href'),
      mimetype: link.getAttribute('type')
    }

    return mediaData
  })
}

const statusPosterService = {
  postStatus,
  uploadMedia
}

export default statusPosterService