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.

140 lines
5.0 KiB

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