2026-04-26 23:47:23 +05:00
|
|
|
import type { APIRoute } from 'astro';
|
2026-04-27 00:18:09 +05:00
|
|
|
import crypto from 'crypto';
|
2026-04-26 23:47:23 +05:00
|
|
|
|
|
|
|
|
const POCKETBASE_URL = import.meta.env.POCKETBASE_URL || 'http://127.0.0.1:8090';
|
|
|
|
|
|
|
|
|
|
const POCKETBASE_ID_REGEX = /^[a-z0-9]{15}$/;
|
|
|
|
|
|
2026-04-27 00:18:09 +05:00
|
|
|
function getClientIp(request: Request): string {
|
|
|
|
|
const forwarded = request.headers.get('x-forwarded-for');
|
|
|
|
|
if (forwarded) {
|
|
|
|
|
return forwarded.split(',')[0].trim();
|
|
|
|
|
}
|
|
|
|
|
return request.headers.get('x-real-ip') || 'unknown';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function generateVisitorHash(ip: string, userAgent: string): string {
|
|
|
|
|
return crypto.createHash('sha256').update(ip + userAgent).digest('hex').slice(0, 32);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const POST: APIRoute = async ({ request, url }) => {
|
2026-04-26 23:47:23 +05:00
|
|
|
try {
|
|
|
|
|
const postId = url.searchParams.get('postId');
|
|
|
|
|
|
|
|
|
|
if (!postId || !POCKETBASE_ID_REGEX.test(postId)) {
|
|
|
|
|
return new Response(
|
|
|
|
|
JSON.stringify({ error: 'Некорректный postId' }),
|
|
|
|
|
{ status: 400, headers: { 'Content-Type': 'application/json' } }
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const postRes = await fetch(
|
|
|
|
|
`${POCKETBASE_URL}/api/collections/posts/records/${postId}`,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (!postRes.ok) {
|
|
|
|
|
return new Response(
|
|
|
|
|
JSON.stringify({ error: 'Пост не найден' }),
|
|
|
|
|
{ status: 404, headers: { 'Content-Type': 'application/json' } }
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const post = await postRes.json();
|
|
|
|
|
|
2026-04-27 00:18:09 +05:00
|
|
|
const ip = getClientIp(request);
|
|
|
|
|
const userAgent = request.headers.get('user-agent') || 'unknown';
|
|
|
|
|
const visitorHash = generateVisitorHash(ip, userAgent);
|
|
|
|
|
|
|
|
|
|
const now = new Date();
|
|
|
|
|
const yesterday = new Date(now.getTime() - 24 * 60 * 60 * 1000);
|
|
|
|
|
const yesterdayStr = yesterday.toISOString();
|
|
|
|
|
|
|
|
|
|
const existingViewRes = await fetch(
|
|
|
|
|
`${POCKETBASE_URL}/api/collections/post_views/records?` +
|
|
|
|
|
new URLSearchParams({
|
|
|
|
|
filter: `post="${postId}" && visitor_hash="${visitorHash}" && created >= "${yesterdayStr}"`,
|
|
|
|
|
})
|
2026-04-26 23:47:23 +05:00
|
|
|
);
|
|
|
|
|
|
2026-04-27 00:18:09 +05:00
|
|
|
let isNewView = false;
|
|
|
|
|
|
|
|
|
|
if (existingViewRes.ok) {
|
|
|
|
|
const existingData = await existingViewRes.json();
|
|
|
|
|
if (existingData.items?.length === 0) {
|
|
|
|
|
isNewView = true;
|
|
|
|
|
|
|
|
|
|
await fetch(
|
|
|
|
|
`${POCKETBASE_URL}/api/collections/post_views/records`,
|
|
|
|
|
{
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: { 'Content-Type': 'application/json' },
|
|
|
|
|
body: JSON.stringify({
|
|
|
|
|
post: postId,
|
|
|
|
|
visitor_hash: visitorHash,
|
|
|
|
|
ip: ip,
|
|
|
|
|
user_agent: userAgent,
|
|
|
|
|
}),
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let totalViews = post.views || 0;
|
|
|
|
|
|
|
|
|
|
if (isNewView) {
|
|
|
|
|
totalViews += 1;
|
|
|
|
|
|
|
|
|
|
await fetch(
|
|
|
|
|
`${POCKETBASE_URL}/api/collections/posts/records/${postId}`,
|
|
|
|
|
{
|
|
|
|
|
method: 'PATCH',
|
|
|
|
|
headers: { 'Content-Type': 'application/json' },
|
|
|
|
|
body: JSON.stringify({ views: totalViews }),
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-26 23:47:23 +05:00
|
|
|
return new Response(
|
2026-04-27 00:18:09 +05:00
|
|
|
JSON.stringify({ views: totalViews, isNewView }),
|
2026-04-26 23:47:23 +05:00
|
|
|
{ status: 200, headers: { 'Content-Type': 'application/json' } }
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('[Increment Views API] Error:', error);
|
|
|
|
|
return new Response(
|
|
|
|
|
JSON.stringify({ error: 'Внутренняя ошибка сервера' }),
|
|
|
|
|
{ status: 500, headers: { 'Content-Type': 'application/json' } }
|
|
|
|
|
);
|
|
|
|
|
}
|
2026-04-27 00:18:09 +05:00
|
|
|
};
|