Skip to content

Commit

Permalink
update discord webhook structs to be cleaner going forward and include
Browse files Browse the repository at this point in the history
beacon url in the initial message
  • Loading branch information
cbackas committed Oct 27, 2024
1 parent 8e5b74d commit 0999150
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 57 deletions.
7 changes: 4 additions & 3 deletions web_service/src/beacon_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ pub async fn process_beacon() {
return;
}

let beacon_data = match troy_status.beacon_url {
Some(beacon_url) => match strava::beacon::get_beacon_data(beacon_url).await {
let beacon_url = troy_status.beacon_url;
let beacon_data = match beacon_url {
Some(ref url) => match strava::beacon::get_beacon_data(url.to_string()).await {
Ok(data) => Some(data),
Err(e) => {
tracing::error!("Failed to get beacon data: {}", e);
Expand All @@ -36,7 +37,7 @@ pub async fn process_beacon() {
db_service::set_troy_status(true).await;
if !troy_status.is_on_trail {
tracing::info!("Troy status updated to on the trails");
discord::send_starting_webhook().await;
discord::send_starting_webhook(beacon_url).await;
}
}
Some(Status::Uploaded) => {
Expand Down
128 changes: 74 additions & 54 deletions web_service/src/discord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,35 @@ use crate::{
utils,
};

struct TOTTWebhook {
troy_status: bool,
struct OnTrailsNotification {
beacon_url: Option<String>,
}

impl From<OnTrailsNotification> for DiscordEmbed {
fn from(val: OnTrailsNotification) -> Self {
let mut embed: DiscordEmbed = DiscordEmbed::default();
embed.title("Troy is no longer on the trails!");
if let Some(beacon_url) = &val.beacon_url {
embed.description("Troy has started a new activity").field(
"Beacon URL",
beacon_url,
false,
);
}
embed
}
}

impl From<OnTrailsNotification> for DiscordMessage {
fn from(val: OnTrailsNotification) -> Self {
DiscordMessage {
embed: Some(val.into()),
..Default::default()
}
}
}

struct OffTrailsNotification {
webhook_data: Option<WebhookData>,
}

Expand All @@ -22,56 +49,57 @@ struct WebhookData {
}
struct WebhookImage(Vec<u8>);

impl From<TOTTWebhook> for DiscordEmbed {
fn from(val: TOTTWebhook) -> Self {
impl From<OffTrailsNotification> for DiscordEmbed {
fn from(val: OffTrailsNotification) -> Self {
let mut embed: DiscordEmbed = DiscordEmbed::default();

embed.title(match val.troy_status {
true => "Troy is on the trails!",
false => "Troy is no longer on the trails!",
});
embed.title("Troy is no longer on the trails!");

if let Some(webhook_data) = &val.webhook_data {
if let Some(image) = &webhook_data.image {
embed.image(EmbedImage::Bytes(ByteImageSource {
bytes: image.0.clone(),
file_name: "map_background.png".to_string(),
}));
tracing::debug!("Image found");
return embed;
} else {
tracing::debug!("No image found");
}
let webhook_data = &val.webhook_data;
if webhook_data.is_none() {
return embed;
}
let webhook_data = webhook_data.as_ref().unwrap();

if let Some(image) = &webhook_data.image {
embed.image(EmbedImage::Bytes(ByteImageSource {
bytes: image.0.clone(),
file_name: "map_background.png".to_string(),
}));
tracing::debug!("Image found");
return embed;
} else {
tracing::debug!("No image found");
}

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

embed
.field("Distance", &format!("{}mi", &webhook_data.distance), true)
.field(
"Elevation Gain",
&format!("{}ft", &webhook_data.total_elevation_gain),
true,
)
.field(
"Average Speed",
&format!("{}mph", &webhook_data.average_speed),
true,
)
.field(
"Top Speed",
&format!("{}mph", &webhook_data.max_speed),
true,
);
};
embed
.field("Distance", &format!("{}mi", &webhook_data.distance), true)
.field(
"Elevation Gain",
&format!("{}ft", &webhook_data.total_elevation_gain),
true,
)
.field(
"Average Speed",
&format!("{}mph", &webhook_data.average_speed),
true,
)
.field(
"Top Speed",
&format!("{}mph", &webhook_data.max_speed),
true,
);

embed
}
}

impl From<TOTTWebhook> for DiscordMessage {
fn from(val: TOTTWebhook) -> Self {
impl From<OffTrailsNotification> for DiscordMessage {
fn from(val: OffTrailsNotification) -> Self {
DiscordMessage {
embed: Some(val.into()),
..Default::default()
Expand Down Expand Up @@ -314,12 +342,8 @@ async fn send_webhook(message: impl Into<DiscordMessage>) {
}
}

pub async fn send_starting_webhook() {
send_webhook(TOTTWebhook {
troy_status: true,
webhook_data: None,
})
.await;
pub async fn send_starting_webhook(beacon_url: Option<String>) {
send_webhook(OnTrailsNotification { beacon_url }).await;
}

pub async fn send_end_webhook(activity_id: Option<i64>) {
Expand All @@ -333,7 +357,7 @@ pub async fn send_end_webhook(activity_id: Option<i64>) {
},
None => None,
};
let strava_stats: Option<WebhookData> = {
let webhook_data: Option<WebhookData> = {
match activity {
None => {
tracing::error!("No last activity found");
Expand Down Expand Up @@ -398,11 +422,7 @@ pub async fn send_end_webhook(activity_id: Option<i64>) {
}
};

send_webhook(TOTTWebhook {
troy_status: false,
webhook_data: strava_stats,
})
.await;
send_webhook(OffTrailsNotification { webhook_data }).await;
}

async fn get_map_image(map_service_url: String, url_params: URLParams) -> anyhow::Result<Vec<u8>> {
Expand Down

1 comment on commit 0999150

@cbackas
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.