2025-04-25 12:27:16 +05:30

47 lines
1.1 KiB
TypeScript

import { Handler } from '@netlify/functions';
import { firestore } from '../../../lib/firebase';
import { verifySignature } from '../../../lib/activitypub/utils/verifySignature.js';
import { handleFollow } from '../../../lib/handleFollow';
const handler: Handler = async (event, context) => {
if (event.httpMethod !== 'POST') {
return {
statusCode: 405,
body: 'Method Not Allowed',
};
}
try {
const inboxData = JSON.parse(event.body || '{}');
const username = event.path.split('/')[2]; // /users/:username/inbox
const isVerified = await verifySignature(event.headers, event.body);
if (!isVerified) {
return {
statusCode: 401,
body: 'Unauthorized',
};
}
if (inboxData.type === 'Follow') {
await handleFollow(inboxData, username);
return {
statusCode: 202,
body: 'Follow request accepted',
};
}
return {
statusCode: 200,
body: 'Activity received',
};
} catch (error) {
return {
statusCode: 500,
body: JSON.stringify({ error: error.message }),
};
}
};
export { handler };