chore: Move the PingList trait to src/traits.rs

Signed-off-by: Awiteb <a@4rs.nl>
This commit is contained in:
Awiteb 2024-05-29 22:37:44 +03:00
parent cffada6b3b
commit 53f58f25ee
Signed by: awiteb
GPG key ID: 3F6B55640AA6682F
2 changed files with 82 additions and 59 deletions

View file

@ -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<Vec<PingedBot>> {
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 {

79
src/traits.rs Normal file
View file

@ -0,0 +1,79 @@
// A simple API to ping telegram bots and returns if it's online or not.
// Copyright (C) 2023-2024 Awiteb <a@4rs.nl>
//
// 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 <https://www.gnu.org/licenses/agpl-3.0>.
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<Vec<PingedBot>> {
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();
}
}