From 72d1554f3356475b84b4c8617fbdb6d70653bb28 Mon Sep 17 00:00:00 2001 From: itsusinn Date: Sat, 30 Oct 2021 21:13:05 +0800 Subject: [PATCH] refactor: helper functions of Database --- src/ext.rs | 104 ++++++++++++++++++++++++++++++++ src/main.rs | 1 + src/message/handlers/receive.rs | 2 +- src/message/handlers/send.rs | 2 +- 4 files changed, 107 insertions(+), 2 deletions(-) create mode 100644 src/ext.rs diff --git a/src/ext.rs b/src/ext.rs new file mode 100644 index 0000000..a2a7cb4 --- /dev/null +++ b/src/ext.rs @@ -0,0 +1,104 @@ +use std::{convert::TryInto, ops::Deref}; + +use mesagisto_client::{OkExt, OptionExt, db::DB as INNER}; + + +#[derive(Singleton, Default)] +pub struct Db{} +impl Db { + #[inline] + pub fn put_msg_id_0(&self, target: &i64, uid: &i32, id: &i32) -> anyhow::Result<()> { + INNER.put_msg_id( + target.to_be_bytes().to_vec(), + uid.to_be_bytes().to_vec(), + id.to_be_bytes().to_vec(), + true, + ) + } + // no reverse + #[inline] + pub fn put_msg_id_ir_0(&self, target: &i64, uid: &i32, id: &i32) -> anyhow::Result<()> { + INNER.put_msg_id( + target.to_be_bytes().to_vec(), + uid.to_be_bytes().to_vec(), + id.to_be_bytes().to_vec(), + false, + ) + } + #[inline] + pub fn put_msg_id_1(&self, target: &i64, uid: &Vec, id: &i32) -> anyhow::Result<()> { + INNER.put_msg_id( + target.to_be_bytes().to_vec(), + uid.clone(), + id.to_be_bytes().to_vec(), + true, + ) + } + #[inline] + pub fn put_msg_id_ir_1(&self, target: &i64, uid: &Vec, id: &i32) -> anyhow::Result<()> { + INNER.put_msg_id( + target.to_be_bytes().to_vec(), + uid.clone(), + id.to_be_bytes().to_vec(), + false, + ) + } + #[inline] + pub fn put_msg_id_2(&self, target: &i64, uid: &i32, id: &Vec) -> anyhow::Result<()> { + INNER.put_msg_id( + target.to_be_bytes().to_vec(), + uid.to_be_bytes().to_vec(), + id.clone(), + true, + ) + } + #[inline] + pub fn put_msg_id_ir_2(&self, target: &i64, uid: &i32, id: &Vec) -> anyhow::Result<()> { + INNER.put_msg_id( + target.to_be_bytes().to_vec(), + uid.to_be_bytes().to_vec(), + id.clone(), + false, + ) + } + #[inline] + pub fn put_msg_id_3(&self, target: &u64, uid: &u64, id: &Vec) -> anyhow::Result<()> { + INNER.put_msg_id( + target.to_be_bytes().to_vec(), + uid.to_be_bytes().to_vec(), + id.clone(), + true, + ) + } + #[inline] + pub fn put_msg_id_ir_3(&self, target: &u64, uid: &u64, id: &Vec) -> anyhow::Result<()> { + INNER.put_msg_id( + target.to_be_bytes().to_vec(), + uid.to_be_bytes().to_vec(), + id.clone(), + false, + ) + } + #[inline] + pub fn get_msg_id_1(&self, target: &i64, id: &Vec) -> anyhow::Result> { + let be_bytes = match INNER.get_msg_id(&target.to_be_bytes().to_vec(), id)? { + Some(v) => match v.len() { + 4 => v, + _ => return Ok(None), + }, + None => return Ok(None), + }; + i32::from_be_bytes(be_bytes.try_into().unwrap()).some().ok() + } + #[inline] + pub fn get_msg_id_2(&self, target: &i64, id: &Vec) -> anyhow::Result>> { + INNER.get_msg_id(&target.to_be_bytes().to_vec(), id) + } +} +impl Deref for Db { + type Target = mesagisto_client::db::Db; + + fn deref(&self) -> &Self::Target { + &*INNER + } +} diff --git a/src/main.rs b/src/main.rs index 221c02d..a62505f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,6 +25,7 @@ mod despatch; mod message; mod net; mod webhook; +pub mod ext; fn main() { std::env::set_var("RUST_BACKTRACE", "1"); diff --git a/src/message/handlers/receive.rs b/src/message/handlers/receive.rs index 7d282a8..dfd08e6 100644 --- a/src/message/handlers/receive.rs +++ b/src/message/handlers/receive.rs @@ -1,9 +1,9 @@ use crate::CONFIG; use crate::TG_BOT; +use crate::ext::DB; use mesagisto_client::{ cache::CACHE, data::{message::MessageType, message::Message,Packet}, - db::DB }; use teloxide::payloads::SendMessageSetters; use teloxide::prelude::Requester; diff --git a/src/message/handlers/send.rs b/src/message/handlers/send.rs index f8d33f1..4a93cd9 100644 --- a/src/message/handlers/send.rs +++ b/src/message/handlers/send.rs @@ -2,13 +2,13 @@ use crate::config::CONFIG; use crate::bot::TG_BOT; use crate::message::handlers::receive::receive_from_server; use crate::message::Cx; +use crate::ext::DB; use arcstr::ArcStr; use mesagisto_client::EitherExt; use mesagisto_client::data::message::{MessageType, Profile}; use mesagisto_client::data::{message, Packet}; use mesagisto_client::server::SERVER; use mesagisto_client::res::RES; -use mesagisto_client::db::DB; use std::sync::Arc; use teloxide::prelude::*;