feat(web): filter updated, more readable filter code

This commit is contained in:
əlemi 2024-05-31 15:54:22 +02:00
parent b0f47de278
commit 6469dbe85e
2 changed files with 52 additions and 46 deletions

View file

@ -45,51 +45,39 @@ pub fn Item(
let config = use_context::<Signal<crate::Config>>().expect("missing config context"); let config = use_context::<Signal<crate::Config>>().expect("missing config context");
let id = item.id().unwrap_or_default().to_string(); let id = item.id().unwrap_or_default().to_string();
let sep = if sep { Some(view! { <hr /> }) } else { None }; let sep = if sep { Some(view! { <hr /> }) } 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 // special case for placeholder activities
Some(apb::ObjectType::Note) | Some(apb::ObjectType::Document(_)) => (move || { apb::ObjectType::Note | apb::ObjectType::Document(_) =>
if !config.get().filters.replies && item.in_reply_to().id().is_some() { Some(view! { <Object object=item.clone() />{sep.clone()} }.into_view()),
None
} else if config.get().filters.orphans {
Some(view! { <Object object=item.clone() />{sep.clone()} })
} else {
None
}
}).into_view(),
// everything else // everything else
Some(apb::ObjectType::Activity(t)) => (move || { apb::ObjectType::Activity(t) => {
if config.get().filters.visible(apb::ObjectType::Activity(t)) { let object_id = item.object().id().unwrap_or_default();
let object_id = item.object().id().unwrap_or_default(); let object = match t {
if !replies && !config.get().filters.replies && CACHE.get(&object_id).map(|x| x.in_reply_to().id().is_some()).unwrap_or(false) { apb::ActivityType::Create | apb::ActivityType::Announce =>
None CACHE.get(&object_id).map(|obj| {
} else { view! { <Object object=obj /> }
let object = match t { }.into_view()),
apb::ActivityType::Create | apb::ActivityType::Announce => apb::ActivityType::Follow =>
CACHE.get(&object_id).map(|obj| { CACHE.get(&object_id).map(|obj| {
view! { <Object object=obj /> } view! {
}.into_view()), <div class="ml-1">
apb::ActivityType::Follow => <ActorBanner object=obj />
CACHE.get(&object_id).map(|obj| { <FollowRequestButtons activity_id=id.clone() actor_id=object_id />
view! { </div>
<div class="ml-1"> }
<ActorBanner object=obj /> }.into_view()),
<FollowRequestButtons activity_id=id.clone() actor_id=object_id /> _ => None,
</div> };
} Some(view! {
}.into_view()), <ActivityLine activity=item.clone() />
_ => None, {object}
}; {sep.clone()}
Some(view! { }.into_view())
<ActivityLine activity=item.clone() /> },
{object}
{sep.clone()}
})
}
} else {
None
}
}).into_view(),
// should never happen // should never happen
_ => view! { <p><code>type not implemented</code></p> }.into_view(), t => Some(view! { <p><code>type not implemented : {t.as_ref().to_string()}</code></p> }.into_view()),
} }
} }

View file

@ -36,21 +36,39 @@ pub struct FiltersConfig {
#[serde_inline_default(true)] #[serde_inline_default(true)]
pub follows: bool, pub follows: bool,
#[serde_inline_default(false)]
pub updates: bool,
#[serde_inline_default(true)] #[serde_inline_default(true)]
pub orphans: bool, pub orphans: bool,
} }
impl FiltersConfig { impl FiltersConfig {
pub fn visible(&self, object_type: apb::ObjectType) -> bool { pub fn visible(&self, item: &crate::Object) -> bool {
match object_type { 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::Note | apb::ObjectType::Document(_) => self.orphans,
apb::ObjectType::Activity(apb::ActivityType::Like | apb::ActivityType::EmojiReact) => self.likes, apb::ObjectType::Activity(apb::ActivityType::Like | apb::ActivityType::EmojiReact) => self.likes,
apb::ObjectType::Activity(apb::ActivityType::Create) => self.creates, apb::ObjectType::Activity(apb::ActivityType::Create) => self.creates,
apb::ObjectType::Activity(apb::ActivityType::Announce) => self.announces, apb::ObjectType::Activity(apb::ActivityType::Announce) => self.announces,
apb::ObjectType::Activity(apb::ActivityType::Update) => self.updates,
apb::ObjectType::Activity( apb::ObjectType::Activity(
apb::ActivityType::Follow | apb::ActivityType::Accept(_) | apb::ActivityType::Reject(_) apb::ActivityType::Follow | apb::ActivityType::Accept(_) | apb::ActivityType::Reject(_)
) => self.follows, ) => self.follows,
_ => true, _ => 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
} }
} }