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.

161 lines
5.5 KiB

8 years ago
  1. import { importFetchedStatus, importFetchedStatuses } from './importer';
  2. import api, { getLinks } from 'flavours/glitch/util/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 expandDirectTimeline = ({ maxId } = {}, done = noOp) => expandTimeline('direct', '/api/v1/timelines/direct', { max_id: maxId }, done);
  79. export const expandAccountTimeline = (accountId, { maxId, withReplies } = {}) => expandTimeline(`account:${accountId}${withReplies ? ':with_replies' : ''}`, `/api/v1/accounts/${accountId}/statuses`, { exclude_replies: !withReplies, max_id: maxId });
  80. export const expandAccountFeaturedTimeline = accountId => expandTimeline(`account:${accountId}:pinned`, `/api/v1/accounts/${accountId}/statuses`, { pinned: true });
  81. export const expandAccountMediaTimeline = (accountId, { maxId } = {}) => expandTimeline(`account:${accountId}:media`, `/api/v1/accounts/${accountId}/statuses`, { max_id: maxId, only_media: true, limit: 40 });
  82. export const expandListTimeline = (id, { maxId } = {}, done = noOp) => expandTimeline(`list:${id}`, `/api/v1/timelines/list/${id}`, { max_id: maxId }, done);
  83. export const expandHashtagTimeline = (hashtag, { maxId, tags } = {}, done = noOp) => {
  84. return expandTimeline(`hashtag:${hashtag}`, `/api/v1/timelines/tag/${hashtag}`, {
  85. max_id: maxId,
  86. any: parseTags(tags, 'any'),
  87. all: parseTags(tags, 'all'),
  88. none: parseTags(tags, 'none'),
  89. }, done);
  90. };
  91. export function expandTimelineRequest(timeline, isLoadingMore) {
  92. return {
  93. type: TIMELINE_EXPAND_REQUEST,
  94. timeline,
  95. skipLoading: !isLoadingMore,
  96. };
  97. };
  98. export function expandTimelineSuccess(timeline, statuses, next, partial, isLoadingRecent, isLoadingMore) {
  99. return {
  100. type: TIMELINE_EXPAND_SUCCESS,
  101. timeline,
  102. statuses,
  103. next,
  104. partial,
  105. isLoadingRecent,
  106. skipLoading: !isLoadingMore,
  107. };
  108. };
  109. export function expandTimelineFail(timeline, error, isLoadingMore) {
  110. return {
  111. type: TIMELINE_EXPAND_FAIL,
  112. timeline,
  113. error,
  114. skipLoading: !isLoadingMore,
  115. };
  116. };
  117. export function scrollTopTimeline(timeline, top) {
  118. return {
  119. type: TIMELINE_SCROLL_TOP,
  120. timeline,
  121. top,
  122. };
  123. };
  124. export function connectTimeline(timeline) {
  125. return {
  126. type: TIMELINE_CONNECT,
  127. timeline,
  128. };
  129. };
  130. export function disconnectTimeline(timeline) {
  131. return {
  132. type: TIMELINE_DISCONNECT,
  133. timeline,
  134. };
  135. };