闭社主体 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.

664 lines
16 KiB

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