2024-03-27 12:47:09 +00:00
|
|
|
import { debounce } from 'lodash';
|
|
|
|
|
|
|
|
import type { MarkerJSON } from 'mastodon/api_types/markers';
|
2024-05-22 14:45:18 +00:00
|
|
|
import { getAccessToken } from 'mastodon/initial_state';
|
2024-04-26 17:11:27 +00:00
|
|
|
import type { AppDispatch, RootState } from 'mastodon/store';
|
2024-03-27 12:47:09 +00:00
|
|
|
import { createAppAsyncThunk } from 'mastodon/store/typed_functions';
|
|
|
|
|
2024-05-22 14:45:18 +00:00
|
|
|
import api from '../api';
|
2024-03-27 12:47:09 +00:00
|
|
|
import { compareId } from '../compare_id';
|
|
|
|
|
|
|
|
export const synchronouslySubmitMarkers = createAppAsyncThunk(
|
|
|
|
'markers/submit',
|
|
|
|
async (_args, { getState }) => {
|
2024-05-22 14:45:18 +00:00
|
|
|
const accessToken = getAccessToken();
|
2024-03-27 12:47:09 +00:00
|
|
|
const params = buildPostMarkersParams(getState());
|
|
|
|
|
2024-05-22 14:45:18 +00:00
|
|
|
if (
|
|
|
|
Object.keys(params).length === 0 ||
|
|
|
|
!accessToken ||
|
|
|
|
accessToken === ''
|
|
|
|
) {
|
2024-03-27 12:47:09 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 ('fetch' in window && 'keepalive' in new Request('')) {
|
|
|
|
await fetch('/api/v1/markers', {
|
|
|
|
keepalive: true,
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
Authorization: `Bearer ${accessToken}`,
|
|
|
|
},
|
|
|
|
body: JSON.stringify(params),
|
|
|
|
});
|
|
|
|
|
|
|
|
return;
|
2024-10-02 10:03:04 +00:00
|
|
|
} else if ('sendBeacon' in navigator) {
|
2024-03-27 12:47:09 +00:00
|
|
|
// 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();
|
|
|
|
|
|
|
|
formData.append('bearer_token', accessToken);
|
|
|
|
|
|
|
|
for (const [id, value] of Object.entries(params)) {
|
|
|
|
if (value.last_read_id)
|
|
|
|
formData.append(`${id}[last_read_id]`, value.last_read_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (navigator.sendBeacon('/api/v1/markers', formData)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If neither Fetch nor sendBeacon worked, try to perform a synchronous
|
|
|
|
// request.
|
|
|
|
try {
|
|
|
|
const client = new XMLHttpRequest();
|
|
|
|
|
|
|
|
client.open('POST', '/api/v1/markers', false);
|
|
|
|
client.setRequestHeader('Content-Type', 'application/json');
|
|
|
|
client.setRequestHeader('Authorization', `Bearer ${accessToken}`);
|
|
|
|
client.send(JSON.stringify(params));
|
2024-09-11 13:59:46 +00:00
|
|
|
} catch {
|
2024-03-27 12:47:09 +00:00
|
|
|
// Do not make the BeforeUnload handler error out
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
interface MarkerParam {
|
|
|
|
last_read_id?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getLastNotificationId(state: RootState): string | undefined {
|
2024-09-16 13:12:10 +00:00
|
|
|
return state.notificationGroups.lastReadId;
|
2024-03-27 12:47:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const buildPostMarkersParams = (state: RootState) => {
|
|
|
|
const params = {} as { home?: MarkerParam; notifications?: MarkerParam };
|
|
|
|
|
|
|
|
const lastNotificationId = getLastNotificationId(state);
|
|
|
|
|
|
|
|
if (
|
|
|
|
lastNotificationId &&
|
|
|
|
compareId(lastNotificationId, state.markers.notifications) > 0
|
|
|
|
) {
|
|
|
|
params.notifications = {
|
|
|
|
last_read_id: lastNotificationId,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return params;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const submitMarkersAction = createAppAsyncThunk<{
|
|
|
|
home: string | undefined;
|
|
|
|
notifications: string | undefined;
|
|
|
|
}>('markers/submitAction', async (_args, { getState }) => {
|
2024-05-22 14:45:18 +00:00
|
|
|
const accessToken = getAccessToken();
|
2024-03-27 12:47:09 +00:00
|
|
|
const params = buildPostMarkersParams(getState());
|
|
|
|
|
2024-05-22 14:45:18 +00:00
|
|
|
if (Object.keys(params).length === 0 || !accessToken || accessToken === '') {
|
2024-03-27 12:47:09 +00:00
|
|
|
return { home: undefined, notifications: undefined };
|
|
|
|
}
|
|
|
|
|
2024-05-22 14:45:18 +00:00
|
|
|
await api().post<MarkerJSON>('/api/v1/markers', params);
|
2024-03-27 12:47:09 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
home: params.home?.last_read_id,
|
|
|
|
notifications: params.notifications?.last_read_id,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
const debouncedSubmitMarkers = debounce(
|
2024-04-26 17:11:27 +00:00
|
|
|
(dispatch: AppDispatch) => {
|
|
|
|
void dispatch(submitMarkersAction());
|
2024-03-27 12:47:09 +00:00
|
|
|
},
|
|
|
|
300000,
|
|
|
|
{
|
|
|
|
leading: true,
|
|
|
|
trailing: true,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
export const submitMarkers = createAppAsyncThunk(
|
|
|
|
'markers/submit',
|
|
|
|
(params: { immediate?: boolean }, { dispatch }) => {
|
|
|
|
debouncedSubmitMarkers(dispatch);
|
|
|
|
|
|
|
|
if (params.immediate) {
|
|
|
|
debouncedSubmitMarkers.flush();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2024-05-22 14:45:18 +00:00
|
|
|
export const fetchMarkers = createAppAsyncThunk('markers/fetch', async () => {
|
|
|
|
const response = await api().get<Record<string, MarkerJSON>>(
|
|
|
|
`/api/v1/markers`,
|
|
|
|
{ params: { timeline: ['notifications'] } },
|
|
|
|
);
|
2024-03-27 12:47:09 +00:00
|
|
|
|
2024-05-22 14:45:18 +00:00
|
|
|
return { markers: response.data };
|
|
|
|
});
|