-export const submitMarkers = () => (dispatch, getState) => {
+import api from '../api';
+import { debounce } from 'lodash';
+import compareId from '../compare_id';
+import { showAlertForError } from './alerts';
+
+export const MARKERS_SUBMIT_SUCCESS = 'MARKERS_SUBMIT_SUCCESS';
+
+export const synchronouslySubmitMarkers = () => (dispatch, getState) => {
const accessToken = getState().getIn(['meta', 'access_token'], '');
- const params = {};
+ const params = _buildParams(getState());
+
+ if (Object.keys(params).length === 0) {
+ return;
+ }
+
+ if (window.fetch) {
+ fetch('/api/v1/markers', {
+ keepalive: true,
+ method: 'POST',
+ headers: {
+ 'Content-Type': 'application/json',
+ 'Authorization': `Bearer ${accessToken}`,
+ },
+ body: JSON.stringify(params),
+ });
+ } else {
+ const client = new XMLHttpRequest();
+
+ client.open('POST', '/api/v1/markers', false);
+ client.setRequestHeader('Content-Type', 'application/json');
+ client.setRequestHeader('Authorization', `Bearer ${accessToken}`);
+ client.SUBMIT(JSON.stringify(params));
+ }
+};
- const lastHomeId = getState().getIn(['timelines', 'home', 'items', 0]);
- const lastNotificationId = getState().getIn(['notifications', 'items', 0, 'id']);
+const _buildParams = (state) => {
+ const params = {};
- if (lastHomeId) {
+ const lastHomeId = state.getIn(['timelines', 'home', 'items', 0]);
+ const lastNotificationId = state.getIn(['notifications', 'items', 0, 'id']);
+
+ if (lastHomeId && compareId(lastHomeId, state.getIn(['markers', 'home'])) > 0) {
params.home = {
last_read_id: lastHomeId,
};
}
- if (lastNotificationId) {
+ if (lastNotificationId && compareId(lastNotificationId, state.getIn(['markers', 'notifications'])) > 0) {
params.notifications = {
last_read_id: lastNotificationId,
};
}
+ return params;
+};
+
+const debouncedSubmitMarkers = debounce((dispatch, getState) => {
+ const params = _buildParams(getState());
+
if (Object.keys(params).length === 0) {
return;
}
- const client = new XMLHttpRequest();
+ api().post('/api/v1/markers', params).then(() => {
+ dispatch(submitMarkersSuccess(params));
+ }).catch(error => {
+ dispatch(showAlertForError(error));
+ });
+}, 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,
+ };
+};
- client.open('POST', '/api/v1/markers', false);
- client.setRequestHeader('Content-Type', 'application/json');
- client.setRequestHeader('Authorization', `Bearer ${accessToken}`);
- client.send(JSON.stringify(params));
+export function submitMarkers() {
+ return (dispatch, getState) => debouncedSubmitMarkers(dispatch, getState);
};
importFetchedStatus,
importFetchedStatuses,
} from './importer';
+import { submitMarkers } from './markers';
import { saveSettings } from './settings';
import { defineMessages } from 'react-intl';
import { List as ImmutableList } from 'immutable';
filtered = regex && regex.test(searchIndex);
}
+ dispatch(submitMarkers());
+
if (showInColumn) {
dispatch(importFetchedAccount(notification.account));
dispatch(expandNotificationsSuccess(response.data, next ? next.uri : null, isLoadingMore, isLoadingRecent, isLoadingRecent && preferPendingItems));
fetchRelatedRelationships(dispatch, response.data);
+ dispatch(submitMarkers());
}).catch(error => {
dispatch(expandNotificationsFail(error, isLoadingMore));
}).finally(() => {
import { importFetchedStatus, importFetchedStatuses } from './importer';
+import { submitMarkers } from './markers';
import api, { getLinks } from 'mastodon/api';
import { Map as ImmutableMap, List as ImmutableList } from 'immutable';
import compareId from 'mastodon/compare_id';
status,
usePendingItems: preferPendingItems,
});
+
+ if (timeline === 'home') {
+ dispatch(submitMarkers());
+ }
};
};
const next = getLinks(response).refs.find(link => link.rel === 'next');
dispatch(importFetchedStatuses(response.data));
dispatch(expandTimelineSuccess(timelineId, response.data, next ? next.uri : null, response.status === 206, isLoadingRecent, isLoadingMore, isLoadingRecent && preferPendingItems));
+
+ if (timelineId === 'home') {
+ dispatch(submitMarkers());
+ }
}).catch(error => {
dispatch(expandTimelineFail(timelineId, error, isLoadingMore));
}).finally(() => {
import { fetchFilters } from '../../actions/filters';
import { clearHeight } from '../../actions/height_cache';
import { focusApp, unfocusApp } from 'mastodon/actions/app';
-import { submitMarkers } from 'mastodon/actions/markers';
+import { synchronouslySubmitMarkers } from 'mastodon/actions/markers';
import { WrappedSwitch, WrappedRoute } from './util/react_router_helpers';
import UploadArea from './components/upload_area';
import ColumnsAreaContainer from './containers/columns_area_container';
handleBeforeUnload = e => {
const { intl, dispatch, isComposing, hasComposingText, hasMediaAttachments } = this.props;
- dispatch(submitMarkers());
+ dispatch(synchronouslySubmitMarkers());
if (isComposing && (hasComposingText || hasMediaAttachments)) {
// Setting returnValue to any string causes confirmation dialog.