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-27 01:04:06 +05:00
|
|
|
import { initPbServer } from '@lib/pbServer';
|
2026-04-26 23:47:23 +05:00
|
|
|
|
|
|
|
|
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-27 20:20:25 +05:00
|
|
|
console.log('[Increment Views] PB_URL:', import.meta.env.POCKETBASE_URL);
|
|
|
|
|
console.log('[Increment Views] PB_ADMIN_EMAIL:', import.meta.env.PB_ADMIN_EMAIL);
|
|
|
|
|
console.log('[Increment Views] PB_ADMIN_PASSWORD:', import.meta.env.PB_ADMIN_PASSWORD ? '***SET***' : '***NOT SET***');
|
|
|
|
|
|
2026-04-26 23:47:23 +05:00
|
|
|
try {
|
2026-04-27 20:20:25 +05:00
|
|
|
console.log('[Increment Views] Calling initPbServer...');
|
2026-04-27 01:04:06 +05:00
|
|
|
const pb = await initPbServer();
|
2026-04-27 20:20:25 +05:00
|
|
|
console.log('[Increment Views] initPbServer done, auth valid:', pb.authStore.isValid);
|
2026-04-27 01:04:06 +05:00
|
|
|
|
2026-04-26 23:47:23 +05:00
|
|
|
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' } }
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-27 01:04:06 +05:00
|
|
|
let post;
|
|
|
|
|
try {
|
|
|
|
|
post = await pb.collection('posts').getOne(postId);
|
|
|
|
|
} catch {
|
2026-04-26 23:47:23 +05:00
|
|
|
return new Response(
|
|
|
|
|
JSON.stringify({ error: 'Пост не найден' }),
|
|
|
|
|
{ status: 404, headers: { 'Content-Type': 'application/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();
|
|
|
|
|
|
2026-04-27 01:04:06 +05:00
|
|
|
let existingViews;
|
|
|
|
|
try {
|
|
|
|
|
existingViews = await pb.collection('post_views').getList(1, 1, {
|
2026-04-27 00:18:09 +05:00
|
|
|
filter: `post="${postId}" && visitor_hash="${visitorHash}" && created >= "${yesterdayStr}"`,
|
2026-04-27 01:04:06 +05:00
|
|
|
});
|
|
|
|
|
} catch {
|
|
|
|
|
existingViews = { totalItems: 0 };
|
|
|
|
|
}
|
2026-04-26 23:47:23 +05:00
|
|
|
|
2026-04-27 00:18:09 +05:00
|
|
|
let isNewView = false;
|
|
|
|
|
|
2026-04-27 01:04:06 +05:00
|
|
|
if (existingViews.totalItems === 0) {
|
|
|
|
|
isNewView = true;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
await pb.collection('post_views').create({
|
|
|
|
|
post: postId,
|
|
|
|
|
visitor_hash: visitorHash,
|
|
|
|
|
ip: ip,
|
|
|
|
|
user_agent: userAgent,
|
|
|
|
|
});
|
|
|
|
|
} catch {
|
|
|
|
|
// ignore
|
2026-04-27 00:18:09 +05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let totalViews = post.views || 0;
|
|
|
|
|
|
|
|
|
|
if (isNewView) {
|
|
|
|
|
totalViews += 1;
|
|
|
|
|
|
2026-04-27 01:04:06 +05:00
|
|
|
try {
|
|
|
|
|
await pb.collection('posts').update(postId, { views: totalViews });
|
|
|
|
|
} catch {
|
|
|
|
|
// ignore
|
|
|
|
|
}
|
2026-04-27 00:18:09 +05:00
|
|
|
}
|
|
|
|
|
|
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) {
|
2026-04-27 01:04:06 +05:00
|
|
|
console.error('[Increment Views] Error:', error);
|
2026-04-26 23:47:23 +05:00
|
|
|
return new Response(
|
|
|
|
|
JSON.stringify({ error: 'Внутренняя ошибка сервера' }),
|
|
|
|
|
{ status: 500, headers: { 'Content-Type': 'application/json' } }
|
|
|
|
|
);
|
|
|
|
|
}
|
2026-04-27 01:04:06 +05:00
|
|
|
};
|