NO OPTOUTS!
This commit is contained in:
parent
a046a381c5
commit
d27797bc2a
|
@ -1,4 +1,4 @@
|
|||
[submodule "web"]
|
||||
path = web
|
||||
url = https://github.com/boring-nick/justlog
|
||||
url = https://git.nadeko.net/Fijxu/rustlog-web
|
||||
branch = frontend-only-new
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
pub mod cache;
|
||||
|
||||
use self::cache::UsersCache;
|
||||
use crate::{config::Config, db::delete_user_logs, error::Error, Result};
|
||||
use crate::{config::Config, error::Error, Result};
|
||||
use anyhow::Context;
|
||||
use dashmap::DashSet;
|
||||
use std::{collections::HashMap, sync::Arc};
|
||||
|
@ -13,7 +13,7 @@ pub struct App {
|
|||
pub helix_client: HelixClient<'static, reqwest::Client>,
|
||||
pub token: Arc<AppAccessToken>,
|
||||
pub users: UsersCache,
|
||||
pub optout_codes: Arc<DashSet<String>>,
|
||||
// pub optout_codes: Arc<DashSet<String>>,
|
||||
pub db: Arc<clickhouse::Client>,
|
||||
pub config: Arc<Config>,
|
||||
}
|
||||
|
@ -113,17 +113,17 @@ impl App {
|
|||
}
|
||||
}
|
||||
|
||||
pub async fn optout_user(&self, user_id: &str) -> anyhow::Result<()> {
|
||||
delete_user_logs(&self.db, user_id)
|
||||
.await
|
||||
.context("Could not delete logs")?;
|
||||
// pub async fn optout_user(&self, user_id: &str) -> anyhow::Result<()> {
|
||||
// delete_user_logs(&self.db, user_id)
|
||||
// .await
|
||||
// .context("Could not delete logs")?;
|
||||
|
||||
self.config.opt_out.insert(user_id.to_owned(), true);
|
||||
self.config.save()?;
|
||||
info!("User {user_id} opted out");
|
||||
// self.config.opt_out.insert(user_id.to_owned(), true);
|
||||
// self.config.save()?;
|
||||
// info!("User {user_id} opted out");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
// Ok(())
|
||||
// }
|
||||
|
||||
pub fn check_opted_out(&self, channel_id: &str, user_id: Option<&str>) -> Result<()> {
|
||||
if self.config.opt_out.contains_key(channel_id) {
|
||||
|
|
42
src/bot.rs
42
src/bot.rs
|
@ -241,9 +241,9 @@ impl Bot {
|
|||
self.update_channels(client, &args, ChannelAction::Part)
|
||||
.await?
|
||||
}
|
||||
"optout" => {
|
||||
self.optout_user(&args, sender_login, sender_id).await?;
|
||||
}
|
||||
// "optout" => {
|
||||
// self.optout_user(&args, sender_login, sender_id).await?;
|
||||
// }
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
|
@ -251,27 +251,27 @@ impl Bot {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
async fn optout_user(
|
||||
&self,
|
||||
args: &[&str],
|
||||
sender_login: &str,
|
||||
sender_id: &str,
|
||||
) -> anyhow::Result<()> {
|
||||
let arg = args.first().context("No optout code provided")?;
|
||||
if self.app.optout_codes.remove(*arg).is_some() {
|
||||
self.app.optout_user(sender_id).await?;
|
||||
// async fn optout_user(
|
||||
// &self,
|
||||
// args: &[&str],
|
||||
// sender_login: &str,
|
||||
// sender_id: &str,
|
||||
// ) -> anyhow::Result<()> {
|
||||
// let arg = args.first().context("No optout code provided")?;
|
||||
// if self.app.optout_codes.remove(*arg).is_some() {
|
||||
// self.app.optout_user(sender_id).await?;
|
||||
|
||||
Ok(())
|
||||
} else if self.check_admin(sender_login).is_ok() {
|
||||
let user_id = self.app.get_user_id_by_name(arg).await?;
|
||||
// Ok(())
|
||||
// } else if self.check_admin(sender_login).is_ok() {
|
||||
// let user_id = self.app.get_user_id_by_name(arg).await?;
|
||||
|
||||
self.app.optout_user(&user_id).await?;
|
||||
// self.app.optout_user(&user_id).await?;
|
||||
|
||||
Ok(())
|
||||
} else {
|
||||
Err(anyhow!("Invalid optout code"))
|
||||
}
|
||||
}
|
||||
// Ok(())
|
||||
// } else {
|
||||
// Err(anyhow!("Invalid optout code"))
|
||||
// }
|
||||
// }
|
||||
|
||||
async fn update_channels<C: LoginCredentials>(
|
||||
&self,
|
||||
|
|
|
@ -254,14 +254,14 @@ pub async fn read_random_channel_line(db: &Client, channel_id: &str) -> Result<S
|
|||
Ok(text)
|
||||
}
|
||||
|
||||
pub async fn delete_user_logs(_db: &Client, _user_id: &str) -> Result<()> {
|
||||
// info!("Deleting all logs for user {user_id}");
|
||||
// db.query("ALTER TABLE message DELETE WHERE user_id = ?")
|
||||
// .bind(user_id)
|
||||
// .execute()
|
||||
// .await?;
|
||||
Ok(())
|
||||
}
|
||||
// pub async fn delete_user_logs(_db: &Client, _user_id: &str) -> Result<()> {
|
||||
// // info!("Deleting all logs for user {user_id}");
|
||||
// // db.query("ALTER TABLE message DELETE WHERE user_id = ?")
|
||||
// // .bind(user_id)
|
||||
// // .execute()
|
||||
// // .await?;
|
||||
// Ok(())
|
||||
// }
|
||||
|
||||
fn apply_limit_offset(query: &mut String, limit: Option<u64>, offset: Option<u64>) {
|
||||
if let Some(limit) = limit {
|
||||
|
|
|
@ -104,7 +104,7 @@ async fn run(config: Config, db: clickhouse::Client) -> anyhow::Result<()> {
|
|||
users: UsersCache::default(),
|
||||
config: Arc::new(config),
|
||||
db: Arc::new(db),
|
||||
optout_codes: Arc::default(),
|
||||
// optout_codes: Arc::default(),
|
||||
};
|
||||
|
||||
let (bot_tx, bot_rx) = mpsc::channel(1);
|
||||
|
|
|
@ -369,25 +369,25 @@ async fn random_user_line(
|
|||
Ok((no_cache_header(), logs))
|
||||
}
|
||||
|
||||
pub async fn optout(app: State<App>) -> Json<String> {
|
||||
let mut rng = thread_rng();
|
||||
let optout_code: String = (0..5).map(|_| rng.sample(Alphanumeric) as char).collect();
|
||||
// pub async fn optout(app: State<App>) -> Json<String> {
|
||||
// let mut rng = thread_rng();
|
||||
// let optout_code: String = (0..5).map(|_| rng.sample(Alphanumeric) as char).collect();
|
||||
|
||||
app.optout_codes.insert(optout_code.clone());
|
||||
// app.optout_codes.insert(optout_code.clone());
|
||||
|
||||
{
|
||||
let codes = app.optout_codes.clone();
|
||||
let optout_code = optout_code.clone();
|
||||
tokio::spawn(async move {
|
||||
tokio::time::sleep(Duration::from_secs(60)).await;
|
||||
if codes.remove(&optout_code).is_some() {
|
||||
debug!("Dropping optout code {optout_code}");
|
||||
}
|
||||
});
|
||||
}
|
||||
// {
|
||||
// let codes = app.optout_codes.clone();
|
||||
// let optout_code = optout_code.clone();
|
||||
// tokio::spawn(async move {
|
||||
// tokio::time::sleep(Duration::from_secs(60)).await;
|
||||
// if codes.remove(&optout_code).is_some() {
|
||||
// debug!("Dropping optout code {optout_code}");
|
||||
// }
|
||||
// });
|
||||
// }
|
||||
|
||||
Json(optout_code)
|
||||
}
|
||||
// Json(optout_code)
|
||||
// }
|
||||
|
||||
fn cache_header(secs: u64) -> TypedHeader<CacheControl> {
|
||||
TypedHeader(
|
||||
|
|
|
@ -137,7 +137,7 @@ pub async fn run(app: App, mut shutdown_rx: ShutdownRx, bot_tx: Sender<BotMessag
|
|||
op.description("Get a random line from the user's logs in a channel")
|
||||
}),
|
||||
)
|
||||
.api_route("/optout", post(handlers::optout))
|
||||
// .api_route("/optout", post(handlers::optout))
|
||||
.api_route("/capabilities", get(capabilities))
|
||||
.route("/docs", Redoc::new("/openapi.json").axum_route())
|
||||
.route("/openapi.json", get(serve_openapi))
|
||||
|
|
Loading…
Reference in New Issue