Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include ride name in notifications #15

Merged
merged 3 commits into from
Feb 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/db_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ impl DbService {
let result = db.connect()?.execute(statement, params).await;

if result.is_err() {
return Err(result.unwrap_err().into());
return Err(result.err().unwrap().into());
}

let result = result.unwrap();
Expand Down
32 changes: 22 additions & 10 deletions src/route_handlers/webhooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub struct WebhookRequest {
}

struct WebhookData {
name: Option<String>,
distance: f64,
total_elevation_gain: f64,
average_speed: f64,
Expand Down Expand Up @@ -50,12 +51,6 @@ pub async fn handler(Json(payload): Json<WebhookRequest>) -> impl axum::response
}

async fn send_discord_webhook(is_on_the_trails: bool) {
let content = if is_on_the_trails {
"Troy is on the trails!"
} else {
"Troy is no longer on the trails!"
};

let strava_stats: Option<WebhookData> = match is_on_the_trails {
true => None,
false => {
Expand All @@ -75,12 +70,20 @@ async fn send_discord_webhook(is_on_the_trails: bool) {
}

Some(last_activity) => {
let name = match last_activity.name.clone().as_str() {
"Afternoon Mountain Bike Ride" => None,
"Morning Mountain Bike Ride" => None,
"Evening Mountain Bike Ride" => None,
"Lunch Mountain Bike Ride" => None,
_ => Some(last_activity.name),
};
let distance = meters_to_miles(last_activity.distance, false);
let total_elevation_gain =
meters_to_feet(last_activity.total_elevation_gain, true);
let average_speed = mps_to_miph(last_activity.average_speed, false);
let max_speed = mps_to_miph(last_activity.max_speed, false);
Some(WebhookData {
name,
distance,
total_elevation_gain,
average_speed,
Expand All @@ -105,12 +108,21 @@ async fn send_discord_webhook(is_on_the_trails: bool) {
let message = &mut Message::new();
message.username("TOTT").avatar_url(avatar_url);
message.embed(|embed| {
embed.title(content).footer(
"Powered by troyonthetrails.com",
Some(avatar_url.to_string()),
);
embed
.title(match is_on_the_trails {
true => "Troy is on the trails!",
false => "Troy is no longer on the trails!",
})
.footer(
"Powered by troyonthetrails.com",
Some(avatar_url.to_string()),
);

if let Some(webhook_data) = &strava_stats {
if let Some(name) = &webhook_data.name {
embed.description(name);
}

embed
.field("Distance", &format!("{}mi", &webhook_data.distance), true)
.field(
Expand Down
Loading