From 9002103c7bbfa962fec099df1eee99e2f1fbc85b Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Fri, 14 Jun 2024 01:32:23 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AF=95=E8=AF=95=E6=96=B0=E7=9A=84=E5=85=B1?= =?UTF-8?q?=E4=BA=AB=E6=96=B9=E5=BC=8F=EF=BC=8C=E8=99=BD=E7=84=B6=E7=95=A5?= =?UTF-8?q?=E5=BE=AE=E9=BA=BB=E7=83=A6=EF=BC=8C=E4=BD=86=E8=87=B3=E5=B0=91?= =?UTF-8?q?=E6=AF=94=E4=B9=8B=E5=89=8D=E5=85=A8=E5=B1=80=E5=85=B1=E4=BA=AB?= =?UTF-8?q?=E5=A5=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ica-rs/src/data_struct/tailchat/status.rs | 31 +++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/ica-rs/src/data_struct/tailchat/status.rs b/ica-rs/src/data_struct/tailchat/status.rs index 8041faa..8bd0ad9 100644 --- a/ica-rs/src/data_struct/tailchat/status.rs +++ b/ica-rs/src/data_struct/tailchat/status.rs @@ -1,4 +1,7 @@ +use std::sync::Arc; + use serde::{Deserialize, Serialize}; +use tokio::sync::RwLock; use crate::data_struct::tailchat::UserId; @@ -29,3 +32,31 @@ pub struct UpdateDMConverse { #[serde(rename = "updatedAt")] pub updated_at: String, } + +pub type Writeable = Arc>; + +#[derive(Debug, Clone)] +pub struct BotStatus { + user_id: Writeable, +} + +impl BotStatus { + pub fn new(user_id: UserId) -> Self { + Self { + user_id: Arc::new(RwLock::new(user_id)), + } + } + + pub async fn get_user_id(&self) -> UserId { self.user_id.read().await.clone() } + + pub fn block_get_user_id(&self) -> UserId { + tokio::task::block_in_place(|| { + let rt = tokio::runtime::Runtime::new().unwrap(); + rt.block_on(self.get_user_id()) + }) + } + + pub async fn set_user_id(&self, user_id: UserId) { + self.user_id.write().await.clone_from(&user_id); + } +}