From 53f58f25eea5d3b3b2b5a7f5c73f3bb19b5fa91c Mon Sep 17 00:00:00 2001 From: Awiteb Date: Wed, 29 May 2024 22:37:44 +0300 Subject: [PATCH] chore: Move the `PingList` trait to `src/traits.rs` Signed-off-by: Awiteb --- src/main.rs | 62 ++-------------------------------------- src/traits.rs | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 59 deletions(-) create mode 100644 src/traits.rs diff --git a/src/main.rs b/src/main.rs index 61910f5..d3008e6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -23,6 +23,9 @@ use salvo::{conn::TcpListener, Listener}; mod api; mod superbot; +mod traits; + +pub(crate) use traits::PingList; #[derive(Default, Clone)] pub(crate) struct PingedBot { @@ -31,65 +34,6 @@ pub(crate) struct PingedBot { is_response: bool, } -pub(crate) trait PingList { - fn clear_outdead(&self); - fn add_new(&self, telegram_id: u64); - fn check(&self, telegram_id: u64) -> bool; - fn new_res(&self, telegram_id: u64); -} - -impl PingList for Mutex> { - fn clear_outdead(&self) { - log::info!("Clear the dead pings"); - let dead_time = chrono::Utc::now().timestamp() - 60; - let mut bots = self - .lock() - .expect("Another holder paniced while holding the lock"); - *bots = bots - .iter() - .filter(|b| b.ping_in > dead_time) - .cloned() - .collect(); - } - - fn add_new(&self, telegram_id: u64) { - log::debug!("Adding new bot to the list: {telegram_id}"); - self.lock() - .expect("Another holder paniced while holding the lock") - .push(PingedBot::new(telegram_id)); - } - - fn check(&self, telegram_id: u64) -> bool { - log::debug!("Checking the {telegram_id} if is response"); - self.clear_outdead(); - let result = self - .lock() - .expect("Another holder paniced while holding the lock") - .iter() - .any(|b| b.telegram_id == telegram_id && b.is_response); - log::debug!("Response status: {result}"); - result - } - - fn new_res(&self, telegram_id: u64) { - log::debug!("New res from: {telegram_id}"); - let mut bots = self - .lock() - .expect("Another holder paniced while holding the lock"); - *bots = bots - .iter() - .cloned() - .map(|b| { - if b.telegram_id == telegram_id { - log::info!("Found the sender in the list"); - b.new_res() - } else { - b - } - }) - .collect(); - } -} impl PingedBot { pub(crate) fn new(telegram_id: u64) -> Self { diff --git a/src/traits.rs b/src/traits.rs new file mode 100644 index 0000000..5bf3c0f --- /dev/null +++ b/src/traits.rs @@ -0,0 +1,79 @@ +// A simple API to ping telegram bots and returns if it's online or not. +// Copyright (C) 2023-2024 Awiteb +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +use std::sync::Mutex; + +use crate::PingedBot; + +pub(crate) trait PingList { + fn clear_outdead(&self); + fn add_new(&self, telegram_id: u64); + fn check(&self, telegram_id: u64) -> bool; + fn new_res(&self, telegram_id: u64); +} + +impl PingList for Mutex> { + fn clear_outdead(&self) { + log::info!("Clear the dead pings"); + let dead_time = chrono::Utc::now().timestamp() - 60; + let mut bots = self + .lock() + .expect("Another holder paniced while holding the lock"); + *bots = bots + .iter() + .filter(|b| b.ping_in > dead_time) + .cloned() + .collect(); + } + + fn add_new(&self, telegram_id: u64) { + log::debug!("Adding new bot to the list: {telegram_id}"); + self.lock() + .expect("Another holder paniced while holding the lock") + .push(PingedBot::new(telegram_id)); + } + + fn check(&self, telegram_id: u64) -> bool { + log::debug!("Checking the {telegram_id} if is response"); + self.clear_outdead(); + let result = self + .lock() + .expect("Another holder paniced while holding the lock") + .iter() + .any(|b| b.telegram_id == telegram_id && b.is_response); + log::debug!("Response status: {result}"); + result + } + + fn new_res(&self, telegram_id: u64) { + log::debug!("New res from: {telegram_id}"); + let mut bots = self + .lock() + .expect("Another holder paniced while holding the lock"); + *bots = bots + .iter() + .cloned() + .map(|b| { + if b.telegram_id == telegram_id { + log::info!("Found the sender in the list"); + b.new_res() + } else { + b + } + }) + .collect(); + } +}