2024-03-27 13:47:09 +01:00
|
|
|
import { debounce } from 'lodash';
|
|
|
|
|
|
|
|
import type { MarkerJSON } from 'flavours/glitch/api_types/markers';
|
2024-05-22 16:45:18 +02:00
|
|
|
import { getAccessToken } from 'flavours/glitch/initial_state';
|
2024-04-26 19:11:27 +02:00
|
|
|
import type { AppDispatch, RootState } from 'flavours/glitch/store';
|
2024-03-27 13:47:09 +01:00
|
|
|
import { createAppAsyncThunk } from 'flavours/glitch/store/typed_functions';
|
|
|
|
|
2024-05-22 16:45:18 +02:00
|
|
|
import api from '../api';
|
2024-03-27 13:47:09 +01:00
|
|
|
import { compareId } from '../compare_id';
|
|
|
|
|
|
|
|
export const synchronouslySubmitMarkers = createAppAsyncThunk(
|
|
|
|
'markers/submit',
|
|
|
|
async (_args, { getState }) => {
|
2024-05-22 16:45:18 +02:00
|
|
|
const accessToken = getAccessToken();
|
2024-03-27 13:47:09 +01:00
|
|
|
const params = buildPostMarkersParams(getState());
|
|
|
|
|
2024-05-22 16:45:18 +02:00
|
|
|
if (
|
|
|
|
Object.keys(params).length === 0 ||
|
|
|
|
!accessToken ||
|
|
|
|
accessToken === ''
|
|
|
|
) {
|
2024-03-27 13:47:09 +01: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;
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
|
|
} else if ('navigator' && 'sendBeacon' in navigator) {
|
|
|
|
// 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 15:59:46 +02:00
|
|
|
} catch {
|
2024-03-27 13:47:09 +01:00
|
|
|
// Do not make the BeforeUnload handler error out
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
interface MarkerParam {
|
|
|
|
last_read_id?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getLastNotificationId(state: RootState): string | undefined {
|
2024-09-16 15:12:10 +02:00
|
|
|
return state.notificationGroups.lastReadId;
|
2024-03-27 13:47:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const buildPostMarkersParams = (state: RootState) => {
|
|
|
|
const params = {} as { home?: MarkerParam; notifications?: MarkerParam };
|
|
|
|
|
|
|
|
const lastNotificationId = getLastNotificationId(state);
|
|
|
|
|
|
|
|
if (
|
|
|
|
lastNotificationId &&
|
|
|
|
lastNotificationId !== '0' &&
|
|
|
|
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 16:45:18 +02:00
|
|
|
const accessToken = getAccessToken();
|
2024-03-27 13:47:09 +01:00
|
|
|
const params = buildPostMarkersParams(getState());
|
|
|
|
|
2024-05-22 16:45:18 +02:00
|
|
|
if (Object.keys(params).length === 0 || !accessToken || accessToken === '') {
|
2024-03-27 13:47:09 +01:00
|
|
|
return { home: undefined, notifications: undefined };
|
|
|
|
}
|
|
|
|
|
2024-05-22 16:45:18 +02:00
|
|
|
await api().post<MarkerJSON>('/api/v1/markers', params);
|
2024-03-27 13:47:09 +01:00
|
|
|
|
|
|
|
return {
|
|
|
|
home: params.home?.last_read_id,
|
|
|
|
notifications: params.notifications?.last_read_id,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
const debouncedSubmitMarkers = debounce(
|
2024-04-26 19:11:27 +02:00
|
|
|
(dispatch: AppDispatch) => {
|
|
|
|
void dispatch(submitMarkersAction());
|
2024-03-27 13:47:09 +01: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 16:45:18 +02: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 13:47:09 +01:00
|
|
|
|
2024-05-22 16:45:18 +02:00
|
|
|
return { markers: response.data };
|
|
|
|
});
|