]> cat aescling's git repositories - mastodon.git/blob - app/javascript/mastodon/actions/statuses.js
Add support for editing for published statuses (#16697)
[mastodon.git] / app / javascript / mastodon / actions / statuses.js
1 import api from '../api';
2
3 import { deleteFromTimelines } from './timelines';
4 import { importFetchedStatus, importFetchedStatuses, importFetchedAccount } from './importer';
5 import { ensureComposeIsVisible } from './compose';
6
7 export const STATUS_FETCH_REQUEST = 'STATUS_FETCH_REQUEST';
8 export const STATUS_FETCH_SUCCESS = 'STATUS_FETCH_SUCCESS';
9 export const STATUS_FETCH_FAIL = 'STATUS_FETCH_FAIL';
10
11 export const STATUS_DELETE_REQUEST = 'STATUS_DELETE_REQUEST';
12 export const STATUS_DELETE_SUCCESS = 'STATUS_DELETE_SUCCESS';
13 export const STATUS_DELETE_FAIL = 'STATUS_DELETE_FAIL';
14
15 export const CONTEXT_FETCH_REQUEST = 'CONTEXT_FETCH_REQUEST';
16 export const CONTEXT_FETCH_SUCCESS = 'CONTEXT_FETCH_SUCCESS';
17 export const CONTEXT_FETCH_FAIL = 'CONTEXT_FETCH_FAIL';
18
19 export const STATUS_MUTE_REQUEST = 'STATUS_MUTE_REQUEST';
20 export const STATUS_MUTE_SUCCESS = 'STATUS_MUTE_SUCCESS';
21 export const STATUS_MUTE_FAIL = 'STATUS_MUTE_FAIL';
22
23 export const STATUS_UNMUTE_REQUEST = 'STATUS_UNMUTE_REQUEST';
24 export const STATUS_UNMUTE_SUCCESS = 'STATUS_UNMUTE_SUCCESS';
25 export const STATUS_UNMUTE_FAIL = 'STATUS_UNMUTE_FAIL';
26
27 export const STATUS_REVEAL = 'STATUS_REVEAL';
28 export const STATUS_HIDE = 'STATUS_HIDE';
29 export const STATUS_COLLAPSE = 'STATUS_COLLAPSE';
30
31 export const REDRAFT = 'REDRAFT';
32
33 export function fetchStatusRequest(id, skipLoading) {
34 return {
35 type: STATUS_FETCH_REQUEST,
36 id,
37 skipLoading,
38 };
39 };
40
41 export function fetchStatus(id) {
42 return (dispatch, getState) => {
43 const skipLoading = getState().getIn(['statuses', id], null) !== null;
44
45 dispatch(fetchContext(id));
46
47 if (skipLoading) {
48 return;
49 }
50
51 dispatch(fetchStatusRequest(id, skipLoading));
52
53 api(getState).get(`/api/v1/statuses/${id}`).then(response => {
54 dispatch(importFetchedStatus(response.data));
55 dispatch(fetchStatusSuccess(skipLoading));
56 }).catch(error => {
57 dispatch(fetchStatusFail(id, error, skipLoading));
58 });
59 };
60 };
61
62 export function fetchStatusSuccess(skipLoading) {
63 return {
64 type: STATUS_FETCH_SUCCESS,
65 skipLoading,
66 };
67 };
68
69 export function fetchStatusFail(id, error, skipLoading) {
70 return {
71 type: STATUS_FETCH_FAIL,
72 id,
73 error,
74 skipLoading,
75 skipAlert: true,
76 };
77 };
78
79 export function redraft(status, raw_text) {
80 return {
81 type: REDRAFT,
82 status,
83 raw_text,
84 };
85 };
86
87 export function deleteStatus(id, routerHistory, withRedraft = false) {
88 return (dispatch, getState) => {
89 let status = getState().getIn(['statuses', id]);
90
91 if (status.get('poll')) {
92 status = status.set('poll', getState().getIn(['polls', status.get('poll')]));
93 }
94
95 dispatch(deleteStatusRequest(id));
96
97 api(getState).delete(`/api/v1/statuses/${id}`).then(response => {
98 dispatch(deleteStatusSuccess(id));
99 dispatch(deleteFromTimelines(id));
100 dispatch(importFetchedAccount(response.data.account));
101
102 if (withRedraft) {
103 dispatch(redraft(status, response.data.text));
104 ensureComposeIsVisible(getState, routerHistory);
105 }
106 }).catch(error => {
107 dispatch(deleteStatusFail(id, error));
108 });
109 };
110 };
111
112 export function deleteStatusRequest(id) {
113 return {
114 type: STATUS_DELETE_REQUEST,
115 id: id,
116 };
117 };
118
119 export function deleteStatusSuccess(id) {
120 return {
121 type: STATUS_DELETE_SUCCESS,
122 id: id,
123 };
124 };
125
126 export function deleteStatusFail(id, error) {
127 return {
128 type: STATUS_DELETE_FAIL,
129 id: id,
130 error: error,
131 };
132 };
133
134 export const updateStatus = status => dispatch =>
135 dispatch(importFetchedStatus(status));
136
137 export function fetchContext(id) {
138 return (dispatch, getState) => {
139 dispatch(fetchContextRequest(id));
140
141 api(getState).get(`/api/v1/statuses/${id}/context`).then(response => {
142 dispatch(importFetchedStatuses(response.data.ancestors.concat(response.data.descendants)));
143 dispatch(fetchContextSuccess(id, response.data.ancestors, response.data.descendants));
144
145 }).catch(error => {
146 if (error.response && error.response.status === 404) {
147 dispatch(deleteFromTimelines(id));
148 }
149
150 dispatch(fetchContextFail(id, error));
151 });
152 };
153 };
154
155 export function fetchContextRequest(id) {
156 return {
157 type: CONTEXT_FETCH_REQUEST,
158 id,
159 };
160 };
161
162 export function fetchContextSuccess(id, ancestors, descendants) {
163 return {
164 type: CONTEXT_FETCH_SUCCESS,
165 id,
166 ancestors,
167 descendants,
168 statuses: ancestors.concat(descendants),
169 };
170 };
171
172 export function fetchContextFail(id, error) {
173 return {
174 type: CONTEXT_FETCH_FAIL,
175 id,
176 error,
177 skipAlert: true,
178 };
179 };
180
181 export function muteStatus(id) {
182 return (dispatch, getState) => {
183 dispatch(muteStatusRequest(id));
184
185 api(getState).post(`/api/v1/statuses/${id}/mute`).then(() => {
186 dispatch(muteStatusSuccess(id));
187 }).catch(error => {
188 dispatch(muteStatusFail(id, error));
189 });
190 };
191 };
192
193 export function muteStatusRequest(id) {
194 return {
195 type: STATUS_MUTE_REQUEST,
196 id,
197 };
198 };
199
200 export function muteStatusSuccess(id) {
201 return {
202 type: STATUS_MUTE_SUCCESS,
203 id,
204 };
205 };
206
207 export function muteStatusFail(id, error) {
208 return {
209 type: STATUS_MUTE_FAIL,
210 id,
211 error,
212 };
213 };
214
215 export function unmuteStatus(id) {
216 return (dispatch, getState) => {
217 dispatch(unmuteStatusRequest(id));
218
219 api(getState).post(`/api/v1/statuses/${id}/unmute`).then(() => {
220 dispatch(unmuteStatusSuccess(id));
221 }).catch(error => {
222 dispatch(unmuteStatusFail(id, error));
223 });
224 };
225 };
226
227 export function unmuteStatusRequest(id) {
228 return {
229 type: STATUS_UNMUTE_REQUEST,
230 id,
231 };
232 };
233
234 export function unmuteStatusSuccess(id) {
235 return {
236 type: STATUS_UNMUTE_SUCCESS,
237 id,
238 };
239 };
240
241 export function unmuteStatusFail(id, error) {
242 return {
243 type: STATUS_UNMUTE_FAIL,
244 id,
245 error,
246 };
247 };
248
249 export function hideStatus(ids) {
250 if (!Array.isArray(ids)) {
251 ids = [ids];
252 }
253
254 return {
255 type: STATUS_HIDE,
256 ids,
257 };
258 };
259
260 export function revealStatus(ids) {
261 if (!Array.isArray(ids)) {
262 ids = [ids];
263 }
264
265 return {
266 type: STATUS_REVEAL,
267 ids,
268 };
269 };
270
271 export function toggleStatusCollapse(id, isCollapsed) {
272 return {
273 type: STATUS_COLLAPSE,
274 id,
275 isCollapsed,
276 };
277 }
This page took 0.09041 seconds and 4 git commands to generate.