use tokio::sync::{mpsc, oneshot};
pub const QUEUE_CAPACITY: usize = 100;
pub enum PostAction {
Reply {
tweet_id: String,
content: String,
media_ids: Vec<String>,
result_tx: Option<oneshot::Sender<Result<String, String>>>,
},
Tweet {
content: String,
media_ids: Vec<String>,
result_tx: Option<oneshot::Sender<Result<String, String>>>,
},
ThreadTweet {
content: String,
in_reply_to: String,
media_ids: Vec<String>,
result_tx: Option<oneshot::Sender<Result<String, String>>>,
},
}
impl std::fmt::Debug for PostAction {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
PostAction::Reply {
tweet_id,
content,
media_ids,
..
} => f
.debug_struct("Reply")
.field("tweet_id", tweet_id)
.field("content_len", &content.len())
.field("media_count", &media_ids.len())
.finish(),
PostAction::Tweet {
content, media_ids, ..
} => f
.debug_struct("Tweet")
.field("content_len", &content.len())
.field("media_count", &media_ids.len())
.finish(),
PostAction::ThreadTweet {
content,
in_reply_to,
media_ids,
..
} => f
.debug_struct("ThreadTweet")
.field("in_reply_to", in_reply_to)
.field("content_len", &content.len())
.field("media_count", &media_ids.len())
.finish(),
}
}
}
#[async_trait::async_trait]
pub trait PostExecutor: Send + Sync {
async fn execute_reply(
&self,
tweet_id: &str,
content: &str,
media_ids: &[String],
) -> Result<String, String>;
async fn execute_tweet(&self, content: &str, media_ids: &[String]) -> Result<String, String>;
}
pub fn create_posting_queue() -> (mpsc::Sender<PostAction>, mpsc::Receiver<PostAction>) {
mpsc::channel(QUEUE_CAPACITY)
}
#[async_trait::async_trait]
pub trait ApprovalQueue: Send + Sync {
async fn queue_reply(
&self,
tweet_id: &str,
content: &str,
media_paths: &[String],
) -> Result<i64, String>;
async fn queue_tweet(&self, content: &str, media_paths: &[String]) -> Result<i64, String>;
}