This commit is contained in:
Daniel Sockwell 2020-04-18 17:08:05 -04:00
parent 0e21b189bd
commit 9e12c58cbe
11 changed files with 212 additions and 141 deletions

View File

@ -6,6 +6,7 @@ pub(crate) use checked_event::{CheckedEvent, Id};
pub(crate) use dynamic_event::{DynEvent, EventKind};
pub(crate) use err::EventErr;
use hashbrown::HashSet;
use serde::Serialize;
use std::convert::TryFrom;
use std::string::String;
@ -18,6 +19,18 @@ pub enum Event {
Ping,
}
pub(crate) trait Payload {
fn language_unset(&self) -> bool;
fn language(&self) -> String;
fn involved_users(&self) -> HashSet<Id>;
fn author(&self) -> &Id;
fn sent_from(&self) -> &str;
}
impl Event {
pub(crate) fn to_json_string(&self) -> String {
if let Event::Ping = self {
@ -43,6 +56,26 @@ impl Event {
}
}
pub(crate) fn update_payload(&self) -> Option<&checked_event::Status> {
if let Self::TypeSafe(CheckedEvent::Update { payload, .. }) = self {
Some(&payload)
} else {
None
}
}
pub(crate) fn dyn_update_payload(&self) -> Option<&dynamic_event::DynStatus> {
if let Self::Dynamic(DynEvent {
kind: EventKind::Update(s),
..
}) = self
{
Some(&s)
} else {
None
}
}
fn event_name(&self) -> String {
String::from(match self {
Self::TypeSafe(checked) => match checked {

View File

@ -16,7 +16,7 @@ pub(in crate::event) use announcement_reaction::AnnouncementReaction;
use conversation::Conversation;
pub(crate) use id::Id;
use notification::Notification;
use status::Status;
pub(crate) use status::Status;
use serde::Deserialize;

View File

@ -3,6 +3,7 @@ mod attachment;
mod card;
mod poll;
use super::super::Payload;
use super::account::Account;
use super::emoji::Emoji;
use super::id::Id;
@ -14,7 +15,7 @@ use attachment::Attachment;
use card::Card;
use poll::Poll;
use crate::request::Blocks;
//use crate::request::Blocks;
use hashbrown::HashSet;
use serde::{Deserialize, Serialize};
@ -46,7 +47,7 @@ pub struct Status {
reblog: Option<Box<Status>>,
poll: Option<Poll>,
card: Option<Card>,
language: Option<String>,
pub(crate) language: Option<String>,
text: Option<String>,
// ↓↓↓ Only for authorized users
@ -57,67 +58,29 @@ pub struct Status {
pinned: Option<bool>,
}
impl Status {
/// Returns `true` if the status is filtered out based on its language
pub(crate) fn language_not(&self, allowed_langs: &HashSet<String>) -> bool {
const ALLOW: bool = false;
const REJECT: bool = true;
let reject_and_maybe_log = |toot_language| {
log::info!("Filtering out toot from `{}`", &self.account.acct);
log::info!("Toot language: `{}`", toot_language);
log::info!("Recipient's allowed languages: `{:?}`", allowed_langs);
REJECT
};
if allowed_langs.is_empty() {
return ALLOW; // listing no allowed_langs results in allowing all languages
}
match self.language.as_ref() {
Some(toot_language) if allowed_langs.contains(toot_language) => ALLOW,
None => ALLOW, // If toot language is unknown, toot is always allowed
Some(empty) if empty == &String::new() => ALLOW,
Some(toot_language) => reject_and_maybe_log(toot_language),
impl Payload for Status {
fn language_unset(&self) -> bool {
match &self.language {
None => true,
Some(empty) if empty == &String::new() => true,
Some(_language) => false,
}
}
/// Returns `true` if the Status originated from a blocked domain, is from an account
/// that has blocked the current user, or if the User's list of blocked/muted users
/// includes a user involved in the Status.
fn language(&self) -> String {
self.language.clone().unwrap_or_default()
}
/// Returns all users involved in the `Status`.
///
/// A user is involved in the Status/toot if they:
/// * Are mentioned in this toot
/// * Wrote this toot
/// * Wrote a toot that this toot is replying to (if any)
/// * Wrote the toot that this toot is boosting (if any)
pub(crate) fn involves_any(&self, blocks: &Blocks) -> bool {
const ALLOW: bool = false;
const REJECT: bool = true;
let Blocks {
blocked_users,
blocking_users,
blocked_domains,
} = blocks;
let user_id = &Id(self.account.id.0);
if blocking_users.contains(user_id) || self.involves(blocked_users) {
REJECT
} else {
let full_username = &self.account.acct;
match full_username.split('@').nth(1) {
Some(originating_domain) if blocked_domains.contains(originating_domain) => REJECT,
Some(_) | None => ALLOW, // None means the local instance, which can't be blocked
}
}
}
fn involves(&self, blocked_users: &HashSet<Id>) -> bool {
fn involved_users(&self) -> HashSet<Id> {
// involved_users = mentioned_users + author + replied-to user + boosted user
let mut involved_users: HashSet<Id> = self
.mentions
.iter()
.map(|mention| Id(mention.id.0))
.collect();
let mut involved_users: HashSet<Id> = self.mentions.iter().map(|m| Id(m.id.0)).collect();
// author
involved_users.insert(Id(self.account.id.0));
@ -129,6 +92,15 @@ impl Status {
if let Some(boosted_status) = self.reblog.clone() {
involved_users.insert(Id(boosted_status.account.id.0));
}
!involved_users.is_disjoint(blocked_users)
involved_users
}
fn author(&self) -> &Id {
&self.account.id
}
fn sent_from(&self) -> &str {
let sender_username = &self.account.acct;
sender_username.split('@').nth(1).unwrap_or_default() // default occurs when sent from local instance
}
}

View File

@ -1,5 +1,5 @@
use super::Payload;
use super::{EventErr, Id};
use crate::request::Blocks;
use std::convert::TryFrom;
@ -49,8 +49,14 @@ impl DynEvent {
Ok(self)
}
}
pub(crate) fn update(&self) -> Option<DynStatus> {
if let EventKind::Update(status) = self.kind.clone() {
Some(status)
} else {
None
}
}
}
impl DynStatus {
pub(crate) fn new(payload: &Value) -> Result<Self> {
use EventErr::*;
@ -67,68 +73,50 @@ impl DynStatus {
boosted_user: Id::try_from(&payload["reblog"]["account"]["id"]).ok(),
})
}
/// Returns `true` if the status is filtered out based on its language
pub(crate) fn language_not(&self, allowed_langs: &HashSet<String>) -> bool {
const ALLOW: bool = false;
const REJECT: bool = true;
}
if allowed_langs.is_empty() {
return ALLOW; // listing no allowed_langs results in allowing all languages
}
match self.language.clone() {
Some(toot_language) if allowed_langs.contains(&toot_language) => ALLOW, //
None => ALLOW, // If toot language is unknown, toot is always allowed
Some(empty) if empty == String::new() => ALLOW,
Some(_toot_language) => REJECT,
impl Payload for DynStatus {
fn language_unset(&self) -> bool {
match &self.language {
None => true,
Some(empty) if empty == &String::new() => true,
Some(_language) => false,
}
}
/// Returns `true` if the toot contained in this Event originated from a blocked domain,
/// is from an account that has blocked the current user, or if the User's list of
/// blocked/muted users includes a user involved in the toot.
fn language(&self) -> String {
self.language.clone().unwrap_or_default()
}
/// Returns all users involved in the `Status`.
///
/// A user is involved in the toot if they:
/// A user is involved in the Status/toot if they:
/// * Are mentioned in this toot
/// * Wrote this toot
/// * Wrote a toot that this toot is replying to (if any)
/// * Wrote the toot that this toot is boosting (if any)
pub(crate) fn involves_any(&self, blocks: &Blocks) -> bool {
const ALLOW: bool = false;
const REJECT: bool = true;
let Blocks {
blocked_users,
blocking_users,
blocked_domains,
} = blocks;
if self.involves(blocked_users) || blocking_users.contains(&self.id) {
REJECT
} else {
match self.username.split('@').nth(1) {
Some(originating_domain) if blocked_domains.contains(originating_domain) => REJECT,
Some(_) | None => ALLOW, // None means the local instance, which can't be blocked
}
}
}
fn involves(&self, blocked_users: &HashSet<Id>) -> bool {
// mentions
fn involved_users(&self) -> HashSet<Id> {
// involved_users = mentioned_users + author + replied-to user + boosted user
let mut involved_users: HashSet<Id> = self.mentioned_users.clone();
// author
involved_users.insert(self.id);
// replied-to user
if let Some(user_id) = self.replied_to_user {
involved_users.insert(user_id);
}
// boosted user
if let Some(user_id) = self.boosted_user {
involved_users.insert(user_id);
if let Some(boosted_status) = self.boosted_user {
involved_users.insert(boosted_status);
}
involved_users
}
!involved_users.is_disjoint(blocked_users)
fn author(&self) -> &Id {
&self.id
}
fn sent_from(&self) -> &str {
let sender_username = &self.username;
sender_username.split('@').nth(1).unwrap_or_default() // default occurs when sent from local instance
}
}

View File

@ -35,9 +35,9 @@
//! polls the `Receiver` and the frequency with which the `Receiver` polls Redis.
//!
//#![warn(clippy::pedantic)]
#![warn(clippy::pedantic)]
#![allow(clippy::try_err, clippy::match_bool)]
//#![allow(clippy::large_enum_variant)]
#![allow(clippy::large_enum_variant)]
pub use err::Error;

View File

@ -6,15 +6,12 @@ mod timeline;
pub mod err;
mod subscription;
pub(crate) use self::postgres::PgPool;
pub(crate) use err::Error;
pub(crate) use subscription::Blocks;
pub use subscription::Subscription;
pub(crate) use timeline::Stream;
pub use timeline::Timeline;
use timeline::{Content, Reach};
use timeline::{Content, Reach, Stream};
use self::postgres::PgPool;
use self::query::Query;
use crate::config::Postgres;
use warp::filters::BoxedFilter;

View File

@ -18,6 +18,22 @@ impl Timeline {
Self(Stream::Unset, Reach::Local, Content::Notification)
}
pub(crate) fn is_public(&self) -> bool {
if let Self(Stream::Public, _, _) = self {
true
} else {
false
}
}
pub(crate) fn tag(&self) -> Option<i64> {
if let Self(Stream::Hashtag(id), _, _) = self {
Some(*id)
} else {
None
}
}
pub(crate) fn to_redis_raw_timeline(&self, hashtag: Option<&String>) -> Result<String> {
// TODO -- does this need to account for namespaces?
use {Content::*, Error::*, Reach::*, Stream::*};

View File

@ -6,7 +6,7 @@ use super::Error as ManagerErr;
use super::RedisCmd;
use crate::config::Redis;
use crate::event::Event;
use crate::request::{Stream, Timeline};
use crate::request::Timeline;
use futures::{Async, Poll};
use lru::LruCache;
@ -118,12 +118,9 @@ impl RedisConn {
}
pub(crate) fn send_cmd(&mut self, cmd: RedisCmd, timeline: &Timeline) -> Result<()> {
let hashtag = match timeline {
Timeline(Stream::Hashtag(id), _, _) => self.tag_name_cache.get(id),
_non_hashtag_timeline => None,
};
let hashtag = timeline.tag().and_then(|id| self.tag_name_cache.get(&id));
let tl = timeline.to_redis_raw_timeline(hashtag)?;
let (primary_cmd, secondary_cmd) = cmd.into_sendable(&tl);
self.primary.write_all(&primary_cmd)?;
self.secondary.write_all(&secondary_cmd)?;

View File

@ -7,7 +7,7 @@ pub(crate) use err::Error;
use super::{RedisCmd, RedisConn};
use crate::config;
use crate::event::Event;
use crate::request::{Stream, Subscription, Timeline};
use crate::request::{Subscription, Timeline};
use futures::{Async, Stream as _Stream};
use hashbrown::HashMap;
@ -50,7 +50,7 @@ impl Manager {
pub fn subscribe(&mut self, subscription: &Subscription) {
let (tag, tl) = (subscription.hashtag_name.clone(), subscription.timeline);
if let (Some(hashtag), Timeline(Stream::Hashtag(id), _, _)) = (tag, tl) {
if let (Some(hashtag), Some(id)) = (tag, tl.tag()) {
self.redis_connection.update_cache(hashtag, id);
};

View File

@ -24,15 +24,24 @@ impl Sse {
let event_stream = sse_rx
.filter(move |(timeline, _)| target_timeline == *timeline)
.filter_map(move |(timeline, event)| {
use crate::event::Payload;
use crate::event::{
CheckedEvent, CheckedEvent::Update, DynEvent, Event::*, EventKind,
};
}; // TODO -- move up
use crate::request::Stream::Public;
match event {
TypeSafe(Update { payload, queued_at }) => match timeline {
Timeline(Public, _, _) if payload.language_not(&allowed_langs) => None,
_ if payload.involves_any(&blocks) => None,
tl if tl.is_public()
&& !payload.language_unset()
&& !allowed_langs.is_empty()
&& !allowed_langs.contains(&payload.language()) =>
{
None
}
_ if blocks.blocked_users.is_disjoint(&payload.involved_users()) => None,
_ if blocks.blocking_users.contains(payload.author()) => None,
_ if blocks.blocked_domains.contains(payload.sent_from()) => None,
_ => Event::TypeSafe(CheckedEvent::Update { payload, queued_at })
.to_warp_reply(),
},
@ -40,8 +49,17 @@ impl Sse {
Dynamic(dyn_event) => {
if let EventKind::Update(s) = dyn_event.kind {
match timeline {
Timeline(Public, _, _) if s.language_not(&allowed_langs) => None,
_ if s.involves_any(&blocks) => None,
tl if tl.is_public()
&& !s.language_unset()
&& !allowed_langs.is_empty()
&& !allowed_langs.contains(&s.language()) =>
{
None
}
_ if blocks.blocked_users.is_disjoint(&s.involved_users()) => None,
_ if blocks.blocking_users.contains(s.author()) => None,
_ if blocks.blocked_domains.contains(s.sent_from()) => None,
_ => Dynamic(DynEvent {
kind: EventKind::Update(s),
..dyn_event

View File

@ -50,38 +50,88 @@ 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)
self.send_msg(&event)?
} else if target_timeline == tl {
use crate::event::{CheckedEvent::Update, Event::*, EventKind};
use crate::request::Stream::Public;
let blocks = &self.subscription.blocks;
let allowed_langs = &self.subscription.allowed_langs;
match event {
TypeSafe(Update { payload, queued_at }) => match tl {
Timeline(Public, _, _) if payload.language_not(allowed_langs) => Ok(()),
_ if payload.involves_any(&blocks) => Ok(()),
_ => self.send_msg(&TypeSafe(Update { payload, queued_at })),
},
TypeSafe(non_update) => self.send_msg(&TypeSafe(non_update)),
Dynamic(dyn_event) => {
if let EventKind::Update(s) = dyn_event.kind.clone() {
match tl {
Timeline(Public, _, _) if s.language_not(allowed_langs) => Ok(()),
_ if s.involves_any(&blocks) => Ok(()),
_ => self.send_msg(&Dynamic(dyn_event)),
if let Some(payload) = event.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)?,
}
} 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()) =>
{
()
}
} else {
self.send_msg(&Dynamic(dyn_event))
_ 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)?,
}
}
Ping => unreachable!(), // handled pings above
}
// 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(())
()
}
Ok(())
})
}