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.

176 lines
6.1 KiB

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