Skip to content

Commit

Permalink
feat(web-scraping): notify users about failed attempts to check chang…
Browse files Browse the repository at this point in the history
…es in resources or content
  • Loading branch information
azasypkin committed Nov 19, 2023
1 parent 64c73b3 commit 473191e
Show file tree
Hide file tree
Showing 17 changed files with 863 additions and 159 deletions.
14 changes: 14 additions & 0 deletions assets/templates/tracker_styles.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<style>
.navigate-link {
display: block;
width: 250px;
margin: auto;
padding: 10px 20px;
text-align: center;
text-decoration: none;
color: #5e1d3f;
background-color: #fed047;
border-radius: 5px;
font-weight: bold;
}
</style>
19 changes: 3 additions & 16 deletions assets/templates/web_page_content_tracker_changes_email.hbs
Original file line number Diff line number Diff line change
@@ -1,28 +1,15 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>"{{tracker_name}}" content tracker detected changes</title>
<title>"{{tracker_name}}" tracker detected content changes</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
{{> email_styles}}
<style>
.navigate-link {
display: block;
width: 250px;
margin: auto;
padding: 10px 20px;
text-align: center;
text-decoration: none;
color: #5e1d3f;
background-color: #fed047;
border-radius: 5px;
font-weight: bold;
}
</style>
{{> tracker_styles}}
</head>
<body>
<div class="container">
<h1>"{{tracker_name}}" content tracker detected changes</h1>
<h1>"{{tracker_name}}" tracker detected content changes</h1>
<p>To learn more, visit the <b>Content trackers</b> page:</p>
<a class="navigate-link" href="{{back_link}}">Web Scraping → Content trackers</a>
<p>If the button above doesn't work, you can navigate to the following URL directly: </p>
Expand Down
21 changes: 21 additions & 0 deletions assets/templates/web_page_content_tracker_changes_error_email.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>"{{tracker_name}}" tracker failed to check for content changes</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
{{> email_styles}}
{{> tracker_styles}}
</head>
<body>
<div class="container">
<h1>"{{tracker_name}}" tracker failed to check for content changes</h1>
<p>There was an error while checking content: <b>{{error_message}}</b>.</p>
<p>To check the tracker configuration and re-try, visit the <b>Content trackers</b> page:</p>
<a class="navigate-link" href="{{back_link}}">Web Scraping → Content trackers</a>
<p>If the button above doesn't work, you can navigate to the following URL directly: </p>
<p>{{back_link}}</p>
<a href="{{home_link}}"><img src="cid:secutils-logo" alt="Secutils.dev logo" width="89" height="14" /></a>
</div>
</body>
</html>
19 changes: 3 additions & 16 deletions assets/templates/web_page_resources_tracker_changes_email.hbs
Original file line number Diff line number Diff line change
@@ -1,28 +1,15 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>"{{tracker_name}}" resources tracker detected {{changes_count}} changes</title>
<title>"{{tracker_name}}" tracker detected {{changes_count}} changes in resources</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
{{> email_styles}}
<style>
.navigate-link {
display: block;
width: 250px;
margin: auto;
padding: 10px 20px;
text-align: center;
text-decoration: none;
color: #5e1d3f;
background-color: #fed047;
border-radius: 5px;
font-weight: bold;
}
</style>
{{> tracker_styles}}
</head>
<body>
<div class="container">
<h1>"{{tracker_name}}" resources tracker detected {{changes_count}} changes</h1>
<h1>"{{tracker_name}}" tracker detected {{changes_count}} changes in resources</h1>
<p>To learn more, visit the <b>Resources trackers</b> page:</p>
<a class="navigate-link" href="{{back_link}}">Web Scraping → Resources trackers</a>
<p>If the button above doesn't work, you can navigate to the following URL directly: </p>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>"{{tracker_name}}" tracker failed to check for changes in resources</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
{{> email_styles}}
{{> tracker_styles}}
</head>
<body>
<div class="container">
<h1>"{{tracker_name}}" tracker failed to check for changes in resources</h1>
<p>There was an error while checking resources: <b>{{error_message}}</b>.</p>
<p>To check the tracker configuration and re-try, visit the <b>Resources trackers</b> page:</p>
<a class="navigate-link" href="{{back_link}}">Web Scraping → Resources trackers</a>
<p>If the button above doesn't work, you can navigate to the following URL directly: </p>
<p>{{back_link}}</p>
<a href="{{home_link}}"><img src="cid:secutils-logo" alt="Secutils.dev logo" width="89" height="14" /></a>
</div>
</body>
</html>
137 changes: 129 additions & 8 deletions src/notifications/notification_content_template.rs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,49 @@ pub const NOTIFICATION_LOGO_BYTES: &[u8] =
pub async fn compile_to_email<DR: DnsResolver, ET: EmailTransport>(
api: &Api<DR, ET>,
tracker_name: &str,
error_message: Option<&str>,
) -> anyhow::Result<EmailNotificationContent> {
let back_link = format!("{}ws/web_scraping__content", api.config.public_url);
Ok(EmailNotificationContent::html_with_attachments(
format!(
"Notification: \"{}\" content tracker detected changes",
tracker_name
),
format!(
"\"{}\" content tracker detected changes. Visit {} to learn more.",
tracker_name, back_link
),
api.templates.render(
"web_page_content_tracker_changes_email",
&json!({

let (subject, text, html) = if let Some(error_message) = error_message {
(
format!("[Secutils.dev] Check failed: \"{}\"", tracker_name),
format!(
"\"{}\" tracker failed to check for content changes due to the following error: {error_message}. Visit {} to learn more.",
tracker_name, back_link
),
api.templates.render(
"web_page_content_tracker_changes_error_email",
&json!({
"tracker_name": tracker_name,
"error_message": error_message,
"back_link": back_link,
"home_link": api.config.public_url.as_str(),
}),
)?,
)?
)
} else {
(
format!("[Secutils.dev] Change detected: \"{}\"", tracker_name),
format!(
"\"{}\" tracker detected content changes. Visit {} to learn more.",
tracker_name, back_link
),
api.templates.render(
"web_page_content_tracker_changes_email",
&json!({
"tracker_name": tracker_name,
"back_link": back_link,
"home_link": api.config.public_url.as_str(),
}),
)?,
)
};

Ok(EmailNotificationContent::html_with_attachments(
subject,
text,
html,
vec![EmailNotificationAttachment::inline(
"secutils-logo",
"image/png",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,50 @@ pub async fn compile_to_email<DR: DnsResolver, ET: EmailTransport>(
api: &Api<DR, ET>,
tracker_name: &str,
changes_count: usize,
error_message: Option<&str>,
) -> anyhow::Result<EmailNotificationContent> {
let back_link = format!("{}ws/web_scraping__resources", api.config.public_url);

let (subject, text, html) = if let Some(error_message) = error_message {
(
format!("[Secutils.dev] Check failed: \"{}\"", tracker_name),
format!(
"\"{}\" tracker failed to check for changes in resources due to the following error: {error_message}. Visit {} to learn more.",
tracker_name, back_link
),
api.templates.render(
"web_page_resources_tracker_changes_error_email",
&json!({
"tracker_name": tracker_name,
"error_message": error_message,
"back_link": back_link,
"home_link": api.config.public_url.as_str(),
}),
)?
)
} else {
(
format!("[Secutils.dev] Change detected: \"{}\"", tracker_name),
format!(
"\"{}\" tracker detected {} changes in resources. Visit {} to learn more.",
tracker_name, changes_count, back_link
),
api.templates.render(
"web_page_resources_tracker_changes_email",
&json!({
"tracker_name": tracker_name,
"changes_count": changes_count,
"back_link": back_link,
"home_link": api.config.public_url.as_str(),
}),
)?,
)
};

Ok(EmailNotificationContent::html_with_attachments(
format!(
"Notification: \"{}\" resources tracker detected {} changes",
tracker_name, changes_count
),
format!(
"\"{}\" resources tracker detected {} changes. Visit {} to learn more.",
tracker_name, changes_count, back_link
),
api.templates.render(
"web_page_resources_tracker_changes_email",
&json!({
"tracker_name": tracker_name,
"changes_count": changes_count,
"back_link": back_link,
"home_link": api.config.public_url.as_str(),
}),
)?,
subject,
text,
html,
vec![EmailNotificationAttachment::inline(
"secutils-logo",
"image/png",
Expand Down
Loading

0 comments on commit 473191e

Please sign in to comment.