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.

672 lines
17 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_TIMELINE_FETCH_REQUEST = 'ACCOUNT_TIMELINE_FETCH_REQUEST';
  19. export const ACCOUNT_TIMELINE_FETCH_SUCCESS = 'ACCOUNT_TIMELINE_FETCH_SUCCESS';
  20. export const ACCOUNT_TIMELINE_FETCH_FAIL = 'ACCOUNT_TIMELINE_FETCH_FAIL';
  21. export const ACCOUNT_TIMELINE_EXPAND_REQUEST = 'ACCOUNT_TIMELINE_EXPAND_REQUEST';
  22. export const ACCOUNT_TIMELINE_EXPAND_SUCCESS = 'ACCOUNT_TIMELINE_EXPAND_SUCCESS';
  23. export const ACCOUNT_TIMELINE_EXPAND_FAIL = 'ACCOUNT_TIMELINE_EXPAND_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(fetchAccountRequest(id));
  54. api(getState).get(`/api/v1/accounts/${id}`).then(response => {
  55. dispatch(fetchAccountSuccess(response.data));
  56. dispatch(fetchRelationships([id]));
  57. }).catch(error => {
  58. dispatch(fetchAccountFail(id, error));
  59. });
  60. };
  61. };
  62. export function fetchAccountTimeline(id, replace = false) {
  63. return (dispatch, getState) => {
  64. const ids = getState().getIn(['timelines', 'accounts_timelines', id, 'items'], Immutable.List());
  65. const newestId = ids.size > 0 ? ids.first() : null;
  66. let params = '';
  67. let skipLoading = false;
  68. if (newestId !== null && !replace) {
  69. params = `?since_id=${newestId}`;
  70. skipLoading = true;
  71. }
  72. dispatch(fetchAccountTimelineRequest(id, skipLoading));
  73. api(getState).get(`/api/v1/accounts/${id}/statuses${params}`).then(response => {
  74. dispatch(fetchAccountTimelineSuccess(id, response.data, replace, skipLoading));
  75. }).catch(error => {
  76. dispatch(fetchAccountTimelineFail(id, error, skipLoading));
  77. });
  78. };
  79. };
  80. export function expandAccountTimeline(id) {
  81. return (dispatch, getState) => {
  82. const lastId = getState().getIn(['timelines', 'accounts_timelines', id, 'items'], Immutable.List()).last();
  83. dispatch(expandAccountTimelineRequest(id));
  84. api(getState).get(`/api/v1/accounts/${id}/statuses`, {
  85. params: {
  86. limit: 10,
  87. max_id: lastId
  88. }
  89. }).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, skipLoading) {
  172. return {
  173. type: ACCOUNT_TIMELINE_FETCH_REQUEST,
  174. id,
  175. skipLoading
  176. };
  177. };
  178. export function fetchAccountTimelineSuccess(id, statuses, replace, skipLoading) {
  179. return {
  180. type: ACCOUNT_TIMELINE_FETCH_SUCCESS,
  181. id,
  182. statuses,
  183. replace,
  184. skipLoading
  185. };
  186. };
  187. export function fetchAccountTimelineFail(id, error, skipLoading) {
  188. return {
  189. type: ACCOUNT_TIMELINE_FETCH_FAIL,
  190. id,
  191. error,
  192. skipLoading
  193. };
  194. };
  195. export function expandAccountTimelineRequest(id) {
  196. return {
  197. type: ACCOUNT_TIMELINE_EXPAND_REQUEST,
  198. id
  199. };
  200. };
  201. export function expandAccountTimelineSuccess(id, statuses) {
  202. return {
  203. type: ACCOUNT_TIMELINE_EXPAND_SUCCESS,
  204. id,
  205. statuses
  206. };
  207. };
  208. export function expandAccountTimelineFail(id, error) {
  209. return {
  210. type: ACCOUNT_TIMELINE_EXPAND_FAIL,
  211. id,
  212. error
  213. };
  214. };
  215. export function blockAccount(id) {
  216. return (dispatch, getState) => {
  217. dispatch(blockAccountRequest(id));
  218. api(getState).post(`/api/v1/accounts/${id}/block`).then(response => {
  219. // Pass in entire statuses map so we can use it to filter stuff in different parts of the reducers
  220. dispatch(blockAccountSuccess(response.data, getState().get('statuses')));
  221. }).catch(error => {
  222. dispatch(blockAccountFail(id, error));
  223. });
  224. };
  225. };
  226. export function unblockAccount(id) {
  227. return (dispatch, getState) => {
  228. dispatch(unblockAccountRequest(id));
  229. api(getState).post(`/api/v1/accounts/${id}/unblock`).then(response => {
  230. dispatch(unblockAccountSuccess(response.data));
  231. }).catch(error => {
  232. dispatch(unblockAccountFail(id, error));
  233. });
  234. };
  235. };
  236. export function blockAccountRequest(id) {
  237. return {
  238. type: ACCOUNT_BLOCK_REQUEST,
  239. id
  240. };
  241. };
  242. export function blockAccountSuccess(relationship, statuses) {
  243. return {
  244. type: ACCOUNT_BLOCK_SUCCESS,
  245. relationship,
  246. statuses
  247. };
  248. };
  249. export function blockAccountFail(error) {
  250. return {
  251. type: ACCOUNT_BLOCK_FAIL,
  252. error
  253. };
  254. };
  255. export function unblockAccountRequest(id) {
  256. return {
  257. type: ACCOUNT_UNBLOCK_REQUEST,
  258. id
  259. };
  260. };
  261. export function unblockAccountSuccess(relationship) {
  262. return {
  263. type: ACCOUNT_UNBLOCK_SUCCESS,
  264. relationship
  265. };
  266. };
  267. export function unblockAccountFail(error) {
  268. return {
  269. type: ACCOUNT_UNBLOCK_FAIL,
  270. error
  271. };
  272. };
  273. export function fetchFollowers(id) {
  274. return (dispatch, getState) => {
  275. dispatch(fetchFollowersRequest(id));
  276. api(getState).get(`/api/v1/accounts/${id}/followers`).then(response => {
  277. const next = getLinks(response).refs.find(link => link.rel === 'next');
  278. dispatch(fetchFollowersSuccess(id, response.data, next ? next.uri : null));
  279. dispatch(fetchRelationships(response.data.map(item => item.id)));
  280. }).catch(error => {
  281. dispatch(fetchFollowersFail(id, error));
  282. });
  283. };
  284. };
  285. export function fetchFollowersRequest(id) {
  286. return {
  287. type: FOLLOWERS_FETCH_REQUEST,
  288. id
  289. };
  290. };
  291. export function fetchFollowersSuccess(id, accounts, next) {
  292. return {
  293. type: FOLLOWERS_FETCH_SUCCESS,
  294. id,
  295. accounts,
  296. next
  297. };
  298. };
  299. export function fetchFollowersFail(id, error) {
  300. return {
  301. type: FOLLOWERS_FETCH_FAIL,
  302. id,
  303. error
  304. };
  305. };
  306. export function expandFollowers(id) {
  307. return (dispatch, getState) => {
  308. const url = getState().getIn(['user_lists', 'followers', id, 'next']);
  309. if (url === null) {
  310. return;
  311. }
  312. dispatch(expandFollowersRequest(id));
  313. api(getState).get(url).then(response => {
  314. const next = getLinks(response).refs.find(link => link.rel === 'next');
  315. dispatch(expandFollowersSuccess(id, response.data, next ? next.uri : null));
  316. dispatch(fetchRelationships(response.data.map(item => item.id)));
  317. }).catch(error => {
  318. dispatch(expandFollowersFail(id, error));
  319. });
  320. };
  321. };
  322. export function expandFollowersRequest(id) {
  323. return {
  324. type: FOLLOWERS_EXPAND_REQUEST,
  325. id
  326. };
  327. };
  328. export function expandFollowersSuccess(id, accounts, next) {
  329. return {
  330. type: FOLLOWERS_EXPAND_SUCCESS,
  331. id,
  332. accounts,
  333. next
  334. };
  335. };
  336. export function expandFollowersFail(id, error) {
  337. return {
  338. type: FOLLOWERS_EXPAND_FAIL,
  339. id,
  340. error
  341. };
  342. };
  343. export function fetchFollowing(id) {
  344. return (dispatch, getState) => {
  345. dispatch(fetchFollowingRequest(id));
  346. api(getState).get(`/api/v1/accounts/${id}/following`).then(response => {
  347. const next = getLinks(response).refs.find(link => link.rel === 'next');
  348. dispatch(fetchFollowingSuccess(id, response.data, next ? next.uri : null));
  349. dispatch(fetchRelationships(response.data.map(item => item.id)));
  350. }).catch(error => {
  351. dispatch(fetchFollowingFail(id, error));
  352. });
  353. };
  354. };
  355. export function fetchFollowingRequest(id) {
  356. return {
  357. type: FOLLOWING_FETCH_REQUEST,
  358. id
  359. };
  360. };
  361. export function fetchFollowingSuccess(id, accounts, next) {
  362. return {
  363. type: FOLLOWING_FETCH_SUCCESS,
  364. id,
  365. accounts,
  366. next
  367. };
  368. };
  369. export function fetchFollowingFail(id, error) {
  370. return {
  371. type: FOLLOWING_FETCH_FAIL,
  372. id,
  373. error
  374. };
  375. };
  376. export function expandFollowing(id) {
  377. return (dispatch, getState) => {
  378. const url = getState().getIn(['user_lists', 'following', id, 'next']);
  379. if (url === null) {
  380. return;
  381. }
  382. dispatch(expandFollowingRequest(id));
  383. api(getState).get(url).then(response => {
  384. const next = getLinks(response).refs.find(link => link.rel === 'next');
  385. dispatch(expandFollowingSuccess(id, response.data, next ? next.uri : null));
  386. dispatch(fetchRelationships(response.data.map(item => item.id)));
  387. }).catch(error => {
  388. dispatch(expandFollowingFail(id, error));
  389. });
  390. };
  391. };
  392. export function expandFollowingRequest(id) {
  393. return {
  394. type: FOLLOWING_EXPAND_REQUEST,
  395. id
  396. };
  397. };
  398. export function expandFollowingSuccess(id, accounts, next) {
  399. return {
  400. type: FOLLOWING_EXPAND_SUCCESS,
  401. id,
  402. accounts,
  403. next
  404. };
  405. };
  406. export function expandFollowingFail(id, error) {
  407. return {
  408. type: FOLLOWING_EXPAND_FAIL,
  409. id,
  410. error
  411. };
  412. };
  413. export function fetchRelationships(account_ids) {
  414. return (dispatch, getState) => {
  415. if (account_ids.length === 0) {
  416. return;
  417. }
  418. dispatch(fetchRelationshipsRequest(account_ids));
  419. api(getState).get(`/api/v1/accounts/relationships?${account_ids.map(id => `id[]=${id}`).join('&')}`).then(response => {
  420. dispatch(fetchRelationshipsSuccess(response.data));
  421. }).catch(error => {
  422. dispatch(fetchRelationshipsFail(error));
  423. });
  424. };
  425. };
  426. export function fetchRelationshipsRequest(ids) {
  427. return {
  428. type: RELATIONSHIPS_FETCH_REQUEST,
  429. ids,
  430. skipLoading: true
  431. };
  432. };
  433. export function fetchRelationshipsSuccess(relationships) {
  434. return {
  435. type: RELATIONSHIPS_FETCH_SUCCESS,
  436. relationships,
  437. skipLoading: true
  438. };
  439. };
  440. export function fetchRelationshipsFail(error) {
  441. return {
  442. type: RELATIONSHIPS_FETCH_FAIL,
  443. error,
  444. skipLoading: true
  445. };
  446. };
  447. export function fetchFollowRequests() {
  448. return (dispatch, getState) => {
  449. dispatch(fetchFollowRequestsRequest());
  450. api(getState).get('/api/v1/follow_requests').then(response => {
  451. const next = getLinks(response).refs.find(link => link.rel === 'next');
  452. dispatch(fetchFollowRequestsSuccess(response.data, next ? next.uri : null))
  453. }).catch(error => dispatch(fetchFollowRequestsFail(error)));
  454. };
  455. };
  456. export function fetchFollowRequestsRequest() {
  457. return {
  458. type: FOLLOW_REQUESTS_FETCH_REQUEST
  459. };
  460. };
  461. export function fetchFollowRequestsSuccess(accounts, next) {
  462. return {
  463. type: FOLLOW_REQUESTS_FETCH_SUCCESS,
  464. accounts,
  465. next
  466. };
  467. };
  468. export function fetchFollowRequestsFail(error) {
  469. return {
  470. type: FOLLOW_REQUESTS_FETCH_FAIL,
  471. error
  472. };
  473. };
  474. export function expandFollowRequests() {
  475. return (dispatch, getState) => {
  476. const url = getState().getIn(['user_lists', 'follow_requests', 'next']);
  477. if (url === null) {
  478. return;
  479. }
  480. dispatch(expandFollowRequestsRequest());
  481. api(getState).get(url).then(response => {
  482. const next = getLinks(response).refs.find(link => link.rel === 'next');
  483. dispatch(expandFollowRequestsSuccess(response.data, next ? next.uri : null))
  484. }).catch(error => dispatch(expandFollowRequestsFail(error)));
  485. };
  486. };
  487. export function expandFollowRequestsRequest() {
  488. return {
  489. type: FOLLOW_REQUESTS_EXPAND_REQUEST
  490. };
  491. };
  492. export function expandFollowRequestsSuccess(accounts, next) {
  493. return {
  494. type: FOLLOW_REQUESTS_EXPAND_SUCCESS,
  495. accounts,
  496. next
  497. };
  498. };
  499. export function expandFollowRequestsFail(error) {
  500. return {
  501. type: FOLLOW_REQUESTS_EXPAND_FAIL,
  502. error
  503. };
  504. };
  505. export function authorizeFollowRequest(id) {
  506. return (dispatch, getState) => {
  507. dispatch(authorizeFollowRequestRequest(id));
  508. api(getState)
  509. .post(`/api/v1/follow_requests/${id}/authorize`)
  510. .then(response => dispatch(authorizeFollowRequestSuccess(id)))
  511. .catch(error => dispatch(authorizeFollowRequestFail(id, error)));
  512. };
  513. };
  514. export function authorizeFollowRequestRequest(id) {
  515. return {
  516. type: FOLLOW_REQUEST_AUTHORIZE_REQUEST,
  517. id
  518. };
  519. };
  520. export function authorizeFollowRequestSuccess(id) {
  521. return {
  522. type: FOLLOW_REQUEST_AUTHORIZE_SUCCESS,
  523. id
  524. };
  525. };
  526. export function authorizeFollowRequestFail(id, error) {
  527. return {
  528. type: FOLLOW_REQUEST_AUTHORIZE_FAIL,
  529. id,
  530. error
  531. };
  532. };
  533. export function rejectFollowRequest(id) {
  534. return (dispatch, getState) => {
  535. dispatch(rejectFollowRequestRequest(id));
  536. api(getState)
  537. .post(`/api/v1/follow_requests/${id}/reject`)
  538. .then(response => dispatch(rejectFollowRequestSuccess(id)))
  539. .catch(error => dispatch(rejectFollowRequestFail(id, error)));
  540. };
  541. };
  542. export function rejectFollowRequestRequest(id) {
  543. return {
  544. type: FOLLOW_REQUEST_REJECT_REQUEST,
  545. id
  546. };
  547. };
  548. export function rejectFollowRequestSuccess(id) {
  549. return {
  550. type: FOLLOW_REQUEST_REJECT_SUCCESS,
  551. id
  552. };
  553. };
  554. export function rejectFollowRequestFail(id, error) {
  555. return {
  556. type: FOLLOW_REQUEST_REJECT_FAIL,
  557. id,
  558. error
  559. };
  560. };