This commit is contained in:
jjbrunton committed 2026-01-04 19:16:31 +00:00
commit c552956516
23 files changed
+5193

No files matched your search

+50
View File
@@ -0,0 +1,50 @@
import type { RedditApiClient, RedditPost } from '../reddit/api.js';
import type { PostsRepository } from '../database/repositories.js';
import type { Config } from '../config/index.js';
import type { Logger } from '../utils/logger.js';
export class MonitorService {
constructor(
private redditApi: RedditApiClient,
private postsRepo: PostsRepository,
private config: Config,
private logger: Logger
) {}
async getUnprocessedPosts(): Promise<RedditPost[]> {
const unprocessedPosts: RedditPost[] = [];
for (const subreddit of this.config.reddit.subreddits) {
try {
this.logger.debug(`Checking r/${subreddit} for new posts...`);
const posts = await this.redditApi.getNewPosts(subreddit);
let newCount = 0;
for (const post of posts) {
if (!this.postsRepo.isProcessed(post.id)) {
unprocessedPosts.push(post);
newCount++;
}
}
if (newCount > 0) {
this.logger.info(`Found ${newCount} new posts in r/${subreddit}`);
}
} catch (error) {
this.logger.error(`Failed to fetch posts from r/${subreddit}:`, error);
}
// Small delay between subreddit fetches to be nice to Reddit
await this.sleep(1000);
}
// Sort by creation time (oldest first, so we process in order)
unprocessedPosts.sort((a, b) => a.created_utc - b.created_utc);
return unprocessedPosts;
}
private sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}
}