feat: initial zeropost-engine structure
- AI service with Anthropic claude-sonnet-4-6 - Bull queue for async generation jobs - Routes: /api/generate, /api/channels, /api/posts - PostgreSQL schema: users, channels, posts, generation_jobs - Supports: post, article, topics generation types
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
const { query } = require('../config/db');
|
||||
|
||||
// GET /api/channels - list channels for user
|
||||
router.get('/', async (req, res) => {
|
||||
const userId = req.headers['x-user-id'];
|
||||
if (!userId) return res.status(401).json({ error: 'Unauthorized' });
|
||||
const { rows } = await query(`SELECT * FROM channels WHERE user_id=$1 ORDER BY created_at DESC`, [userId]);
|
||||
res.json(rows);
|
||||
});
|
||||
|
||||
// POST /api/channels - create channel
|
||||
router.post('/', async (req, res) => {
|
||||
const userId = req.headers['x-user-id'];
|
||||
if (!userId) return res.status(401).json({ error: 'Unauthorized' });
|
||||
const { name, tgChannelId, botToken, topic, tone, language } = req.body;
|
||||
if (!name) return res.status(400).json({ error: 'name is required' });
|
||||
const { rows } = await query(
|
||||
`INSERT INTO channels (user_id, name, tg_channel_id, bot_token, topic, tone, language) VALUES ($1,$2,$3,$4,$5,$6,$7) RETURNING *`,
|
||||
[userId, name, tgChannelId || null, botToken || null, topic || '', tone || 'neutral', language || 'ru']
|
||||
);
|
||||
res.json(rows[0]);
|
||||
});
|
||||
|
||||
// PATCH /api/channels/:id - update channel
|
||||
router.patch('/:id', async (req, res) => {
|
||||
const userId = req.headers['x-user-id'];
|
||||
const { name, topic, tone, language, botToken, tgChannelId, isActive, postSchedule } = req.body;
|
||||
const { rows } = await query(
|
||||
`UPDATE channels SET
|
||||
name=COALESCE($1,name), topic=COALESCE($2,topic), tone=COALESCE($3,tone),
|
||||
language=COALESCE($4,language), bot_token=COALESCE($5,bot_token),
|
||||
tg_channel_id=COALESCE($6,tg_channel_id), is_active=COALESCE($7,is_active),
|
||||
post_schedule=COALESCE($8,post_schedule)
|
||||
WHERE id=$9 AND user_id=$10 RETURNING *`,
|
||||
[name, topic, tone, language, botToken, tgChannelId, isActive, postSchedule ? JSON.stringify(postSchedule) : null, req.params.id, userId]
|
||||
);
|
||||
if (!rows.length) return res.status(404).json({ error: 'Channel not found' });
|
||||
res.json(rows[0]);
|
||||
});
|
||||
|
||||
// DELETE /api/channels/:id
|
||||
router.delete('/:id', async (req, res) => {
|
||||
const userId = req.headers['x-user-id'];
|
||||
await query(`DELETE FROM channels WHERE id=$1 AND user_id=$2`, [req.params.id, userId]);
|
||||
res.json({ ok: true });
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
@@ -0,0 +1,40 @@
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
const { query } = require('../config/db');
|
||||
const generationQueue = require('../workers/generation');
|
||||
|
||||
// POST /api/generate - create generation job
|
||||
router.post('/', async (req, res) => {
|
||||
try {
|
||||
const { type, topic, tone = 'neutral', language = 'ru', channelContext = '', keywords = [], channelId } = req.body;
|
||||
|
||||
if (!type || !topic) return res.status(400).json({ error: 'type and topic are required' });
|
||||
if (!['post', 'article', 'topics'].includes(type)) return res.status(400).json({ error: 'Invalid type' });
|
||||
|
||||
const { rows } = await query(
|
||||
`INSERT INTO generation_jobs (type, channel_id, topic, status) VALUES ($1,$2,$3,'pending') RETURNING id`,
|
||||
[type, channelId || null, topic]
|
||||
);
|
||||
const jobId = rows[0].id;
|
||||
|
||||
await generationQueue.add({ jobId, type, topic, tone, language, channelContext, keywords });
|
||||
|
||||
res.json({ jobId, status: 'pending' });
|
||||
} catch (err) {
|
||||
console.error('[Route] POST /generate', err);
|
||||
res.status(500).json({ error: err.message });
|
||||
}
|
||||
});
|
||||
|
||||
// GET /api/generate/:id - get job status
|
||||
router.get('/:id', async (req, res) => {
|
||||
try {
|
||||
const { rows } = await query(`SELECT * FROM generation_jobs WHERE id=$1`, [req.params.id]);
|
||||
if (!rows.length) return res.status(404).json({ error: 'Job not found' });
|
||||
res.json(rows[0]);
|
||||
} catch (err) {
|
||||
res.status(500).json({ error: err.message });
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
@@ -0,0 +1,51 @@
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
const { query } = require('../config/db');
|
||||
const axios = require('axios');
|
||||
|
||||
// POST /api/posts/publish - publish a post to Telegram immediately
|
||||
router.post('/publish', async (req, res) => {
|
||||
try {
|
||||
const { channelId, content, userId } = req.body;
|
||||
if (!channelId || !content) return res.status(400).json({ error: 'channelId and content required' });
|
||||
|
||||
const { rows } = await query(
|
||||
`SELECT * FROM channels WHERE id=$1 AND user_id=$2`,
|
||||
[channelId, userId]
|
||||
);
|
||||
if (!rows.length) return res.status(404).json({ error: 'Channel not found' });
|
||||
|
||||
const ch = rows[0];
|
||||
if (!ch.bot_token || !ch.tg_channel_id) return res.status(400).json({ error: 'Channel has no bot_token or tg_channel_id' });
|
||||
|
||||
const tgRes = await axios.post(`https://api.telegram.org/bot${ch.bot_token}/sendMessage`, {
|
||||
chat_id: ch.tg_channel_id,
|
||||
text: content,
|
||||
parse_mode: 'HTML',
|
||||
});
|
||||
|
||||
const msgId = tgRes.data?.result?.message_id;
|
||||
|
||||
const { rows: postRows } = await query(
|
||||
`INSERT INTO posts (channel_id, content, status, published_at, tg_message_id) VALUES ($1,$2,'published',NOW(),$3) RETURNING *`,
|
||||
[channelId, content, msgId]
|
||||
);
|
||||
|
||||
res.json(postRows[0]);
|
||||
} catch (err) {
|
||||
console.error('[Route] POST /posts/publish', err.message);
|
||||
res.status(500).json({ error: err.message });
|
||||
}
|
||||
});
|
||||
|
||||
// GET /api/posts?channelId=X - list posts
|
||||
router.get('/', async (req, res) => {
|
||||
const { channelId } = req.query;
|
||||
const { rows } = await query(
|
||||
`SELECT p.*, g.type as job_type FROM posts p LEFT JOIN generation_jobs g ON p.job_id=g.id WHERE p.channel_id=$1 ORDER BY p.created_at DESC LIMIT 50`,
|
||||
[channelId]
|
||||
);
|
||||
res.json(rows);
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
Reference in New Issue
Block a user