]> cat aescling's git repositories - mastodon.git/blob - app/javascript/mastodon/actions/timelines.js
Improve eslint rules (#3147)
[mastodon.git] / app / javascript / mastodon / actions / timelines.js
1 import api, { getLinks } from '../api';
2 import Immutable from 'immutable';
3
4 export const TIMELINE_UPDATE = 'TIMELINE_UPDATE';
5 export const TIMELINE_DELETE = 'TIMELINE_DELETE';
6
7 export const TIMELINE_REFRESH_REQUEST = 'TIMELINE_REFRESH_REQUEST';
8 export const TIMELINE_REFRESH_SUCCESS = 'TIMELINE_REFRESH_SUCCESS';
9 export const TIMELINE_REFRESH_FAIL = 'TIMELINE_REFRESH_FAIL';
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
17 export const TIMELINE_CONNECT = 'TIMELINE_CONNECT';
18 export const TIMELINE_DISCONNECT = 'TIMELINE_DISCONNECT';
19
20 export function refreshTimelineSuccess(timeline, statuses, skipLoading, next) {
21 return {
22 type: TIMELINE_REFRESH_SUCCESS,
23 timeline,
24 statuses,
25 skipLoading,
26 next,
27 };
28 };
29
30 export function updateTimeline(timeline, status) {
31 return (dispatch, getState) => {
32 const references = status.reblog ? getState().get('statuses').filter((item, itemId) => (itemId === status.reblog.id || item.get('reblog') === status.reblog.id)).map((_, itemId) => itemId) : [];
33
34 dispatch({
35 type: TIMELINE_UPDATE,
36 timeline,
37 status,
38 references,
39 });
40 };
41 };
42
43 export function deleteFromTimelines(id) {
44 return (dispatch, getState) => {
45 const accountId = getState().getIn(['statuses', id, 'account']);
46 const references = getState().get('statuses').filter(status => status.get('reblog') === id).map(status => [status.get('id'), status.get('account')]);
47 const reblogOf = getState().getIn(['statuses', id, 'reblog'], null);
48
49 dispatch({
50 type: TIMELINE_DELETE,
51 id,
52 accountId,
53 references,
54 reblogOf,
55 });
56 };
57 };
58
59 export function refreshTimelineRequest(timeline, id, skipLoading) {
60 return {
61 type: TIMELINE_REFRESH_REQUEST,
62 timeline,
63 id,
64 skipLoading,
65 };
66 };
67
68 export function refreshTimeline(timeline, id = null) {
69 return function (dispatch, getState) {
70 if (getState().getIn(['timelines', timeline, 'isLoading'])) {
71 return;
72 }
73
74 const ids = getState().getIn(['timelines', timeline, 'items'], Immutable.List());
75 const newestId = ids.size > 0 ? ids.first() : null;
76 let params = getState().getIn(['timelines', timeline, 'params'], {});
77 const path = getState().getIn(['timelines', timeline, 'path'])(id);
78
79 let skipLoading = false;
80
81 if (newestId !== null && getState().getIn(['timelines', timeline, 'loaded']) && (id === null || getState().getIn(['timelines', timeline, 'id']) === id)) {
82 if (id === null && getState().getIn(['timelines', timeline, 'online'])) {
83 // Skip refreshing when timeline is live anyway
84 return;
85 }
86
87 params = { ...params, since_id: newestId };
88 skipLoading = true;
89 } else if (getState().getIn(['timelines', timeline, 'loaded'])) {
90 skipLoading = true;
91 }
92
93 dispatch(refreshTimelineRequest(timeline, id, skipLoading));
94
95 api(getState).get(path, { params }).then(response => {
96 const next = getLinks(response).refs.find(link => link.rel === 'next');
97 dispatch(refreshTimelineSuccess(timeline, response.data, skipLoading, next ? next.uri : null));
98 }).catch(error => {
99 dispatch(refreshTimelineFail(timeline, error, skipLoading));
100 });
101 };
102 };
103
104 export function refreshTimelineFail(timeline, error, skipLoading) {
105 return {
106 type: TIMELINE_REFRESH_FAIL,
107 timeline,
108 error,
109 skipLoading,
110 };
111 };
112
113 export function expandTimeline(timeline) {
114 return (dispatch, getState) => {
115 if (getState().getIn(['timelines', timeline, 'isLoading'])) {
116 return;
117 }
118
119 if (getState().getIn(['timelines', timeline, 'items']).size === 0) {
120 return;
121 }
122
123 const path = getState().getIn(['timelines', timeline, 'path'])(getState().getIn(['timelines', timeline, 'id']));
124 const params = getState().getIn(['timelines', timeline, 'params'], {});
125 const lastId = getState().getIn(['timelines', timeline, 'items']).last();
126
127 dispatch(expandTimelineRequest(timeline));
128
129 api(getState).get(path, {
130 params: {
131 ...params,
132 max_id: lastId,
133 limit: 10,
134 },
135 }).then(response => {
136 const next = getLinks(response).refs.find(link => link.rel === 'next');
137 dispatch(expandTimelineSuccess(timeline, response.data, next ? next.uri : null));
138 }).catch(error => {
139 dispatch(expandTimelineFail(timeline, error));
140 });
141 };
142 };
143
144 export function expandTimelineRequest(timeline) {
145 return {
146 type: TIMELINE_EXPAND_REQUEST,
147 timeline,
148 };
149 };
150
151 export function expandTimelineSuccess(timeline, statuses, next) {
152 return {
153 type: TIMELINE_EXPAND_SUCCESS,
154 timeline,
155 statuses,
156 next,
157 };
158 };
159
160 export function expandTimelineFail(timeline, error) {
161 return {
162 type: TIMELINE_EXPAND_FAIL,
163 timeline,
164 error,
165 };
166 };
167
168 export function scrollTopTimeline(timeline, top) {
169 return {
170 type: TIMELINE_SCROLL_TOP,
171 timeline,
172 top,
173 };
174 };
175
176 export function connectTimeline(timeline) {
177 return {
178 type: TIMELINE_CONNECT,
179 timeline,
180 };
181 };
182
183 export function disconnectTimeline(timeline) {
184 return {
185 type: TIMELINE_DISCONNECT,
186 timeline,
187 };
188 };
This page took 0.10154 seconds and 5 git commands to generate.