use futures::Sink; use misskey::{ClientExt, HttpClient}; use std::{error::Error, task::Poll}; use tokio::runtime::Runtime; use url::Url; pub struct MisskeyPublisher { client: HttpClient, } impl MisskeyPublisher { pub fn new(url: &String, token: String) -> Result> { Ok(Self { client: HttpClient::with_token(Url::parse(url)?, token)?, }) } } 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 runtime = Runtime::new()?; let fut = self.client.create_note(item); runtime.block_on(fut)?; 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(())) } }