feat(bots): Consolidate bot posting into autonomous module and improve caching
- Remove scheduled posting logic and consolidate all bot posting into autonomous module - Simplify cron endpoint to only call processAllAutonomousBots with cleaner response format - Add background post caching for swarm follow operations via cacheSwarmUserPosts - Update background scheduler to remove scheduled posting references and streamline logging - Improve autonomous module documentation to clarify schedule-based posting with optional sources - Enhance error tracking and reporting across bot task execution - Reduce code duplication by eliminating separate scheduled/autonomous processing paths
This commit is contained in:
@@ -1,11 +1,10 @@
|
||||
/**
|
||||
* Cron endpoint for bot scheduled posting
|
||||
* Cron endpoint for bot autonomous posting
|
||||
*
|
||||
* Call this endpoint periodically (e.g., every minute) via cron job or PM2
|
||||
*/
|
||||
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { processScheduledPosts } from '@/lib/bots/scheduler';
|
||||
import { processAllAutonomousBots } from '@/lib/bots/autonomous';
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
@@ -18,23 +17,18 @@ export async function POST(request: NextRequest) {
|
||||
}
|
||||
|
||||
try {
|
||||
// Process scheduled posts
|
||||
const scheduledResult = await processScheduledPosts();
|
||||
|
||||
// Process autonomous bots
|
||||
const autonomousResult = await processAllAutonomousBots();
|
||||
const results = await processAllAutonomousBots();
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
scheduled: {
|
||||
processed: scheduledResult.processed,
|
||||
skipped: scheduledResult.skipped,
|
||||
errors: scheduledResult.errors.length,
|
||||
},
|
||||
autonomous: {
|
||||
total: autonomousResult.length,
|
||||
posted: autonomousResult.filter(r => r.result.posted).length,
|
||||
},
|
||||
total: results.length,
|
||||
posted: results.filter(r => r.result.posted).length,
|
||||
errors: results.filter(r => r.error).length,
|
||||
details: results.map(r => ({
|
||||
handle: r.botHandle,
|
||||
posted: r.result.posted,
|
||||
reason: r.result.reason || r.error,
|
||||
})),
|
||||
timestamp: new Date().toISOString(),
|
||||
});
|
||||
} catch (error) {
|
||||
|
||||
@@ -7,7 +7,7 @@ import { resolveRemoteUser } from '@/lib/activitypub/fetch';
|
||||
import { createFollowActivity, createUndoActivity } from '@/lib/activitypub/activities';
|
||||
import { deliverActivity } from '@/lib/activitypub/outbox';
|
||||
import { cacheRemoteUserPosts } from '@/lib/activitypub/cache';
|
||||
import { isSwarmNode, deliverSwarmFollow, deliverSwarmUnfollow } from '@/lib/swarm/interactions';
|
||||
import { isSwarmNode, deliverSwarmFollow, deliverSwarmUnfollow, cacheSwarmUserPosts } from '@/lib/swarm/interactions';
|
||||
|
||||
type RouteContext = { params: Promise<{ handle: string }> };
|
||||
|
||||
@@ -156,6 +156,11 @@ export async function POST(request: Request, context: RouteContext) {
|
||||
.set({ followingCount: currentUser.followingCount + 1 })
|
||||
.where(eq(users.id, currentUser.id));
|
||||
|
||||
// Cache the remote user's recent posts in the background
|
||||
cacheSwarmUserPosts(remote.handle, remote.domain, targetHandle, 20)
|
||||
.then(result => console.log(`[Swarm] Cached ${result.cached} posts for ${targetHandle}`))
|
||||
.catch(err => console.error('[Swarm] Error caching remote posts:', err));
|
||||
|
||||
console.log(`[Swarm] Follow delivered to ${remote.domain} for @${remote.handle}`);
|
||||
return NextResponse.json({ success: true, following: true, remote: true, swarm: true });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user