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

511 lines
12 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 function setAccountSelf(account) {
  41. return {
  42. type: ACCOUNT_SET_SELF,
  43. account
  44. };
  45. };
  46. export function fetchAccount(id) {
  47. return (dispatch, getState) => {
  48. dispatch(fetchAccountRequest(id));
  49. api(getState).get(`/api/v1/accounts/${id}`).then(response => {
  50. dispatch(fetchAccountSuccess(response.data));
  51. dispatch(fetchRelationships([id]));
  52. }).catch(error => {
  53. dispatch(fetchAccountFail(id, error));
  54. });
  55. };
  56. };
  57. export function fetchAccountTimeline(id, replace = false) {
  58. return (dispatch, getState) => {
  59. dispatch(fetchAccountTimelineRequest(id));
  60. const ids = getState().getIn(['timelines', 'accounts_timelines', id], Immutable.List());
  61. const newestId = ids.size > 0 ? ids.first() : null;
  62. let params = '';
  63. if (newestId !== null && !replace) {
  64. params = `?since_id=${newestId}`;
  65. }
  66. api(getState).get(`/api/v1/accounts/${id}/statuses${params}`).then(response => {
  67. dispatch(fetchAccountTimelineSuccess(id, response.data, replace));
  68. }).catch(error => {
  69. dispatch(fetchAccountTimelineFail(id, error));
  70. });
  71. };
  72. };
  73. export function expandAccountTimeline(id) {
  74. return (dispatch, getState) => {
  75. const lastId = getState().getIn(['timelines', 'accounts_timelines', id], Immutable.List()).last();
  76. dispatch(expandAccountTimelineRequest(id));
  77. api(getState).get(`/api/v1/accounts/${id}/statuses?max_id=${lastId}`).then(response => {
  78. dispatch(expandAccountTimelineSuccess(id, response.data));
  79. }).catch(error => {
  80. dispatch(expandAccountTimelineFail(id, error));
  81. });
  82. };
  83. };
  84. export function fetchAccountRequest(id) {
  85. return {
  86. type: ACCOUNT_FETCH_REQUEST,
  87. id
  88. };
  89. };
  90. export function fetchAccountSuccess(account) {
  91. return {
  92. type: ACCOUNT_FETCH_SUCCESS,
  93. account
  94. };
  95. };
  96. export function fetchAccountFail(id, error) {
  97. return {
  98. type: ACCOUNT_FETCH_FAIL,
  99. id,
  100. error
  101. };
  102. };
  103. export function followAccount(id) {
  104. return (dispatch, getState) => {
  105. dispatch(followAccountRequest(id));
  106. api(getState).post(`/api/v1/accounts/${id}/follow`).then(response => {
  107. dispatch(followAccountSuccess(response.data));
  108. }).catch(error => {
  109. dispatch(followAccountFail(error));
  110. });
  111. };
  112. };
  113. export function unfollowAccount(id) {
  114. return (dispatch, getState) => {
  115. dispatch(unfollowAccountRequest(id));
  116. api(getState).post(`/api/v1/accounts/${id}/unfollow`).then(response => {
  117. dispatch(unfollowAccountSuccess(response.data));
  118. }).catch(error => {
  119. dispatch(unfollowAccountFail(error));
  120. });
  121. }
  122. };
  123. export function followAccountRequest(id) {
  124. return {
  125. type: ACCOUNT_FOLLOW_REQUEST,
  126. id
  127. };
  128. };
  129. export function followAccountSuccess(relationship) {
  130. return {
  131. type: ACCOUNT_FOLLOW_SUCCESS,
  132. relationship
  133. };
  134. };
  135. export function followAccountFail(error) {
  136. return {
  137. type: ACCOUNT_FOLLOW_FAIL,
  138. error
  139. };
  140. };
  141. export function unfollowAccountRequest(id) {
  142. return {
  143. type: ACCOUNT_UNFOLLOW_REQUEST,
  144. id
  145. };
  146. };
  147. export function unfollowAccountSuccess(relationship) {
  148. return {
  149. type: ACCOUNT_UNFOLLOW_SUCCESS,
  150. relationship
  151. };
  152. };
  153. export function unfollowAccountFail(error) {
  154. return {
  155. type: ACCOUNT_UNFOLLOW_FAIL,
  156. error
  157. };
  158. };
  159. export function fetchAccountTimelineRequest(id) {
  160. return {
  161. type: ACCOUNT_TIMELINE_FETCH_REQUEST,
  162. id
  163. };
  164. };
  165. export function fetchAccountTimelineSuccess(id, statuses, replace) {
  166. return {
  167. type: ACCOUNT_TIMELINE_FETCH_SUCCESS,
  168. id,
  169. statuses,
  170. replace
  171. };
  172. };
  173. export function fetchAccountTimelineFail(id, error) {
  174. return {
  175. type: ACCOUNT_TIMELINE_FETCH_FAIL,
  176. id,
  177. error
  178. };
  179. };
  180. export function expandAccountTimelineRequest(id) {
  181. return {
  182. type: ACCOUNT_TIMELINE_EXPAND_REQUEST,
  183. id
  184. };
  185. };
  186. export function expandAccountTimelineSuccess(id, statuses) {
  187. return {
  188. type: ACCOUNT_TIMELINE_EXPAND_SUCCESS,
  189. id,
  190. statuses
  191. };
  192. };
  193. export function expandAccountTimelineFail(id, error) {
  194. return {
  195. type: ACCOUNT_TIMELINE_EXPAND_FAIL,
  196. id,
  197. error
  198. };
  199. };
  200. export function blockAccount(id) {
  201. return (dispatch, getState) => {
  202. dispatch(blockAccountRequest(id));
  203. api(getState).post(`/api/v1/accounts/${id}/block`).then(response => {
  204. // Pass in entire statuses map so we can use it to filter stuff in different parts of the reducers
  205. dispatch(blockAccountSuccess(response.data, getState().get('statuses')));
  206. }).catch(error => {
  207. dispatch(blockAccountFail(id, error));
  208. });
  209. };
  210. };
  211. export function unblockAccount(id) {
  212. return (dispatch, getState) => {
  213. dispatch(unblockAccountRequest(id));
  214. api(getState).post(`/api/v1/accounts/${id}/unblock`).then(response => {
  215. dispatch(unblockAccountSuccess(response.data));
  216. }).catch(error => {
  217. dispatch(unblockAccountFail(id, error));
  218. });
  219. };
  220. };
  221. export function blockAccountRequest(id) {
  222. return {
  223. type: ACCOUNT_BLOCK_REQUEST,
  224. id
  225. };
  226. };
  227. export function blockAccountSuccess(relationship, statuses) {
  228. return {
  229. type: ACCOUNT_BLOCK_SUCCESS,
  230. relationship,
  231. statuses
  232. };
  233. };
  234. export function blockAccountFail(error) {
  235. return {
  236. type: ACCOUNT_BLOCK_FAIL,
  237. error
  238. };
  239. };
  240. export function unblockAccountRequest(id) {
  241. return {
  242. type: ACCOUNT_UNBLOCK_REQUEST,
  243. id
  244. };
  245. };
  246. export function unblockAccountSuccess(relationship) {
  247. return {
  248. type: ACCOUNT_UNBLOCK_SUCCESS,
  249. relationship
  250. };
  251. };
  252. export function unblockAccountFail(error) {
  253. return {
  254. type: ACCOUNT_UNBLOCK_FAIL,
  255. error
  256. };
  257. };
  258. export function fetchFollowers(id) {
  259. return (dispatch, getState) => {
  260. dispatch(fetchFollowersRequest(id));
  261. api(getState).get(`/api/v1/accounts/${id}/followers`).then(response => {
  262. const next = getLinks(response).refs.find(link => link.rel === 'next');
  263. dispatch(fetchFollowersSuccess(id, response.data, next ? next.uri : null));
  264. dispatch(fetchRelationships(response.data.map(item => item.id)));
  265. }).catch(error => {
  266. dispatch(fetchFollowersFail(id, error));
  267. });
  268. };
  269. };
  270. export function fetchFollowersRequest(id) {
  271. return {
  272. type: FOLLOWERS_FETCH_REQUEST,
  273. id
  274. };
  275. };
  276. export function fetchFollowersSuccess(id, accounts, next) {
  277. return {
  278. type: FOLLOWERS_FETCH_SUCCESS,
  279. id,
  280. accounts,
  281. next
  282. };
  283. };
  284. export function fetchFollowersFail(id, error) {
  285. return {
  286. type: FOLLOWERS_FETCH_FAIL,
  287. id,
  288. error
  289. };
  290. };
  291. export function expandFollowers(id) {
  292. return (dispatch, getState) => {
  293. const url = getState().getIn(['user_lists', 'followers', id, 'next']);
  294. if (url === null) {
  295. return;
  296. }
  297. dispatch(expandFollowersRequest(id));
  298. api(getState).get(url).then(response => {
  299. const next = getLinks(response).refs.find(link => link.rel === 'next');
  300. dispatch(expandFollowersSuccess(id, response.data, next ? next.uri : null));
  301. dispatch(fetchRelationships(response.data.map(item => item.id)));
  302. }).catch(error => {
  303. dispatch(expandFollowersFail(id, error));
  304. });
  305. };
  306. };
  307. export function expandFollowersRequest(id) {
  308. return {
  309. type: FOLLOWERS_EXPAND_REQUEST,
  310. id
  311. };
  312. };
  313. export function expandFollowersSuccess(id, accounts, next) {
  314. return {
  315. type: FOLLOWERS_EXPAND_SUCCESS,
  316. id,
  317. accounts,
  318. next
  319. };
  320. };
  321. export function expandFollowersFail(id, error) {
  322. return {
  323. type: FOLLOWERS_EXPAND_FAIL,
  324. id,
  325. error
  326. };
  327. };
  328. export function fetchFollowing(id) {
  329. return (dispatch, getState) => {
  330. dispatch(fetchFollowingRequest(id));
  331. api(getState).get(`/api/v1/accounts/${id}/following`).then(response => {
  332. const next = getLinks(response).refs.find(link => link.rel === 'next');
  333. dispatch(fetchFollowingSuccess(id, response.data, next ? next.uri : null));
  334. dispatch(fetchRelationships(response.data.map(item => item.id)));
  335. }).catch(error => {
  336. dispatch(fetchFollowingFail(id, error));
  337. });
  338. };
  339. };
  340. export function fetchFollowingRequest(id) {
  341. return {
  342. type: FOLLOWING_FETCH_REQUEST,
  343. id
  344. };
  345. };
  346. export function fetchFollowingSuccess(id, accounts, next) {
  347. return {
  348. type: FOLLOWING_FETCH_SUCCESS,
  349. id,
  350. accounts,
  351. next
  352. };
  353. };
  354. export function fetchFollowingFail(id, error) {
  355. return {
  356. type: FOLLOWING_FETCH_FAIL,
  357. id,
  358. error
  359. };
  360. };
  361. export function expandFollowing(id) {
  362. return (dispatch, getState) => {
  363. const url = getState().getIn(['user_lists', 'following', id, 'next']);
  364. if (url === null) {
  365. return;
  366. }
  367. dispatch(expandFollowingRequest(id));
  368. api(getState).get(url).then(response => {
  369. const next = getLinks(response).refs.find(link => link.rel === 'next');
  370. dispatch(expandFollowingSuccess(id, response.data, next ? next.uri : null));
  371. dispatch(fetchRelationships(response.data.map(item => item.id)));
  372. }).catch(error => {
  373. dispatch(expandFollowingFail(id, error));
  374. });
  375. };
  376. };
  377. export function expandFollowingRequest(id) {
  378. return {
  379. type: FOLLOWING_EXPAND_REQUEST,
  380. id
  381. };
  382. };
  383. export function expandFollowingSuccess(id, accounts, next) {
  384. return {
  385. type: FOLLOWING_EXPAND_SUCCESS,
  386. id,
  387. accounts,
  388. next
  389. };
  390. };
  391. export function expandFollowingFail(id, error) {
  392. return {
  393. type: FOLLOWING_EXPAND_FAIL,
  394. id,
  395. error
  396. };
  397. };
  398. export function fetchRelationships(account_ids) {
  399. return (dispatch, getState) => {
  400. dispatch(fetchRelationshipsRequest(account_ids));
  401. api(getState).get(`/api/v1/accounts/relationships?${account_ids.map(id => `id[]=${id}`).join('&')}`).then(response => {
  402. dispatch(fetchRelationshipsSuccess(response.data));
  403. }).catch(error => {
  404. dispatch(fetchRelationshipsFail(error));
  405. });
  406. };
  407. };
  408. export function fetchRelationshipsRequest(ids) {
  409. return {
  410. type: RELATIONSHIPS_FETCH_REQUEST,
  411. ids
  412. };
  413. };
  414. export function fetchRelationshipsSuccess(relationships) {
  415. return {
  416. type: RELATIONSHIPS_FETCH_SUCCESS,
  417. relationships
  418. };
  419. };
  420. export function fetchRelationshipsFail(error) {
  421. return {
  422. type: RELATIONSHIPS_FETCH_FAIL,
  423. error
  424. };
  425. };