]> cat aescling's git repositories - mastodon.git/blob - app/javascript/flavours/glitch/actions/statuses.js
[Glitch] Fix dimensions of preview cards, fix crash in web UI, fix warning
[mastodon.git] / app / javascript / flavours / glitch / actions / statuses.js
1 import api from 'flavours/glitch/util/api';
2
3 import { deleteFromTimelines } from './timelines';
4
5 export const STATUS_FETCH_REQUEST = 'STATUS_FETCH_REQUEST';
6 export const STATUS_FETCH_SUCCESS = 'STATUS_FETCH_SUCCESS';
7 export const STATUS_FETCH_FAIL = 'STATUS_FETCH_FAIL';
8
9 export const STATUS_DELETE_REQUEST = 'STATUS_DELETE_REQUEST';
10 export const STATUS_DELETE_SUCCESS = 'STATUS_DELETE_SUCCESS';
11 export const STATUS_DELETE_FAIL = 'STATUS_DELETE_FAIL';
12
13 export const CONTEXT_FETCH_REQUEST = 'CONTEXT_FETCH_REQUEST';
14 export const CONTEXT_FETCH_SUCCESS = 'CONTEXT_FETCH_SUCCESS';
15 export const CONTEXT_FETCH_FAIL = 'CONTEXT_FETCH_FAIL';
16
17 export const STATUS_MUTE_REQUEST = 'STATUS_MUTE_REQUEST';
18 export const STATUS_MUTE_SUCCESS = 'STATUS_MUTE_SUCCESS';
19 export const STATUS_MUTE_FAIL = 'STATUS_MUTE_FAIL';
20
21 export const STATUS_UNMUTE_REQUEST = 'STATUS_UNMUTE_REQUEST';
22 export const STATUS_UNMUTE_SUCCESS = 'STATUS_UNMUTE_SUCCESS';
23 export const STATUS_UNMUTE_FAIL = 'STATUS_UNMUTE_FAIL';
24
25 export const REDRAFT = 'REDRAFT';
26
27 export function fetchStatusRequest(id, skipLoading) {
28 return {
29 type: STATUS_FETCH_REQUEST,
30 id,
31 skipLoading,
32 };
33 };
34
35 export function fetchStatus(id) {
36 return (dispatch, getState) => {
37 const skipLoading = getState().getIn(['statuses', id], null) !== null;
38
39 dispatch(fetchContext(id));
40
41 if (skipLoading) {
42 return;
43 }
44
45 dispatch(fetchStatusRequest(id, skipLoading));
46
47 api(getState).get(`/api/v1/statuses/${id}`).then(response => {
48 dispatch(fetchStatusSuccess(response.data, skipLoading));
49 }).catch(error => {
50 dispatch(fetchStatusFail(id, error, skipLoading));
51 });
52 };
53 };
54
55 export function fetchStatusSuccess(status, skipLoading) {
56 return {
57 type: STATUS_FETCH_SUCCESS,
58 status,
59 skipLoading,
60 };
61 };
62
63 export function fetchStatusFail(id, error, skipLoading) {
64 return {
65 type: STATUS_FETCH_FAIL,
66 id,
67 error,
68 skipLoading,
69 skipAlert: true,
70 };
71 };
72
73 export function redraft(status) {
74 return {
75 type: REDRAFT,
76 status,
77 };
78 };
79
80 export function deleteStatus(id, router, withRedraft = false) {
81 return (dispatch, getState) => {
82 const status = getState().getIn(['statuses', id]);
83
84 dispatch(deleteStatusRequest(id));
85
86 api(getState).delete(`/api/v1/statuses/${id}`).then(() => {
87 dispatch(deleteStatusSuccess(id));
88 dispatch(deleteFromTimelines(id));
89
90 if (withRedraft) {
91 dispatch(redraft(status));
92
93 if (!getState().getIn(['compose', 'mounted'])) {
94 router.push('/statuses/new');
95 }
96 }
97 }).catch(error => {
98 dispatch(deleteStatusFail(id, error));
99 });
100 };
101 };
102
103 export function deleteStatusRequest(id) {
104 return {
105 type: STATUS_DELETE_REQUEST,
106 id: id,
107 };
108 };
109
110 export function deleteStatusSuccess(id) {
111 return {
112 type: STATUS_DELETE_SUCCESS,
113 id: id,
114 };
115 };
116
117 export function deleteStatusFail(id, error) {
118 return {
119 type: STATUS_DELETE_FAIL,
120 id: id,
121 error: error,
122 };
123 };
124
125 export function fetchContext(id) {
126 return (dispatch, getState) => {
127 dispatch(fetchContextRequest(id));
128
129 api(getState).get(`/api/v1/statuses/${id}/context`).then(response => {
130 dispatch(fetchContextSuccess(id, response.data.ancestors, response.data.descendants));
131
132 }).catch(error => {
133 if (error.response && error.response.status === 404) {
134 dispatch(deleteFromTimelines(id));
135 }
136
137 dispatch(fetchContextFail(id, error));
138 });
139 };
140 };
141
142 export function fetchContextRequest(id) {
143 return {
144 type: CONTEXT_FETCH_REQUEST,
145 id,
146 };
147 };
148
149 export function fetchContextSuccess(id, ancestors, descendants) {
150 return {
151 type: CONTEXT_FETCH_SUCCESS,
152 id,
153 ancestors,
154 descendants,
155 statuses: ancestors.concat(descendants),
156 };
157 };
158
159 export function fetchContextFail(id, error) {
160 return {
161 type: CONTEXT_FETCH_FAIL,
162 id,
163 error,
164 skipAlert: true,
165 };
166 };
167
168 export function muteStatus(id) {
169 return (dispatch, getState) => {
170 dispatch(muteStatusRequest(id));
171
172 api(getState).post(`/api/v1/statuses/${id}/mute`).then(() => {
173 dispatch(muteStatusSuccess(id));
174 }).catch(error => {
175 dispatch(muteStatusFail(id, error));
176 });
177 };
178 };
179
180 export function muteStatusRequest(id) {
181 return {
182 type: STATUS_MUTE_REQUEST,
183 id,
184 };
185 };
186
187 export function muteStatusSuccess(id) {
188 return {
189 type: STATUS_MUTE_SUCCESS,
190 id,
191 };
192 };
193
194 export function muteStatusFail(id, error) {
195 return {
196 type: STATUS_MUTE_FAIL,
197 id,
198 error,
199 };
200 };
201
202 export function unmuteStatus(id) {
203 return (dispatch, getState) => {
204 dispatch(unmuteStatusRequest(id));
205
206 api(getState).post(`/api/v1/statuses/${id}/unmute`).then(() => {
207 dispatch(unmuteStatusSuccess(id));
208 }).catch(error => {
209 dispatch(unmuteStatusFail(id, error));
210 });
211 };
212 };
213
214 export function unmuteStatusRequest(id) {
215 return {
216 type: STATUS_UNMUTE_REQUEST,
217 id,
218 };
219 };
220
221 export function unmuteStatusSuccess(id) {
222 return {
223 type: STATUS_UNMUTE_SUCCESS,
224 id,
225 };
226 };
227
228 export function unmuteStatusFail(id, error) {
229 return {
230 type: STATUS_UNMUTE_FAIL,
231 id,
232 error,
233 };
234 };
This page took 0.273152 seconds and 4 git commands to generate.