lemmy/crates/api_common/src/websocket/handlers/captcha.rs
Dessalines 63f54a3103
Making the chat server an actor. (#2793)
* Making the chat server an actor.

- Fixes #2778
- #2787

* Forgot to add handlers folder.

* Some cleanup.

* Forgot to remove a comment.

* Address PR comments.

* Using ToString for enum operations.
2023-04-13 06:53:55 -04:00

46 lines
1.1 KiB
Rust

use crate::websocket::{chat_server::ChatServer, structs::CaptchaItem};
use actix::{Context, Handler, Message};
use lemmy_db_schema::utils::naive_now;
/// Adding a Captcha
#[derive(Message)]
#[rtype(result = "()")]
pub struct AddCaptcha {
pub captcha: CaptchaItem,
}
impl Handler<AddCaptcha> for ChatServer {
type Result = ();
fn handle(&mut self, msg: AddCaptcha, _: &mut Context<Self>) -> Self::Result {
self.captchas.push(msg.captcha);
}
}
/// Checking a Captcha
#[derive(Message)]
#[rtype(bool)]
pub struct CheckCaptcha {
pub uuid: String,
pub answer: String,
}
impl Handler<CheckCaptcha> for ChatServer {
type Result = bool;
fn handle(&mut self, msg: CheckCaptcha, _: &mut Context<Self>) -> Self::Result {
// Remove all the ones that are past the expire time
self.captchas.retain(|x| x.expires.gt(&naive_now()));
let check = self
.captchas
.iter()
.any(|r| r.uuid == msg.uuid && r.answer.to_lowercase() == msg.answer.to_lowercase());
// Remove this uuid so it can't be re-checked (Checks only work once)
self.captchas.retain(|x| x.uuid != msg.uuid);
check
}
}