use super::{truncate, MentionResult, MentionsLoop};
use crate::automation::loop_helpers::{LoopStorage, LoopTweet};
use std::sync::Arc;
impl MentionsLoop {
pub(crate) async fn process_mention(
&self,
mention: &LoopTweet,
storage: &Arc<dyn LoopStorage>,
) -> MentionResult {
if self.safety.has_replied_to(&mention.id).await {
tracing::debug!(tweet_id = %mention.id, "Already replied to mention, skipping");
return MentionResult::Skipped {
tweet_id: mention.id.clone(),
reason: "already replied".to_string(),
};
}
if !self.safety.can_reply().await {
tracing::warn!(tweet_id = %mention.id, "Reply rate limit reached, skipping");
return MentionResult::Skipped {
tweet_id: mention.id.clone(),
reason: "rate limited".to_string(),
};
}
let reply_output = match self
.generator
.generate_reply_with_rag(&mention.text, &mention.author_username, true)
.await
{
Ok(output) => output,
Err(e) => {
tracing::error!(
tweet_id = %mention.id,
error = %e,
"Failed to generate reply for mention"
);
return MentionResult::Failed {
tweet_id: mention.id.clone(),
error: e.to_string(),
};
}
};
let reply_text = reply_output.text;
tracing::info!(
author = %mention.author_username,
"Replied to mention from @{}",
mention.author_username,
);
if self.dry_run {
tracing::info!(
"DRY RUN: Would reply to mention {} by @{}: \"{}\"",
mention.id,
mention.author_username,
reply_text
);
} else {
if let Err(e) = self.poster.send_reply(&mention.id, &reply_text).await {
tracing::error!(
tweet_id = %mention.id,
error = %e,
"Failed to send reply to posting queue"
);
return MentionResult::Failed {
tweet_id: mention.id.clone(),
error: e.to_string(),
};
}
if let Err(e) = self.safety.record_reply(&mention.id, &reply_text).await {
tracing::warn!(
tweet_id = %mention.id,
error = %e,
"Failed to record reply (post may have been sent)"
);
}
}
let _ = storage
.log_action(
"mention_reply",
if self.dry_run { "dry_run" } else { "success" },
&format!(
"Reply to @{}: {}",
mention.author_username,
truncate(&reply_text, 50)
),
)
.await;
MentionResult::Replied {
tweet_id: mention.id.clone(),
author: mention.author_username.clone(),
reply_text,
}
}
}