]> cat aescling's git repositories - mastodon.git/blob - app/javascript/mastodon/actions/timelines.js
Summary: fix slowness due to layout thrashing when reloading a large … (#12661)
[mastodon.git] / app / javascript / mastodon / actions / timelines.js
1 import { importFetchedStatus, importFetchedStatuses } from './importer';
2 import api, { getLinks } from 'mastodon/api';
3 import { Map as ImmutableMap, List as ImmutableList } from 'immutable';
4 import compareId from 'mastodon/compare_id';
5 import { usePendingItems as preferPendingItems } from 'mastodon/initial_state';
6
7 export const TIMELINE_UPDATE = 'TIMELINE_UPDATE';
8 export const TIMELINE_DELETE = 'TIMELINE_DELETE';
9 export const TIMELINE_CLEAR = 'TIMELINE_CLEAR';
10
11 export const TIMELINE_EXPAND_REQUEST = 'TIMELINE_EXPAND_REQUEST';
12 export const TIMELINE_EXPAND_SUCCESS = 'TIMELINE_EXPAND_SUCCESS';
13 export const TIMELINE_EXPAND_FAIL = 'TIMELINE_EXPAND_FAIL';
14
15 export const TIMELINE_SCROLL_TOP = 'TIMELINE_SCROLL_TOP';
16 export const TIMELINE_LOAD_PENDING = 'TIMELINE_LOAD_PENDING';
17 export const TIMELINE_DISCONNECT = 'TIMELINE_DISCONNECT';
18 export const TIMELINE_CONNECT = 'TIMELINE_CONNECT';
19
20 export const CURRENTLY_VIEWING = 'CURRENTLY_VIEWING';
21
22 export const updateCurrentlyViewing = (timeline, id) => ({
23 type: CURRENTLY_VIEWING,
24 timeline,
25 id,
26 });
27
28 export const loadPending = timeline => ({
29 type: TIMELINE_LOAD_PENDING,
30 timeline,
31 });
32
33 export function updateTimeline(timeline, status, accept) {
34 return dispatch => {
35 if (typeof accept === 'function' && !accept(status)) {
36 return;
37 }
38
39 dispatch(importFetchedStatus(status));
40
41 dispatch({
42 type: TIMELINE_UPDATE,
43 timeline,
44 status,
45 usePendingItems: preferPendingItems,
46 });
47 };
48 };
49
50 export function deleteFromTimelines(id) {
51 return (dispatch, getState) => {
52 const accountId = getState().getIn(['statuses', id, 'account']);
53 const references = getState().get('statuses').filter(status => status.get('reblog') === id).map(status => [status.get('id'), status.get('account')]);
54 const reblogOf = getState().getIn(['statuses', id, 'reblog'], null);
55
56 dispatch({
57 type: TIMELINE_DELETE,
58 id,
59 accountId,
60 references,
61 reblogOf,
62 });
63 };
64 };
65
66 export function clearTimeline(timeline) {
67 return (dispatch) => {
68 dispatch({ type: TIMELINE_CLEAR, timeline });
69 };
70 };
71
72 const noOp = () => {};
73
74 const parseTags = (tags = {}, mode) => {
75 return (tags[mode] || []).map((tag) => {
76 return tag.value;
77 });
78 };
79
80 export function expandTimeline(timelineId, path, params = {}, done = noOp) {
81 return (dispatch, getState) => {
82 const timeline = getState().getIn(['timelines', timelineId], ImmutableMap());
83 const isLoadingMore = !!params.max_id;
84
85 if (timeline.get('isLoading')) {
86 done();
87 return;
88 }
89
90 if (!params.max_id && !params.pinned && (timeline.get('items', ImmutableList()).size + timeline.get('pendingItems', ImmutableList()).size) > 0) {
91 const a = timeline.getIn(['pendingItems', 0]);
92 const b = timeline.getIn(['items', 0]);
93
94 if (a && b && compareId(a, b) > 0) {
95 params.since_id = a;
96 } else {
97 params.since_id = b || a;
98 }
99 }
100
101 const isLoadingRecent = !!params.since_id;
102
103 dispatch(expandTimelineRequest(timelineId, isLoadingMore));
104
105 api(getState).get(path, { params }).then(response => {
106 const next = getLinks(response).refs.find(link => link.rel === 'next');
107 dispatch(importFetchedStatuses(response.data));
108 dispatch(expandTimelineSuccess(timelineId, response.data, next ? next.uri : null, response.status === 206, isLoadingRecent, isLoadingMore, isLoadingRecent && preferPendingItems));
109 done();
110 }).catch(error => {
111 dispatch(expandTimelineFail(timelineId, error, isLoadingMore));
112 done();
113 });
114 };
115 };
116
117 export const expandHomeTimeline = ({ maxId } = {}, done = noOp) => expandTimeline('home', '/api/v1/timelines/home', { max_id: maxId }, done);
118 export const expandPublicTimeline = ({ maxId, onlyMedia } = {}, done = noOp) => expandTimeline(`public${onlyMedia ? ':media' : ''}`, '/api/v1/timelines/public', { max_id: maxId, only_media: !!onlyMedia }, done);
119 export const expandCommunityTimeline = ({ maxId, onlyMedia } = {}, done = noOp) => expandTimeline(`community${onlyMedia ? ':media' : ''}`, '/api/v1/timelines/public', { local: true, max_id: maxId, only_media: !!onlyMedia }, done);
120 export const expandAccountTimeline = (accountId, { maxId, withReplies } = {}) => expandTimeline(`account:${accountId}${withReplies ? ':with_replies' : ''}`, `/api/v1/accounts/${accountId}/statuses`, { exclude_replies: !withReplies, max_id: maxId });
121 export const expandAccountFeaturedTimeline = accountId => expandTimeline(`account:${accountId}:pinned`, `/api/v1/accounts/${accountId}/statuses`, { pinned: true });
122 export const expandAccountMediaTimeline = (accountId, { maxId } = {}) => expandTimeline(`account:${accountId}:media`, `/api/v1/accounts/${accountId}/statuses`, { max_id: maxId, only_media: true, limit: 40 });
123 export const expandListTimeline = (id, { maxId } = {}, done = noOp) => expandTimeline(`list:${id}`, `/api/v1/timelines/list/${id}`, { max_id: maxId }, done);
124 export const expandHashtagTimeline = (hashtag, { maxId, tags } = {}, done = noOp) => {
125 return expandTimeline(`hashtag:${hashtag}`, `/api/v1/timelines/tag/${hashtag}`, {
126 max_id: maxId,
127 any: parseTags(tags, 'any'),
128 all: parseTags(tags, 'all'),
129 none: parseTags(tags, 'none'),
130 }, done);
131 };
132
133 export function expandTimelineRequest(timeline, isLoadingMore) {
134 return {
135 type: TIMELINE_EXPAND_REQUEST,
136 timeline,
137 skipLoading: !isLoadingMore,
138 };
139 };
140
141 export function expandTimelineSuccess(timeline, statuses, next, partial, isLoadingRecent, isLoadingMore, usePendingItems) {
142 return {
143 type: TIMELINE_EXPAND_SUCCESS,
144 timeline,
145 statuses,
146 next,
147 partial,
148 isLoadingRecent,
149 usePendingItems,
150 skipLoading: !isLoadingMore,
151 };
152 };
153
154 export function expandTimelineFail(timeline, error, isLoadingMore) {
155 return {
156 type: TIMELINE_EXPAND_FAIL,
157 timeline,
158 error,
159 skipLoading: !isLoadingMore,
160 };
161 };
162
163 export function scrollTopTimeline(timeline, top) {
164 return {
165 type: TIMELINE_SCROLL_TOP,
166 timeline,
167 top,
168 };
169 };
170
171 export function connectTimeline(timeline) {
172 return {
173 type: TIMELINE_CONNECT,
174 timeline,
175 };
176 };
177
178 export const disconnectTimeline = timeline => ({
179 type: TIMELINE_DISCONNECT,
180 timeline,
181 usePendingItems: preferPendingItems,
182 });
This page took 0.116975 seconds and 4 git commands to generate.