feature: admins can now manually opt out users (#12)

* feature: admins can now manually opt out users

* fix: formatting
This commit is contained in:
bisspector 2023-07-30 16:32:32 +02:00 committed by GitHub
parent 607cbfb8b9
commit 58b66e6be4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 14 deletions

View File

@ -1,10 +1,11 @@
pub mod cache;
use self::cache::UsersCache;
use crate::{config::Config, error::Error, Result};
use crate::{config::Config, db::delete_user_logs, error::Error, Result};
use anyhow::Context;
use dashmap::DashSet;
use std::{collections::HashMap, sync::Arc};
use tracing::debug;
use tracing::{debug, info};
use twitch_api2::{helix::users::GetUsersRequest, twitch_oauth2::AppAccessToken, HelixClient};
#[derive(Clone)]
@ -112,6 +113,18 @@ 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")?;
self.config.opt_out.insert(user_id.to_owned(), true);
self.config.save()?;
info!("User {user_id} opted out");
Ok(())
}
pub fn check_opted_out(&self, channel_id: &str, user_id: Option<&str>) -> Result<()> {
if self.config.opt_out.contains_key(channel_id) {
return Err(Error::OptedOut);

View File

@ -1,6 +1,6 @@
use crate::{
app::App,
db::{delete_user_logs, schema::Message},
db::schema::Message,
logs::extract::{extract_channel_and_user_from_raw, extract_raw_timestamp},
ShutdownRx,
};
@ -242,7 +242,7 @@ impl Bot {
.await?
}
"optout" => {
self.optout_user(&args, sender_id).await?;
self.optout_user(&args, sender_login, sender_id).await?;
}
_ => (),
}
@ -251,19 +251,27 @@ impl Bot {
Ok(())
}
async fn optout_user(&self, args: &[&str], sender_id: &str) -> anyhow::Result<()> {
let code = args.first().context("No optout code provided")?;
if self.app.optout_codes.remove(*code).is_some() {
delete_user_logs(&self.app.db, sender_id)
.await
.context("Could not delete logs")?;
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?;
self.app.config.opt_out.insert(sender_id.to_owned(), true);
self.app.config.save()?;
info!("User {sender_id} opted out");
Ok(())
} else {
Err(anyhow!("Invalid optout code"))
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?;
Ok(())
} else {
Err(anyhow!("Invalid optout code"))
}
}
}