feat: Зеро-персонаж, auto-publish, auto-series, channel-stats, fallback covers
- Персонаж Зеро: 23 позы (zeroCharacter.js), скрипты генерации - Auto-publish статей в TG: multipart upload, кнопки, режим alternating Zero/cover - Fallback цепочка обложек: aiprimetech gpt-5.5 → Pollinations → local SVG (6 палитр) - Auto-series: Claude haiku определяет серию для каждой статьи автоматически - Channel stats: подписчики, история, delta 24h/7d - Photo-search: Yandex API, профили доменов, Redis лимиты - Scheduled posts runner: backfill, preview, queue, cancel - promptBuilder: author_persona Зеро, голос от первого лица - Fixes: dollar-placeholder bugs в PATCH channels/autogen, listArticles фильтры - AI model: gpt-5.5 для image generation
This commit is contained in:
+94
-2
@@ -1,6 +1,8 @@
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
const articlesSvc = require('../services/articles');
|
||||
const autoPublish = require('../services/articleAutoPublish');
|
||||
const autoSeries = require('../services/articleAutoSeries');
|
||||
const { query } = require('../config/db');
|
||||
|
||||
// GET /api/articles — список опубликованных
|
||||
@@ -23,6 +25,15 @@ router.get('/tags', async (_, res) => {
|
||||
} catch (err) { res.status(500).json({ error: err.message }); }
|
||||
});
|
||||
|
||||
|
||||
// GET /api/articles/home — данные для главной страницы (hero, byCategory, popular, recent)
|
||||
router.get('/home', async (req, res) => {
|
||||
try {
|
||||
const data = await articlesSvc.getHomeArticles();
|
||||
res.json(data);
|
||||
} catch (err) { res.status(500).json({ error: err.message }); }
|
||||
});
|
||||
|
||||
// GET /api/articles/admin — все статьи для админки (включая черновики)
|
||||
router.get('/admin', async (req, res) => {
|
||||
try {
|
||||
@@ -38,6 +49,61 @@ router.get('/admin', async (req, res) => {
|
||||
} catch (err) { res.status(500).json({ error: err.message }); }
|
||||
});
|
||||
|
||||
// GET /api/articles/admin/search — typeahead-поиск по статьям.
|
||||
// Параметры: q (подстрока в title), status (default published), category, limit (default 20),
|
||||
// channel_id (если задан — пометит already_in_channel, was_published_in_channel)
|
||||
router.get('/admin/search', async (req, res) => {
|
||||
try {
|
||||
const q = (req.query.q || '').trim();
|
||||
const status = req.query.status || 'published';
|
||||
const category = req.query.category || null;
|
||||
const limit = Math.min(parseInt(req.query.limit) || 20, 50);
|
||||
const channelId = req.query.channel_id ? parseInt(req.query.channel_id) : null;
|
||||
|
||||
const params = [];
|
||||
let where = [];
|
||||
if (status && status !== 'any') { params.push(status); where.push(`status=$${params.length}`); }
|
||||
if (category) { params.push(category); where.push(`category=$${params.length}`); }
|
||||
if (q) { params.push(`%${q.toLowerCase()}%`); where.push(`lower(title) LIKE $${params.length}`); }
|
||||
|
||||
params.push(limit);
|
||||
const sql = `
|
||||
SELECT id, slug, title, excerpt, cover_url, category, status, published_at
|
||||
FROM articles
|
||||
${where.length ? 'WHERE ' + where.join(' AND ') : ''}
|
||||
ORDER BY published_at DESC NULLS LAST, created_at DESC
|
||||
LIMIT $${params.length}`;
|
||||
const { rows: items } = await query(sql, params);
|
||||
|
||||
// Если задан channel_id — для каждого item ищем, был ли уже опубликован в этом канале (через scheduled_posts.status='sent')
|
||||
if (channelId && items.length) {
|
||||
const ids = items.map(a => a.id);
|
||||
const { rows: sent } = await query(
|
||||
`SELECT article_id, MAX(published_at) AS last_sent_at
|
||||
FROM scheduled_posts
|
||||
WHERE channel_id=$1 AND article_id = ANY($2::int[]) AND status='sent'
|
||||
GROUP BY article_id`,
|
||||
[channelId, ids]
|
||||
);
|
||||
const sentMap = Object.fromEntries(sent.map(r => [r.article_id, r.last_sent_at]));
|
||||
const { rows: pending } = await query(
|
||||
`SELECT article_id, MIN(scheduled_at) AS next_scheduled_at
|
||||
FROM scheduled_posts
|
||||
WHERE channel_id=$1 AND article_id = ANY($2::int[]) AND status='pending'
|
||||
GROUP BY article_id`,
|
||||
[channelId, ids]
|
||||
);
|
||||
const pendingMap = Object.fromEntries(pending.map(r => [r.article_id, r.next_scheduled_at]));
|
||||
for (const it of items) {
|
||||
it.was_sent_to_channel = sentMap[it.id] || null;
|
||||
it.next_scheduled_at = pendingMap[it.id] || null;
|
||||
}
|
||||
}
|
||||
|
||||
res.json({ items, count: items.length });
|
||||
} catch (err) { res.status(500).json({ error: err.message }); }
|
||||
});
|
||||
|
||||
// GET /api/articles/id/:id — одна статья по числовому id
|
||||
router.get('/id/:id', async (req, res) => {
|
||||
try {
|
||||
@@ -50,9 +116,18 @@ router.get('/id/:id', async (req, res) => {
|
||||
// POST /api/articles/generate
|
||||
router.post('/generate', async (req, res) => {
|
||||
try {
|
||||
const { topic, keywords = [], tags = [], autoPublish = true, category = 'ai-tools' } = req.body;
|
||||
const { topic, keywords = [], tags = [], autoPublish: autoPub = true, category = 'ai-tools' } = req.body;
|
||||
if (!topic) return res.status(400).json({ error: 'topic is required' });
|
||||
const article = await articlesSvc.generateAndSaveArticle({ topic, keywords, tags, autoPublish, category });
|
||||
const article = await articlesSvc.generateAndSaveArticle({ topic, keywords, tags, autoPublish: autoPub, category });
|
||||
// Hook: автопубликация в каналы
|
||||
if (article && article.status === 'published') {
|
||||
autoPublish.scheduleForArticle(article.id).catch(err => {
|
||||
console.error('[articles] auto-publish hook failed:', err.message);
|
||||
});
|
||||
autoSeries.addToSeries(article.id).catch(err => {
|
||||
console.error('[articles] auto-series hook failed:', err.message);
|
||||
});
|
||||
}
|
||||
res.json(article);
|
||||
} catch (err) {
|
||||
console.error('[Articles] generate', err);
|
||||
@@ -84,11 +159,28 @@ router.patch('/:id', async (req, res) => {
|
||||
if (!fields.length) return res.status(400).json({ error: 'Nothing to update' });
|
||||
fields.push(`updated_at=NOW()`);
|
||||
vals.push(req.params.id);
|
||||
|
||||
// Сначала проверим прежний status — чтобы понимать, был ли переход draft → published
|
||||
const { rows: prevRows } = await query(`SELECT status FROM articles WHERE id=$1`, [req.params.id]);
|
||||
const prevStatus = prevRows[0]?.status;
|
||||
|
||||
const { rows } = await query(
|
||||
`UPDATE articles SET ${fields.join(', ')} WHERE id=$${i} RETURNING *`,
|
||||
vals
|
||||
);
|
||||
if (!rows.length) return res.status(404).json({ error: 'Not found' });
|
||||
|
||||
// Hook: если статья только что стала published
|
||||
const newStatus = rows[0].status;
|
||||
if (newStatus === 'published' && prevStatus !== 'published') {
|
||||
autoPublish.scheduleForArticle(rows[0].id).catch(err => {
|
||||
console.error('[articles] auto-publish hook failed:', err.message);
|
||||
});
|
||||
autoSeries.addToSeries(rows[0].id).catch(err => {
|
||||
console.error('[articles] auto-series hook failed:', err.message);
|
||||
});
|
||||
}
|
||||
|
||||
res.json(rows[0]);
|
||||
} catch (err) { res.status(500).json({ error: err.message }); }
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user