use futures::Sink; use misskey::{Client, ClientExt, HttpClient, model::note::Visibility}; use std::{error::Error, task::Poll}; use url::Url; pub struct MisskeyPublisher { client: HttpClient, post_visibility: Visibility, } impl MisskeyPublisher { pub fn new(url: &String, token: String, vis: Visibility) -> Result> { Ok(Self { client: HttpClient::with_token(Url::parse(url)?, token)?, post_visibility: vis, }) } } impl Sink for MisskeyPublisher { type Error = Box; fn poll_ready( self: std::pin::Pin<&mut Self>, _: &mut std::task::Context<'_>, ) -> std::task::Poll> { Poll::Ready(Ok(())) } fn start_send(self: std::pin::Pin<&mut Self>, item: String) -> Result<(), Self::Error> { let mut req = self.client.build_note(); let req = req.text(item).visibility(self.post_visibility).as_request(); smol::block_on(self.client.request(req))?; Ok(()) } fn poll_flush( self: std::pin::Pin<&mut Self>, _: &mut std::task::Context<'_>, ) -> std::task::Poll> { Poll::Ready(Ok(())) } fn poll_close( self: std::pin::Pin<&mut Self>, _: &mut std::task::Context<'_>, ) -> std::task::Poll> { Poll::Ready(Ok(())) } }