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.

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