change moderate and initiatives
This commit is contained in:
@@ -2,63 +2,76 @@ const router = require('express').Router();
|
||||
const { moderationText } = require('./initiatives-ai-agents/moderation.ts');
|
||||
const { generatePicture } = require('./initiatives-ai-agents/picture.ts');
|
||||
const { getSupabaseClient } = require('./supabaseClient');
|
||||
const { getGigaAuth } = require('./get-constants');
|
||||
|
||||
// Обработчик для модерации текста
|
||||
async function getGigaKey() {
|
||||
const GIGA_AUTH = await getGigaAuth();
|
||||
return GIGA_AUTH;
|
||||
}
|
||||
|
||||
// Обработчик для модерации и создания инициативы
|
||||
router.post('/moderate', async (req, res) => {
|
||||
|
||||
const GIGA_AUTH = await getGigaKey();
|
||||
|
||||
try {
|
||||
const { title, body } = req.body;
|
||||
if (!title || !body) {
|
||||
res.status(400).json({ error: 'Заголовок и текст обязательны' });
|
||||
const { title, description, building_id, creator_id, target_amount, status } = req.body;
|
||||
|
||||
if (!title || !description) {
|
||||
res.status(400).json({ error: 'Заголовок и описание обязательны' });
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('Запрос на модерацию:', { title: title.substring(0, 50), body: body.substring(0, 100) });
|
||||
if (!building_id || !creator_id) {
|
||||
res.status(400).json({ error: 'ID дома и создателя обязательны' });
|
||||
return;
|
||||
}
|
||||
|
||||
const [comment, fixedText, isApproved] = await moderationText(title, body);
|
||||
// Валидация статуса, если передан
|
||||
const validStatuses = ['moderation', 'review', 'fundraising', 'approved', 'rejected'];
|
||||
if (status && !validStatuses.includes(status)) {
|
||||
res.status(400).json({ error: `Недопустимый статус. Допустимые значения: ${validStatuses.join(', ')}` });
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('Запрос на модерацию:', { title: title.substring(0, 50), description: description.substring(0, 100) });
|
||||
|
||||
// Модерация текста (передаем title и description как body)
|
||||
const [comment, fixedText, isApproved] = await moderationText(title, description, GIGA_AUTH);
|
||||
|
||||
console.log('Результат модерации получен:', { comment, fixedText: fixedText?.substring(0, 100), isApproved });
|
||||
|
||||
// Дополнительная проверка на стороне сервера
|
||||
if (!isApproved && (!comment || comment.trim() === '')) {
|
||||
console.warn('Обнаружен некорректный результат модерации - пустой комментарий при отклонении');
|
||||
}
|
||||
|
||||
res.json({
|
||||
comment,
|
||||
fixedText,
|
||||
isApproved
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error in moderation:', error);
|
||||
res.status(500).json({ error: 'Внутренняя ошибка сервера', details: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// Обработчик для генерации изображений
|
||||
router.post('/generate-image', async (req, res) => {
|
||||
try {
|
||||
const { prompt, userId } = req.body;
|
||||
if (!prompt) {
|
||||
res.status(400).json({ error: 'Необходимо указать запрос для генерации' });
|
||||
// Если модерация не прошла, возвращаем undefined
|
||||
if (!isApproved) {
|
||||
if (!comment || comment.trim() === '') {
|
||||
console.warn('Обнаружен некорректный результат модерации - пустой комментарий при отклонении');
|
||||
}
|
||||
|
||||
res.json({
|
||||
comment,
|
||||
fixedText,
|
||||
isApproved,
|
||||
initiative: undefined
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
// Генерируем изображение
|
||||
const imageBuffer = await generatePicture(prompt);
|
||||
|
||||
//console.log('Изображение получено, размер буфера:', imageBuffer?.length || 0, 'байт');
|
||||
// Модерация прошла, генерируем изображение используя заголовок как промпт
|
||||
console.log('Модерация прошла, генерируем изображение с промптом:', title);
|
||||
|
||||
const imageBuffer = await generatePicture(title, GIGA_AUTH);
|
||||
|
||||
if (!imageBuffer || imageBuffer.length === 0) {
|
||||
res.status(500).json({ error: 'Получен пустой буфер изображения' });
|
||||
return;
|
||||
}
|
||||
|
||||
//console.log('Начинаем загрузку в Supabase Storage...');
|
||||
|
||||
// Получаем Supabase клиент и создаем имя файла
|
||||
const supabase = getSupabaseClient();
|
||||
const timestamp = Date.now();
|
||||
const filename = `image_${userId || 'user'}_${timestamp}.jpg`;
|
||||
const filename = `image_${creator_id}_${timestamp}.jpg`;
|
||||
|
||||
// Загружаем изображение в Supabase Storage
|
||||
let uploadResult;
|
||||
let retries = 0;
|
||||
const maxRetries = 5;
|
||||
@@ -76,7 +89,6 @@ router.post('/generate-image', async (req, res) => {
|
||||
break; // Успешная загрузка
|
||||
}
|
||||
|
||||
//console.warn(`Попытка загрузки ${retries + 1} неудачна:`, uploadResult.error);
|
||||
retries++;
|
||||
|
||||
if (retries < maxRetries) {
|
||||
@@ -84,7 +96,7 @@ router.post('/generate-image', async (req, res) => {
|
||||
await new Promise(resolve => setTimeout(resolve, 1000 * retries));
|
||||
}
|
||||
} catch (error) {
|
||||
//console.warn(`Попытка загрузки ${retries + 1} неудачна (исключение):`, error.message);
|
||||
console.warn(`Попытка загрузки ${retries + 1} неудачна (исключение):`, error.message);
|
||||
retries++;
|
||||
|
||||
if (retries < maxRetries) {
|
||||
@@ -97,26 +109,54 @@ router.post('/generate-image', async (req, res) => {
|
||||
}
|
||||
|
||||
if (uploadResult?.error) {
|
||||
//console.error('Supabase storage error after all retries:', uploadResult.error);
|
||||
console.error('Supabase storage error after all retries:', uploadResult.error);
|
||||
res.status(500).json({ error: 'Ошибка при сохранении изображения после нескольких попыток' });
|
||||
return;
|
||||
}
|
||||
|
||||
//console.log('Изображение успешно загружено в Supabase Storage:', filename);
|
||||
console.log('Изображение успешно загружено в Supabase Storage:', filename);
|
||||
|
||||
// Получаем публичный URL
|
||||
const { data: urlData } = supabase.storage
|
||||
.from('images')
|
||||
.getPublicUrl(filename);
|
||||
|
||||
|
||||
// Определяем статус: если передан в запросе, используем его, иначе 'review'
|
||||
const finalStatus = status || 'review';
|
||||
|
||||
// Создаем инициативу в базе данных
|
||||
const { data: initiative, error: initiativeError } = await supabase
|
||||
.from('initiatives')
|
||||
.insert([{
|
||||
building_id,
|
||||
creator_id,
|
||||
title: fixedText || title,
|
||||
description,
|
||||
status: finalStatus,
|
||||
target_amount: target_amount || null,
|
||||
current_amount: 0,
|
||||
image_url: urlData.publicUrl
|
||||
}])
|
||||
.select()
|
||||
.single();
|
||||
|
||||
if (initiativeError) {
|
||||
console.error('Ошибка создания инициативы:', initiativeError);
|
||||
res.status(500).json({ error: 'Ошибка при создании инициативы', details: initiativeError.message });
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('Инициатива успешно создана:', initiative.id);
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
imageUrl: urlData.publicUrl,
|
||||
imagePath: filename
|
||||
comment,
|
||||
fixedText,
|
||||
isApproved,
|
||||
initiative
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
//console.error('Error in image generation:', error);
|
||||
console.error('Error in moderation and initiative creation:', error);
|
||||
res.status(500).json({ error: 'Внутренняя ошибка сервера', details: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user