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.

202 lines
6.9 KiB

7 years ago
3 years ago
  1. import { importFetchedStatus, importFetchedStatuses } from './importer';
  2. import { submitMarkers } from './markers';
  3. import api, { getLinks } from 'mastodon/api';
  4. import { Map as ImmutableMap, List as ImmutableList } from 'immutable';
  5. import compareId from 'mastodon/compare_id';
  6. import { usePendingItems as preferPendingItems } from 'mastodon/initial_state';
  7. import { fetchContext } from './statuses';
  8. export const TIMELINE_UPDATE = 'TIMELINE_UPDATE';
  9. export const TIMELINE_DELETE = 'TIMELINE_DELETE';
  10. export const TIMELINE_CLEAR = 'TIMELINE_CLEAR';
  11. export const TIMELINE_EXPAND_REQUEST = 'TIMELINE_EXPAND_REQUEST';
  12. export const TIMELINE_EXPAND_SUCCESS = 'TIMELINE_EXPAND_SUCCESS';
  13. export const TIMELINE_EXPAND_FAIL = 'TIMELINE_EXPAND_FAIL';
  14. export const TIMELINE_SCROLL_TOP = 'TIMELINE_SCROLL_TOP';
  15. export const TIMELINE_LOAD_PENDING = 'TIMELINE_LOAD_PENDING';
  16. export const TIMELINE_DISCONNECT = 'TIMELINE_DISCONNECT';
  17. export const TIMELINE_CONNECT = 'TIMELINE_CONNECT';
  18. export const TIMELINE_MARK_AS_PARTIAL = 'TIMELINE_MARK_AS_PARTIAL';
  19. export const loadPending = timeline => ({
  20. type: TIMELINE_LOAD_PENDING,
  21. timeline,
  22. });
  23. export function updateTimeline(timeline, status, accept) {
  24. return (dispatch, getState) => {
  25. if (typeof accept === 'function' && !accept(status)) {
  26. return;
  27. }
  28. if (getState().getIn(['timelines', timeline, 'isPartial'])) {
  29. // Prevent new items from being added to a partial timeline,
  30. // since it will be reloaded anyway
  31. return;
  32. }
  33. dispatch(importFetchedStatus(status));
  34. dispatch({
  35. type: TIMELINE_UPDATE,
  36. timeline,
  37. status,
  38. usePendingItems: preferPendingItems,
  39. });
  40. if (timeline === 'home') {
  41. dispatch(submitMarkers());
  42. }
  43. };
  44. };
  45. export function deleteFromTimelines(id) {
  46. return (dispatch, getState) => {
  47. const accountId = getState().getIn(['statuses', id, 'account']);
  48. const references = getState().get('statuses').filter(status => status.get('reblog') === id).map(status => status.get('id'));
  49. const reblogOf = getState().getIn(['statuses', id, 'reblog'], null);
  50. dispatch({
  51. type: TIMELINE_DELETE,
  52. id,
  53. accountId,
  54. references,
  55. reblogOf,
  56. });
  57. };
  58. };
  59. export function clearTimeline(timeline) {
  60. return (dispatch) => {
  61. dispatch({ type: TIMELINE_CLEAR, timeline });
  62. };
  63. };
  64. const noOp = () => {};
  65. const parseTags = (tags = {}, mode) => {
  66. return (tags[mode] || []).map((tag) => {
  67. return tag.value;
  68. });
  69. };
  70. export function expandTimeline(timelineId, path, params = {}, done = noOp) {
  71. return (dispatch, getState) => {
  72. const timeline = getState().getIn(['timelines', timelineId], ImmutableMap());
  73. const isLoadingMore = !!params.max_id;
  74. if (timeline.get('isLoading')) {
  75. done();
  76. return;
  77. }
  78. if (!params.max_id && !params.pinned && (timeline.get('items', ImmutableList()).size + timeline.get('pendingItems', ImmutableList()).size) > 0) {
  79. const a = timeline.getIn(['pendingItems', 0]);
  80. const b = timeline.getIn(['items', 0]);
  81. if (a && b && compareId(a, b) > 0) {
  82. params.since_id = a;
  83. } else {
  84. params.since_id = b || a;
  85. }
  86. }
  87. const isLoadingRecent = !!params.since_id;
  88. dispatch(expandTimelineRequest(timelineId, isLoadingMore));
  89. api(getState).get(path, { params }).then(response => {
  90. const next = getLinks(response).refs.find(link => link.rel === 'next');
  91. dispatch(importFetchedStatuses(response.data));
  92. dispatch(expandTimelineSuccess(timelineId, response.data, next ? next.uri : null, response.status === 206, isLoadingRecent, isLoadingMore, isLoadingRecent && preferPendingItems));
  93. response.data.forEach(status => {
  94. dispatch(fetchContext(status.reblog ? status.reblog.id : status.id));
  95. });
  96. if (timelineId === 'home') {
  97. dispatch(submitMarkers());
  98. }
  99. }).catch(error => {
  100. dispatch(expandTimelineFail(timelineId, error, isLoadingMore));
  101. }).finally(() => {
  102. done();
  103. });
  104. };
  105. };
  106. export const expandHomeTimeline = ({ maxId } = {}, done = noOp) => expandTimeline('home', '/api/v1/timelines/home', { max_id: maxId }, done);
  107. export const expandPublicTimeline = ({ maxId, onlyMedia, onlyRemote } = {}, done = noOp) => expandTimeline(`public${onlyRemote ? ':remote' : ''}${onlyMedia ? ':media' : ''}`, '/api/v1/timelines/public', { remote: !!onlyRemote, max_id: maxId, only_media: !!onlyMedia }, done);
  108. export const expandCommunityTimeline = ({ maxId, onlyMedia } = {}, done = noOp) => expandTimeline(`community${onlyMedia ? ':media' : ''}`, '/api/v1/timelines/public', { local: true, max_id: maxId, only_media: !!onlyMedia }, done);
  109. export const expandAccountTimeline = (accountId, { maxId, withReplies } = {}) => expandTimeline(`account:${accountId}${withReplies ? ':with_replies' : ''}`, `/api/v1/accounts/${accountId}/statuses`, { exclude_replies: !withReplies, max_id: maxId });
  110. export const expandAccountFeaturedTimeline = accountId => expandTimeline(`account:${accountId}:pinned`, `/api/v1/accounts/${accountId}/statuses`, { pinned: true });
  111. export const expandAccountMediaTimeline = (accountId, { maxId } = {}) => expandTimeline(`account:${accountId}:media`, `/api/v1/accounts/${accountId}/statuses`, { max_id: maxId, only_media: true, limit: 40 });
  112. export const expandListTimeline = (id, { maxId } = {}, done = noOp) => expandTimeline(`list:${id}`, `/api/v1/timelines/list/${id}`, { max_id: maxId }, done);
  113. export const expandHashtagTimeline = (hashtag, { maxId, tags, local } = {}, done = noOp) => {
  114. return expandTimeline(`hashtag:${hashtag}${local ? ':local' : ''}`, `/api/v1/timelines/tag/${hashtag}`, {
  115. max_id: maxId,
  116. any: parseTags(tags, 'any'),
  117. all: parseTags(tags, 'all'),
  118. none: parseTags(tags, 'none'),
  119. local: local,
  120. }, done);
  121. };
  122. export function expandTimelineRequest(timeline, isLoadingMore) {
  123. return {
  124. type: TIMELINE_EXPAND_REQUEST,
  125. timeline,
  126. skipLoading: !isLoadingMore,
  127. };
  128. };
  129. export function expandTimelineSuccess(timeline, statuses, next, partial, isLoadingRecent, isLoadingMore, usePendingItems) {
  130. return {
  131. type: TIMELINE_EXPAND_SUCCESS,
  132. timeline,
  133. statuses,
  134. next,
  135. partial,
  136. isLoadingRecent,
  137. usePendingItems,
  138. skipLoading: !isLoadingMore,
  139. };
  140. };
  141. export function expandTimelineFail(timeline, error, isLoadingMore) {
  142. return {
  143. type: TIMELINE_EXPAND_FAIL,
  144. timeline,
  145. error,
  146. skipLoading: !isLoadingMore,
  147. skipNotFound: timeline.startsWith('account:'),
  148. };
  149. };
  150. export function scrollTopTimeline(timeline, top) {
  151. return {
  152. type: TIMELINE_SCROLL_TOP,
  153. timeline,
  154. top,
  155. };
  156. };
  157. export function connectTimeline(timeline) {
  158. return {
  159. type: TIMELINE_CONNECT,
  160. timeline,
  161. };
  162. };
  163. export const disconnectTimeline = timeline => ({
  164. type: TIMELINE_DISCONNECT,
  165. timeline,
  166. usePendingItems: preferPendingItems,
  167. });
  168. export const markAsPartial = timeline => ({
  169. type: TIMELINE_MARK_AS_PARTIAL,
  170. timeline,
  171. });