bazzar/crates/channels/src/emails.rs
2022-11-05 19:04:38 +01:00

98 lines
2.2 KiB
Rust

use bytes::Bytes;
use rumqttc::QoS;
use serde::de::DeserializeOwned;
use crate::{AsyncClient, DeserializePayload};
impl AsyncClient {
pub async fn emit_reset_password(&self, account: &reset_password::Input) {
self.publish_or_log(Topic::ResetPassword, QoS::AtLeastOnce, true, account)
.await;
}
pub async fn emit_test(&self, account: &model::Account) {
self.publish_or_log(Topic::Test, QoS::AtLeastOnce, false, account)
.await;
}
}
#[derive(Debug, thiserror::Error, serde::Serialize, serde::Deserialize)]
pub enum Error {}
pub static CLIENT_NAME: &str = "email-sender";
#[derive(Copy, Clone, Debug, PartialOrd, PartialEq, serde::Serialize, serde::Deserialize)]
pub enum Topic {
ResetPassword,
Test,
}
impl Topic {
pub fn to_str(self) -> &'static str {
match self {
Topic::ResetPassword => "emails/reset-password",
Topic::Test => "emails/test",
}
}
}
impl DeserializePayload for Topic {
fn deserialize_payload<T: DeserializeOwned>(self, bytes: Bytes) -> Option<T> {
bincode::deserialize(bytes.as_ref()).ok()
}
}
impl Into<String> for Topic {
fn into(self) -> String {
String::from(self.to_str())
}
}
impl<'s> PartialEq<&'s str> for Topic {
fn eq(&self, other: &&'s str) -> bool {
self.to_str() == *other
}
}
impl PartialEq<String> for Topic {
fn eq(&self, other: &String) -> bool {
self.to_str() == other.as_str()
}
}
pub mod test_mail {
#[derive(Debug)]
pub struct Input {
pub receiver: model::Email,
}
}
pub mod reset_password {
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct Input {
pub login: model::Login,
pub email: model::Email,
pub reset_token: model::ResetToken,
}
}
pub mod welcome {
#[derive(Debug)]
pub struct Input {
pub login: model::Login,
pub email: model::Email,
}
}
pub mod mqtt {
use config::SharedAppConfig;
use rumqttc::EventLoop;
use crate::emails::CLIENT_NAME;
use crate::AsyncClient;
pub fn create_client(config: SharedAppConfig) -> (AsyncClient, EventLoop) {
crate::mqtt::create_client(CLIENT_NAME, config.lock().email_sender().mqtt_addr())
}
}