mirror of
https://github.com/lone-cloud/prism
synced 2026-06-04 12:13:28 -07:00
63 lines
1.7 KiB
TypeScript
63 lines
1.7 KiB
TypeScript
import { sendGroupMessage } from '@/modules/signal';
|
|
import { getGroupId, getOrCreateGroup, remove } from '@/modules/store';
|
|
import { formatAsSignalMessage, parseUnifiedPushRequest } from '@/modules/unifiedpush';
|
|
|
|
const handleMatrixNotify = async (req: Request) => {
|
|
const message = await parseUnifiedPushRequest(req);
|
|
const groupId = getGroupId(message.endpoint);
|
|
|
|
if (!groupId) {
|
|
return new Response('Endpoint not registered', { status: 404 });
|
|
}
|
|
|
|
const signalMessage = formatAsSignalMessage(message);
|
|
await sendGroupMessage(groupId, signalMessage);
|
|
|
|
return Response.json({ success: true });
|
|
};
|
|
|
|
const handleRegister = async (req: Request) => {
|
|
const url = new URL(req.url);
|
|
const endpointId = url.pathname.split('/')[2] ?? '';
|
|
const { appName } = (await req.json()) as {
|
|
appName: string;
|
|
token?: string;
|
|
};
|
|
|
|
await getOrCreateGroup(endpointId, appName);
|
|
|
|
const proto = req.headers.get('x-forwarded-proto') || 'http';
|
|
const host = req.headers.get('host') || 'localhost:8080';
|
|
const baseUrl = `${proto}://${host}`;
|
|
const endpoint = `${baseUrl}/_matrix/push/v1/notify/${endpointId}`;
|
|
|
|
return Response.json({ endpoint, gateway: 'matrix' });
|
|
};
|
|
|
|
const handleUnregister = async (req: Request) => {
|
|
const url = new URL(req.url);
|
|
const endpointId = url.pathname.split('/')[2] ?? '';
|
|
remove(endpointId);
|
|
return new Response(null, { status: 204 });
|
|
};
|
|
|
|
const handleDiscovery = () =>
|
|
Response.json({
|
|
unifiedpush: { version: 1 },
|
|
gateway: 'matrix',
|
|
});
|
|
|
|
export const unifiedPushRoutes = {
|
|
'/up': {
|
|
GET: handleDiscovery,
|
|
},
|
|
|
|
'/_matrix/push/v1/notify': {
|
|
POST: handleMatrixNotify,
|
|
},
|
|
|
|
'/up/:instance': {
|
|
POST: handleRegister,
|
|
DELETE: handleUnregister,
|
|
},
|
|
};
|