The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

160 lines
5.4 KiB

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