Refactor App struct

This commit is contained in:
Marc Plano-Lesay 2025-05-21 15:32:43 +10:00
parent f9692b8ddf
commit ee7d7971be
Signed by: kernald
GPG key ID: 66A41B08CC62A6CF
13 changed files with 1147 additions and 163 deletions

View file

@ -1,19 +1,18 @@
use crate::actions::action::{Action, ProcessedMagnets};
use crate::actions::bitmagnet::BitmagnetAction;
use crate::actions::factory::init_action;
use crate::actions::transmission::TransmissionAction;
use crate::actions::action::ProcessedMagnets;
use crate::config::Config;
use crate::db::Database;
use crate::notifications::factory::init_notification;
use crate::notifications::notification::Notification;
use crate::notifications::ntfy::NtfyNotification;
use crate::reddit_client::{RedditClient, RedditPost};
use crate::report;
use color_eyre::eyre::{Result, WrapErr};
use log::{debug, info, warn};
use crate::reddit_client::RedditPost;
use crate::services::action::ActionService;
use crate::services::database::DatabaseService;
use crate::services::notification::NotificationService;
use crate::services::post_processor::PostProcessorService;
use crate::services::reddit::RedditService;
use crate::services::report::ReportService;
use color_eyre::eyre::Result;
use multimap::MultiMap;
use regex::Regex;
use std::cell::RefCell;
use std::collections::{HashMap, HashSet};
use std::rc::Rc;
/// Trait for configurations that can be enabled or disabled
pub trait Enableable {
@ -23,44 +22,37 @@ pub trait Enableable {
/// Application state and behavior
pub struct App {
db: Database,
config: Config,
actions: Vec<Box<dyn Action>>,
notifications: Vec<Box<dyn Notification>>,
reddit_client: RedditClient,
action_service: ActionService,
notification_service: NotificationService,
reddit_service: RedditService,
post_processor: PostProcessorService,
report_service: ReportService,
}
impl App {
/// Create a new App instance
pub fn new(db: Database, config: Config) -> Self {
let db_service = Rc::new(RefCell::new(DatabaseService::new(db)));
Self {
db,
config,
actions: Vec::new(),
notifications: Vec::new(),
reddit_client: RedditClient::new(),
action_service: ActionService::new(Rc::clone(&db_service)),
notification_service: NotificationService::new(),
reddit_service: RedditService::new(),
post_processor: PostProcessorService::new(Rc::clone(&db_service)),
report_service: ReportService::new(),
}
}
/// Initialize actions based on configuration
pub fn init_actions(&mut self) -> Result<()> {
if let Some(action) = init_action(&self.config.bitmagnet, BitmagnetAction::new)? {
self.actions.push(action);
}
if let Some(action) = init_action(&self.config.transmission, TransmissionAction::new)? {
self.actions.push(action);
}
Ok(())
self.action_service.init_actions(&self.config)
}
/// Initialize notifications based on configuration
pub fn init_notifications(&mut self) -> Result<()> {
if let Some(notification) = init_notification(&self.config.ntfy, NtfyNotification::new)? {
self.notifications.push(notification);
}
Ok(())
self.notification_service.init_notifications(&self.config)
}
/// Fetch posts from Reddit
@ -70,15 +62,10 @@ impl App {
unique_usernames.insert(source_config.username.clone());
}
let mut user_posts = MultiMap::new();
for username in unique_usernames {
info!("Fetching posts from user [{}]", username);
let submissions = self
.reddit_client
.fetch_user_submissions(&username, post_count)
.await?;
user_posts.insert_many(username, submissions);
}
let user_posts = self
.reddit_service
.fetch_posts_from_users(unique_usernames, post_count)
.await?;
Ok(user_posts)
}
@ -88,86 +75,13 @@ impl App {
&mut self,
user_posts: &MultiMap<String, RedditPost>,
) -> Result<usize> {
let mut total_new_links = 0;
for (source_name, source_config) in &self.config.sources {
info!("Processing source [{}]", source_name);
let username = source_config.username.clone();
let title_filter = match &source_config.title_filter {
Some(filter) => Some(
Regex::new(filter.as_str())
.context(format!("Invalid regex pattern: {}", filter))?,
),
None => None,
};
if let Some(submissions) = user_posts.get_vec(&username) {
for post in submissions
.iter()
.filter(|s| filter_post(&s.title, title_filter.clone()))
{
let title = &post.title;
let body = &post.body;
let subreddit = &post.subreddit;
let magnet_links = crate::magnet::extract_magnet_links(body);
if !magnet_links.is_empty() {
let post_info = crate::PostInfo {
title: title.to_string(),
submitter: username.clone(),
subreddit: subreddit.to_string(),
magnet_links,
timestamp: post.created,
imdb_id: source_config.imdb_id.clone(),
tags: source_config.tags.clone(),
};
// Store the post info in the database
match self.db.store_magnets(&post_info) {
Ok(count) => {
total_new_links += count;
}
Err(e) => {
warn!("Failed to store post info in database: {}", e);
}
}
}
}
}
}
Ok(total_new_links)
self.post_processor
.process_sources(user_posts, &self.config.sources)
}
/// Process magnet links with actions
pub async fn process_actions(&mut self) -> Result<HashMap<String, ProcessedMagnets>> {
let mut action_results = HashMap::new();
for action in &mut self.actions {
debug!("Processing magnet links with {}", action.get_name());
match action.process_unprocessed_magnets(&mut self.db).await {
Ok(processed_magnets) => {
let count = processed_magnets.success.len();
debug!(
"Successfully processed {} magnet links with {}",
count,
action.get_name()
);
action_results.insert(action.get_name().to_string(), processed_magnets);
}
Err(e) => {
warn!(
"Failed to process magnet links with {}: {}",
action.get_name(),
e
);
}
}
}
Ok(action_results)
self.action_service.process_actions().await
}
/// Send notifications
@ -176,28 +90,9 @@ impl App {
action_results: &HashMap<String, ProcessedMagnets>,
total_new_links: usize,
) -> Result<()> {
for notification in &self.notifications {
match notification
.send_notification(action_results, total_new_links)
.await
{
Ok(_) => {
debug!(
"Successfully sent notification to {}",
notification.get_name()
);
}
Err(e) => {
warn!(
"Failed to send notification to {}: {}",
notification.get_name(),
e
);
}
}
}
Ok(())
self.notification_service
.send_notifications(action_results, total_new_links)
.await
}
/// Generate and display report
@ -206,9 +101,8 @@ impl App {
action_results: &HashMap<String, ProcessedMagnets>,
total_new_links: usize,
) {
for line in report::generate_report(action_results, total_new_links, true).lines() {
info!("{}", line);
}
self.report_service
.generate_report(action_results, total_new_links);
}
pub async fn run(&mut self, post_count: u32) -> Result<()> {
@ -234,11 +128,3 @@ impl App {
Ok(())
}
}
/// Filters posts based on a title filter pattern
fn filter_post(title: &str, title_filter: Option<Regex>) -> bool {
match title_filter {
Some(pattern) => pattern.is_match(title),
None => true,
}
}