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.

148 lines
5.1 KiB

7 years ago
  1. import { importFetchedStatus, importFetchedStatuses } from './importer';
  2. import api, { getLinks } from '../api';
  3. import { Map as ImmutableMap, List as ImmutableList } from 'immutable';
  4. export const TIMELINE_UPDATE = 'TIMELINE_UPDATE';
  5. export const TIMELINE_DELETE = 'TIMELINE_DELETE';
  6. export const TIMELINE_CLEAR = 'TIMELINE_CLEAR';
  7. export const TIMELINE_EXPAND_REQUEST = 'TIMELINE_EXPAND_REQUEST';
  8. export const TIMELINE_EXPAND_SUCCESS = 'TIMELINE_EXPAND_SUCCESS';
  9. export const TIMELINE_EXPAND_FAIL = 'TIMELINE_EXPAND_FAIL';
  10. export const TIMELINE_SCROLL_TOP = 'TIMELINE_SCROLL_TOP';
  11. export const TIMELINE_DISCONNECT = 'TIMELINE_DISCONNECT';
  12. export function updateTimeline(timeline, status, accept) {
  13. return dispatch => {
  14. if (typeof accept === 'function' && !accept(status)) {
  15. return;
  16. }
  17. dispatch(importFetchedStatus(status));
  18. dispatch({
  19. type: TIMELINE_UPDATE,
  20. timeline,
  21. status,
  22. });
  23. };
  24. };
  25. export function deleteFromTimelines(id) {
  26. return (dispatch, getState) => {
  27. const accountId = getState().getIn(['statuses', id, 'account']);
  28. const references = getState().get('statuses').filter(status => status.get('reblog') === id).map(status => [status.get('id'), status.get('account')]);
  29. const reblogOf = getState().getIn(['statuses', id, 'reblog'], null);
  30. dispatch({
  31. type: TIMELINE_DELETE,
  32. id,
  33. accountId,
  34. references,
  35. reblogOf,
  36. });
  37. };
  38. };
  39. export function clearTimeline(timeline) {
  40. return (dispatch) => {
  41. dispatch({ type: TIMELINE_CLEAR, timeline });
  42. };
  43. };
  44. const noOp = () => {};
  45. const parseTags = (tags = {}, mode) => {
  46. return (tags[mode] || []).map((tag) => {
  47. return tag.value;
  48. });
  49. };
  50. export function expandTimeline(timelineId, path, params = {}, done = noOp) {
  51. return (dispatch, getState) => {
  52. const timeline = getState().getIn(['timelines', timelineId], ImmutableMap());
  53. const isLoadingMore = !!params.max_id;
  54. if (timeline.get('isLoading')) {
  55. done();
  56. return;
  57. }
  58. if (!params.max_id && !params.pinned && timeline.get('items', ImmutableList()).size > 0) {
  59. params.since_id = timeline.getIn(['items', 0]);
  60. }
  61. dispatch(expandTimelineRequest(timelineId, isLoadingMore));
  62. api(getState).get(path, { params }).then(response => {
  63. const next = getLinks(response).refs.find(link => link.rel === 'next');
  64. dispatch(importFetchedStatuses(response.data));
  65. dispatch(expandTimelineSuccess(timelineId, response.data, next ? next.uri : null, response.code === 206, isLoadingMore));
  66. done();
  67. }).catch(error => {
  68. dispatch(expandTimelineFail(timelineId, error, isLoadingMore));
  69. done();
  70. });
  71. };
  72. };
  73. export const expandHomeTimeline = ({ maxId } = {}, done = noOp) => expandTimeline('home', '/api/v1/timelines/home', { max_id: maxId }, done);
  74. export const expandPublicTimeline = ({ maxId, onlyMedia } = {}, done = noOp) => expandTimeline(`public${onlyMedia ? ':media' : ''}`, '/api/v1/timelines/public', { max_id: maxId, only_media: !!onlyMedia }, done);
  75. export const expandCommunityTimeline = ({ maxId, onlyMedia } = {}, done = noOp) => expandTimeline(`community${onlyMedia ? ':media' : ''}`, '/api/v1/timelines/public', { local: true, max_id: maxId, only_media: !!onlyMedia }, done);
  76. export const expandAccountTimeline = (accountId, { maxId, withReplies } = {}) => expandTimeline(`account:${accountId}${withReplies ? ':with_replies' : ''}`, `/api/v1/accounts/${accountId}/statuses`, { exclude_replies: !withReplies, max_id: maxId });
  77. export const expandAccountFeaturedTimeline = accountId => expandTimeline(`account:${accountId}:pinned`, `/api/v1/accounts/${accountId}/statuses`, { pinned: true });
  78. export const expandAccountMediaTimeline = (accountId, { maxId } = {}) => expandTimeline(`account:${accountId}:media`, `/api/v1/accounts/${accountId}/statuses`, { max_id: maxId, only_media: true });
  79. export const expandListTimeline = (id, { maxId } = {}, done = noOp) => expandTimeline(`list:${id}`, `/api/v1/timelines/list/${id}`, { max_id: maxId }, done);
  80. export const expandHashtagTimeline = (hashtag, { maxId, tags } = {}, done = noOp) => {
  81. return expandTimeline(`hashtag:${hashtag}`, `/api/v1/timelines/tag/${hashtag}`, {
  82. max_id: maxId,
  83. any: parseTags(tags, 'any'),
  84. all: parseTags(tags, 'all'),
  85. none: parseTags(tags, 'none'),
  86. }, done);
  87. };
  88. export function expandTimelineRequest(timeline, isLoadingMore) {
  89. return {
  90. type: TIMELINE_EXPAND_REQUEST,
  91. timeline,
  92. skipLoading: !isLoadingMore,
  93. };
  94. };
  95. export function expandTimelineSuccess(timeline, statuses, next, partial, isLoadingMore) {
  96. return {
  97. type: TIMELINE_EXPAND_SUCCESS,
  98. timeline,
  99. statuses,
  100. next,
  101. partial,
  102. skipLoading: !isLoadingMore,
  103. };
  104. };
  105. export function expandTimelineFail(timeline, error, isLoadingMore) {
  106. return {
  107. type: TIMELINE_EXPAND_FAIL,
  108. timeline,
  109. error,
  110. skipLoading: !isLoadingMore,
  111. };
  112. };
  113. export function scrollTopTimeline(timeline, top) {
  114. return {
  115. type: TIMELINE_SCROLL_TOP,
  116. timeline,
  117. top,
  118. };
  119. };
  120. export function disconnectTimeline(timeline) {
  121. return {
  122. type: TIMELINE_DISCONNECT,
  123. timeline,
  124. };
  125. };