logo

mastofe

My custom branche(s) on git.pleroma.social/pleroma/mastofe git clone https://hacktivis.me/git/mastofe.git

timelines.js (4583B)


  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 expandAccountTimeline = (accountId, { maxId, withReplies } = {}) => expandTimeline(`account:${accountId}${withReplies ? ':with_replies' : ''}`, `/api/v1/accounts/${accountId}/statuses`, { exclude_replies: !withReplies, max_id: maxId });
  73. export const expandAccountMediaTimeline = (accountId, { maxId } = {}) => expandTimeline(`account:${accountId}:media`, `/api/v1/accounts/${accountId}/statuses`, { max_id: maxId, only_media: true });
  74. export const expandHashtagTimeline = (hashtag, { maxId } = {}) => expandTimeline(`hashtag:${hashtag}`, `/api/v1/timelines/tag/${hashtag}`, { max_id: maxId });
  75. export const expandListTimeline = (id, { maxId } = {}) => expandTimeline(`list:${id}`, `/api/v1/timelines/list/${id}`, { max_id: maxId });
  76. export function expandTimelineRequest(timeline) {
  77. return {
  78. type: TIMELINE_EXPAND_REQUEST,
  79. timeline,
  80. };
  81. };
  82. export function expandTimelineSuccess(timeline, statuses, next, partial) {
  83. return {
  84. type: TIMELINE_EXPAND_SUCCESS,
  85. timeline,
  86. statuses,
  87. next,
  88. partial,
  89. };
  90. };
  91. export function expandTimelineFail(timeline, error) {
  92. return {
  93. type: TIMELINE_EXPAND_FAIL,
  94. timeline,
  95. error,
  96. };
  97. };
  98. export function scrollTopTimeline(timeline, top) {
  99. return {
  100. type: TIMELINE_SCROLL_TOP,
  101. timeline,
  102. top,
  103. };
  104. };
  105. export function disconnectTimeline(timeline) {
  106. return {
  107. type: TIMELINE_DISCONNECT,
  108. timeline,
  109. };
  110. };