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.
 
 
 
 

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