Compare commits

..

No commits in common. "d0ac11ea898b27999ecd2dd6228c2132a69d63b4" and "cd590b73fefbe259843d79d3586210fd9a0c36e0" have entirely different histories.

3 changed files with 15 additions and 43 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 from {}", uri); info!("Got response, parsing feed");
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,11 +82,7 @@ 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: if entry.links.len() > 0 { link: entry.links[0].href.clone(),
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, error }; use tracing::{ info, debug };
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(1024); let (bcast_tx, bcast_rx) = broadcast::channel(16);
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,24 +63,8 @@ async fn main() -> anyhow::Result<()> {
tokio::spawn(async move { tokio::spawn(async move {
loop { loop {
let feed: Option<_> = match fetch_and_parse_feed(&feed_config.url).await { let feed = fetch_and_parse_feed(&feed_config.url).await
Ok(f) => Some(f), .expect("Failed to parse feed");
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,8 +32,6 @@ 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,
@ -99,24 +97,18 @@ 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(()),
recv = rx.recv() => match recv { Ok((msg, rooms)) = rx.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:?}");
} }
} }
} }
} }