NO OPTOUTS!

This commit is contained in:
Fijxu 2024-03-03 01:35:20 -03:00
parent a046a381c5
commit d27797bc2a
7 changed files with 59 additions and 59 deletions

2
.gitmodules vendored
View File

@ -1,4 +1,4 @@
[submodule "web"] [submodule "web"]
path = web path = web
url = https://github.com/boring-nick/justlog url = https://git.nadeko.net/Fijxu/rustlog-web
branch = frontend-only-new branch = frontend-only-new

View File

@ -1,7 +1,7 @@
pub mod cache; pub mod cache;
use self::cache::UsersCache; 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 anyhow::Context;
use dashmap::DashSet; use dashmap::DashSet;
use std::{collections::HashMap, sync::Arc}; use std::{collections::HashMap, sync::Arc};
@ -13,7 +13,7 @@ pub struct App {
pub helix_client: HelixClient<'static, reqwest::Client>, pub helix_client: HelixClient<'static, reqwest::Client>,
pub token: Arc<AppAccessToken>, pub token: Arc<AppAccessToken>,
pub users: UsersCache, pub users: UsersCache,
pub optout_codes: Arc<DashSet<String>>, // pub optout_codes: Arc<DashSet<String>>,
pub db: Arc<clickhouse::Client>, pub db: Arc<clickhouse::Client>,
pub config: Arc<Config>, pub config: Arc<Config>,
} }
@ -113,17 +113,17 @@ impl App {
} }
} }
pub async fn optout_user(&self, user_id: &str) -> anyhow::Result<()> { // pub async fn optout_user(&self, user_id: &str) -> anyhow::Result<()> {
delete_user_logs(&self.db, user_id) // delete_user_logs(&self.db, user_id)
.await // .await
.context("Could not delete logs")?; // .context("Could not delete logs")?;
self.config.opt_out.insert(user_id.to_owned(), true); // self.config.opt_out.insert(user_id.to_owned(), true);
self.config.save()?; // self.config.save()?;
info!("User {user_id} opted out"); // info!("User {user_id} opted out");
Ok(()) // Ok(())
} // }
pub fn check_opted_out(&self, channel_id: &str, user_id: Option<&str>) -> Result<()> { pub fn check_opted_out(&self, channel_id: &str, user_id: Option<&str>) -> Result<()> {
if self.config.opt_out.contains_key(channel_id) { if self.config.opt_out.contains_key(channel_id) {

View File

@ -241,9 +241,9 @@ impl Bot {
self.update_channels(client, &args, ChannelAction::Part) self.update_channels(client, &args, ChannelAction::Part)
.await? .await?
} }
"optout" => { // "optout" => {
self.optout_user(&args, sender_login, sender_id).await?; // self.optout_user(&args, sender_login, sender_id).await?;
} // }
_ => (), _ => (),
} }
} }
@ -251,27 +251,27 @@ impl Bot {
Ok(()) Ok(())
} }
async fn optout_user( // async fn optout_user(
&self, // &self,
args: &[&str], // args: &[&str],
sender_login: &str, // sender_login: &str,
sender_id: &str, // sender_id: &str,
) -> anyhow::Result<()> { // ) -> anyhow::Result<()> {
let arg = args.first().context("No optout code provided")?; // let arg = args.first().context("No optout code provided")?;
if self.app.optout_codes.remove(*arg).is_some() { // if self.app.optout_codes.remove(*arg).is_some() {
self.app.optout_user(sender_id).await?; // self.app.optout_user(sender_id).await?;
Ok(()) // Ok(())
} else if self.check_admin(sender_login).is_ok() { // } else if self.check_admin(sender_login).is_ok() {
let user_id = self.app.get_user_id_by_name(arg).await?; // 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(()) // Ok(())
} else { // } else {
Err(anyhow!("Invalid optout code")) // Err(anyhow!("Invalid optout code"))
} // }
} // }
async fn update_channels<C: LoginCredentials>( async fn update_channels<C: LoginCredentials>(
&self, &self,

View File

@ -254,14 +254,14 @@ pub async fn read_random_channel_line(db: &Client, channel_id: &str) -> Result<S
Ok(text) Ok(text)
} }
pub async fn delete_user_logs(_db: &Client, _user_id: &str) -> Result<()> { // pub async fn delete_user_logs(_db: &Client, _user_id: &str) -> Result<()> {
// info!("Deleting all logs for user {user_id}"); // // info!("Deleting all logs for user {user_id}");
// db.query("ALTER TABLE message DELETE WHERE user_id = ?") // // db.query("ALTER TABLE message DELETE WHERE user_id = ?")
// .bind(user_id) // // .bind(user_id)
// .execute() // // .execute()
// .await?; // // .await?;
Ok(()) // Ok(())
} // }
fn apply_limit_offset(query: &mut String, limit: Option<u64>, offset: Option<u64>) { fn apply_limit_offset(query: &mut String, limit: Option<u64>, offset: Option<u64>) {
if let Some(limit) = limit { if let Some(limit) = limit {

View File

@ -104,7 +104,7 @@ async fn run(config: Config, db: clickhouse::Client) -> anyhow::Result<()> {
users: UsersCache::default(), users: UsersCache::default(),
config: Arc::new(config), config: Arc::new(config),
db: Arc::new(db), db: Arc::new(db),
optout_codes: Arc::default(), // optout_codes: Arc::default(),
}; };
let (bot_tx, bot_rx) = mpsc::channel(1); let (bot_tx, bot_rx) = mpsc::channel(1);

View File

@ -369,25 +369,25 @@ async fn random_user_line(
Ok((no_cache_header(), logs)) Ok((no_cache_header(), logs))
} }
pub async fn optout(app: State<App>) -> Json<String> { // pub async fn optout(app: State<App>) -> Json<String> {
let mut rng = thread_rng(); // let mut rng = thread_rng();
let optout_code: String = (0..5).map(|_| rng.sample(Alphanumeric) as char).collect(); // 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 codes = app.optout_codes.clone();
let optout_code = optout_code.clone(); // let optout_code = optout_code.clone();
tokio::spawn(async move { // tokio::spawn(async move {
tokio::time::sleep(Duration::from_secs(60)).await; // tokio::time::sleep(Duration::from_secs(60)).await;
if codes.remove(&optout_code).is_some() { // if codes.remove(&optout_code).is_some() {
debug!("Dropping optout code {optout_code}"); // debug!("Dropping optout code {optout_code}");
} // }
}); // });
} // }
Json(optout_code) // Json(optout_code)
} // }
fn cache_header(secs: u64) -> TypedHeader<CacheControl> { fn cache_header(secs: u64) -> TypedHeader<CacheControl> {
TypedHeader( TypedHeader(

View File

@ -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") 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)) .api_route("/capabilities", get(capabilities))
.route("/docs", Redoc::new("/openapi.json").axum_route()) .route("/docs", Redoc::new("/openapi.json").axum_route())
.route("/openapi.json", get(serve_openapi)) .route("/openapi.json", get(serve_openapi))