Compare commits

..

4 Commits

3 changed files with 43 additions and 15 deletions

View File

@ -56,7 +56,7 @@ pub async fn fetch_and_parse_feed(uri: &str) -> Result<Feed, Box<dyn Error>> {
let http_client = reqwest::Client::builder().default_headers(headers).build()?; let http_client = reqwest::Client::builder().default_headers(headers).build()?;
let response = http_client.get(uri).send().await?.text().await?; let response = http_client.get(uri).send().await?.text().await?;
info!("Got response, parsing feed"); info!("Got response, parsing feed from {}", uri);
let feed = parser::parse(response.as_bytes())?; let feed = parser::parse(response.as_bytes())?;
let feed_title = match feed.title.clone() { let feed_title = match feed.title.clone() {
@ -82,7 +82,11 @@ pub fn format_entry(feed: Feed, entry: model::Entry) -> Result<Entry, Box<dyn Er
Some(t) => t.content, Some(t) => t.content,
None => String::from("Untitled") None => String::from("Untitled")
}, },
link: entry.links[0].href.clone(), link: if entry.links.len() > 0 {
entry.links[0].href.clone()
} else {
String::from("#")
},
ts: match entry.updated { ts: match entry.updated {
Some(d) => d, Some(d) => d,
None => entry.published.unwrap_or(Utc::now()) None => entry.published.unwrap_or(Utc::now())

View File

@ -39,7 +39,7 @@ use crate::{
}; };
use std::{ sync::Arc, cmp::max }; use std::{ sync::Arc, cmp::max };
use tracing::{ info, debug }; use tracing::{ info, debug, error };
use chrono::DateTime; use chrono::DateTime;
#[tokio::main] #[tokio::main]
@ -51,7 +51,7 @@ async fn main() -> anyhow::Result<()> {
// This message passing channel is used for sending messages to the matrix module, // This message passing channel is used for sending messages to the matrix module,
// it holds a tuple with an HTML message and a list of rooms to post to // it holds a tuple with an HTML message and a list of rooms to post to
let (bcast_tx, bcast_rx) = broadcast::channel(16); let (bcast_tx, bcast_rx) = broadcast::channel(1024);
let state_db = FeedReaderStateDb::new("state.yaml").await let state_db = FeedReaderStateDb::new("state.yaml").await
.expect("Failed to initialize feed reader state db"); .expect("Failed to initialize feed reader state db");
@ -63,8 +63,24 @@ async fn main() -> anyhow::Result<()> {
tokio::spawn(async move { tokio::spawn(async move {
loop { loop {
let feed = fetch_and_parse_feed(&feed_config.url).await let feed: Option<_> = match fetch_and_parse_feed(&feed_config.url).await {
.expect("Failed to parse feed"); Ok(f) => Some(f),
Err(e) => {
error!("Failed to parse feed {}: {:?}",
feed_config.url,
e
);
None
}
};
let feed = if feed.is_none() {
info!("Sleeping for {} seconds before refreshing this feed", feed_config.delay);
sleep(Duration::from_secs(feed_config.delay)).await;
continue;
} else {
feed.unwrap()
};
let rooms = feed_config.rooms.clone(); let rooms = feed_config.rooms.clone();

View File

@ -32,6 +32,8 @@ use tokio::{
sync::broadcast sync::broadcast
}; };
use tracing::info;
async fn on_stripped_state_member( async fn on_stripped_state_member(
room_member: StrippedRoomMemberEvent, room_member: StrippedRoomMemberEvent,
client: Client, client: Client,
@ -97,18 +99,24 @@ pub async fn login_and_sync<T: Clone>(
loop { loop {
tokio::select! { tokio::select! {
Ok(_) = client.sync(settings.clone()) => return Ok(()), Ok(_) = client.sync(settings.clone()) => return Ok(()),
Ok((msg, rooms)) = rx.recv() => { recv = rx.recv() => match recv {
Ok((msg, rooms)) => {
let msg = &msg.clone(); let msg = &msg.clone();
for r in rooms { for r in rooms {
info!("Joining room {}", r);
let room = client.join_room_by_id(<&RoomId>::try_from(r.as_str()) let room = client.join_room_by_id(<&RoomId>::try_from(r.as_str())
.expect("invalid room ID") .expect("invalid room ID")
).await.expect(format!("Failed to join room {}", r).as_str()); ).await.expect(format!("Failed to join room {}", r).as_str());
info!("Posting to room {}", r);
room.send(RoomMessageEventContent::text_html(msg.clone(), msg)).await room.send(RoomMessageEventContent::text_html(msg.clone(), msg)).await
.expect("Failed to send matrix event"); .expect("Failed to send matrix event");
} }
},
Err(e) => {
panic!("Broadcast channel is lagging: {e:?}");
}
} }
} }
} }