From 6469dbe85e6caf33f4ed4b42d104385dad8de368 Mon Sep 17 00:00:00 2001 From: alemi Date: Fri, 31 May 2024 15:54:22 +0200 Subject: [PATCH] feat(web): filter updated, more readable filter code --- web/src/components/activity.rs | 74 ++++++++++++++-------------------- web/src/config.rs | 24 +++++++++-- 2 files changed, 52 insertions(+), 46 deletions(-) diff --git a/web/src/components/activity.rs b/web/src/components/activity.rs index 406c97c6..9198a562 100644 --- a/web/src/components/activity.rs +++ b/web/src/components/activity.rs @@ -45,51 +45,39 @@ pub fn Item( let config = use_context::>().expect("missing config context"); let id = item.id().unwrap_or_default().to_string(); let sep = if sep { Some(view! {
}) } else { None }; - match item.object_type() { + if !replies && !config.get().filters.visible(&item) { + return None; + } + match item.object_type().unwrap_or(apb::ObjectType::Object) { // special case for placeholder activities - Some(apb::ObjectType::Note) | Some(apb::ObjectType::Document(_)) => (move || { - if !config.get().filters.replies && item.in_reply_to().id().is_some() { - None - } else if config.get().filters.orphans { - Some(view! { {sep.clone()} }) - } else { - None - } - }).into_view(), + apb::ObjectType::Note | apb::ObjectType::Document(_) => + Some(view! { {sep.clone()} }.into_view()), // everything else - Some(apb::ObjectType::Activity(t)) => (move || { - if config.get().filters.visible(apb::ObjectType::Activity(t)) { - let object_id = item.object().id().unwrap_or_default(); - if !replies && !config.get().filters.replies && CACHE.get(&object_id).map(|x| x.in_reply_to().id().is_some()).unwrap_or(false) { - None - } else { - let object = match t { - apb::ActivityType::Create | apb::ActivityType::Announce => - CACHE.get(&object_id).map(|obj| { - view! { } - }.into_view()), - apb::ActivityType::Follow => - CACHE.get(&object_id).map(|obj| { - view! { -
- - -
- } - }.into_view()), - _ => None, - }; - Some(view! { - - {object} - {sep.clone()} - }) - } - } else { - None - } - }).into_view(), + apb::ObjectType::Activity(t) => { + let object_id = item.object().id().unwrap_or_default(); + let object = match t { + apb::ActivityType::Create | apb::ActivityType::Announce => + CACHE.get(&object_id).map(|obj| { + view! { } + }.into_view()), + apb::ActivityType::Follow => + CACHE.get(&object_id).map(|obj| { + view! { +
+ + +
+ } + }.into_view()), + _ => None, + }; + Some(view! { + + {object} + {sep.clone()} + }.into_view()) + }, // should never happen - _ => view! {

type not implemented

}.into_view(), + t => Some(view! {

type not implemented : {t.as_ref().to_string()}

}.into_view()), } } diff --git a/web/src/config.rs b/web/src/config.rs index 68cf97ac..d5d44c27 100644 --- a/web/src/config.rs +++ b/web/src/config.rs @@ -36,21 +36,39 @@ pub struct FiltersConfig { #[serde_inline_default(true)] pub follows: bool, + #[serde_inline_default(false)] + pub updates: bool, + #[serde_inline_default(true)] pub orphans: bool, } impl FiltersConfig { - pub fn visible(&self, object_type: apb::ObjectType) -> bool { - match object_type { + pub fn visible(&self, item: &crate::Object) -> bool { + use apb::{Object, Activity}; + + let type_filter = match item.object_type().unwrap_or(apb::ObjectType::Object) { apb::ObjectType::Note | apb::ObjectType::Document(_) => self.orphans, apb::ObjectType::Activity(apb::ActivityType::Like | apb::ActivityType::EmojiReact) => self.likes, apb::ObjectType::Activity(apb::ActivityType::Create) => self.creates, apb::ObjectType::Activity(apb::ActivityType::Announce) => self.announces, + apb::ObjectType::Activity(apb::ActivityType::Update) => self.updates, apb::ObjectType::Activity( apb::ActivityType::Follow | apb::ActivityType::Accept(_) | apb::ActivityType::Reject(_) ) => self.follows, _ => true, - } + }; + let mut reply_filter = true; + + if + item.in_reply_to().id().is_some() || + item.object().get().map(|x| + x.in_reply_to().id().is_some() + ).unwrap_or(false) + { + reply_filter = self.replies; + }; + + type_filter && reply_filter } }