2020-05-30 00:14:16 +10:00
|
|
|
import api from '../api';
|
|
|
|
import { debounce } from 'lodash';
|
2023-05-09 11:11:56 +10:00
|
|
|
import { compareId } from '../compare_id';
|
2022-09-29 12:39:33 +10:00
|
|
|
import { List as ImmutableList } from 'immutable';
|
2020-05-30 00:14:16 +10:00
|
|
|
|
2020-09-27 04:57:07 +10:00
|
|
|
export const MARKERS_FETCH_REQUEST = 'MARKERS_FETCH_REQUEST';
|
|
|
|
export const MARKERS_FETCH_SUCCESS = 'MARKERS_FETCH_SUCCESS';
|
|
|
|
export const MARKERS_FETCH_FAIL = 'MARKERS_FETCH_FAIL';
|
2020-05-30 00:14:16 +10:00
|
|
|
export const MARKERS_SUBMIT_SUCCESS = 'MARKERS_SUBMIT_SUCCESS';
|
|
|
|
|
|
|
|
export const synchronouslySubmitMarkers = () => (dispatch, getState) => {
|
2019-09-06 21:55:51 +10:00
|
|
|
const accessToken = getState().getIn(['meta', 'access_token'], '');
|
2020-05-30 00:14:16 +10:00
|
|
|
const params = _buildParams(getState());
|
|
|
|
|
2022-09-29 12:39:33 +10:00
|
|
|
if (Object.keys(params).length === 0 || accessToken === '') {
|
2020-05-30 00:14:16 +10:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-30 03:25:57 +10:00
|
|
|
// The Fetch API allows us to perform requests that will be carried out
|
|
|
|
// after the page closes. But that only works if the `keepalive` attribute
|
|
|
|
// is supported.
|
|
|
|
if (window.fetch && 'keepalive' in new Request('')) {
|
2020-05-30 00:14:16 +10:00
|
|
|
fetch('/api/v1/markers', {
|
|
|
|
keepalive: true,
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'Authorization': `Bearer ${accessToken}`,
|
|
|
|
},
|
|
|
|
body: JSON.stringify(params),
|
|
|
|
});
|
2020-12-07 13:23:43 +11:00
|
|
|
|
2020-05-30 03:25:57 +10:00
|
|
|
return;
|
|
|
|
} else if (navigator && navigator.sendBeacon) {
|
|
|
|
// Failing that, we can use sendBeacon, but we have to encode the data as
|
|
|
|
// FormData for DoorKeeper to recognize the token.
|
|
|
|
const formData = new FormData();
|
2020-12-07 13:23:43 +11:00
|
|
|
|
2020-05-30 03:25:57 +10:00
|
|
|
formData.append('bearer_token', accessToken);
|
2020-12-07 13:23:43 +11:00
|
|
|
|
2020-05-30 03:25:57 +10:00
|
|
|
for (const [id, value] of Object.entries(params)) {
|
|
|
|
formData.append(`${id}[last_read_id]`, value.last_read_id);
|
|
|
|
}
|
2020-12-07 13:23:43 +11:00
|
|
|
|
2020-05-30 03:25:57 +10:00
|
|
|
if (navigator.sendBeacon('/api/v1/markers', formData)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If neither Fetch nor sendBeacon worked, try to perform a synchronous
|
|
|
|
// request.
|
|
|
|
try {
|
2020-05-30 00:14:16 +10:00
|
|
|
const client = new XMLHttpRequest();
|
|
|
|
|
|
|
|
client.open('POST', '/api/v1/markers', false);
|
|
|
|
client.setRequestHeader('Content-Type', 'application/json');
|
|
|
|
client.setRequestHeader('Authorization', `Bearer ${accessToken}`);
|
2023-04-05 18:56:55 +10:00
|
|
|
client.send(JSON.stringify(params));
|
2020-05-30 03:25:57 +10:00
|
|
|
} catch (e) {
|
|
|
|
// Do not make the BeforeUnload handler error out
|
2020-05-30 00:14:16 +10:00
|
|
|
}
|
|
|
|
};
|
2019-09-06 21:55:51 +10:00
|
|
|
|
2020-05-30 00:14:16 +10:00
|
|
|
const _buildParams = (state) => {
|
|
|
|
const params = {};
|
2019-09-06 21:55:51 +10:00
|
|
|
|
2022-09-29 12:39:33 +10:00
|
|
|
const lastHomeId = state.getIn(['timelines', 'home', 'items'], ImmutableList()).find(item => item !== null);
|
2020-09-27 04:57:07 +10:00
|
|
|
const lastNotificationId = state.getIn(['notifications', 'lastReadId']);
|
2020-05-30 00:14:16 +10:00
|
|
|
|
|
|
|
if (lastHomeId && compareId(lastHomeId, state.getIn(['markers', 'home'])) > 0) {
|
2019-09-06 21:55:51 +10:00
|
|
|
params.home = {
|
|
|
|
last_read_id: lastHomeId,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-05-30 00:14:16 +10:00
|
|
|
if (lastNotificationId && compareId(lastNotificationId, state.getIn(['markers', 'notifications'])) > 0) {
|
2019-09-06 21:55:51 +10:00
|
|
|
params.notifications = {
|
|
|
|
last_read_id: lastNotificationId,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-05-30 00:14:16 +10:00
|
|
|
return params;
|
|
|
|
};
|
|
|
|
|
|
|
|
const debouncedSubmitMarkers = debounce((dispatch, getState) => {
|
2022-09-29 12:39:33 +10:00
|
|
|
const accessToken = getState().getIn(['meta', 'access_token'], '');
|
|
|
|
const params = _buildParams(getState());
|
2020-05-30 00:14:16 +10:00
|
|
|
|
2022-09-29 12:39:33 +10:00
|
|
|
if (Object.keys(params).length === 0 || accessToken === '') {
|
2019-09-06 21:55:51 +10:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-12-07 13:23:43 +11:00
|
|
|
api(getState).post('/api/v1/markers', params).then(() => {
|
2020-05-30 00:14:16 +10:00
|
|
|
dispatch(submitMarkersSuccess(params));
|
2020-12-07 13:23:43 +11:00
|
|
|
}).catch(() => {});
|
2020-05-30 00:14:16 +10:00
|
|
|
}, 300000, { leading: true, trailing: true });
|
|
|
|
|
|
|
|
export function submitMarkersSuccess({ home, notifications }) {
|
|
|
|
return {
|
|
|
|
type: MARKERS_SUBMIT_SUCCESS,
|
|
|
|
home: (home || {}).last_read_id,
|
|
|
|
notifications: (notifications || {}).last_read_id,
|
|
|
|
};
|
2022-12-19 02:51:37 +11:00
|
|
|
}
|
2019-09-06 21:55:51 +10:00
|
|
|
|
2020-10-01 12:17:46 +10:00
|
|
|
export function submitMarkers(params = {}) {
|
|
|
|
const result = (dispatch, getState) => debouncedSubmitMarkers(dispatch, getState);
|
2020-12-07 13:23:43 +11:00
|
|
|
|
2020-10-01 12:17:46 +10:00
|
|
|
if (params.immediate === true) {
|
|
|
|
debouncedSubmitMarkers.flush();
|
|
|
|
}
|
2020-12-07 13:23:43 +11:00
|
|
|
|
2020-10-01 12:17:46 +10:00
|
|
|
return result;
|
2022-12-19 02:51:37 +11:00
|
|
|
}
|
2020-09-27 04:57:07 +10:00
|
|
|
|
|
|
|
export const fetchMarkers = () => (dispatch, getState) => {
|
|
|
|
const params = { timeline: ['notifications'] };
|
|
|
|
|
|
|
|
dispatch(fetchMarkersRequest());
|
|
|
|
|
|
|
|
api(getState).get('/api/v1/markers', { params }).then(response => {
|
|
|
|
dispatch(fetchMarkersSuccess(response.data));
|
|
|
|
}).catch(error => {
|
|
|
|
dispatch(fetchMarkersFail(error));
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export function fetchMarkersRequest() {
|
|
|
|
return {
|
|
|
|
type: MARKERS_FETCH_REQUEST,
|
|
|
|
skipLoading: true,
|
|
|
|
};
|
2022-12-19 02:51:37 +11:00
|
|
|
}
|
2020-09-27 04:57:07 +10:00
|
|
|
|
|
|
|
export function fetchMarkersSuccess(markers) {
|
|
|
|
return {
|
|
|
|
type: MARKERS_FETCH_SUCCESS,
|
|
|
|
markers,
|
|
|
|
skipLoading: true,
|
|
|
|
};
|
2022-12-19 02:51:37 +11:00
|
|
|
}
|
2020-09-27 04:57:07 +10:00
|
|
|
|
|
|
|
export function fetchMarkersFail(error) {
|
|
|
|
return {
|
|
|
|
type: MARKERS_FETCH_FAIL,
|
|
|
|
error,
|
|
|
|
skipLoading: true,
|
|
|
|
skipAlert: true,
|
|
|
|
};
|
2022-12-19 02:51:37 +11:00
|
|
|
}
|