forked from alemi/upub
feat(web): filter updated, more readable filter code
This commit is contained in:
parent
b0f47de278
commit
6469dbe85e
2 changed files with 52 additions and 46 deletions
|
@ -45,51 +45,39 @@ pub fn Item(
|
|||
let config = use_context::<Signal<crate::Config>>().expect("missing config context");
|
||||
let id = item.id().unwrap_or_default().to_string();
|
||||
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
|
||||
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! { <Object object=item.clone() />{sep.clone()} })
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}).into_view(),
|
||||
apb::ObjectType::Note | apb::ObjectType::Document(_) =>
|
||||
Some(view! { <Object object=item.clone() />{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! { <Object object=obj /> }
|
||||
}.into_view()),
|
||||
apb::ActivityType::Follow =>
|
||||
CACHE.get(&object_id).map(|obj| {
|
||||
view! {
|
||||
<div class="ml-1">
|
||||
<ActorBanner object=obj />
|
||||
<FollowRequestButtons activity_id=id.clone() actor_id=object_id />
|
||||
</div>
|
||||
}
|
||||
}.into_view()),
|
||||
_ => None,
|
||||
};
|
||||
Some(view! {
|
||||
<ActivityLine activity=item.clone() />
|
||||
{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! { <Object object=obj /> }
|
||||
}.into_view()),
|
||||
apb::ActivityType::Follow =>
|
||||
CACHE.get(&object_id).map(|obj| {
|
||||
view! {
|
||||
<div class="ml-1">
|
||||
<ActorBanner object=obj />
|
||||
<FollowRequestButtons activity_id=id.clone() actor_id=object_id />
|
||||
</div>
|
||||
}
|
||||
}.into_view()),
|
||||
_ => None,
|
||||
};
|
||||
Some(view! {
|
||||
<ActivityLine activity=item.clone() />
|
||||
{object}
|
||||
{sep.clone()}
|
||||
}.into_view())
|
||||
},
|
||||
// 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()),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue