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.

149 lines
5.2 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_CLEAR = 'TIMELINE_CLEAR';
  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, accept) {
  12. return (dispatch, getState) => {
  13. if (typeof accept === 'function' && !accept(status)) {
  14. return;
  15. }
  16. dispatch({
  17. type: TIMELINE_UPDATE,
  18. timeline,
  19. status,
  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. export function clearTimeline(timeline) {
  38. return (dispatch) => {
  39. dispatch({ type: TIMELINE_CLEAR, timeline });
  40. };
  41. };
  42. const noOp = () => {};
  43. const parseTags = (tags = {}, mode) => {
  44. return (tags[mode] || []).map((tag) => {
  45. return tag.value;
  46. });
  47. };
  48. export function expandTimeline(timelineId, path, params = {}, done = noOp) {
  49. return (dispatch, getState) => {
  50. const timeline = getState().getIn(['timelines', timelineId], ImmutableMap());
  51. const isLoadingMore = !!params.max_id;
  52. if (timeline.get('isLoading')) {
  53. done();
  54. return;
  55. }
  56. if (!params.max_id && !params.pinned && timeline.get('items', ImmutableList()).size > 0) {
  57. params.since_id = timeline.getIn(['items', 0]);
  58. }
  59. const isLoadingRecent = !!params.since_id;
  60. dispatch(expandTimelineRequest(timelineId, isLoadingMore));
  61. api(getState).get(path, { params }).then(response => {
  62. const next = getLinks(response).refs.find(link => link.rel === 'next');
  63. dispatch(expandTimelineSuccess(timelineId, response.data, next ? next.uri : null, response.code === 206, isLoadingRecent, isLoadingMore));
  64. done();
  65. }).catch(error => {
  66. dispatch(expandTimelineFail(timelineId, error, isLoadingMore));
  67. done();
  68. });
  69. };
  70. };
  71. export const expandHomeTimeline = ({ maxId } = {}, done = noOp) => expandTimeline('home', '/api/v1/timelines/home', { max_id: maxId }, done);
  72. export const expandPublicTimeline = ({ maxId, onlyMedia } = {}, done = noOp) => expandTimeline(`public${onlyMedia ? ':media' : ''}`, '/api/v1/timelines/public', { max_id: maxId, only_media: !!onlyMedia }, done);
  73. export const expandCommunityTimeline = ({ maxId, onlyMedia } = {}, done = noOp) => expandTimeline(`community${onlyMedia ? ':media' : ''}`, '/api/v1/timelines/public', { local: true, max_id: maxId, only_media: !!onlyMedia }, done);
  74. export const expandDirectTimeline = ({ maxId } = {}, done = noOp) => expandTimeline('direct', '/api/v1/timelines/direct', { max_id: maxId }, done);
  75. export const expandAccountTimeline = (accountId, { maxId, withReplies } = {}) => expandTimeline(`account:${accountId}${withReplies ? ':with_replies' : ''}`, `/api/v1/accounts/${accountId}/statuses`, { exclude_replies: !withReplies, max_id: maxId });
  76. export const expandAccountFeaturedTimeline = accountId => expandTimeline(`account:${accountId}:pinned`, `/api/v1/accounts/${accountId}/statuses`, { pinned: true });
  77. export const expandAccountMediaTimeline = (accountId, { maxId } = {}) => expandTimeline(`account:${accountId}:media`, `/api/v1/accounts/${accountId}/statuses`, { max_id: maxId, only_media: true });
  78. export const expandListTimeline = (id, { maxId } = {}, done = noOp) => expandTimeline(`list:${id}`, `/api/v1/timelines/list/${id}`, { max_id: maxId }, done);
  79. export const expandHashtagTimeline = (hashtag, { maxId, tags } = {}, done = noOp) => {
  80. return expandTimeline(`hashtag:${hashtag}`, `/api/v1/timelines/tag/${hashtag}`, {
  81. max_id: maxId,
  82. any: parseTags(tags, 'any'),
  83. all: parseTags(tags, 'all'),
  84. none: parseTags(tags, 'none'),
  85. }, done);
  86. };
  87. export function expandTimelineRequest(timeline, isLoadingMore) {
  88. return {
  89. type: TIMELINE_EXPAND_REQUEST,
  90. timeline,
  91. skipLoading: !isLoadingMore,
  92. };
  93. };
  94. export function expandTimelineSuccess(timeline, statuses, next, partial, isLoadingRecent, isLoadingMore) {
  95. return {
  96. type: TIMELINE_EXPAND_SUCCESS,
  97. timeline,
  98. statuses,
  99. next,
  100. partial,
  101. isLoadingRecent,
  102. skipLoading: !isLoadingMore,
  103. };
  104. };
  105. export function expandTimelineFail(timeline, error, isLoadingMore) {
  106. return {
  107. type: TIMELINE_EXPAND_FAIL,
  108. timeline,
  109. error,
  110. skipLoading: !isLoadingMore,
  111. };
  112. };
  113. export function scrollTopTimeline(timeline, top) {
  114. return {
  115. type: TIMELINE_SCROLL_TOP,
  116. timeline,
  117. top,
  118. };
  119. };
  120. export function disconnectTimeline(timeline) {
  121. return {
  122. type: TIMELINE_DISCONNECT,
  123. timeline,
  124. };
  125. };