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.

661 lines
16 KiB

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