izzilis/src/publish/misskey.rs

50 lines
1.2 KiB
Rust

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<Self, Box<dyn Error>> {
Ok(Self {
client: HttpClient::with_token(Url::parse(url)?, token)?,
})
}
}
impl Sink<String> for MisskeyPublisher {
type Error = Box<dyn Error>;
fn poll_ready(
self: std::pin::Pin<&mut Self>,
_: &mut std::task::Context<'_>,
) -> std::task::Poll<Result<(), Self::Error>> {
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<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
fn poll_close(
self: std::pin::Pin<&mut Self>,
_: &mut std::task::Context<'_>,
) -> std::task::Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
}