mirror of https://github.com/mastodon/flodgatt
Use trait object
This commit is contained in:
parent
9e12c58cbe
commit
9cce3a30fd
|
@ -22,20 +22,20 @@ fn parse_to_timeline(msg: RedisMsg) -> Timeline {
|
|||
assert_eq!(tl, Timeline(User(Id(1)), Federated, All));
|
||||
tl
|
||||
}
|
||||
fn parse_to_checked_event(msg: RedisMsg) -> Event {
|
||||
Event::TypeSafe(serde_json::from_str(msg.event_txt).unwrap())
|
||||
fn parse_to_checked_event(msg: RedisMsg) -> EventKind {
|
||||
EventKind::TypeSafe(serde_json::from_str(msg.event_txt).unwrap())
|
||||
}
|
||||
|
||||
fn parse_to_dyn_event(msg: RedisMsg) -> Event {
|
||||
Event::Dynamic(serde_json::from_str(msg.event_txt).unwrap())
|
||||
fn parse_to_dyn_event(msg: RedisMsg) -> EventKind {
|
||||
EventKind::Dynamic(serde_json::from_str(msg.event_txt).unwrap())
|
||||
}
|
||||
|
||||
fn redis_msg_to_event_string(msg: RedisMsg) -> String {
|
||||
msg.event_txt.to_string()
|
||||
}
|
||||
|
||||
fn string_to_checked_event(event_txt: &String) -> Event {
|
||||
Event::TypeSafe(serde_json::from_str(event_txt).unwrap())
|
||||
fn string_to_checked_event(event_txt: &String) -> EventKind {
|
||||
EventKind::TypeSafe(serde_json::from_str(event_txt).unwrap())
|
||||
}
|
||||
|
||||
fn criterion_benchmark(c: &mut Criterion) {
|
||||
|
|
11
src/event.rs
11
src/event.rs
|
@ -32,6 +32,17 @@ pub(crate) trait Payload {
|
|||
}
|
||||
|
||||
impl Event {
|
||||
pub(crate) fn get_update_payload(&self) -> Option<Box<&(dyn Payload)>> {
|
||||
match self {
|
||||
Event::TypeSafe(CheckedEvent::Update { payload, .. }) => Some(Box::new(payload)),
|
||||
Event::Dynamic(DynEvent {
|
||||
kind: EventKind::Update(s),
|
||||
..
|
||||
}) => Some(Box::new(s)),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn to_json_string(&self) -> String {
|
||||
if let Event::Ping = self {
|
||||
"{}".to_string()
|
||||
|
|
|
@ -50,7 +50,6 @@ impl Ws {
|
|||
let target_timeline = self.subscription.timeline;
|
||||
let incoming_events = self.ws_rx.clone().map_err(|_| ());
|
||||
|
||||
use crate::event::Payload; // TODO -- move up
|
||||
incoming_events.for_each(move |(tl, event)| {
|
||||
if matches!(event, Event::Ping) {
|
||||
self.send_msg(&event)?
|
||||
|
@ -58,78 +57,21 @@ impl Ws {
|
|||
let blocks = &self.subscription.blocks;
|
||||
let allowed_langs = &self.subscription.allowed_langs;
|
||||
|
||||
if let Some(payload) = event.update_payload() {
|
||||
if let Some(update) = event.get_update_payload() {
|
||||
match tl {
|
||||
tl if tl.is_public()
|
||||
&& !payload.language_unset()
|
||||
&& !update.language_unset()
|
||||
&& !allowed_langs.is_empty()
|
||||
&& !allowed_langs.contains(&payload.language()) =>
|
||||
{
|
||||
()
|
||||
}
|
||||
_ if blocks.blocked_users.is_disjoint(&payload.involved_users()) => (),
|
||||
_ if blocks.blocking_users.contains(payload.author()) => (),
|
||||
_ if blocks.blocked_domains.contains(payload.sent_from()) => (),
|
||||
&& !allowed_langs.contains(&update.language()) => {} // skip
|
||||
_ if !blocks.blocked_users.is_disjoint(&update.involved_users()) => {} // skip
|
||||
_ if blocks.blocking_users.contains(update.author()) => {} // skip
|
||||
_ if blocks.blocked_domains.contains(update.sent_from()) => {} // skip
|
||||
_ => self.send_msg(&event)?,
|
||||
}
|
||||
} else {
|
||||
if let Some(payload) = event.dyn_update_payload() {
|
||||
match tl {
|
||||
tl if tl.is_public()
|
||||
&& !payload.language_unset()
|
||||
&& !allowed_langs.is_empty()
|
||||
&& !allowed_langs.contains(&payload.language()) =>
|
||||
{
|
||||
()
|
||||
}
|
||||
_ if blocks.blocked_users.is_disjoint(&payload.involved_users()) => (),
|
||||
_ if blocks.blocking_users.contains(payload.author()) => (),
|
||||
_ if blocks.blocked_domains.contains(payload.sent_from()) => (),
|
||||
_ => self.send_msg(&event)?,
|
||||
}
|
||||
}
|
||||
// send all non-updates
|
||||
self.send_msg(&event)?;
|
||||
}
|
||||
|
||||
// TODO handle non-updates
|
||||
|
||||
// match event {
|
||||
// TypeSafe(Update { payload, queued_at }) => match tl {
|
||||
// tl if tl.is_public()
|
||||
// && !payload.language_unset()
|
||||
// && !allowed_langs.is_empty()
|
||||
// && !allowed_langs.contains(&payload.language()) =>
|
||||
// {
|
||||
// SKIP
|
||||
// }
|
||||
// _ if blocks.blocked_users.is_disjoint(&payload.involved_users()) => SKIP,
|
||||
// _ if blocks.blocking_users.contains(payload.author()) => SKIP,
|
||||
// _ if blocks.blocked_domains.contains(payload.sent_from()) => SKIP,
|
||||
// _ => self.send_msg(&TypeSafe(Update { payload, queued_at })),
|
||||
// },
|
||||
// TypeSafe(non_update) => self.send_msg(&TypeSafe(non_update)),
|
||||
// Dynamic(dyn_event) => {
|
||||
// if let Some(s) = dyn_event.update() {
|
||||
// match tl {
|
||||
// tl if tl.is_public()
|
||||
// && !s.language_unset()
|
||||
// && !allowed_langs.is_empty()
|
||||
// && !allowed_langs.contains(&s.language()) =>
|
||||
// {
|
||||
// SKIP
|
||||
// }
|
||||
// _ if blocks.blocked_users.is_disjoint(&s.involved_users()) => SKIP,
|
||||
// _ if blocks.blocking_users.contains(s.author()) => SKIP,
|
||||
// _ if blocks.blocked_domains.contains(s.sent_from()) => SKIP,
|
||||
// _ => self.send_msg(&Dynamic(dyn_event)),
|
||||
// }
|
||||
// } else {
|
||||
// self.send_msg(&Dynamic(dyn_event))
|
||||
// }
|
||||
// }
|
||||
// Ping => unreachable!(), // handled pings above
|
||||
// }
|
||||
} else {
|
||||
()
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
|
|
Loading…
Reference in New Issue