refactor: simplify fixer state handling

This commit is contained in:
Harsh Shandilya 2024-04-23 18:02:54 +05:30
parent 9cf2a70291
commit bdc9b21bb6
3 changed files with 84 additions and 74 deletions

View File

@ -1,4 +1,4 @@
use crate::{message::BotExt, utils::parse_bool, FIXER_STATE};
use crate::{fixer::FixerState, message::BotExt, utils::parse_bool, FIXER_STATE};
use once_cell::sync::Lazy;
use std::{env, error::Error, marker::Send};
use teloxide::{
@ -11,14 +11,6 @@ use teloxide::{
pub(crate) type FilterState = String;
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct FixerState {
pub(crate) instagram: bool,
pub(crate) medium: bool,
pub(crate) twitter: bool,
pub(crate) youtube: bool,
}
static BOT_OWNER: Lazy<UserId> = Lazy::new(|| {
let value = env::var("BOT_OWNER_ID").expect("BOT_OWNER_ID must be defined");
let id = value
@ -74,22 +66,16 @@ pub(crate) async fn handler(
{
match parse_bool(&filter_state) {
Ok(filter_state) => {
let new_state =
if let Some(cur) = FIXER_STATE.lock().unwrap().get(&message.chat.id) {
FixerState {
instagram: filter_state,
..*cur
}
} else {
FixerState {
instagram: filter_state,
..Default::default()
}
};
FIXER_STATE
.lock()
.unwrap()
.insert(message.chat.id, new_state);
let new_state = if let Ok(ref mut map) = FIXER_STATE.try_lock()
&& let Some(state) = map.get_mut(&message.chat.id)
{
*state.instagram(filter_state)
} else {
*FixerState::default().instagram(filter_state)
};
if let Ok(ref mut map) = FIXER_STATE.try_lock() {
map.insert(message.chat.id, new_state);
}
}
Err(error_message) => {
bot.send_chat_message(message, error_message).await?;
@ -111,22 +97,16 @@ pub(crate) async fn handler(
{
match parse_bool(&filter_state) {
Ok(filter_state) => {
let new_state =
if let Some(cur) = FIXER_STATE.lock().unwrap().get(&message.chat.id) {
FixerState {
medium: filter_state,
..*cur
}
} else {
FixerState {
medium: filter_state,
..Default::default()
}
};
FIXER_STATE
.lock()
.unwrap()
.insert(message.chat.id, new_state);
let new_state = if let Ok(ref mut map) = FIXER_STATE.try_lock()
&& let Some(state) = map.get_mut(&message.chat.id)
{
*state.medium(filter_state)
} else {
*FixerState::default().medium(filter_state)
};
if let Ok(ref mut map) = FIXER_STATE.try_lock() {
map.insert(message.chat.id, new_state);
}
}
Err(error_message) => {
bot.send_chat_message(message, error_message).await?;
@ -154,22 +134,16 @@ pub(crate) async fn handler(
{
match parse_bool(&filter_state) {
Ok(filter_state) => {
let new_state =
if let Some(cur) = FIXER_STATE.lock().unwrap().get(&message.chat.id) {
FixerState {
twitter: filter_state,
..*cur
}
} else {
FixerState {
twitter: filter_state,
..Default::default()
}
};
FIXER_STATE
.lock()
.unwrap()
.insert(message.chat.id, new_state);
let new_state = if let Ok(ref mut map) = FIXER_STATE.try_lock()
&& let Some(state) = map.get_mut(&message.chat.id)
{
*state.twitter(filter_state)
} else {
*FixerState::default().twitter(filter_state)
};
if let Ok(ref mut map) = FIXER_STATE.try_lock() {
map.insert(message.chat.id, new_state);
}
}
Err(error_message) => {
bot.send_chat_message(message, error_message).await?;
@ -191,22 +165,16 @@ pub(crate) async fn handler(
{
match parse_bool(&filter_state) {
Ok(filter_state) => {
let new_state =
if let Some(cur) = FIXER_STATE.lock().unwrap().get(&message.chat.id) {
FixerState {
youtube: filter_state,
..*cur
}
} else {
FixerState {
youtube: filter_state,
..Default::default()
}
};
FIXER_STATE
.lock()
.unwrap()
.insert(message.chat.id, new_state);
let new_state = if let Ok(ref mut map) = FIXER_STATE.try_lock()
&& let Some(state) = map.get_mut(&message.chat.id)
{
*state.youtube(filter_state)
} else {
*FixerState::default().youtube(filter_state)
};
if let Ok(ref mut map) = FIXER_STATE.try_lock() {
map.insert(message.chat.id, new_state);
}
}
Err(error_message) => {
bot.send_chat_message(message, error_message).await?;

41
src/fixer.rs Normal file
View File

@ -0,0 +1,41 @@
#[allow(clippy::struct_excessive_bools)] // Does not apply
#[derive(Clone, Copy, Debug)]
pub(crate) struct FixerState {
pub(crate) instagram: bool,
pub(crate) medium: bool,
pub(crate) twitter: bool,
pub(crate) youtube: bool,
}
impl Default for FixerState {
fn default() -> Self {
Self {
instagram: true,
medium: true,
twitter: true,
youtube: true,
}
}
}
impl FixerState {
pub(crate) fn instagram(&mut self, value: bool) -> &mut FixerState {
self.instagram = value;
self
}
pub(crate) fn medium(&mut self, value: bool) -> &mut FixerState {
self.medium = value;
self
}
pub(crate) fn twitter(&mut self, value: bool) -> &mut FixerState {
self.twitter = value;
self
}
pub(crate) fn youtube(&mut self, value: bool) -> &mut FixerState {
self.youtube = value;
self
}
}

View File

@ -1,6 +1,7 @@
#![feature(let_chains)]
mod commands;
mod deamp;
mod fixer;
#[cfg(feature = "ddinstagram")]
mod instagram;
mod logging;
@ -12,8 +13,8 @@ mod youtube;
use crate::commands::Command;
use crate::logging::TeloxideLogger;
use commands::FixerState;
use dotenvy::dotenv;
use fixer::FixerState;
use once_cell::sync::Lazy;
use std::{
collections::HashMap,