]> cat aescling's git repositories - mastodon.git/blob - app/javascript/mastodon/actions/accounts.js
Restore vanilla components
[mastodon.git] / app / javascript / mastodon / actions / accounts.js
1 import api, { getLinks } from '../api';
2
3 export const ACCOUNT_FETCH_REQUEST = 'ACCOUNT_FETCH_REQUEST';
4 export const ACCOUNT_FETCH_SUCCESS = 'ACCOUNT_FETCH_SUCCESS';
5 export const ACCOUNT_FETCH_FAIL = 'ACCOUNT_FETCH_FAIL';
6
7 export const ACCOUNT_FOLLOW_REQUEST = 'ACCOUNT_FOLLOW_REQUEST';
8 export const ACCOUNT_FOLLOW_SUCCESS = 'ACCOUNT_FOLLOW_SUCCESS';
9 export const ACCOUNT_FOLLOW_FAIL = 'ACCOUNT_FOLLOW_FAIL';
10
11 export const ACCOUNT_UNFOLLOW_REQUEST = 'ACCOUNT_UNFOLLOW_REQUEST';
12 export const ACCOUNT_UNFOLLOW_SUCCESS = 'ACCOUNT_UNFOLLOW_SUCCESS';
13 export const ACCOUNT_UNFOLLOW_FAIL = 'ACCOUNT_UNFOLLOW_FAIL';
14
15 export const ACCOUNT_BLOCK_REQUEST = 'ACCOUNT_BLOCK_REQUEST';
16 export const ACCOUNT_BLOCK_SUCCESS = 'ACCOUNT_BLOCK_SUCCESS';
17 export const ACCOUNT_BLOCK_FAIL = 'ACCOUNT_BLOCK_FAIL';
18
19 export const ACCOUNT_UNBLOCK_REQUEST = 'ACCOUNT_UNBLOCK_REQUEST';
20 export const ACCOUNT_UNBLOCK_SUCCESS = 'ACCOUNT_UNBLOCK_SUCCESS';
21 export const ACCOUNT_UNBLOCK_FAIL = 'ACCOUNT_UNBLOCK_FAIL';
22
23 export const ACCOUNT_MUTE_REQUEST = 'ACCOUNT_MUTE_REQUEST';
24 export const ACCOUNT_MUTE_SUCCESS = 'ACCOUNT_MUTE_SUCCESS';
25 export const ACCOUNT_MUTE_FAIL = 'ACCOUNT_MUTE_FAIL';
26
27 export const ACCOUNT_UNMUTE_REQUEST = 'ACCOUNT_UNMUTE_REQUEST';
28 export const ACCOUNT_UNMUTE_SUCCESS = 'ACCOUNT_UNMUTE_SUCCESS';
29 export const ACCOUNT_UNMUTE_FAIL = 'ACCOUNT_UNMUTE_FAIL';
30
31 export const FOLLOWERS_FETCH_REQUEST = 'FOLLOWERS_FETCH_REQUEST';
32 export const FOLLOWERS_FETCH_SUCCESS = 'FOLLOWERS_FETCH_SUCCESS';
33 export const FOLLOWERS_FETCH_FAIL = 'FOLLOWERS_FETCH_FAIL';
34
35 export const FOLLOWERS_EXPAND_REQUEST = 'FOLLOWERS_EXPAND_REQUEST';
36 export const FOLLOWERS_EXPAND_SUCCESS = 'FOLLOWERS_EXPAND_SUCCESS';
37 export const FOLLOWERS_EXPAND_FAIL = 'FOLLOWERS_EXPAND_FAIL';
38
39 export const FOLLOWING_FETCH_REQUEST = 'FOLLOWING_FETCH_REQUEST';
40 export const FOLLOWING_FETCH_SUCCESS = 'FOLLOWING_FETCH_SUCCESS';
41 export const FOLLOWING_FETCH_FAIL = 'FOLLOWING_FETCH_FAIL';
42
43 export const FOLLOWING_EXPAND_REQUEST = 'FOLLOWING_EXPAND_REQUEST';
44 export const FOLLOWING_EXPAND_SUCCESS = 'FOLLOWING_EXPAND_SUCCESS';
45 export const FOLLOWING_EXPAND_FAIL = 'FOLLOWING_EXPAND_FAIL';
46
47 export const RELATIONSHIPS_FETCH_REQUEST = 'RELATIONSHIPS_FETCH_REQUEST';
48 export const RELATIONSHIPS_FETCH_SUCCESS = 'RELATIONSHIPS_FETCH_SUCCESS';
49 export const RELATIONSHIPS_FETCH_FAIL = 'RELATIONSHIPS_FETCH_FAIL';
50
51 export const FOLLOW_REQUESTS_FETCH_REQUEST = 'FOLLOW_REQUESTS_FETCH_REQUEST';
52 export const FOLLOW_REQUESTS_FETCH_SUCCESS = 'FOLLOW_REQUESTS_FETCH_SUCCESS';
53 export const FOLLOW_REQUESTS_FETCH_FAIL = 'FOLLOW_REQUESTS_FETCH_FAIL';
54
55 export const FOLLOW_REQUESTS_EXPAND_REQUEST = 'FOLLOW_REQUESTS_EXPAND_REQUEST';
56 export const FOLLOW_REQUESTS_EXPAND_SUCCESS = 'FOLLOW_REQUESTS_EXPAND_SUCCESS';
57 export const FOLLOW_REQUESTS_EXPAND_FAIL = 'FOLLOW_REQUESTS_EXPAND_FAIL';
58
59 export const FOLLOW_REQUEST_AUTHORIZE_REQUEST = 'FOLLOW_REQUEST_AUTHORIZE_REQUEST';
60 export const FOLLOW_REQUEST_AUTHORIZE_SUCCESS = 'FOLLOW_REQUEST_AUTHORIZE_SUCCESS';
61 export const FOLLOW_REQUEST_AUTHORIZE_FAIL = 'FOLLOW_REQUEST_AUTHORIZE_FAIL';
62
63 export const FOLLOW_REQUEST_REJECT_REQUEST = 'FOLLOW_REQUEST_REJECT_REQUEST';
64 export const FOLLOW_REQUEST_REJECT_SUCCESS = 'FOLLOW_REQUEST_REJECT_SUCCESS';
65 export const FOLLOW_REQUEST_REJECT_FAIL = 'FOLLOW_REQUEST_REJECT_FAIL';
66
67 export function fetchAccount(id) {
68 return (dispatch, getState) => {
69 dispatch(fetchRelationships([id]));
70
71 if (getState().getIn(['accounts', id], null) !== null) {
72 return;
73 }
74
75 dispatch(fetchAccountRequest(id));
76
77 api(getState).get(`/api/v1/accounts/${id}`).then(response => {
78 dispatch(fetchAccountSuccess(response.data));
79 }).catch(error => {
80 dispatch(fetchAccountFail(id, error));
81 });
82 };
83 };
84
85 export function fetchAccountRequest(id) {
86 return {
87 type: ACCOUNT_FETCH_REQUEST,
88 id,
89 };
90 };
91
92 export function fetchAccountSuccess(account) {
93 return {
94 type: ACCOUNT_FETCH_SUCCESS,
95 account,
96 };
97 };
98
99 export function fetchAccountFail(id, error) {
100 return {
101 type: ACCOUNT_FETCH_FAIL,
102 id,
103 error,
104 skipAlert: true,
105 };
106 };
107
108 export function followAccount(id) {
109 return (dispatch, getState) => {
110 dispatch(followAccountRequest(id));
111
112 api(getState).post(`/api/v1/accounts/${id}/follow`).then(response => {
113 dispatch(followAccountSuccess(response.data));
114 }).catch(error => {
115 dispatch(followAccountFail(error));
116 });
117 };
118 };
119
120 export function unfollowAccount(id) {
121 return (dispatch, getState) => {
122 dispatch(unfollowAccountRequest(id));
123
124 api(getState).post(`/api/v1/accounts/${id}/unfollow`).then(response => {
125 dispatch(unfollowAccountSuccess(response.data, getState().get('statuses')));
126 }).catch(error => {
127 dispatch(unfollowAccountFail(error));
128 });
129 };
130 };
131
132 export function followAccountRequest(id) {
133 return {
134 type: ACCOUNT_FOLLOW_REQUEST,
135 id,
136 };
137 };
138
139 export function followAccountSuccess(relationship) {
140 return {
141 type: ACCOUNT_FOLLOW_SUCCESS,
142 relationship,
143 };
144 };
145
146 export function followAccountFail(error) {
147 return {
148 type: ACCOUNT_FOLLOW_FAIL,
149 error,
150 };
151 };
152
153 export function unfollowAccountRequest(id) {
154 return {
155 type: ACCOUNT_UNFOLLOW_REQUEST,
156 id,
157 };
158 };
159
160 export function unfollowAccountSuccess(relationship, statuses) {
161 return {
162 type: ACCOUNT_UNFOLLOW_SUCCESS,
163 relationship,
164 statuses,
165 };
166 };
167
168 export function unfollowAccountFail(error) {
169 return {
170 type: ACCOUNT_UNFOLLOW_FAIL,
171 error,
172 };
173 };
174
175 export function blockAccount(id) {
176 return (dispatch, getState) => {
177 dispatch(blockAccountRequest(id));
178
179 api(getState).post(`/api/v1/accounts/${id}/block`).then(response => {
180 // Pass in entire statuses map so we can use it to filter stuff in different parts of the reducers
181 dispatch(blockAccountSuccess(response.data, getState().get('statuses')));
182 }).catch(error => {
183 dispatch(blockAccountFail(id, error));
184 });
185 };
186 };
187
188 export function unblockAccount(id) {
189 return (dispatch, getState) => {
190 dispatch(unblockAccountRequest(id));
191
192 api(getState).post(`/api/v1/accounts/${id}/unblock`).then(response => {
193 dispatch(unblockAccountSuccess(response.data));
194 }).catch(error => {
195 dispatch(unblockAccountFail(id, error));
196 });
197 };
198 };
199
200 export function blockAccountRequest(id) {
201 return {
202 type: ACCOUNT_BLOCK_REQUEST,
203 id,
204 };
205 };
206
207 export function blockAccountSuccess(relationship, statuses) {
208 return {
209 type: ACCOUNT_BLOCK_SUCCESS,
210 relationship,
211 statuses,
212 };
213 };
214
215 export function blockAccountFail(error) {
216 return {
217 type: ACCOUNT_BLOCK_FAIL,
218 error,
219 };
220 };
221
222 export function unblockAccountRequest(id) {
223 return {
224 type: ACCOUNT_UNBLOCK_REQUEST,
225 id,
226 };
227 };
228
229 export function unblockAccountSuccess(relationship) {
230 return {
231 type: ACCOUNT_UNBLOCK_SUCCESS,
232 relationship,
233 };
234 };
235
236 export function unblockAccountFail(error) {
237 return {
238 type: ACCOUNT_UNBLOCK_FAIL,
239 error,
240 };
241 };
242
243
244 export function muteAccount(id, notifications) {
245 return (dispatch, getState) => {
246 dispatch(muteAccountRequest(id));
247
248 api(getState).post(`/api/v1/accounts/${id}/mute`, { notifications }).then(response => {
249 // Pass in entire statuses map so we can use it to filter stuff in different parts of the reducers
250 dispatch(muteAccountSuccess(response.data, getState().get('statuses')));
251 }).catch(error => {
252 dispatch(muteAccountFail(id, error));
253 });
254 };
255 };
256
257 export function unmuteAccount(id) {
258 return (dispatch, getState) => {
259 dispatch(unmuteAccountRequest(id));
260
261 api(getState).post(`/api/v1/accounts/${id}/unmute`).then(response => {
262 dispatch(unmuteAccountSuccess(response.data));
263 }).catch(error => {
264 dispatch(unmuteAccountFail(id, error));
265 });
266 };
267 };
268
269 export function muteAccountRequest(id) {
270 return {
271 type: ACCOUNT_MUTE_REQUEST,
272 id,
273 };
274 };
275
276 export function muteAccountSuccess(relationship, statuses) {
277 return {
278 type: ACCOUNT_MUTE_SUCCESS,
279 relationship,
280 statuses,
281 };
282 };
283
284 export function muteAccountFail(error) {
285 return {
286 type: ACCOUNT_MUTE_FAIL,
287 error,
288 };
289 };
290
291 export function unmuteAccountRequest(id) {
292 return {
293 type: ACCOUNT_UNMUTE_REQUEST,
294 id,
295 };
296 };
297
298 export function unmuteAccountSuccess(relationship) {
299 return {
300 type: ACCOUNT_UNMUTE_SUCCESS,
301 relationship,
302 };
303 };
304
305 export function unmuteAccountFail(error) {
306 return {
307 type: ACCOUNT_UNMUTE_FAIL,
308 error,
309 };
310 };
311
312
313 export function fetchFollowers(id) {
314 return (dispatch, getState) => {
315 dispatch(fetchFollowersRequest(id));
316
317 api(getState).get(`/api/v1/accounts/${id}/followers`).then(response => {
318 const next = getLinks(response).refs.find(link => link.rel === 'next');
319
320 dispatch(fetchFollowersSuccess(id, response.data, next ? next.uri : null));
321 dispatch(fetchRelationships(response.data.map(item => item.id)));
322 }).catch(error => {
323 dispatch(fetchFollowersFail(id, error));
324 });
325 };
326 };
327
328 export function fetchFollowersRequest(id) {
329 return {
330 type: FOLLOWERS_FETCH_REQUEST,
331 id,
332 };
333 };
334
335 export function fetchFollowersSuccess(id, accounts, next) {
336 return {
337 type: FOLLOWERS_FETCH_SUCCESS,
338 id,
339 accounts,
340 next,
341 };
342 };
343
344 export function fetchFollowersFail(id, error) {
345 return {
346 type: FOLLOWERS_FETCH_FAIL,
347 id,
348 error,
349 };
350 };
351
352 export function expandFollowers(id) {
353 return (dispatch, getState) => {
354 const url = getState().getIn(['user_lists', 'followers', id, 'next']);
355
356 if (url === null) {
357 return;
358 }
359
360 dispatch(expandFollowersRequest(id));
361
362 api(getState).get(url).then(response => {
363 const next = getLinks(response).refs.find(link => link.rel === 'next');
364
365 dispatch(expandFollowersSuccess(id, response.data, next ? next.uri : null));
366 dispatch(fetchRelationships(response.data.map(item => item.id)));
367 }).catch(error => {
368 dispatch(expandFollowersFail(id, error));
369 });
370 };
371 };
372
373 export function expandFollowersRequest(id) {
374 return {
375 type: FOLLOWERS_EXPAND_REQUEST,
376 id,
377 };
378 };
379
380 export function expandFollowersSuccess(id, accounts, next) {
381 return {
382 type: FOLLOWERS_EXPAND_SUCCESS,
383 id,
384 accounts,
385 next,
386 };
387 };
388
389 export function expandFollowersFail(id, error) {
390 return {
391 type: FOLLOWERS_EXPAND_FAIL,
392 id,
393 error,
394 };
395 };
396
397 export function fetchFollowing(id) {
398 return (dispatch, getState) => {
399 dispatch(fetchFollowingRequest(id));
400
401 api(getState).get(`/api/v1/accounts/${id}/following`).then(response => {
402 const next = getLinks(response).refs.find(link => link.rel === 'next');
403
404 dispatch(fetchFollowingSuccess(id, response.data, next ? next.uri : null));
405 dispatch(fetchRelationships(response.data.map(item => item.id)));
406 }).catch(error => {
407 dispatch(fetchFollowingFail(id, error));
408 });
409 };
410 };
411
412 export function fetchFollowingRequest(id) {
413 return {
414 type: FOLLOWING_FETCH_REQUEST,
415 id,
416 };
417 };
418
419 export function fetchFollowingSuccess(id, accounts, next) {
420 return {
421 type: FOLLOWING_FETCH_SUCCESS,
422 id,
423 accounts,
424 next,
425 };
426 };
427
428 export function fetchFollowingFail(id, error) {
429 return {
430 type: FOLLOWING_FETCH_FAIL,
431 id,
432 error,
433 };
434 };
435
436 export function expandFollowing(id) {
437 return (dispatch, getState) => {
438 const url = getState().getIn(['user_lists', 'following', id, 'next']);
439
440 if (url === null) {
441 return;
442 }
443
444 dispatch(expandFollowingRequest(id));
445
446 api(getState).get(url).then(response => {
447 const next = getLinks(response).refs.find(link => link.rel === 'next');
448
449 dispatch(expandFollowingSuccess(id, response.data, next ? next.uri : null));
450 dispatch(fetchRelationships(response.data.map(item => item.id)));
451 }).catch(error => {
452 dispatch(expandFollowingFail(id, error));
453 });
454 };
455 };
456
457 export function expandFollowingRequest(id) {
458 return {
459 type: FOLLOWING_EXPAND_REQUEST,
460 id,
461 };
462 };
463
464 export function expandFollowingSuccess(id, accounts, next) {
465 return {
466 type: FOLLOWING_EXPAND_SUCCESS,
467 id,
468 accounts,
469 next,
470 };
471 };
472
473 export function expandFollowingFail(id, error) {
474 return {
475 type: FOLLOWING_EXPAND_FAIL,
476 id,
477 error,
478 };
479 };
480
481 export function fetchRelationships(accountIds) {
482 return (dispatch, getState) => {
483 const loadedRelationships = getState().get('relationships');
484 const newAccountIds = accountIds.filter(id => loadedRelationships.get(id, null) === null);
485
486 if (newAccountIds.length === 0) {
487 return;
488 }
489
490 dispatch(fetchRelationshipsRequest(newAccountIds));
491
492 api(getState).get(`/api/v1/accounts/relationships?${newAccountIds.map(id => `id[]=${id}`).join('&')}`).then(response => {
493 dispatch(fetchRelationshipsSuccess(response.data));
494 }).catch(error => {
495 dispatch(fetchRelationshipsFail(error));
496 });
497 };
498 };
499
500 export function fetchRelationshipsRequest(ids) {
501 return {
502 type: RELATIONSHIPS_FETCH_REQUEST,
503 ids,
504 skipLoading: true,
505 };
506 };
507
508 export function fetchRelationshipsSuccess(relationships) {
509 return {
510 type: RELATIONSHIPS_FETCH_SUCCESS,
511 relationships,
512 skipLoading: true,
513 };
514 };
515
516 export function fetchRelationshipsFail(error) {
517 return {
518 type: RELATIONSHIPS_FETCH_FAIL,
519 error,
520 skipLoading: true,
521 };
522 };
523
524 export function fetchFollowRequests() {
525 return (dispatch, getState) => {
526 dispatch(fetchFollowRequestsRequest());
527
528 api(getState).get('/api/v1/follow_requests').then(response => {
529 const next = getLinks(response).refs.find(link => link.rel === 'next');
530 dispatch(fetchFollowRequestsSuccess(response.data, next ? next.uri : null));
531 }).catch(error => dispatch(fetchFollowRequestsFail(error)));
532 };
533 };
534
535 export function fetchFollowRequestsRequest() {
536 return {
537 type: FOLLOW_REQUESTS_FETCH_REQUEST,
538 };
539 };
540
541 export function fetchFollowRequestsSuccess(accounts, next) {
542 return {
543 type: FOLLOW_REQUESTS_FETCH_SUCCESS,
544 accounts,
545 next,
546 };
547 };
548
549 export function fetchFollowRequestsFail(error) {
550 return {
551 type: FOLLOW_REQUESTS_FETCH_FAIL,
552 error,
553 };
554 };
555
556 export function expandFollowRequests() {
557 return (dispatch, getState) => {
558 const url = getState().getIn(['user_lists', 'follow_requests', 'next']);
559
560 if (url === null) {
561 return;
562 }
563
564 dispatch(expandFollowRequestsRequest());
565
566 api(getState).get(url).then(response => {
567 const next = getLinks(response).refs.find(link => link.rel === 'next');
568 dispatch(expandFollowRequestsSuccess(response.data, next ? next.uri : null));
569 }).catch(error => dispatch(expandFollowRequestsFail(error)));
570 };
571 };
572
573 export function expandFollowRequestsRequest() {
574 return {
575 type: FOLLOW_REQUESTS_EXPAND_REQUEST,
576 };
577 };
578
579 export function expandFollowRequestsSuccess(accounts, next) {
580 return {
581 type: FOLLOW_REQUESTS_EXPAND_SUCCESS,
582 accounts,
583 next,
584 };
585 };
586
587 export function expandFollowRequestsFail(error) {
588 return {
589 type: FOLLOW_REQUESTS_EXPAND_FAIL,
590 error,
591 };
592 };
593
594 export function authorizeFollowRequest(id) {
595 return (dispatch, getState) => {
596 dispatch(authorizeFollowRequestRequest(id));
597
598 api(getState)
599 .post(`/api/v1/follow_requests/${id}/authorize`)
600 .then(() => dispatch(authorizeFollowRequestSuccess(id)))
601 .catch(error => dispatch(authorizeFollowRequestFail(id, error)));
602 };
603 };
604
605 export function authorizeFollowRequestRequest(id) {
606 return {
607 type: FOLLOW_REQUEST_AUTHORIZE_REQUEST,
608 id,
609 };
610 };
611
612 export function authorizeFollowRequestSuccess(id) {
613 return {
614 type: FOLLOW_REQUEST_AUTHORIZE_SUCCESS,
615 id,
616 };
617 };
618
619 export function authorizeFollowRequestFail(id, error) {
620 return {
621 type: FOLLOW_REQUEST_AUTHORIZE_FAIL,
622 id,
623 error,
624 };
625 };
626
627
628 export function rejectFollowRequest(id) {
629 return (dispatch, getState) => {
630 dispatch(rejectFollowRequestRequest(id));
631
632 api(getState)
633 .post(`/api/v1/follow_requests/${id}/reject`)
634 .then(() => dispatch(rejectFollowRequestSuccess(id)))
635 .catch(error => dispatch(rejectFollowRequestFail(id, error)));
636 };
637 };
638
639 export function rejectFollowRequestRequest(id) {
640 return {
641 type: FOLLOW_REQUEST_REJECT_REQUEST,
642 id,
643 };
644 };
645
646 export function rejectFollowRequestSuccess(id) {
647 return {
648 type: FOLLOW_REQUEST_REJECT_SUCCESS,
649 id,
650 };
651 };
652
653 export function rejectFollowRequestFail(id, error) {
654 return {
655 type: FOLLOW_REQUEST_REJECT_FAIL,
656 id,
657 error,
658 };
659 };
This page took 0.213498 seconds and 4 git commands to generate.