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.

135 lines
4.8 KiB

7 years ago
  1. import { importFetchedStatus, importFetchedStatuses } from './importer';
  2. import api, { getLinks } from '../api';
  3. import { Map as ImmutableMap } from 'immutable';
  4. export const TIMELINE_UPDATE = 'TIMELINE_UPDATE';
  5. export const TIMELINE_DELETE = 'TIMELINE_DELETE';
  6. export const TIMELINE_EXPAND_REQUEST = 'TIMELINE_EXPAND_REQUEST';
  7. export const TIMELINE_EXPAND_SUCCESS = 'TIMELINE_EXPAND_SUCCESS';
  8. export const TIMELINE_EXPAND_FAIL = 'TIMELINE_EXPAND_FAIL';
  9. export const TIMELINE_SCROLL_TOP = 'TIMELINE_SCROLL_TOP';
  10. export const TIMELINE_DISCONNECT = 'TIMELINE_DISCONNECT';
  11. export const TIMELINE_CONTEXT_UPDATE = 'CONTEXT_UPDATE';
  12. export function updateTimeline(timeline, status) {
  13. return (dispatch, getState) => {
  14. const references = status.reblog ? getState().get('statuses').filter((item, itemId) => (itemId === status.reblog.id || item.get('reblog') === status.reblog.id)).map((_, itemId) => itemId) : [];
  15. const parents = [];
  16. if (status.in_reply_to_id) {
  17. let parent = getState().getIn(['statuses', status.in_reply_to_id]);
  18. while (parent && parent.get('in_reply_to_id')) {
  19. parents.push(parent.get('id'));
  20. parent = getState().getIn(['statuses', parent.get('in_reply_to_id')]);
  21. }
  22. }
  23. dispatch(importFetchedStatus(status));
  24. dispatch({
  25. type: TIMELINE_UPDATE,
  26. timeline,
  27. status,
  28. references,
  29. });
  30. if (parents.length > 0) {
  31. dispatch({
  32. type: TIMELINE_CONTEXT_UPDATE,
  33. status,
  34. references: parents,
  35. });
  36. }
  37. };
  38. };
  39. export function deleteFromTimelines(id) {
  40. return (dispatch, getState) => {
  41. const accountId = getState().getIn(['statuses', id, 'account']);
  42. const references = getState().get('statuses').filter(status => status.get('reblog') === id).map(status => [status.get('id'), status.get('account')]);
  43. const reblogOf = getState().getIn(['statuses', id, 'reblog'], null);
  44. dispatch({
  45. type: TIMELINE_DELETE,
  46. id,
  47. accountId,
  48. references,
  49. reblogOf,
  50. });
  51. };
  52. };
  53. export function expandTimeline(timelineId, path, params = {}) {
  54. return (dispatch, getState) => {
  55. const timeline = getState().getIn(['timelines', timelineId], ImmutableMap());
  56. if (timeline.get('isLoading')) {
  57. return;
  58. }
  59. dispatch(expandTimelineRequest(timelineId));
  60. api(getState).get(path, { params }).then(response => {
  61. const next = getLinks(response).refs.find(link => link.rel === 'next');
  62. dispatch(importFetchedStatuses(response.data));
  63. dispatch(expandTimelineSuccess(timelineId, response.data, next ? next.uri : null, response.code === 206));
  64. }).catch(error => {
  65. dispatch(expandTimelineFail(timelineId, error));
  66. });
  67. };
  68. };
  69. export const expandHomeTimeline = ({ maxId } = {}) => expandTimeline('home', '/api/v1/timelines/home', { max_id: maxId });
  70. export const expandPublicTimeline = ({ maxId } = {}) => expandTimeline('public', '/api/v1/timelines/public', { max_id: maxId });
  71. export const expandCommunityTimeline = ({ maxId } = {}) => expandTimeline('community', '/api/v1/timelines/public', { local: true, max_id: maxId });
  72. export const expandDirectTimeline = ({ maxId } = {}) => expandTimeline('direct', '/api/v1/timelines/direct', { max_id: maxId });
  73. export const expandAccountTimeline = (accountId, { maxId, withReplies } = {}) => expandTimeline(`account:${accountId}${withReplies ? ':with_replies' : ''}`, `/api/v1/accounts/${accountId}/statuses`, { exclude_replies: !withReplies, max_id: maxId });
  74. export const expandAccountFeaturedTimeline = accountId => expandTimeline(`account:${accountId}:pinned`, `/api/v1/accounts/${accountId}/statuses`, { pinned: true });
  75. export const expandAccountMediaTimeline = (accountId, { maxId } = {}) => expandTimeline(`account:${accountId}:media`, `/api/v1/accounts/${accountId}/statuses`, { max_id: maxId, only_media: true });
  76. export const expandHashtagTimeline = (hashtag, { maxId } = {}) => expandTimeline(`hashtag:${hashtag}`, `/api/v1/timelines/tag/${hashtag}`, { max_id: maxId });
  77. export const expandListTimeline = (id, { maxId } = {}) => expandTimeline(`list:${id}`, `/api/v1/timelines/list/${id}`, { max_id: maxId });
  78. export function expandTimelineRequest(timeline) {
  79. return {
  80. type: TIMELINE_EXPAND_REQUEST,
  81. timeline,
  82. };
  83. };
  84. export function expandTimelineSuccess(timeline, statuses, next, partial) {
  85. return {
  86. type: TIMELINE_EXPAND_SUCCESS,
  87. timeline,
  88. statuses,
  89. next,
  90. partial,
  91. };
  92. };
  93. export function expandTimelineFail(timeline, error) {
  94. return {
  95. type: TIMELINE_EXPAND_FAIL,
  96. timeline,
  97. error,
  98. };
  99. };
  100. export function scrollTopTimeline(timeline, top) {
  101. return {
  102. type: TIMELINE_SCROLL_TOP,
  103. timeline,
  104. top,
  105. };
  106. };
  107. export function disconnectTimeline(timeline) {
  108. return {
  109. type: TIMELINE_DISCONNECT,
  110. timeline,
  111. };
  112. };