timelines.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 function fillTimelineGaps(timelineId, path, params = {}, done = noOp) {
  103. return (dispatch, getState) => {
  104. const timeline = getState().getIn(['timelines', timelineId], ImmutableMap());
  105. const items = timeline.get('items');
  106. const nullIndexes = items.map((statusId, index) => statusId === null ? index : null);
  107. const gaps = nullIndexes.map(index => index > 0 ? items.get(index - 1) : null);
  108. // Only expand at most two gaps to avoid doing too many requests
  109. done = gaps.take(2).reduce((done, maxId) => {
  110. return (() => dispatch(expandTimeline(timelineId, path, { ...params, maxId }, done)));
  111. }, done);
  112. done();
  113. };
  114. }
  115. export const expandHomeTimeline = ({ maxId } = {}, done = noOp) => expandTimeline('home', '/api/v1/timelines/home', { max_id: maxId }, done);
  116. 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);
  117. export const expandCommunityTimeline = ({ maxId, onlyMedia } = {}, done = noOp) => expandTimeline(`community${onlyMedia ? ':media' : ''}`, '/api/v1/timelines/public', { local: true, max_id: maxId, only_media: !!onlyMedia }, done);
  118. export const expandAccountTimeline = (accountId, { maxId, withReplies, tagged } = {}) => expandTimeline(`account:${accountId}${withReplies ? ':with_replies' : ''}${tagged ? `:${tagged}` : ''}`, `/api/v1/accounts/${accountId}/statuses`, { exclude_replies: !withReplies, tagged, max_id: maxId });
  119. export const expandAccountFeaturedTimeline = (accountId, { tagged } = {}) => expandTimeline(`account:${accountId}:pinned${tagged ? `:${tagged}` : ''}`, `/api/v1/accounts/${accountId}/statuses`, { pinned: true, tagged });
  120. export const expandAccountMediaTimeline = (accountId, { maxId } = {}) => expandTimeline(`account:${accountId}:media`, `/api/v1/accounts/${accountId}/statuses`, { max_id: maxId, only_media: true, limit: 40 });
  121. export const expandListTimeline = (id, { maxId } = {}, done = noOp) => expandTimeline(`list:${id}`, `/api/v1/timelines/list/${id}`, { max_id: maxId }, done);
  122. export const expandHashtagTimeline = (hashtag, { maxId, tags, local } = {}, done = noOp) => {
  123. return expandTimeline(`hashtag:${hashtag}${local ? ':local' : ''}`, `/api/v1/timelines/tag/${hashtag}`, {
  124. max_id: maxId,
  125. any: parseTags(tags, 'any'),
  126. all: parseTags(tags, 'all'),
  127. none: parseTags(tags, 'none'),
  128. local: local,
  129. }, done);
  130. };
  131. export const fillHomeTimelineGaps = (done = noOp) => fillTimelineGaps('home', '/api/v1/timelines/home', {}, done);
  132. export const fillPublicTimelineGaps = ({ onlyMedia, onlyRemote } = {}, done = noOp) => fillTimelineGaps(`public${onlyRemote ? ':remote' : ''}${onlyMedia ? ':media' : ''}`, '/api/v1/timelines/public', { remote: !!onlyRemote, only_media: !!onlyMedia }, done);
  133. export const fillCommunityTimelineGaps = ({ onlyMedia } = {}, done = noOp) => fillTimelineGaps(`community${onlyMedia ? ':media' : ''}`, '/api/v1/timelines/public', { local: true, only_media: !!onlyMedia }, done);
  134. export const fillListTimelineGaps = (id, done = noOp) => fillTimelineGaps(`list:${id}`, `/api/v1/timelines/list/${id}`, {}, done);
  135. export function expandTimelineRequest(timeline, isLoadingMore) {
  136. return {
  137. type: TIMELINE_EXPAND_REQUEST,
  138. timeline,
  139. skipLoading: !isLoadingMore,
  140. };
  141. };
  142. export function expandTimelineSuccess(timeline, statuses, next, partial, isLoadingRecent, isLoadingMore, usePendingItems) {
  143. return {
  144. type: TIMELINE_EXPAND_SUCCESS,
  145. timeline,
  146. statuses,
  147. next,
  148. partial,
  149. isLoadingRecent,
  150. usePendingItems,
  151. skipLoading: !isLoadingMore,
  152. };
  153. };
  154. export function expandTimelineFail(timeline, error, isLoadingMore) {
  155. return {
  156. type: TIMELINE_EXPAND_FAIL,
  157. timeline,
  158. error,
  159. skipLoading: !isLoadingMore,
  160. skipNotFound: timeline.startsWith('account:'),
  161. };
  162. };
  163. export function scrollTopTimeline(timeline, top) {
  164. return {
  165. type: TIMELINE_SCROLL_TOP,
  166. timeline,
  167. top,
  168. };
  169. };
  170. export function connectTimeline(timeline) {
  171. return {
  172. type: TIMELINE_CONNECT,
  173. timeline,
  174. usePendingItems: preferPendingItems,
  175. };
  176. };
  177. export const disconnectTimeline = timeline => ({
  178. type: TIMELINE_DISCONNECT,
  179. timeline,
  180. usePendingItems: preferPendingItems,
  181. });
  182. export const markAsPartial = timeline => ({
  183. type: TIMELINE_MARK_AS_PARTIAL,
  184. timeline,
  185. });