aboutsummaryrefslogtreecommitdiff
path: root/src/services/status_poster/status_poster.service.js
blob: 1eb10bb61dc476b57570c46224d3cfc30d9e337c (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import { map } from 'lodash'
import apiService from '../api/api.service.js'

const postStatus = ({
  store,
  status,
  spoilerText,
  visibility,
  sensitive,
  poll,
  media = [],
  inReplyToStatusId = undefined,
  contentType = 'text/plain',
  preview = false,
  idempotencyKey = ''
}) => {
  const mediaIds = map(media, 'id')

  return apiService.postStatus({
    credentials: store.state.users.currentUser.credentials,
    status,
    spoilerText,
    visibility,
    sensitive,
    mediaIds,
    inReplyToStatusId,
    contentType,
    poll,
    preview,
    idempotencyKey
  })
    .then((data) => {
      if (!data.error && !preview) {
        store.dispatch('addNewStatuses', {
          statuses: [data],
          timeline: 'friends',
          showImmediately: true,
          noIdUpdate: true // To prevent missing notices on next pull.
        })
      }
      return data
    })
    .catch((err) => {
      return {
        error: err.message
      }
    })
}

const editStatus = ({
  store,
  statusId,
  status,
  spoilerText,
  sensitive,
  poll,
  media = [],
  contentType = 'text/plain'
}) => {
  const mediaIds = map(media, 'id')

  return apiService.editStatus({
    id: statusId,
    credentials: store.state.users.currentUser.credentials,
    status,
    spoilerText,
    sensitive,
    poll,
    mediaIds,
    contentType
  })
    .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
    })
    .catch((err) => {
      console.error('Error editing status', err)
      return {
        error: err.message
      }
    })
}

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

const setMediaDescription = ({ store, id, description }) => {
  const credentials = store.state.users.currentUser.credentials
  return apiService.setMediaDescription({ credentials, id, description })
}

const statusPosterService = {
  postStatus,
  editStatus,
  uploadMedia,
  setMediaDescription
}

export default statusPosterService