Add scheduled social posts, Discord channels config, and LinkedIn personal posting fix

- Scheduled posts: new Schedule tab with multi-platform post scheduling (Discord,
  Mastodon, LinkedIn); SQLite queue with per-target delivery tracking and retry;
  cron script in bin/process-scheduled-posts.php processes due targets inline
- Discord channels: new config section for webhook-based channels used as scheduled
  post targets; webhook URLs stored in .env as DISCORD_WEBHOOK_<KEY>
- LinkedIn: fix botcompose_post to use member_id (urn:li:person) for personal
  profiles instead of hardcoded org author; filter org pages out of compose UI
  and RSS modal picker since org posting is unsupported; label org pages clearly

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Bashy 2026-05-29 15:14:03 +03:00
parent 09e3406847
commit 30b21b6214
9 changed files with 848 additions and 14 deletions

View file

@ -120,9 +120,19 @@ if ($action === 'linkedin_post') {
if (!$page) { echo json_encode(['error' => "LinkedIn page '$page_name' not in config"]); exit; }
if (!$token) { echo json_encode(['error' => 'LinkedIn not connected — use Connect LinkedIn first']); exit; }
$org_id = $page['organization_id'] ?? '';
$page_type = $page['type'] ?? 'organization';
if ($page_type === 'personal') {
$member_id = $config['linkedin']['member_id'] ?? null;
if (!$member_id) { echo json_encode(['error' => 'Personal posting requires member_id — re-run OAuth flow']); exit; }
$author = str_starts_with($member_id, 'urn:') ? $member_id : "urn:li:person:$member_id";
} else {
$org_id = $page['organization_id'] ?? '';
if (!$org_id) { echo json_encode(['error' => "Organization page '$page_name' has no organization_id"]); exit; }
$author = "urn:li:organization:$org_id";
}
$payload = json_encode([
'author' => "urn:li:organization:$org_id",
'author' => $author,
'lifecycleState' => 'PUBLISHED',
'specificContent' => [
'com.linkedin.ugc.ShareContent' => [
@ -153,5 +163,34 @@ if ($action === 'linkedin_post') {
exit;
}
// ── Discord: post via webhook ─────────────────────────────────────────────────
if ($action === 'discord_post') {
$channel_key = $input['channel_key'] ?? '';
$text = trim($input['text'] ?? '');
if (!$channel_key || !$text) { echo json_encode(['error' => 'channel_key and text required']); exit; }
$config = read_config($config_path);
if (!$config) { echo json_encode(['error' => 'config.json not found']); exit; }
$channel = $config['discord_channels'][$channel_key] ?? null;
if (!$channel) { echo json_encode(['error' => "Discord channel '$channel_key' not in config"]); exit; }
$env = read_dot_env($base . '/.env');
$webhook_url = $env['DISCORD_WEBHOOK_' . strtoupper($channel_key)] ?? '';
if (!$webhook_url) {
echo json_encode(['error' => 'DISCORD_WEBHOOK_' . strtoupper($channel_key) . ' not set in .env']); exit;
}
[$status, $resp] = curl_post($webhook_url, ['Content-Type: application/json'], json_encode(['content' => $text]));
if ($status !== 200 && $status !== 204) {
$err = json_decode($resp, true)['message'] ?? "HTTP $status";
echo json_encode(['error' => "Discord error: $err"]); exit;
}
Audit::log($db, 'botcompose_discord_post', $pid, $channel_key);
echo json_encode(['ok' => true]);
exit;
}
http_response_code(400);
echo json_encode(['error' => 'Unknown action']);

View file

@ -159,6 +159,37 @@ if ($action === 'remove_mastodon') {
exit;
}
// ── ADD / SAVE / REMOVE DISCORD CHANNEL ──────────────────────────────────────
if ($action === 'add_discord_channel' || $action === 'save_discord_channel') {
$name = trim($input['name'] ?? '');
$data = $input['data'] ?? [];
if (!$name) { echo json_encode(['error' => 'Channel key required']); exit; }
if (!preg_match('/^[a-zA-Z0-9_]+$/', $name)) {
echo json_encode(['error' => 'Channel key must be alphanumeric/underscore (used as env var suffix)']); exit;
}
if ($action === 'add_discord_channel' && isset($config['discord_channels'][$name])) {
echo json_encode(['error' => 'Channel already exists']); exit;
}
if (!isset($config['discord_channels'])) $config['discord_channels'] = [];
$config['discord_channels'][$name] = ['label' => trim($data['label'] ?? $name)];
write_config($config_path, $config);
Audit::log($db, 'botconfig_discord_channel_' . ($action === 'add_discord_channel' ? 'add' : 'save'), $pid, $name);
echo json_encode(['ok' => true]);
exit;
}
if ($action === 'remove_discord_channel') {
$name = trim($input['name'] ?? '');
if (!$name || !isset($config['discord_channels'][$name])) {
echo json_encode(['error' => 'Channel not found']); exit;
}
unset($config['discord_channels'][$name]);
write_config($config_path, $config);
Audit::log($db, 'botconfig_discord_channel_remove', $pid, $name);
echo json_encode(['ok' => true]);
exit;
}
// ── ADD / SAVE / REMOVE LINKEDIN PAGE ────────────────────────────────────────
if ($action === 'add_linkedin_page' || $action === 'save_linkedin_page') {
$name = trim($input['name'] ?? '');

131
web/api/post_schedule.php Normal file
View file

@ -0,0 +1,131 @@
<?php
header('Content-Type: application/json');
$method = $_SERVER['REQUEST_METHOD'];
$input = $method === 'POST' ? (json_decode(file_get_contents('php://input'), true) ?? []) : [];
$pid = (int)(($method === 'GET' ? $_GET : $input)['project_id'] ?? 0);
$stmt = $db->prepare('SELECT * FROM projects WHERE id = ? AND is_active = 1');
$stmt->execute([$pid]);
$project = $stmt->fetch();
if (!$project) { http_response_code(404); echo json_encode(['error' => 'Project not found']); exit; }
$base = realpath($project['path']);
$config_path = $base . '/data/config.json';
function ps_read_config(string $path): array {
if (!file_exists($path)) return [];
$data = json_decode(file_get_contents($path), true);
return is_array($data) ? $data : [];
}
// ── GET ───────────────────────────────────────────────────────────────────────
if ($method === 'GET') {
$action = $_GET['action'] ?? 'list';
// Return available targets for the schedule form
if ($action === 'targets') {
$config = ps_read_config($config_path);
$discord = [];
$mastodon = [];
$linkedin = [];
foreach ($config['discord_channels'] ?? [] as $key => $ch) {
$discord[] = ['key' => $key, 'label' => $ch['label'] ?? $key];
}
foreach ($config['mastodon'] ?? [] as $key => $acc) {
$mastodon[] = ['key' => $key, 'label' => $acc['label'] ?? $key];
}
foreach ($config['linkedin']['pages'] ?? [] as $key => $page) {
// Only personal profiles can post — org pages not supported by LinkedIn API
if (($page['type'] ?? 'organization') === 'personal') {
$linkedin[] = ['key' => $key, 'label' => $page['label'] ?? $key];
}
}
echo json_encode(['discord' => $discord, 'mastodon' => $mastodon, 'linkedin' => $linkedin]);
exit;
}
// List posts with their targets
$rows = $db->prepare(
'SELECT * FROM scheduled_posts WHERE project_id = ? ORDER BY scheduled_at DESC LIMIT 200'
);
$rows->execute([$pid]);
$posts = $rows->fetchAll();
foreach ($posts as &$post) {
$tgt = $db->prepare('SELECT * FROM scheduled_post_targets WHERE post_id = ? ORDER BY id');
$tgt->execute([$post['id']]);
$post['targets'] = $tgt->fetchAll();
}
echo json_encode(['posts' => $posts]);
exit;
}
if ($method !== 'POST') { http_response_code(405); echo json_encode(['error' => 'Method not allowed']); exit; }
$action = $input['action'] ?? '';
// ── CREATE ────────────────────────────────────────────────────────────────────
if ($action === 'create') {
$content = trim($input['content'] ?? '');
$url = trim($input['url'] ?? '');
$scheduled_at = trim($input['scheduled_at'] ?? '');
$targets = $input['targets'] ?? [];
if (!$content) { echo json_encode(['error' => 'Content required']); exit; }
if (!$scheduled_at) { echo json_encode(['error' => 'Scheduled time required']); exit; }
if (!$targets) { echo json_encode(['error' => 'At least one target required']); exit; }
$db->prepare(
'INSERT INTO scheduled_posts (project_id, content, url, scheduled_at) VALUES (?, ?, ?, ?)'
)->execute([$pid, $content, $url ?: null, $scheduled_at]);
$post_id = (int)$db->lastInsertId();
$stmt = $db->prepare('INSERT INTO scheduled_post_targets (post_id, platform, target) VALUES (?, ?, ?)');
foreach ($targets as $t) {
$platform = $t['platform'] ?? '';
$target = $t['target'] ?? '';
if (!in_array($platform, ['discord', 'mastodon', 'linkedin'], true) || !$target) continue;
$stmt->execute([$post_id, $platform, $target]);
}
Audit::log($db, 'scheduled_post_create', $pid, "post_id=$post_id");
echo json_encode(['ok' => true, 'id' => $post_id]);
exit;
}
// ── DELETE ────────────────────────────────────────────────────────────────────
if ($action === 'delete') {
$id = (int)($input['id'] ?? 0);
if (!$id) { echo json_encode(['error' => 'id required']); exit; }
$db->prepare('DELETE FROM scheduled_posts WHERE id = ? AND project_id = ?')->execute([$id, $pid]);
Audit::log($db, 'scheduled_post_delete', $pid, "post_id=$id");
echo json_encode(['ok' => true]);
exit;
}
// ── RETRY FAILED TARGET ───────────────────────────────────────────────────────
if ($action === 'retry_target') {
$target_id = (int)($input['target_id'] ?? 0);
if (!$target_id) { echo json_encode(['error' => 'target_id required']); exit; }
// Verify target belongs to this project
$row = $db->prepare(
'SELECT t.post_id FROM scheduled_post_targets t
JOIN scheduled_posts p ON p.id = t.post_id
WHERE t.id = ? AND p.project_id = ?'
);
$row->execute([$target_id, $pid]);
$r = $row->fetch();
if (!$r) { echo json_encode(['error' => 'Target not found']); exit; }
$db->prepare("UPDATE scheduled_post_targets SET status = 'pending', error = NULL WHERE id = ?")
->execute([$target_id]);
$db->prepare("UPDATE scheduled_posts SET status = 'pending' WHERE id = ? AND status IN ('done', 'partial')")
->execute([$r['post_id']]);
echo json_encode(['ok' => true]);
exit;
}
http_response_code(400);
echo json_encode(['error' => 'Unknown action']);