diff --git a/Cargo.lock b/Cargo.lock index ae5b534..037113e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -279,7 +279,7 @@ checksum = "216261ddc8289130e551ddcd5ce8a064710c0d064a4d2895c67151c92b5443f6" [[package]] name = "ap-relay" -version = "0.3.31" +version = "0.3.32" dependencies = [ "activitystreams", "activitystreams-ext", diff --git a/Cargo.toml b/Cargo.toml index 114b9a8..bb5aa18 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "ap-relay" description = "A simple activitypub relay" -version = "0.3.31" +version = "0.3.32" authors = ["asonix "] license = "AGPL-3.0" readme = "README.md" diff --git a/src/db.rs b/src/db.rs index 4968526..73f5499 100644 --- a/src/db.rs +++ b/src/db.rs @@ -142,6 +142,14 @@ impl Inner { .map(|s| String::from_utf8_lossy(&s).to_string()) } + fn allowed(&self) -> impl DoubleEndedIterator { + self.allowed_domains + .iter() + .values() + .filter_map(|res| res.ok()) + .map(|s| String::from_utf8_lossy(&s).to_string()) + } + fn connected(&self) -> impl DoubleEndedIterator { self.connected_actor_ids .iter() @@ -273,6 +281,10 @@ impl Db { self.unblock(|inner| Ok(inner.connected().collect())).await } + pub(crate) async fn allowed_domains(&self) -> Result, Error> { + self.unblock(|inner| Ok(inner.allowed().collect())).await + } + pub(crate) async fn save_info(&self, actor_id: IriString, info: Info) -> Result<(), Error> { self.unblock(move |inner| { let vec = serde_json::to_vec(&info)?; diff --git a/src/jobs/instance.rs b/src/jobs/instance.rs index df09ad8..201096c 100644 --- a/src/jobs/instance.rs +++ b/src/jobs/instance.rs @@ -52,22 +52,17 @@ impl QueryInstance { .fetch_json::(instance_uri.as_str()) .await?; - let description = if instance.description.is_empty() { - instance.short_description.unwrap_or_default() - } else { - instance.description - }; + let description = instance.short_description.unwrap_or(instance.description); - if let Some(mut contact) = instance.contact { + if let Some(contact) = instance.contact { let uuid = if let Some(uuid) = state.media.get_uuid(contact.avatar.clone()).await? { - contact.avatar = state.config.generate_url(UrlKind::Media(uuid)); uuid } else { - let uuid = state.media.store_url(contact.avatar.clone()).await?; - contact.avatar = state.config.generate_url(UrlKind::Media(uuid)); - uuid + state.media.store_url(contact.avatar).await? }; + let avatar = state.config.generate_url(UrlKind::Media(uuid)); + state.job_server.queue(CacheMedia::new(uuid)).await?; state @@ -77,7 +72,7 @@ impl QueryInstance { contact.username, contact.display_name, contact.url, - contact.avatar, + avatar, ) .await?; } @@ -87,7 +82,7 @@ impl QueryInstance { state .node_cache .set_instance( - self.actor_id.clone(), + self.actor_id, instance.title, description, instance.version, diff --git a/src/telegram.rs b/src/telegram.rs index 9e52184..39636fb 100644 --- a/src/telegram.rs +++ b/src/telegram.rs @@ -31,6 +31,15 @@ enum Command { #[command(description = "Disallow a domain to connect to the relay (for RESTRICTED_MODE)")] Disallow { domain: String }, + + #[command(description = "List blocked domains")] + ListBlocks, + + #[command(description = "List allowed domains")] + ListAllowed, + + #[command(description = "List connected domains")] + ListConnected, } pub(crate) fn start(admin_handle: String, db: Db, token: &str) { @@ -79,29 +88,40 @@ async fn answer(bot: Bot, msg: Message, cmd: Command, db: Db) -> ResponseResult< bot.send_message(msg.chat.id, Command::descriptions().to_string()) .await?; } - Command::Block { domain } => { - if db.add_blocks(vec![domain.clone()]).await.is_ok() { - bot.send_message(msg.chat.id, format!("{} has been blocked", domain)) - .await?; + Command::Block { domain } if db.add_blocks(vec![domain.clone()]).await.is_ok() => { + bot.send_message(msg.chat.id, format!("{} has been blocked", domain)) + .await?; + } + Command::Unblock { domain } if db.remove_blocks(vec![domain.clone()]).await.is_ok() => { + bot.send_message(msg.chat.id, format!("{} has been unblocked", domain)) + .await?; + } + Command::Allow { domain } if db.add_allows(vec![domain.clone()]).await.is_ok() => { + bot.send_message(msg.chat.id, format!("{} has been allowed", domain)) + .await?; + } + Command::Disallow { domain } if db.remove_allows(vec![domain.clone()]).await.is_ok() => { + bot.send_message(msg.chat.id, format!("{} has been disallowed", domain)) + .await?; + } + Command::ListAllowed => { + if let Ok(allowed) = db.allowed_domains().await { + bot.send_message(msg.chat.id, allowed.join("\n")).await?; } } - Command::Unblock { domain } => { - if db.remove_blocks(vec![domain.clone()]).await.is_ok() { - bot.send_message(msg.chat.id, format!("{} has been unblocked", domain)) - .await?; + Command::ListBlocks => { + if let Ok(blocks) = db.blocks().await { + bot.send_message(msg.chat.id, blocks.join("\n")).await?; } } - Command::Allow { domain } => { - if db.add_allows(vec![domain.clone()]).await.is_ok() { - bot.send_message(msg.chat.id, format!("{} has been allowed", domain)) - .await?; + Command::ListConnected => { + if let Ok(connected) = db.connected_ids().await { + bot.send_message(msg.chat.id, connected.join("\n")).await?; } } - Command::Disallow { domain } => { - if db.remove_allows(vec![domain.clone()]).await.is_ok() { - bot.send_message(msg.chat.id, format!("{} has been disallowed", domain)) - .await?; - } + _ => { + bot.send_message(msg.chat.id, "Internal server error") + .await?; } }