/ src / tasks / unlock_announcements.rs
unlock_announcements.rs
 1  use std::sync::Arc;
 2  
 3  use matrix_sdk::RoomState;
 4  use tracing::{error, info, warn};
 5  
 6  use crate::{
 7      aoc::day::AocDay, context::Context, matrix::utils::message, utils::datetime::sleep_until,
 8  };
 9  
10  pub async fn start(context: Arc<Context>) -> ! {
11      loop {
12          let next = AocDay::next();
13          let datetime = next.unlock_datetime();
14          info!(?next, ?datetime, "waiting until next unlock");
15          sleep_until(datetime).await;
16          info!(?next, ?datetime, "new puzzles unlocked");
17          if let Err(err) = trigger(&context, next).await {
18              error!("Failed to send unlock announcement: {err}");
19          }
20      }
21  }
22  
23  async fn trigger(context: &Context, day: AocDay) -> anyhow::Result<()> {
24      let room = &context.room;
25      if room.state() != RoomState::Joined {
26          warn!("not a member of target room {}", room.room_id());
27          room.join().await?;
28      }
29  
30      let url = day.url();
31      let AocDay { year, day } = day;
32      let link_prefix = &context.config.matrix.link_prefix;
33      room.send(message(format!(
34          "✨ The puzzles of **Advent of Code {year} Day {day}** can now be solved at \
35           [{url}]({link_prefix}{url}) ✨ <!-- 🎉 -->",
36      )))
37      .await?;
38      Ok(())
39  }