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.

199 lines
6.7 KiB

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