Fix like/dislike errors and implement native chat system

This commit is contained in:
2025-09-13 07:51:02 +09:00
parent 8893b4ad22
commit 321547bf27
14 changed files with 1236 additions and 39 deletions

View File

@@ -227,15 +227,65 @@ export class NotificationService {
// Проверить, активен ли пользователь в чате
private async isUserActiveInChat(userId: string, chatWithUserId: string): Promise<boolean> {
// TODO: Реализовать проверку активности пользователя
// Можно использовать Redis для хранения состояния активности
return false;
try {
// Проверяем последнее сообщение пользователя в чате
const result = await query(`
SELECT m.created_at
FROM messages m
JOIN matches mt ON m.match_id = mt.id
WHERE (mt.user1_id = $1 OR mt.user2_id = $1)
AND (mt.user1_id = $2 OR mt.user2_id = $2)
AND m.sender_id = $1
ORDER BY m.created_at DESC
LIMIT 1
`, [userId, chatWithUserId]);
if (result.rows.length === 0) {
return false; // Нет сообщений - не активен
}
const lastMessageTime = new Date(result.rows[0].created_at);
const now = new Date();
const hoursSinceLastMessage = (now.getTime() - lastMessageTime.getTime()) / (1000 * 60 * 60);
// Считаем активным если последнее сообщение было менее 24 часов назад
return hoursSinceLastMessage < 24;
} catch (error) {
console.error('Error checking user activity:', error);
return false;
}
}
// Отправить пуш-уведомление (для будущего использования)
async sendPushNotification(userId: string, title: string, body: string, data?: any): Promise<void> {
// TODO: Интеграция с Firebase Cloud Messaging или другим сервисом пуш-уведомлений
console.log(`Push notification for ${userId}: ${title} - ${body}`);
try {
// Логируем уведомление
console.log(`📱 Push notification prepared for user ${userId}:`);
console.log(`📋 Title: ${title}`);
console.log(`💬 Body: ${body}`);
if (data) {
console.log(`📊 Data:`, JSON.stringify(data, null, 2));
}
// В будущем здесь будет интеграция с Firebase Cloud Messaging
// или другим сервисом пуш-уведомлений:
/*
const message = {
notification: {
title,
body
},
data: data ? JSON.stringify(data) : undefined,
token: await this.getUserPushToken(userId)
};
await admin.messaging().send(message);
console.log(`✅ Push notification sent to user ${userId}`);
*/
} catch (error) {
console.error(`❌ Error preparing push notification for user ${userId}:`, error);
}
}
// Получить настройки уведомлений пользователя
@@ -300,7 +350,7 @@ export class NotificationService {
// Получаем запланированные уведомления
const result = await query(`
SELECT * FROM scheduled_notifications
WHERE scheduled_at <= $1 AND sent = false
WHERE scheduled_at <= $1 AND processed = false
ORDER BY scheduled_at ASC
LIMIT 100
`, [new Date()]);
@@ -318,10 +368,10 @@ export class NotificationService {
// Добавить другие типы уведомлений
}
// Отмечаем как отправленное
// Отмечаем как обработанное
await query(
'UPDATE scheduled_notifications SET sent = true, sent_at = $1 WHERE id = $2',
[new Date(), notification.id]
'UPDATE scheduled_notifications SET processed = true WHERE id = $1',
[notification.id]
);
} catch (error) {
console.error(`Error processing notification ${notification.id}:`, error);