remove captcha

This commit is contained in:
Aravinth Manivannan 2021-07-20 12:36:42 +05:30
parent 22f1a4ccc0
commit 6eac51de10
Signed by: realaravinth
GPG Key ID: AD9F0F08E855ED88
3 changed files with 41 additions and 17 deletions

View File

@ -27,7 +27,7 @@ use tokio::sync::oneshot::channel;
use super::counter::Counter; use super::counter::Counter;
use crate::errors::*; use crate::errors::*;
use crate::master::messages::{AddSite, AddVisitor, Rename}; use crate::master::messages::{AddSite, AddVisitor, RemoveCaptcha, Rename};
use crate::master::Master as MasterTrait; use crate::master::Master as MasterTrait;
/// This Actor manages the [Counter] actors. /// This Actor manages the [Counter] actors.
@ -127,14 +127,10 @@ impl Handler<AddVisitor> for Master {
impl Handler<Rename> for Master { impl Handler<Rename> for Master {
type Result = MessageResult<Rename>; type Result = MessageResult<Rename>;
fn handle(&mut self, m: Rename, ctx: &mut Self::Context) -> Self::Result { fn handle(&mut self, m: Rename, _ctx: &mut Self::Context) -> Self::Result {
self.rename(m); self.rename(m);
let (tx, rx) = channel(); let (tx, rx) = channel();
let fut = async move { let _ = tx.send(Ok(()));
let _ = tx.send(Ok(()));
}
.into_actor(self);
ctx.spawn(fut);
MessageResult(rx) MessageResult(rx)
} }
} }
@ -176,7 +172,7 @@ impl Handler<CleanUp> for Master {
println!("{}", visitor_count); println!("{}", visitor_count);
if visitor_count == 0 && new.is_some() { if visitor_count == 0 && new.is_some() {
addr.send(Stop).await.unwrap(); addr.send(Stop).await.unwrap();
master.send(RemoveSite(id.to_owned())).await.unwrap(); master.send(RemoveCaptcha(id.to_owned())).await.unwrap();
println!("cleaned up"); println!("cleaned up");
} }
} }
@ -191,16 +187,14 @@ impl Handler<CleanUp> for Master {
} }
} }
/// Message to delete [Counter] actor impl Handler<RemoveCaptcha> for Master {
#[derive(Message)] type Result = MessageResult<RemoveCaptcha>;
#[rtype(result = "()")]
pub struct RemoveSite(pub String);
impl Handler<RemoveSite> for Master { fn handle(&mut self, m: RemoveCaptcha, _ctx: &mut Self::Context) -> Self::Result {
type Result = (); let (tx, rx) = channel();
fn handle(&mut self, m: RemoveSite, _ctx: &mut Self::Context) -> Self::Result {
self.rm_site(&m.0); self.rm_site(&m.0);
tx.send(Ok(())).unwrap();
MessageResult(rx)
} }
} }
@ -266,5 +260,7 @@ mod tests {
let mcaptcha_addr = addr.send(GetSite(new_id.into())).await.unwrap(); let mcaptcha_addr = addr.send(GetSite(new_id.into())).await.unwrap();
assert_eq!(mcaptcha_addr, None); assert_eq!(mcaptcha_addr, None);
assert!(addr.send(RemoveCaptcha(new_id.into())).await.is_ok());
} }
} }

View File

@ -32,6 +32,7 @@ pub trait Master:
+ actix::Handler<messages::AddVisitor> + actix::Handler<messages::AddVisitor>
+ actix::Handler<messages::AddSite> + actix::Handler<messages::AddSite>
+ actix::Handler<messages::Rename> + actix::Handler<messages::Rename>
+ actix::Handler<messages::RemoveCaptcha>
{ {
} }
@ -87,10 +88,16 @@ pub mod messages {
pub mcaptcha: MCaptcha, pub mcaptcha: MCaptcha,
} }
/// Message to rename an MCaptcha actor
#[derive(Message, Builder)] #[derive(Message, Builder)]
#[rtype(result = "Receiver<CaptchaResult<()>>")] #[rtype(result = "Receiver<CaptchaResult<()>>")]
pub struct Rename { pub struct Rename {
pub name: String, pub name: String,
pub rename_to: String, pub rename_to: String,
} }
/// Message to delete [Counter] actor
#[derive(Message)]
#[rtype(result = "Receiver<CaptchaResult<()>>")]
pub struct RemoveCaptcha(pub String);
} }

View File

@ -19,7 +19,7 @@ use actix::dev::*;
use tokio::sync::oneshot; use tokio::sync::oneshot;
use crate::errors::*; use crate::errors::*;
use crate::master::messages::{AddSite, AddVisitor, Rename}; use crate::master::messages::{AddSite, AddVisitor, RemoveCaptcha, Rename};
use crate::master::Master as MasterTrait; use crate::master::Master as MasterTrait;
use crate::redis::mcaptcha_redis::MCaptchaRedis; use crate::redis::mcaptcha_redis::MCaptchaRedis;
use crate::redis::RedisConfig; use crate::redis::RedisConfig;
@ -93,6 +93,23 @@ impl Handler<Rename> for Master {
} }
} }
impl Handler<RemoveCaptcha> for Master {
type Result = MessageResult<RemoveCaptcha>;
fn handle(&mut self, m: RemoveCaptcha, ctx: &mut Self::Context) -> Self::Result {
let (tx, rx) = oneshot::channel();
let con = self.redis.get_client();
let fut = async move {
let res = con.delete_captcha(&m.0).await;
let _ = tx.send(res);
}
.into_actor(self);
ctx.wait(fut);
MessageResult(rx)
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
@ -150,6 +167,10 @@ mod tests {
.build() .build()
.unwrap(); .unwrap();
assert!(addr.send(rename).await.is_ok()); assert!(addr.send(rename).await.is_ok());
assert!(addr
.send(RemoveCaptcha(RENAME_CAPTCHA_NAME.into()))
.await
.is_ok());
} }
#[actix_rt::test] #[actix_rt::test]