izzilis/src/publish/mastodon.rs

46 lines
1.1 KiB
Rust
Raw Normal View History

2021-09-30 17:53:33 +01:00
use std::{
error::Error,
pin::Pin,
task::{Context, Poll},
};
2021-07-06 03:13:51 +01:00
use futures::Sink;
use mammut::{status_builder::Visibility, Mastodon, StatusBuilder};
pub struct MastodonPublisher {
mastodon: Mastodon,
2021-09-30 17:53:33 +01:00
post_visibility: Visibility,
2021-07-06 03:13:51 +01:00
}
impl MastodonPublisher {
2021-09-30 17:53:33 +01:00
pub fn new(mastodon: Mastodon, vis: Visibility) -> Self {
Self {
mastodon: mastodon,
post_visibility: vis,
}
2021-07-06 03:13:51 +01:00
}
}
impl Sink<String> for MastodonPublisher {
type Error = Box<dyn Error>;
2021-07-06 03:13:51 +01:00
fn poll_ready(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
fn start_send(self: Pin<&mut Self>, item: String) -> Result<(), Self::Error> {
let mut post = StatusBuilder::new(item);
2021-09-30 17:53:33 +01:00
post.visibility = Some(self.post_visibility);
2021-07-06 03:13:51 +01:00
self.mastodon.new_status(post)?;
Ok(())
}
fn poll_flush(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
fn poll_close(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
}