moved selector -> curator, non-working curator implementation

This commit is contained in:
emilis 2021-10-15 16:37:46 +02:00
parent 4fcb8a9771
commit 4dc210ad2d
7 changed files with 60 additions and 32 deletions

View File

@ -17,6 +17,6 @@ uuid = { version = "0.8.2", features = ["v4"] }
anyhow = "1.0.41"
mammut = "0.13.0"
thiserror = "1.0.26"
misskey = "0.2.0"
misskey = { version = "0.2.0", features = ["aid"] }
url = "2.2.2"
smol = "1.2.5"

18
src/curation/any.rs Normal file
View File

@ -0,0 +1,18 @@
use std::error::Error;
use futures::future::BoxFuture;
use super::Curator;
#[derive(Clone)]
pub struct AnyCurator;
impl Curator for AnyCurator {
type Error = Box<dyn Error>;
type Response = BoxFuture<'static, Result<bool, Self::Error>>;
fn review(&self, _: String) -> Self::Response {
Box::pin(async {Ok(true)})
}
}

View File

@ -3,12 +3,12 @@ use std::error::Error;
use async_std::io::stdin;
use futures::future::BoxFuture;
use super::Selector;
use super::Curator;
#[derive(Debug, Copy, Clone)]
pub struct ConsoleSelector;
pub struct ConsoleCurator;
impl Selector for ConsoleSelector {
impl Curator for ConsoleCurator {
type Error = Box<dyn Error>;
type Response = BoxFuture<'static, Result<bool, Self::Error>>;

View File

@ -2,19 +2,21 @@ use futures::{stream::BoxStream, Future, Stream, TryStreamExt};
use std::fmt::Debug;
use thiserror::Error;
mod any;
mod console;
pub mod telegram;
pub use console::ConsoleSelector;
pub use telegram::TelegramSelector;
pub use any::AnyCurator;
pub use console::ConsoleCurator;
pub use telegram::TelegramCurator;
pub trait Selector {
pub trait Curator: Sync + Send + Clone {
type Error;
type Response: Future<Output = Result<bool, Self::Error>>;
fn review(&self, data: String) -> Self::Response;
}
pub trait SelectorExt<E: Debug, S: Stream<Item = Result<String, E>>>: Selector
pub trait CuratorExt<E: Debug, S: Stream<Item = Result<String, E>>>: Curator
where
Self::Error: Debug,
{
@ -33,9 +35,9 @@ pub enum FilterError<T: Debug, U: Debug> {
impl<
E: Debug + 'static,
T: Selector + Sync + Send + Clone + 'static,
T: Curator + Sync + Send + Clone + 'static,
S: Send + Stream<Item = Result<String, E>> + 'static,
> SelectorExt<E, S> for T
> CuratorExt<E, S> for T
where
T::Response: Send,
T::Error: Debug,

View File

@ -4,7 +4,7 @@ use futures::{
lock::Mutex,
Sink, SinkExt, StreamExt,
};
use std::{collections::HashMap, fmt::Debug, sync::Arc};
use std::{collections::HashMap, error::Error, fmt::Debug, sync::Arc};
use telegram_bot::{
self, types::requests::answer_callback_query::CanAnswerCallbackQuery, Api, ChatRef,
DeleteMessage, EditMessageReplyMarkup, InlineKeyboardButton, InlineKeyboardMarkup, Message,
@ -12,16 +12,16 @@ use telegram_bot::{
};
use uuid::Uuid;
use super::Selector;
use super::Curator;
#[derive(Clone)]
pub struct TelegramSelector {
pub struct TelegramCurator {
client: Arc<Api>,
chat_ref: Arc<Mutex<ChatRef>>,
pending: Arc<Mutex<HashMap<Uuid, Sender<bool>>>>,
}
impl TelegramSelector {
impl TelegramCurator {
pub fn new<S: Sink<ChatRef> + Send + Unpin + 'static>(
api: Arc<Api>,
chat_ref: Arc<Mutex<ChatRef>>,
@ -116,8 +116,8 @@ impl TelegramSelector {
}
}
impl Selector for TelegramSelector {
type Error = telegram_bot::Error;
impl Curator for TelegramCurator {
type Error = Box<dyn Error>;
type Response = BoxFuture<'static, Result<bool, Self::Error>>;

View File

@ -11,18 +11,20 @@ use telegram_bot::{Api, ChatId, ChatRef, ToChatRef};
use crate::{
config::{FediverseConfig, Publisher},
curation::{telegram::get_chat_ref, CuratorExt, TelegramCurator},
publish::MastodonPublisher,
publish::MisskeyPublisher,
selection::{telegram::get_chat_ref, SelectorExt, TelegramSelector},
};
use futures::{SinkExt, StreamExt, TryStreamExt, channel::mpsc::channel, future::Either, sink::unfold};
use futures::{
channel::mpsc::channel, future::Either, sink::unfold, SinkExt, StreamExt, TryStreamExt,
};
use model::SampleModelExt;
mod config;
mod curation;
mod model;
mod publish;
mod selection;
const CONFIG_PATH: &str = "bot_config.json";
@ -40,7 +42,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
}
}?;
let publisher = resolve_publisher(&mut cfg).await?;
let publisher = resolve_publisher(&mut cfg).await?;
let api = Arc::new(Api::new(cfg.bot_token.clone()));
@ -66,7 +68,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
let cfg_clone = cfg.clone();
let mut model = TelegramSelector::new(
let mut model = TelegramCurator::new(
api,
chat,
Box::pin(unfold((), move |_, chat_ref| {
@ -145,9 +147,11 @@ async fn resolve_publisher(
config: &mut config::Config,
) -> Result<Either<MisskeyPublisher, MastodonPublisher>, Box<dyn Error>> {
let publisher = match &config.publisher {
config::Publisher::Misskey(cfg) => {
Either::Left(MisskeyPublisher::new(&cfg.base_url, cfg.token.clone(), cfg.visibility)?)
}
config::Publisher::Misskey(cfg) => Either::Left(MisskeyPublisher::new(
&cfg.base_url,
cfg.token.clone(),
cfg.visibility,
)?),
config::Publisher::Mastodon(cfg) => {
let app = AppBuilder {
client_name: "izzilis",
@ -158,7 +162,7 @@ async fn resolve_publisher(
let mut registration = Registration::new(cfg.base_url.clone());
registration.register(app)?;
let vis = cfg.visibility;
let vis = cfg.visibility;
let mastodon = if let Some(data) = cfg.token.clone() {
Mastodon::from_data(data.clone())
@ -174,7 +178,7 @@ async fn resolve_publisher(
config.publisher = Publisher::Mastodon(FediverseConfig {
base_url: cfg.base_url.clone(),
token: Some(fedi.data.clone()),
visibility: vis.clone(),
visibility: vis.clone(),
});
config.save(CONFIG_PATH)?;

View File

@ -1,18 +1,18 @@
use futures::Sink;
use misskey::{Client, ClientExt, HttpClient, model::note::Visibility};
use misskey::{model::note::Visibility, Client, ClientExt, HttpClient};
use std::{error::Error, task::Poll};
use url::Url;
pub struct MisskeyPublisher {
client: HttpClient,
post_visibility: Visibility,
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,
post_visibility: vis,
})
}
}
@ -28,9 +28,13 @@ impl Sink<String> for MisskeyPublisher {
}
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))?;
let mut req = self.client.build_note();
let req = req.text(item).visibility(self.post_visibility).as_request();
let response = smol::block_on(self.client.request(req))?;
// If there is an ApiResult error, decode it to a regular Result error
if let Option::Some(err) = response.err() {
return Err(Box::new(err));
}
Ok(())
}