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