feat: allow unsubscribing

This commit is contained in:
Patrick 2024-07-18 01:03:05 +02:00
parent af7f2b3f55
commit e7125bb7b2
Signed by: patrick
GPG key ID: 451F95EFB8BECD0F
5 changed files with 53 additions and 7 deletions

7
Cargo.lock generated
View file

@ -1749,6 +1749,7 @@ dependencies = [
"serde_json",
"surf",
"tide",
"urlencoding",
]
[[package]]
@ -2591,6 +2592,12 @@ dependencies = [
"serde",
]
[[package]]
name = "urlencoding"
version = "2.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "daf8dba3b7eb870caf1ddeed7bc9d2a049f3cfdfae7cb521b087cc33ae4c49da"
[[package]]
name = "utf8parse"
version = "0.2.2"

View file

@ -24,6 +24,7 @@ futures-util = "0.3.12"
lettre = "0.11.7"
anyhow = "1.0.86"
clap = { version = "4.5.9", features = ["derive"] }
urlencoding = "2.1.3"
[dependencies.async-std]
version = "*" # Use whatever tide uses.

View file

@ -6,6 +6,9 @@ use lettre::message::Mailbox;
use lettre::transport::smtp::authentication::Credentials;
use lettre::{Message, SmtpTransport, Transport};
use std::env;
use urlencoding::encode;
use crate::CONFIG;
pub fn send_notification(
recipient: &str,
@ -25,6 +28,12 @@ pub fn send_notification(
body += "This is the last update you will get for this pr.\n\
Thx for using this service\n\
Goodbye";
} else {
body += &format!(
"<a href=\"{}/unsubscribe?pr={pr_number}&email={}\">Unsubscribe from this PR</a>",
&CONFIG.url,
encode(recipient)
);
}
let sending_address = env::var("PR_TRACKER_MAIL_ADDRESS")?;
let sending_user = match env::var("PR_TRACKER_MAIL_USER") {

View file

@ -10,7 +10,7 @@ mod systemd;
mod tree;
use std::collections::HashSet;
use std::fs::{remove_dir_all, File};
use std::fs::{remove_dir_all, remove_file, File};
use std::io::BufReader;
use std::path::PathBuf;
use std::{ffi::OsString, fs::read_dir};
@ -40,7 +40,7 @@ use tree::Tree;
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Config {
pub struct Config {
#[arg(long)]
path: PathBuf,
@ -61,9 +61,12 @@ struct Config {
#[arg(long)]
email_white_list: Option<PathBuf>,
#[arg(long)]
url: String,
}
static CONFIG: Lazy<Config> = Lazy::new(Config::parse);
pub static CONFIG: Lazy<Config> = Lazy::new(Config::parse);
static WHITE_LIST: Lazy<HashSet<String>> = Lazy::new(|| {
use std::io::{BufRead, BufReader};
@ -225,6 +228,31 @@ async fn update_subscribers<S>(_request: Request<S>) -> http_types::Result<Respo
.build())
}
async fn unsubscribe<S>(request: Request<S>) -> http_types::Result<Response> {
let pr_number = request.query::<Query>()?.pr;
let email = request.query::<Query>()?.email;
if let Some(email) = email {
for f in read_dir(CONFIG.data_folder.clone())? {
let dir_path = f?.path();
let dir_name = dir_path.file_name().and_then(|x| x.to_str()).unwrap();
if dir_path.is_dir()
&& (pr_number.is_none() || pr_number.as_ref().is_some_and(|x| x == dir_name))
{
match remove_file(dir_path.join(&email)) {
Ok(_) => {}
Err(_) => {}
}
}
}
}
Ok(Response::builder(200)
.content_type(mime::HTML)
.body("")
.build())
}
async fn handle_request<S>(request: Request<S>) -> http_types::Result<Response> {
let mut status = 200;
let mut page = PageTemplate {
@ -290,6 +318,7 @@ async fn main() {
root.at("/").get(handle_request);
root.at("update").get(update_subscribers);
root.at("unsubscribe").get(unsubscribe);
let fd_count = handle_error(listen_fds(true), 71, "sd_listen_fds");

View file

@ -172,17 +172,17 @@
{%- if subscribed -%}
<div class="state-subscribed">You will be notified be by mail when this PR reaches a new branch</div>
{%- endif -%}
<a href="/">Back to home</a>
<form>
<label for="pr">PR number: </label>
<input id="pr" name="pr" type="text" pattern="[1-9][0-9]*" value="{%- match pr_number -%}
{%- when Some with (pr_number) -%}
{{- pr_number -}}
{%- else -%}
{%- endmatch -%}">
{{- pr_number -}}" readonly=readonly {%- else -%} "
{%- endmatch -%}>
{% match pr_number %}
{%- when Some with (pr_number) -%}
<br>
<label for="email">Email: </label>
<label for=" email">Email: </label>
<input id="email" name="email" type="email" value="{%- match email -%}
{%- when Some with (email) -%}
{{- email -}}