89 lines
2.3 KiB
Rust
89 lines
2.3 KiB
Rust
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
|
//
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
use derive_builder::Builder;
|
|
use derive_getters::Getters;
|
|
#[allow(unused_imports)]
|
|
use log::*;
|
|
#[allow(unused_imports)]
|
|
#[cfg(test)]
|
|
use println as info;
|
|
use reqwest::Client;
|
|
use serde::{Deserialize, Serialize};
|
|
use url::Url;
|
|
|
|
#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq, Getters, Builder)]
|
|
#[serde(rename_all = "PascalCase")]
|
|
pub struct MailPitAddress {
|
|
address: String,
|
|
name: String,
|
|
}
|
|
#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq, Getters, Builder)]
|
|
#[serde(rename_all = "PascalCase")]
|
|
pub struct MailPitEmail {
|
|
#[serde(rename = "ID")]
|
|
id: String,
|
|
from: MailPitAddress,
|
|
to: Vec<MailPitAddress>,
|
|
subject: String,
|
|
#[serde(rename = "Snippet")]
|
|
text: String,
|
|
html: Option<String>,
|
|
}
|
|
|
|
#[derive(Clone, Debug, Getters, Builder)]
|
|
pub struct MailPitHTTPClient {
|
|
#[builder(default = "Client::default()")]
|
|
client: Client,
|
|
url: Url,
|
|
}
|
|
|
|
impl MailPitHTTPClient {
|
|
pub async fn list_emails(&self) -> Vec<MailPitEmail> {
|
|
#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq)]
|
|
struct List {
|
|
messages: Vec<MailPitEmail>,
|
|
}
|
|
|
|
let mut u = self.url.clone();
|
|
u.set_path("/api/v1/messages");
|
|
|
|
info!("trying to fetch emails: {}", u.as_str());
|
|
|
|
let list: List = self
|
|
.client
|
|
.get(u.clone())
|
|
.send()
|
|
.await
|
|
.unwrap()
|
|
.json()
|
|
.await
|
|
.unwrap();
|
|
list.messages
|
|
}
|
|
|
|
pub async fn get_email_addressed_to(&self, email_address: &str) -> MailPitEmail {
|
|
self.list_emails()
|
|
.await
|
|
.drain(0..)
|
|
.find(|e| e.to.iter().any(|f| f.address == email_address))
|
|
.unwrap()
|
|
}
|
|
pub async fn delete_email(&self, email: MailPitEmail) {
|
|
#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq)]
|
|
struct DeletePayload {
|
|
#[serde(rename = "IDs")]
|
|
id: Vec<String>,
|
|
}
|
|
|
|
let mut u = self.url.clone();
|
|
u.set_path("/api/v1/messages/");
|
|
|
|
info!("Deleting email from: {:?}", email.from);
|
|
|
|
let payload = DeletePayload { id: vec![email.id] };
|
|
|
|
self.client.delete(u).json(&payload).send().await.unwrap();
|
|
}
|
|
}
|