izzilis/src/publish/misskey.rs

51 lines
1.4 KiB
Rust

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<Self, Box<dyn Error>> {
Ok(Self {
client: HttpClient::with_token(Url::parse(url)?, token)?,
post_visibility: vis,
})
}
}
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 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<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(()))
}
}