The code powering m.abunchtell.com https://m.abunchtell.com
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

175 linhas
6.0 KiB

  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'), status.get('account')]);
  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. done();
  83. }).catch(error => {
  84. dispatch(expandTimelineFail(timelineId, error, isLoadingMore));
  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 } = {}, 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. }, done);
  103. };
  104. export function expandTimelineRequest(timeline, isLoadingMore) {
  105. return {
  106. type: TIMELINE_EXPAND_REQUEST,
  107. timeline,
  108. skipLoading: !isLoadingMore,
  109. };
  110. };
  111. export function expandTimelineSuccess(timeline, statuses, next, partial, isLoadingRecent, isLoadingMore, usePendingItems) {
  112. return {
  113. type: TIMELINE_EXPAND_SUCCESS,
  114. timeline,
  115. statuses,
  116. next,
  117. partial,
  118. isLoadingRecent,
  119. usePendingItems,
  120. skipLoading: !isLoadingMore,
  121. };
  122. };
  123. export function expandTimelineFail(timeline, error, isLoadingMore) {
  124. return {
  125. type: TIMELINE_EXPAND_FAIL,
  126. timeline,
  127. error,
  128. skipLoading: !isLoadingMore,
  129. };
  130. };
  131. export function scrollTopTimeline(timeline, top) {
  132. return {
  133. type: TIMELINE_SCROLL_TOP,
  134. timeline,
  135. top,
  136. };
  137. };
  138. export function connectTimeline(timeline) {
  139. return {
  140. type: TIMELINE_CONNECT,
  141. timeline,
  142. };
  143. };
  144. export const disconnectTimeline = timeline => ({
  145. type: TIMELINE_DISCONNECT,
  146. timeline,
  147. usePendingItems: preferPendingItems,
  148. });