]>
cat aescling's git repositories - mastodon.git/blob - app/javascript/mastodon/actions/statuses.js
1 import api
from '../api';
2 import openDB
from '../storage/db';
3 import { evictStatus
} from '../storage/modifier';
5 import { deleteFromTimelines
} from './timelines';
6 import { importFetchedStatus
, importFetchedStatuses
, importAccount
, importStatus
} from './importer';
7 import { ensureComposeIsVisible
} from './compose';
9 export const STATUS_FETCH_REQUEST
= 'STATUS_FETCH_REQUEST';
10 export const STATUS_FETCH_SUCCESS
= 'STATUS_FETCH_SUCCESS';
11 export const STATUS_FETCH_FAIL
= 'STATUS_FETCH_FAIL';
13 export const STATUS_DELETE_REQUEST
= 'STATUS_DELETE_REQUEST';
14 export const STATUS_DELETE_SUCCESS
= 'STATUS_DELETE_SUCCESS';
15 export const STATUS_DELETE_FAIL
= 'STATUS_DELETE_FAIL';
17 export const CONTEXT_FETCH_REQUEST
= 'CONTEXT_FETCH_REQUEST';
18 export const CONTEXT_FETCH_SUCCESS
= 'CONTEXT_FETCH_SUCCESS';
19 export const CONTEXT_FETCH_FAIL
= 'CONTEXT_FETCH_FAIL';
21 export const STATUS_MUTE_REQUEST
= 'STATUS_MUTE_REQUEST';
22 export const STATUS_MUTE_SUCCESS
= 'STATUS_MUTE_SUCCESS';
23 export const STATUS_MUTE_FAIL
= 'STATUS_MUTE_FAIL';
25 export const STATUS_UNMUTE_REQUEST
= 'STATUS_UNMUTE_REQUEST';
26 export const STATUS_UNMUTE_SUCCESS
= 'STATUS_UNMUTE_SUCCESS';
27 export const STATUS_UNMUTE_FAIL
= 'STATUS_UNMUTE_FAIL';
29 export const STATUS_REVEAL
= 'STATUS_REVEAL';
30 export const STATUS_HIDE
= 'STATUS_HIDE';
31 export const STATUS_COLLAPSE
= 'STATUS_COLLAPSE';
33 export const REDRAFT
= 'REDRAFT';
35 export function fetchStatusRequest(id
, skipLoading
) {
37 type: STATUS_FETCH_REQUEST
,
43 function getFromDB(dispatch
, getState
, accountIndex
, index
, id
) {
44 return new Promise((resolve
, reject
) => {
45 const request
= index
.get(id
);
47 request
.onerror
= reject
;
49 request
.onsuccess
= () => {
52 if (!request
.result
) {
57 dispatch(importStatus(request
.result
));
59 if (getState().getIn(['accounts', request
.result
.account
], null) === null) {
60 promises
.push(new Promise((accountResolve
, accountReject
) => {
61 const accountRequest
= accountIndex
.get(request
.result
.account
);
63 accountRequest
.onerror
= accountReject
;
64 accountRequest
.onsuccess
= () => {
65 if (!request
.result
) {
70 dispatch(importAccount(accountRequest
.result
));
76 if (request
.result
.reblog
&& getState().getIn(['statuses', request
.result
.reblog
], null) === null) {
77 promises
.push(getFromDB(dispatch
, getState
, accountIndex
, index
, request
.result
.reblog
));
80 resolve(Promise
.all(promises
));
85 export function fetchStatus(id
) {
86 return (dispatch
, getState
) => {
87 const skipLoading
= getState().getIn(['statuses', id
], null) !== null;
89 dispatch(fetchContext(id
));
95 dispatch(fetchStatusRequest(id
, skipLoading
));
98 const transaction
= db
.transaction(['accounts', 'statuses'], 'read');
99 const accountIndex
= transaction
.objectStore('accounts').index('id');
100 const index
= transaction
.objectStore('statuses').index('id');
102 return getFromDB(dispatch
, getState
, accountIndex
, index
, id
).then(() => {
109 dispatch(fetchStatusSuccess(skipLoading
));
110 }, () => api(getState
).get(`/api/v1/statuses/${id}`).then(response
=> {
111 dispatch(importFetchedStatus(response
.data
));
112 dispatch(fetchStatusSuccess(skipLoading
));
114 dispatch(fetchStatusFail(id
, error
, skipLoading
));
119 export function fetchStatusSuccess(skipLoading
) {
121 type: STATUS_FETCH_SUCCESS
,
126 export function fetchStatusFail(id
, error
, skipLoading
) {
128 type: STATUS_FETCH_FAIL
,
136 export function redraft(status
, raw_text
) {
144 export function deleteStatus(id
, routerHistory
, withRedraft
= false) {
145 return (dispatch
, getState
) => {
146 let status
= getState().getIn(['statuses', id
]);
148 if (status
.get('poll')) {
149 status
= status
.set('poll', getState().getIn(['polls', status
.get('poll')]));
152 dispatch(deleteStatusRequest(id
));
154 api(getState
).delete(`/api/v1/statuses/${id}`).then(response
=> {
156 dispatch(deleteStatusSuccess(id
));
157 dispatch(deleteFromTimelines(id
));
160 dispatch(redraft(status
, response
.data
.text
));
161 ensureComposeIsVisible(getState
, routerHistory
);
164 dispatch(deleteStatusFail(id
, error
));
169 export function deleteStatusRequest(id
) {
171 type: STATUS_DELETE_REQUEST
,
176 export function deleteStatusSuccess(id
) {
178 type: STATUS_DELETE_SUCCESS
,
183 export function deleteStatusFail(id
, error
) {
185 type: STATUS_DELETE_FAIL
,
191 export function fetchContext(id
) {
192 return (dispatch
, getState
) => {
193 dispatch(fetchContextRequest(id
));
195 api(getState
).get(`/api/v1/statuses/${id}/context`).then(response
=> {
196 dispatch(importFetchedStatuses(response
.data
.ancestors
.concat(response
.data
.descendants
)));
197 dispatch(fetchContextSuccess(id
, response
.data
.ancestors
, response
.data
.descendants
));
200 if (error
.response
&& error
.response
.status
=== 404) {
201 dispatch(deleteFromTimelines(id
));
204 dispatch(fetchContextFail(id
, error
));
209 export function fetchContextRequest(id
) {
211 type: CONTEXT_FETCH_REQUEST
,
216 export function fetchContextSuccess(id
, ancestors
, descendants
) {
218 type: CONTEXT_FETCH_SUCCESS
,
222 statuses: ancestors
.concat(descendants
),
226 export function fetchContextFail(id
, error
) {
228 type: CONTEXT_FETCH_FAIL
,
235 export function muteStatus(id
) {
236 return (dispatch
, getState
) => {
237 dispatch(muteStatusRequest(id
));
239 api(getState
).post(`/api/v1/statuses/${id}/mute`).then(() => {
240 dispatch(muteStatusSuccess(id
));
242 dispatch(muteStatusFail(id
, error
));
247 export function muteStatusRequest(id
) {
249 type: STATUS_MUTE_REQUEST
,
254 export function muteStatusSuccess(id
) {
256 type: STATUS_MUTE_SUCCESS
,
261 export function muteStatusFail(id
, error
) {
263 type: STATUS_MUTE_FAIL
,
269 export function unmuteStatus(id
) {
270 return (dispatch
, getState
) => {
271 dispatch(unmuteStatusRequest(id
));
273 api(getState
).post(`/api/v1/statuses/${id}/unmute`).then(() => {
274 dispatch(unmuteStatusSuccess(id
));
276 dispatch(unmuteStatusFail(id
, error
));
281 export function unmuteStatusRequest(id
) {
283 type: STATUS_UNMUTE_REQUEST
,
288 export function unmuteStatusSuccess(id
) {
290 type: STATUS_UNMUTE_SUCCESS
,
295 export function unmuteStatusFail(id
, error
) {
297 type: STATUS_UNMUTE_FAIL
,
303 export function hideStatus(ids
) {
304 if (!Array
.isArray(ids
)) {
314 export function revealStatus(ids
) {
315 if (!Array
.isArray(ids
)) {
325 export function toggleStatusCollapse(id
, isCollapsed
) {
327 type: STATUS_COLLAPSE
,
This page took 0.128052 seconds and 4 git commands to generate.