Add pre-commit

This commit is contained in:
2023-09-24 22:37:40 +02:00
parent 0afe3acfcd
commit 452040e83a
51 changed files with 771 additions and 596 deletions

View File

@@ -1,14 +1,14 @@
pub mod bot_manager_client;
use axum::extract::{State, Path};
use axum::extract::{Path, State};
use axum::response::IntoResponse;
use axum::routing::post;
use once_cell::sync::Lazy;
use reqwest::StatusCode;
use smartstring::alias::String as SmartString;
use teloxide::stop::{mk_stop_token, StopToken, StopFlag};
use teloxide::stop::{mk_stop_token, StopFlag, StopToken};
use teloxide::update_listeners::{StatefulListener, UpdateListener};
use tokio::sync::mpsc::{UnboundedSender, self};
use tokio::sync::mpsc::{self, UnboundedSender};
use tokio_stream::wrappers::UnboundedReceiverStream;
use tracing::log;
use url::Url;
@@ -24,7 +24,7 @@ use tokio::sync::RwLock;
use smallvec::SmallVec;
use teloxide::adaptors::throttle::Limits;
use teloxide::types::{BotCommand, UpdateKind};
use tokio::time::{sleep, Duration, self};
use tokio::time::{self, sleep, Duration};
use tower_http::trace::TraceLayer;
use teloxide::prelude::*;
@@ -35,15 +35,12 @@ use self::bot_manager_client::get_bots;
pub use self::bot_manager_client::{BotCache, BotData};
use crate::config;
type UpdateSender = mpsc::UnboundedSender<Result<Update, std::convert::Infallible>>;
fn tuple_first_mut<A, B>(tuple: &mut (A, B)) -> &mut A {
&mut tuple.0
}
pub static USER_ACTIVITY_CACHE: Lazy<Cache<UserId, ()>> = Lazy::new(|| {
Cache::builder()
.time_to_idle(Duration::from_secs(5 * 60))
@@ -65,9 +62,17 @@ pub static CHAT_DONATION_NOTIFICATIONS_CACHE: Lazy<Cache<ChatId, ()>> = Lazy::ne
.build()
});
type Routes = Arc<RwLock<HashMap<String, (StopToken, ClosableSender<Result<Update, std::convert::Infallible>>)>>>;
type Routes = Arc<
RwLock<
HashMap<
String,
(
StopToken,
ClosableSender<Result<Update, std::convert::Infallible>>,
),
>,
>,
>;
struct ClosableSender<T> {
origin: std::sync::Arc<std::sync::RwLock<Option<mpsc::UnboundedSender<T>>>>,
@@ -75,13 +80,17 @@ struct ClosableSender<T> {
impl<T> Clone for ClosableSender<T> {
fn clone(&self) -> Self {
Self { origin: self.origin.clone() }
Self {
origin: self.origin.clone(),
}
}
}
impl<T> ClosableSender<T> {
fn new(sender: mpsc::UnboundedSender<T>) -> Self {
Self { origin: std::sync::Arc::new(std::sync::RwLock::new(Some(sender))) }
Self {
origin: std::sync::Arc::new(std::sync::RwLock::new(Some(sender))),
}
}
fn get(&self) -> Option<mpsc::UnboundedSender<T>> {
@@ -93,7 +102,6 @@ impl<T> ClosableSender<T> {
}
}
#[derive(Default, Clone)]
struct ServerState {
routers: Routes,
@@ -102,7 +110,7 @@ struct ServerState {
pub struct BotsManager {
port: u16,
state: ServerState
state: ServerState,
}
impl BotsManager {
@@ -111,12 +119,19 @@ impl BotsManager {
port: 8000,
state: ServerState {
routers: Arc::new(RwLock::new(HashMap::new()))
}
routers: Arc::new(RwLock::new(HashMap::new())),
},
}
}
fn get_listener(&self) -> (StopToken, StopFlag, UnboundedSender<Result<Update, std::convert::Infallible>>, impl UpdateListener<Err = Infallible>) {
fn get_listener(
&self,
) -> (
StopToken,
StopFlag,
UnboundedSender<Result<Update, std::convert::Infallible>>,
impl UpdateListener<Err = Infallible>,
) {
let (tx, rx): (UpdateSender, _) = mpsc::unbounded_channel();
let (stop_token, stop_flag) = mk_stop_token();
@@ -126,9 +141,7 @@ impl BotsManager {
let listener = StatefulListener::new(
(stream, stop_token.clone()),
tuple_first_mut,
|state: &mut (_, StopToken)| {
state.1.clone()
},
|state: &mut (_, StopToken)| state.1.clone(),
);
(stop_token, stop_flag, tx, listener)
@@ -187,7 +200,7 @@ impl BotsManager {
true
}
async fn check(&mut self){
async fn check(&mut self) {
let bots_data = get_bots().await;
match bots_data {
@@ -202,7 +215,7 @@ impl BotsManager {
self.start_bot(bot_data).await;
}
}
},
}
Err(err) => {
log::info!("{:?}", err);
}
@@ -215,7 +228,6 @@ impl BotsManager {
Path(token): Path<String>,
input: String,
) -> impl IntoResponse {
let routes = routers.read().await;
let tx = routes.get(&token);
@@ -232,7 +244,7 @@ impl BotsManager {
sender.close();
};
return StatusCode::SERVICE_UNAVAILABLE;
},
}
};
match serde_json::from_str::<Update>(&input) {