闭社主体 forked from https://github.com/tootsuite/mastodon
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

659 lines
16 KiB

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