mirror of https://github.com/mastodon/flodgatt
Improve module privacy (#136)
* Adjust module privacy * Use trait object * Finish module privacy refactor
This commit is contained in:
parent
4a456c6e90
commit
016f49a2d8
|
@ -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) {
|
||||
|
|
|
@ -1,13 +1,12 @@
|
|||
pub(crate) use postgres_cfg::Postgres;
|
||||
pub(crate) use redis_cfg::Redis;
|
||||
|
||||
use deployment_cfg::Deployment;
|
||||
pub use self::deployment_cfg::Deployment;
|
||||
pub use self::postgres_cfg::Postgres;
|
||||
pub use self::redis_cfg::Redis;
|
||||
|
||||
use self::environmental_variables::EnvVar;
|
||||
use super::err::FatalErr;
|
||||
|
||||
use hashbrown::HashMap;
|
||||
use std::env;
|
||||
|
||||
use std::fmt;
|
||||
mod deployment_cfg;
|
||||
mod deployment_cfg_types;
|
||||
mod environmental_variables;
|
||||
|
@ -16,13 +15,13 @@ mod postgres_cfg_types;
|
|||
mod redis_cfg;
|
||||
mod redis_cfg_types;
|
||||
|
||||
type Result<T> = std::result::Result<T, FatalErr>;
|
||||
type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
pub fn merge_dotenv() -> Result<()> {
|
||||
let env_file = match env::var("ENV").ok().as_deref() {
|
||||
Some("production") => ".env.production",
|
||||
Some("development") | None => ".env",
|
||||
Some(v) => Err(FatalErr::config("ENV", v, "`production` or `development`"))?,
|
||||
Some(v) => Err(Error::config("ENV", v, "`production` or `development`"))?,
|
||||
};
|
||||
let res = dotenv::from_filename(env_file);
|
||||
|
||||
|
@ -58,3 +57,47 @@ pub fn from_env<'a>(
|
|||
|
||||
Ok((pg_cfg, redis_cfg, deployment_cfg))
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Error {
|
||||
Config(String),
|
||||
UrlEncoding(urlencoding::FromUrlEncodingError),
|
||||
UrlParse(url::ParseError),
|
||||
}
|
||||
|
||||
impl std::error::Error for Error {}
|
||||
|
||||
impl fmt::Display for Error {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> std::result::Result<(), fmt::Error> {
|
||||
write!(
|
||||
f,
|
||||
"{}",
|
||||
match self {
|
||||
Self::Config(e) => e.to_string(),
|
||||
Self::UrlEncoding(e) => format!("could not parse POSTGRES_URL.\n{:7}{:?}", "", e),
|
||||
Self::UrlParse(e) => format!("could parse Postgres URL.\n{:7}{}", "", e),
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl Error {
|
||||
pub fn config<T: fmt::Display>(var: T, value: T, allowed_vals: T) -> Self {
|
||||
Self::Config(format!(
|
||||
"{0} is set to `{1}`, which is invalid.\n{3:7}{0} must be {2}.",
|
||||
var, value, allowed_vals, ""
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<urlencoding::FromUrlEncodingError> for Error {
|
||||
fn from(e: urlencoding::FromUrlEncodingError) -> Self {
|
||||
Self::UrlEncoding(e)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<url::ParseError> for Error {
|
||||
fn from(e: url::ParseError) -> Self {
|
||||
Self::UrlParse(e)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use super::{deployment_cfg_types::*, EnvVar};
|
||||
use crate::err::FatalErr;
|
||||
use super::deployment_cfg_types::*;
|
||||
use super::{EnvVar, Error};
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct Deployment<'a> {
|
||||
|
@ -13,7 +13,7 @@ pub struct Deployment<'a> {
|
|||
}
|
||||
|
||||
impl Deployment<'_> {
|
||||
pub(crate) fn from_env(env: &EnvVar) -> Result<Self, FatalErr> {
|
||||
pub(crate) fn from_env(env: &EnvVar) -> Result<Self, Error> {
|
||||
let mut cfg = Self {
|
||||
env: Env::default().maybe_update(env.get("NODE_ENV"))?,
|
||||
log_level: LogLevel::default().maybe_update(env.get("RUST_LOG"))?,
|
||||
|
|
|
@ -61,6 +61,7 @@ impl fmt::Display for EnvVar {
|
|||
}
|
||||
}
|
||||
#[macro_export]
|
||||
#[doc(hidden)]
|
||||
macro_rules! maybe_update {
|
||||
($name:ident; $item: tt:$type:ty) => (
|
||||
pub(crate) fn $name(self, item: Option<$type>) -> Self {
|
||||
|
@ -76,7 +77,9 @@ macro_rules! maybe_update {
|
|||
None => Self { ..self }
|
||||
}
|
||||
})}
|
||||
|
||||
#[macro_export]
|
||||
#[doc(hidden)]
|
||||
macro_rules! from_env_var {
|
||||
($(#[$outer:meta])*
|
||||
let name = $name:ident;
|
||||
|
@ -106,15 +109,14 @@ macro_rules! from_env_var {
|
|||
fn inner_from_str($arg: &str) -> Option<$type> {
|
||||
$body
|
||||
}
|
||||
pub(crate) fn maybe_update(
|
||||
self,
|
||||
var: Option<&String>,
|
||||
) -> Result<Self, crate::err::FatalErr> {
|
||||
pub(crate) fn maybe_update(self, var: Option<&String>) -> Result<Self, super::Error> {
|
||||
Ok(match var {
|
||||
Some(empty_string) if empty_string.is_empty() => Self::default(),
|
||||
Some(value) => Self(Self::inner_from_str(value).ok_or_else(|| {
|
||||
crate::err::FatalErr::config($env_var, value, $allowed_values)
|
||||
})?),
|
||||
Some(value) => {
|
||||
Self(Self::inner_from_str(value).ok_or_else(|| {
|
||||
super::Error::config($env_var, value, $allowed_values)
|
||||
})?)
|
||||
}
|
||||
None => self,
|
||||
})
|
||||
}
|
||||
|
|
|
@ -1,17 +1,19 @@
|
|||
use super::{postgres_cfg_types::*, EnvVar};
|
||||
use crate::err::FatalErr;
|
||||
use super::postgres_cfg_types::*;
|
||||
use super::{EnvVar, Error};
|
||||
|
||||
use url::Url;
|
||||
use urlencoding;
|
||||
|
||||
type Result<T> = std::result::Result<T, FatalErr>;
|
||||
type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
/// Configuration values for Postgres
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Postgres {
|
||||
pub(crate) user: PgUser,
|
||||
pub(crate) host: PgHost,
|
||||
pub(crate) password: PgPass,
|
||||
pub(crate) database: PgDatabase,
|
||||
/// The name of the postgres database to connect to
|
||||
pub database: PgDatabase,
|
||||
pub(crate) port: PgPort,
|
||||
pub(crate) ssl_mode: PgSslMode,
|
||||
}
|
||||
|
@ -27,7 +29,7 @@ impl EnvVar {
|
|||
"password" => self.maybe_add_env_var("DB_PASS", Some(v.to_string())),
|
||||
"host" => self.maybe_add_env_var("DB_HOST", Some(v.to_string())),
|
||||
"sslmode" => self.maybe_add_env_var("DB_SSLMODE", Some(v.to_string())),
|
||||
_ => Err(FatalErr::config(
|
||||
_ => Err(Error::config(
|
||||
"POSTGRES_URL",
|
||||
&k,
|
||||
"a URL with parameters `password`, `user`, `host`, and `sslmode` only",
|
||||
|
|
|
@ -1,10 +1,9 @@
|
|||
use super::redis_cfg_types::*;
|
||||
use super::EnvVar;
|
||||
use crate::err::FatalErr;
|
||||
use super::{EnvVar, Error};
|
||||
|
||||
use url::Url;
|
||||
|
||||
type Result<T> = std::result::Result<T, FatalErr>;
|
||||
type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct Redis {
|
||||
|
@ -33,7 +32,7 @@ impl EnvVar {
|
|||
match k.to_string().as_str() {
|
||||
"password" => self.maybe_add_env_var("REDIS_PASSWORD", Some(v.to_string())),
|
||||
"db" => self.maybe_add_env_var("REDIS_DB", Some(v.to_string())),
|
||||
_ => Err(FatalErr::config(
|
||||
_ => Err(Error::config(
|
||||
"REDIS_URL",
|
||||
&k,
|
||||
"a URL with parameters `password`, `db`, only",
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use crate::from_env_var;
|
||||
use crate::from_env_var; //macro
|
||||
use std::time::Duration;
|
||||
//use std::{fmt, net::IpAddr, os::unix::net::UnixListener, str::FromStr, time::Duration};
|
||||
//use strum_macros::{EnumString, EnumVariantNames};
|
||||
|
|
74
src/err.rs
74
src/err.rs
|
@ -1,86 +1,80 @@
|
|||
use crate::request::RequestErr;
|
||||
use crate::response::ManagerErr;
|
||||
use crate::config;
|
||||
use crate::request;
|
||||
use crate::response;
|
||||
|
||||
use std::fmt;
|
||||
|
||||
pub enum FatalErr {
|
||||
ReceiverErr(ManagerErr),
|
||||
pub enum Error {
|
||||
Response(response::Error),
|
||||
Logger(log::SetLoggerError),
|
||||
Postgres(RequestErr),
|
||||
Postgres(request::Error),
|
||||
Unrecoverable,
|
||||
StdIo(std::io::Error),
|
||||
// config errs
|
||||
UrlParse(url::ParseError),
|
||||
UrlEncoding(urlencoding::FromUrlEncodingError),
|
||||
ConfigErr(String),
|
||||
Config(config::Error),
|
||||
}
|
||||
|
||||
impl FatalErr {
|
||||
impl Error {
|
||||
pub fn log(msg: impl fmt::Display) {
|
||||
eprintln!("{}", msg);
|
||||
}
|
||||
|
||||
pub fn config<T: fmt::Display>(var: T, value: T, allowed_vals: T) -> Self {
|
||||
Self::ConfigErr(format!(
|
||||
"{0} is set to `{1}`, which is invalid.\n{3:7}{0} must be {2}.",
|
||||
var, value, allowed_vals, ""
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for FatalErr {}
|
||||
impl fmt::Debug for FatalErr {
|
||||
impl std::error::Error for Error {}
|
||||
|
||||
impl fmt::Debug for Error {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
||||
write!(f, "{}", self)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for FatalErr {
|
||||
impl fmt::Display for Error {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
||||
use FatalErr::*;
|
||||
use Error::*;
|
||||
write!(
|
||||
f,
|
||||
"{}",
|
||||
match self {
|
||||
ReceiverErr(e) => format!("{}", e),
|
||||
Response(e) => format!("{}", e),
|
||||
Logger(e) => format!("{}", e),
|
||||
StdIo(e) => format!("{}", e),
|
||||
Postgres(e) => format!("could not connect to Postgres.\n{:7}{}", "", e),
|
||||
ConfigErr(e) => e.to_string(),
|
||||
UrlParse(e) => format!("could parse Postgres URL.\n{:7}{}", "", e),
|
||||
UrlEncoding(e) => format!("could not parse POSTGRES_URL.\n{:7}{:?}", "", e),
|
||||
Config(e) => format!("{}", e),
|
||||
Unrecoverable => "Flodgatt will now shut down.".into(),
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<RequestErr> for FatalErr {
|
||||
fn from(e: RequestErr) -> Self {
|
||||
#[doc(hidden)]
|
||||
impl From<request::Error> for Error {
|
||||
fn from(e: request::Error) -> Self {
|
||||
Self::Postgres(e)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ManagerErr> for FatalErr {
|
||||
fn from(e: ManagerErr) -> Self {
|
||||
Self::ReceiverErr(e)
|
||||
#[doc(hidden)]
|
||||
impl From<response::Error> for Error {
|
||||
fn from(e: response::Error) -> Self {
|
||||
Self::Response(e)
|
||||
}
|
||||
}
|
||||
impl From<urlencoding::FromUrlEncodingError> for FatalErr {
|
||||
fn from(e: urlencoding::FromUrlEncodingError) -> Self {
|
||||
Self::UrlEncoding(e)
|
||||
|
||||
#[doc(hidden)]
|
||||
impl From<config::Error> for Error {
|
||||
fn from(e: config::Error) -> Self {
|
||||
Self::Config(e)
|
||||
}
|
||||
}
|
||||
impl From<url::ParseError> for FatalErr {
|
||||
fn from(e: url::ParseError) -> Self {
|
||||
Self::UrlParse(e)
|
||||
}
|
||||
}
|
||||
impl From<std::io::Error> for FatalErr {
|
||||
|
||||
#[doc(hidden)]
|
||||
impl From<std::io::Error> for Error {
|
||||
fn from(e: std::io::Error) -> Self {
|
||||
Self::StdIo(e)
|
||||
}
|
||||
}
|
||||
impl From<log::SetLoggerError> for FatalErr {
|
||||
|
||||
#[doc(hidden)]
|
||||
impl From<log::SetLoggerError> for Error {
|
||||
fn from(e: log::SetLoggerError) -> Self {
|
||||
Self::Logger(e)
|
||||
}
|
||||
|
|
|
@ -1,134 +0,0 @@
|
|||
mod application;
|
||||
mod attachment;
|
||||
mod card;
|
||||
mod poll;
|
||||
|
||||
use super::account::Account;
|
||||
use super::emoji::Emoji;
|
||||
use super::id::Id;
|
||||
use super::mention::Mention;
|
||||
use super::tag::Tag;
|
||||
use super::visibility::Visibility;
|
||||
use application::Application;
|
||||
use attachment::Attachment;
|
||||
use card::Card;
|
||||
use poll::Poll;
|
||||
|
||||
use crate::request::Blocks;
|
||||
|
||||
use hashbrown::HashSet;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::boxed::Box;
|
||||
use std::string::String;
|
||||
|
||||
#[serde(deny_unknown_fields)]
|
||||
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
|
||||
pub struct Status {
|
||||
id: Id,
|
||||
uri: String,
|
||||
created_at: String,
|
||||
account: Account,
|
||||
content: String,
|
||||
visibility: Visibility,
|
||||
sensitive: bool,
|
||||
spoiler_text: String,
|
||||
media_attachments: Vec<Attachment>,
|
||||
application: Option<Application>, // Should be non-optional?
|
||||
mentions: Vec<Mention>,
|
||||
tags: Vec<Tag>,
|
||||
emojis: Vec<Emoji>,
|
||||
reblogs_count: i64,
|
||||
favourites_count: i64,
|
||||
replies_count: i64,
|
||||
url: Option<String>,
|
||||
in_reply_to_id: Option<Id>,
|
||||
in_reply_to_account_id: Option<Id>,
|
||||
reblog: Option<Box<Status>>,
|
||||
poll: Option<Poll>,
|
||||
card: Option<Card>,
|
||||
language: Option<String>,
|
||||
|
||||
text: Option<String>,
|
||||
// ↓↓↓ Only for authorized users
|
||||
favourited: Option<bool>,
|
||||
reblogged: Option<bool>,
|
||||
muted: Option<bool>,
|
||||
bookmarked: Option<bool>,
|
||||
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),
|
||||
}
|
||||
}
|
||||
|
||||
/// 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.
|
||||
///
|
||||
/// 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 {
|
||||
// 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();
|
||||
|
||||
// author
|
||||
involved_users.insert(Id(self.account.id.0));
|
||||
// replied-to user
|
||||
if let Some(user_id) = self.in_reply_to_account_id {
|
||||
involved_users.insert(Id(user_id.0));
|
||||
}
|
||||
// boosted user
|
||||
if let Some(boosted_status) = self.reblog.clone() {
|
||||
involved_users.insert(Id(boosted_status.account.id.0));
|
||||
}
|
||||
!involved_users.is_disjoint(blocked_users)
|
||||
}
|
||||
}
|
18
src/lib.rs
18
src/lib.rs
|
@ -35,12 +35,22 @@
|
|||
//! 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;
|
||||
|
||||
pub mod config;
|
||||
pub mod err;
|
||||
pub mod event;
|
||||
mod err;
|
||||
pub mod request;
|
||||
pub mod response;
|
||||
|
||||
/// A user ID.
|
||||
///
|
||||
/// Internally, Mastodon IDs are i64s, but are sent to clients as string because
|
||||
/// JavaScript numbers don't support i64s. This newtype serializes to/from a string, but
|
||||
/// keeps the i64 as the "true" value for internal use.
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
|
||||
#[doc(hidden)]
|
||||
pub struct Id(pub i64);
|
||||
|
|
40
src/main.rs
40
src/main.rs
|
@ -1,9 +1,7 @@
|
|||
use flodgatt::config;
|
||||
use flodgatt::err::FatalErr;
|
||||
use flodgatt::event::Event;
|
||||
use flodgatt::request::{Handler, Subscription, Timeline};
|
||||
use flodgatt::response::redis;
|
||||
use flodgatt::response::stream;
|
||||
use flodgatt::response::{Event, RedisManager, SseStream, WsStream};
|
||||
use flodgatt::Error;
|
||||
|
||||
use futures::{future::lazy, stream::Stream as _};
|
||||
use std::fs;
|
||||
|
@ -16,7 +14,7 @@ use tokio::timer::Interval;
|
|||
use warp::ws::Ws2;
|
||||
use warp::Filter;
|
||||
|
||||
fn main() -> Result<(), FatalErr> {
|
||||
fn main() -> Result<(), Error> {
|
||||
config::merge_dotenv()?;
|
||||
pretty_env_logger::try_init()?;
|
||||
let (postgres_cfg, redis_cfg, cfg) = config::from_env(dotenv::vars().collect())?;
|
||||
|
@ -27,7 +25,7 @@ fn main() -> Result<(), FatalErr> {
|
|||
let (cmd_tx, cmd_rx) = mpsc::unbounded_channel();
|
||||
|
||||
let request = Handler::new(&postgres_cfg, *cfg.whitelist_mode)?;
|
||||
let shared_manager = redis::Manager::try_from(&redis_cfg, event_tx, cmd_rx)?.into_arc();
|
||||
let shared_manager = RedisManager::try_from(&redis_cfg, event_tx, cmd_rx)?.into_arc();
|
||||
|
||||
// Server Sent Events
|
||||
let sse_manager = shared_manager.clone();
|
||||
|
@ -37,10 +35,10 @@ fn main() -> Result<(), FatalErr> {
|
|||
.and(warp::sse())
|
||||
.map(move |subscription: Subscription, sse: warp::sse::Sse| {
|
||||
log::info!("Incoming SSE request for {:?}", subscription.timeline);
|
||||
let mut manager = sse_manager.lock().unwrap_or_else(redis::Manager::recover);
|
||||
let mut manager = sse_manager.lock().unwrap_or_else(RedisManager::recover);
|
||||
manager.subscribe(&subscription);
|
||||
|
||||
stream::Sse::send_events(sse, sse_cmd_tx.clone(), subscription, sse_rx.clone())
|
||||
SseStream::send_events(sse, sse_cmd_tx.clone(), subscription, sse_rx.clone())
|
||||
})
|
||||
.with(warp::reply::with::header("Connection", "keep-alive"));
|
||||
|
||||
|
@ -51,10 +49,10 @@ fn main() -> Result<(), FatalErr> {
|
|||
.and(warp::ws::ws2())
|
||||
.map(move |subscription: Subscription, ws: Ws2| {
|
||||
log::info!("Incoming websocket request for {:?}", subscription.timeline);
|
||||
let mut manager = ws_manager.lock().unwrap_or_else(redis::Manager::recover);
|
||||
let mut manager = ws_manager.lock().unwrap_or_else(RedisManager::recover);
|
||||
manager.subscribe(&subscription);
|
||||
let token = subscription.access_token.clone().unwrap_or_default(); // token sent for security
|
||||
let ws_stream = stream::Ws::new(cmd_tx.clone(), event_rx.clone(), subscription);
|
||||
let ws_stream = WsStream::new(cmd_tx.clone(), event_rx.clone(), subscription);
|
||||
|
||||
(ws.on_upgrade(move |ws| ws_stream.send_to(ws)), token)
|
||||
})
|
||||
|
@ -66,9 +64,9 @@ fn main() -> Result<(), FatalErr> {
|
|||
let (r1, r3) = (shared_manager.clone(), shared_manager.clone());
|
||||
request.health().map(|| "OK")
|
||||
.or(request.status()
|
||||
.map(move || r1.lock().unwrap_or_else(redis::Manager::recover).count()))
|
||||
.map(move || r1.lock().unwrap_or_else(RedisManager::recover).count()))
|
||||
.or(request.status_per_timeline()
|
||||
.map(move || r3.lock().unwrap_or_else(redis::Manager::recover).list()))
|
||||
.map(move || r3.lock().unwrap_or_else(RedisManager::recover).list()))
|
||||
};
|
||||
#[cfg(not(feature = "stub_status"))]
|
||||
let status = request.health().map(|| "OK");
|
||||
|
@ -78,22 +76,14 @@ fn main() -> Result<(), FatalErr> {
|
|||
.allow_methods(cfg.cors.allowed_methods)
|
||||
.allow_headers(cfg.cors.allowed_headers);
|
||||
|
||||
// use futures::future::Future;
|
||||
let streaming_server = move || {
|
||||
let manager = shared_manager.clone();
|
||||
let stream = Interval::new(Instant::now(), poll_freq)
|
||||
// .take(1200)
|
||||
.map_err(|e| log::error!("{}", e))
|
||||
.for_each(
|
||||
move |_| {
|
||||
let mut manager = manager.lock().unwrap_or_else(redis::Manager::recover);
|
||||
manager.poll_broadcast().map_err(FatalErr::log)
|
||||
}, // ).and_then(|_| {
|
||||
// log::info!("shutting down!");
|
||||
// std::process::exit(0);
|
||||
// futures::future::ok(())
|
||||
// }
|
||||
);
|
||||
.for_each(move |_| {
|
||||
let mut manager = manager.lock().unwrap_or_else(RedisManager::recover);
|
||||
manager.poll_broadcast().map_err(Error::log)
|
||||
});
|
||||
|
||||
warp::spawn(lazy(move || stream));
|
||||
warp::serve(ws.or(sse).with(cors).or(status).recover(Handler::err))
|
||||
|
@ -109,5 +99,5 @@ fn main() -> Result<(), FatalErr> {
|
|||
let server_addr = SocketAddr::new(*cfg.address, *cfg.port);
|
||||
tokio::run(lazy(move || streaming_server().bind(server_addr)));
|
||||
}
|
||||
Err(FatalErr::Unrecoverable) // on get here if there's an unrecoverable error in poll_broadcast.
|
||||
Err(Error::Unrecoverable) // only get here if there's an unrecoverable error in poll_broadcast.
|
||||
}
|
||||
|
|
|
@ -1,21 +1,19 @@
|
|||
//! Parse the client request and return a Subscription
|
||||
mod postgres;
|
||||
mod query;
|
||||
pub mod timeline;
|
||||
mod timeline;
|
||||
|
||||
mod err;
|
||||
mod subscription;
|
||||
|
||||
pub(crate) use self::err::RequestErr;
|
||||
pub(crate) use self::postgres::PgPool;
|
||||
|
||||
pub(crate) use subscription::Blocks;
|
||||
pub use subscription::Subscription;
|
||||
pub use err::{Error, Timeline as TimelineErr};
|
||||
pub use subscription::{Blocks, Subscription};
|
||||
pub use timeline::Timeline;
|
||||
pub(crate) use timeline::{Content, Reach, Stream, TimelineErr};
|
||||
use timeline::{Content, Reach, Stream};
|
||||
|
||||
pub use self::postgres::PgPool;
|
||||
use self::query::Query;
|
||||
use crate::config;
|
||||
use crate::config::Postgres;
|
||||
use warp::filters::BoxedFilter;
|
||||
use warp::http::StatusCode;
|
||||
use warp::path;
|
||||
|
@ -26,7 +24,7 @@ mod sse_test;
|
|||
#[cfg(test)]
|
||||
mod ws_test;
|
||||
|
||||
type Result<T> = std::result::Result<T, err::RequestErr>;
|
||||
type Result<T> = std::result::Result<T, err::Error>;
|
||||
|
||||
/// Helper macro to match on the first of any of the provided filters
|
||||
macro_rules! any_of {
|
||||
|
@ -62,7 +60,7 @@ pub struct Handler {
|
|||
}
|
||||
|
||||
impl Handler {
|
||||
pub fn new(postgres_cfg: &config::Postgres, whitelist_mode: bool) -> Result<Self> {
|
||||
pub fn new(postgres_cfg: &Postgres, whitelist_mode: bool) -> Result<Self> {
|
||||
Ok(Self {
|
||||
pg_conn: PgPool::new(postgres_cfg, whitelist_mode)?,
|
||||
})
|
||||
|
|
|
@ -1,15 +1,15 @@
|
|||
use std::fmt;
|
||||
#[derive(Debug)]
|
||||
pub enum RequestErr {
|
||||
pub enum Error {
|
||||
PgPool(r2d2::Error),
|
||||
Pg(postgres::Error),
|
||||
}
|
||||
|
||||
impl std::error::Error for RequestErr {}
|
||||
impl std::error::Error for Error {}
|
||||
|
||||
impl fmt::Display for RequestErr {
|
||||
impl fmt::Display for Error {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
||||
use RequestErr::*;
|
||||
use Error::*;
|
||||
let msg = match self {
|
||||
PgPool(e) => format!("{}", e),
|
||||
Pg(e) => format!("{}", e),
|
||||
|
@ -18,13 +18,40 @@ impl fmt::Display for RequestErr {
|
|||
}
|
||||
}
|
||||
|
||||
impl From<r2d2::Error> for RequestErr {
|
||||
impl From<r2d2::Error> for Error {
|
||||
fn from(e: r2d2::Error) -> Self {
|
||||
Self::PgPool(e)
|
||||
}
|
||||
}
|
||||
impl From<postgres::Error> for RequestErr {
|
||||
impl From<postgres::Error> for Error {
|
||||
fn from(e: postgres::Error) -> Self {
|
||||
Self::Pg(e)
|
||||
}
|
||||
}
|
||||
// TODO make Timeline & TimelineErr their own top-level module
|
||||
#[derive(Debug)]
|
||||
pub enum Timeline {
|
||||
MissingHashtag,
|
||||
InvalidInput,
|
||||
BadTag,
|
||||
}
|
||||
|
||||
impl std::error::Error for Timeline {}
|
||||
|
||||
impl From<std::num::ParseIntError> for Timeline {
|
||||
fn from(_error: std::num::ParseIntError) -> Self {
|
||||
Self::InvalidInput
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Timeline {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
||||
use Timeline::*;
|
||||
let msg = match self {
|
||||
InvalidInput => "The timeline text from Redis could not be parsed into a supported timeline. TODO: add incoming timeline text",
|
||||
MissingHashtag => "Attempted to send a hashtag timeline without supplying a tag name",
|
||||
BadTag => "No hashtag exists with the specified hashtag ID"
|
||||
};
|
||||
write!(f, "{}", msg)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
use super::err;
|
||||
use super::timeline::{Scope, UserData};
|
||||
use crate::config;
|
||||
use crate::event::Id;
|
||||
use crate::Id;
|
||||
|
||||
use ::postgres;
|
||||
use hashbrown::HashSet;
|
||||
|
@ -15,7 +15,7 @@ pub struct PgPool {
|
|||
whitelist_mode: bool,
|
||||
}
|
||||
|
||||
type Result<T> = std::result::Result<T, err::RequestErr>;
|
||||
type Result<T> = std::result::Result<T, err::Error>;
|
||||
type Rejectable<T> = std::result::Result<T, warp::Rejection>;
|
||||
|
||||
impl PgPool {
|
||||
|
|
|
@ -3,7 +3,6 @@
|
|||
// #[cfg(test)]
|
||||
// mod test {
|
||||
// use super::*;
|
||||
// use crate::parse_client_request::user::{Blocks, Filter, OauthScope, PgPool};
|
||||
|
||||
// macro_rules! test_public_endpoint {
|
||||
// ($name:ident {
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
use super::postgres::PgPool;
|
||||
use super::query::Query;
|
||||
use super::{Content, Reach, Stream, Timeline};
|
||||
use crate::event::Id;
|
||||
use crate::Id;
|
||||
|
||||
use hashbrown::HashSet;
|
||||
|
||||
|
@ -17,17 +17,19 @@ use warp::reject::Rejection;
|
|||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct Subscription {
|
||||
pub timeline: Timeline,
|
||||
pub(crate) allowed_langs: HashSet<String>,
|
||||
pub(crate) blocks: Blocks,
|
||||
pub(crate) hashtag_name: Option<String>,
|
||||
pub allowed_langs: HashSet<String>,
|
||||
/// [Blocks](./request/struct.Blocks.html)
|
||||
pub blocks: Blocks,
|
||||
pub hashtag_name: Option<String>,
|
||||
pub access_token: Option<String>,
|
||||
}
|
||||
|
||||
/// Blocked and muted users and domains
|
||||
#[derive(Clone, Default, Debug, PartialEq)]
|
||||
pub(crate) struct Blocks {
|
||||
pub(crate) blocked_domains: HashSet<String>,
|
||||
pub(crate) blocked_users: HashSet<Id>,
|
||||
pub(crate) blocking_users: HashSet<Id>,
|
||||
pub struct Blocks {
|
||||
pub blocked_domains: HashSet<String>,
|
||||
pub blocked_users: HashSet<Id>,
|
||||
pub blocking_users: HashSet<Id>,
|
||||
}
|
||||
|
||||
impl Default for Subscription {
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
pub(crate) use self::err::TimelineErr;
|
||||
pub(crate) use self::inner::{Content, Reach, Scope, Stream, UserData};
|
||||
use super::err::Timeline as Error;
|
||||
use super::query::Query;
|
||||
|
||||
use lru::LruCache;
|
||||
|
@ -8,7 +8,7 @@ use warp::reject::Rejection;
|
|||
mod err;
|
||||
mod inner;
|
||||
|
||||
type Result<T> = std::result::Result<T, TimelineErr>;
|
||||
type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
#[derive(Clone, Debug, Copy, Eq, Hash, PartialEq)]
|
||||
pub struct Timeline(pub(crate) Stream, pub(crate) Reach, pub(crate) Content);
|
||||
|
@ -18,9 +18,25 @@ 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::*, Reach::*, Stream::*, TimelineErr::*};
|
||||
use {Content::*, Error::*, Reach::*, Stream::*};
|
||||
|
||||
Ok(match self {
|
||||
Timeline(Public, Federated, All) => "timeline:public".to_string(),
|
||||
|
@ -42,7 +58,7 @@ impl Timeline {
|
|||
}
|
||||
Timeline(List(id), Federated, All) => ["timeline:list:", &id.to_string()].concat(),
|
||||
Timeline(Direct(id), Federated, All) => ["timeline:direct:", &id.to_string()].concat(),
|
||||
Timeline(_one, _two, _three) => Err(TimelineErr::InvalidInput)?,
|
||||
Timeline(_one, _two, _three) => Err(Error::InvalidInput)?,
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -50,7 +66,7 @@ impl Timeline {
|
|||
timeline: &str,
|
||||
cache: &mut LruCache<String, i64>,
|
||||
) -> Result<Self> {
|
||||
use {Content::*, Reach::*, Stream::*, TimelineErr::*};
|
||||
use {Content::*, Error::*, Reach::*, Stream::*};
|
||||
let mut tag_id = |t: &str| cache.get(&t.to_string()).map_or(Err(BadTag), |id| Ok(*id));
|
||||
|
||||
Ok(match &timeline.split(':').collect::<Vec<&str>>()[..] {
|
||||
|
|
|
@ -1,28 +1 @@
|
|||
use std::fmt;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum TimelineErr {
|
||||
MissingHashtag,
|
||||
InvalidInput,
|
||||
BadTag,
|
||||
}
|
||||
|
||||
impl std::error::Error for TimelineErr {}
|
||||
|
||||
impl From<std::num::ParseIntError> for TimelineErr {
|
||||
fn from(_error: std::num::ParseIntError) -> Self {
|
||||
Self::InvalidInput
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for TimelineErr {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
||||
use TimelineErr::*;
|
||||
let msg = match self {
|
||||
InvalidInput => "The timeline text from Redis could not be parsed into a supported timeline. TODO: add incoming timeline text",
|
||||
MissingHashtag => "Attempted to send a hashtag timeline without supplying a tag name",
|
||||
BadTag => "No hashtag exists with the specified hashtag ID"
|
||||
};
|
||||
write!(f, "{}", msg)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use super::TimelineErr;
|
||||
use crate::event::Id;
|
||||
use super::Error;
|
||||
use crate::Id;
|
||||
|
||||
use hashbrown::HashSet;
|
||||
use std::convert::TryFrom;
|
||||
|
@ -36,18 +36,18 @@ pub(crate) enum Scope {
|
|||
}
|
||||
|
||||
impl TryFrom<&str> for Scope {
|
||||
type Error = TimelineErr;
|
||||
type Error = Error;
|
||||
|
||||
fn try_from(s: &str) -> Result<Self, TimelineErr> {
|
||||
fn try_from(s: &str) -> Result<Self, Error> {
|
||||
match s {
|
||||
"read" => Ok(Scope::Read),
|
||||
"read:statuses" => Ok(Scope::Statuses),
|
||||
"read:notifications" => Ok(Scope::Notifications),
|
||||
"read:lists" => Ok(Scope::Lists),
|
||||
"write" | "follow" => Err(TimelineErr::InvalidInput), // ignore write scopes
|
||||
"write" | "follow" => Err(Error::InvalidInput), // ignore write scopes
|
||||
unexpected => {
|
||||
log::warn!("Ignoring unknown scope `{}`", unexpected);
|
||||
Err(TimelineErr::InvalidInput)
|
||||
Err(Error::InvalidInput)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,14 +1,8 @@
|
|||
//! Filters for the WebSocket endpoint
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// #[cfg(test)]
|
||||
// mod test {
|
||||
// use super::*;
|
||||
// use crate::parse_client_request::user::{Blocks, Filter, OauthScope};
|
||||
|
||||
// macro_rules! test_public_endpoint {
|
||||
// ($name:ident {
|
||||
|
|
|
@ -1,9 +1,17 @@
|
|||
//! Stream the updates appropriate for a given `User`/`timeline` pair from Redis.
|
||||
|
||||
pub mod redis;
|
||||
pub mod stream;
|
||||
pub use event::Event;
|
||||
pub use redis::Manager as RedisManager;
|
||||
pub use stream::{Sse as SseStream, Ws as WsStream};
|
||||
|
||||
pub(crate) use redis::ManagerErr;
|
||||
pub(self) use event::err::Event as EventErr;
|
||||
pub(self) use event::Payload;
|
||||
|
||||
pub(crate) mod event;
|
||||
mod redis;
|
||||
mod stream;
|
||||
|
||||
pub use redis::Error;
|
||||
|
||||
#[cfg(feature = "bench")]
|
||||
pub use redis::msg::{RedisMsg, RedisParseOutput};
|
||||
|
|
|
@ -1,11 +1,12 @@
|
|||
mod checked_event;
|
||||
mod dynamic_event;
|
||||
mod err;
|
||||
pub mod err;
|
||||
|
||||
pub(crate) use checked_event::{CheckedEvent, Id};
|
||||
pub(crate) use dynamic_event::{DynEvent, EventKind};
|
||||
pub(crate) use err::EventErr;
|
||||
use self::checked_event::CheckedEvent;
|
||||
use self::dynamic_event::{DynEvent, EventKind};
|
||||
use crate::Id;
|
||||
|
||||
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 {
|
||||
|
@ -84,14 +117,14 @@ impl Event {
|
|||
}
|
||||
|
||||
impl TryFrom<String> for Event {
|
||||
type Error = EventErr;
|
||||
type Error = err::Event;
|
||||
|
||||
fn try_from(event_txt: String) -> Result<Event, Self::Error> {
|
||||
Event::try_from(event_txt.as_str())
|
||||
}
|
||||
}
|
||||
impl TryFrom<&str> for Event {
|
||||
type Error = EventErr;
|
||||
type Error = err::Event;
|
||||
|
||||
fn try_from(event_txt: &str) -> Result<Event, Self::Error> {
|
||||
match serde_json::from_str(event_txt) {
|
|
@ -11,13 +11,13 @@ mod status;
|
|||
mod tag;
|
||||
mod visibility;
|
||||
|
||||
use announcement::Announcement;
|
||||
pub(in crate::event) use announcement_reaction::AnnouncementReaction;
|
||||
use conversation::Conversation;
|
||||
pub(crate) use id::Id;
|
||||
use notification::Notification;
|
||||
use status::Status;
|
||||
pub(self) use super::Payload;
|
||||
pub(super) use announcement_reaction::AnnouncementReaction;
|
||||
pub(crate) use status::Status;
|
||||
|
||||
use announcement::Announcement;
|
||||
use conversation::Conversation;
|
||||
use notification::Notification;
|
||||
use serde::Deserialize;
|
||||
|
||||
#[serde(rename_all = "snake_case", tag = "event", deny_unknown_fields)]
|
|
@ -1,4 +1,5 @@
|
|||
use super::{emoji::Emoji, id::Id, visibility::Visibility};
|
||||
use super::{emoji::Emoji, visibility::Visibility};
|
||||
use crate::Id;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[serde(deny_unknown_fields)]
|
|
@ -1,4 +1,5 @@
|
|||
use super::super::EventErr;
|
||||
use super::super::err;
|
||||
use crate::Id;
|
||||
|
||||
use serde::{
|
||||
de::{self, Visitor},
|
||||
|
@ -7,19 +8,11 @@ use serde::{
|
|||
use serde_json::Value;
|
||||
use std::{convert::TryFrom, fmt, num::ParseIntError, str::FromStr};
|
||||
|
||||
/// A user ID.
|
||||
///
|
||||
/// Internally, Mastodon IDs are i64s, but are sent to clients as string because
|
||||
/// JavaScript numbers don't support i64s. This newtype serializes to/from a string, but
|
||||
/// keeps the i64 as the "true" value for internal use.
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct Id(pub i64);
|
||||
|
||||
impl TryFrom<&Value> for Id {
|
||||
type Error = EventErr;
|
||||
type Error = err::Event;
|
||||
|
||||
fn try_from(v: &Value) -> Result<Self, Self::Error> {
|
||||
Ok(v.as_str().ok_or(EventErr::DynParse)?.parse()?)
|
||||
Ok(v.as_str().ok_or(err::Event::DynParse)?.parse()?)
|
||||
}
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
use super::id::Id;
|
||||
use crate::Id;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[serde(deny_unknown_fields)]
|
|
@ -0,0 +1,103 @@
|
|||
mod application;
|
||||
mod attachment;
|
||||
mod card;
|
||||
mod poll;
|
||||
|
||||
use super::account::Account;
|
||||
use super::emoji::Emoji;
|
||||
use super::mention::Mention;
|
||||
use super::tag::Tag;
|
||||
use super::visibility::Visibility;
|
||||
use super::Payload;
|
||||
use crate::Id;
|
||||
use application::Application;
|
||||
use attachment::Attachment;
|
||||
use card::Card;
|
||||
use hashbrown::HashSet;
|
||||
use poll::Poll;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::boxed::Box;
|
||||
use std::string::String;
|
||||
|
||||
#[serde(deny_unknown_fields)]
|
||||
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
|
||||
pub struct Status {
|
||||
id: Id,
|
||||
uri: String,
|
||||
created_at: String,
|
||||
account: Account,
|
||||
content: String,
|
||||
visibility: Visibility,
|
||||
sensitive: bool,
|
||||
spoiler_text: String,
|
||||
media_attachments: Vec<Attachment>,
|
||||
application: Option<Application>, // Should be non-optional?
|
||||
mentions: Vec<Mention>,
|
||||
tags: Vec<Tag>,
|
||||
emojis: Vec<Emoji>,
|
||||
reblogs_count: i64,
|
||||
favourites_count: i64,
|
||||
replies_count: i64,
|
||||
url: Option<String>,
|
||||
in_reply_to_id: Option<Id>,
|
||||
in_reply_to_account_id: Option<Id>,
|
||||
reblog: Option<Box<Status>>,
|
||||
poll: Option<Poll>,
|
||||
card: Option<Card>,
|
||||
pub(crate) language: Option<String>,
|
||||
|
||||
text: Option<String>,
|
||||
// ↓↓↓ Only for authorized users
|
||||
favourited: Option<bool>,
|
||||
reblogged: Option<bool>,
|
||||
muted: Option<bool>,
|
||||
bookmarked: Option<bool>,
|
||||
pinned: Option<bool>,
|
||||
}
|
||||
|
||||
impl Payload for Status {
|
||||
fn language_unset(&self) -> bool {
|
||||
match &self.language {
|
||||
None => true,
|
||||
Some(empty) if empty == &String::new() => true,
|
||||
Some(_language) => false,
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
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(|m| Id(m.id.0)).collect();
|
||||
|
||||
// author
|
||||
involved_users.insert(Id(self.account.id.0));
|
||||
// replied-to user
|
||||
if let Some(user_id) = self.in_reply_to_account_id {
|
||||
involved_users.insert(Id(user_id.0));
|
||||
}
|
||||
// boosted user
|
||||
if let Some(boosted_status) = self.reblog.clone() {
|
||||
involved_users.insert(Id(boosted_status.account.id.0));
|
||||
}
|
||||
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
|
||||
}
|
||||
}
|
|
@ -1,5 +1,6 @@
|
|||
use super::{EventErr, Id};
|
||||
use crate::request::Blocks;
|
||||
use super::err;
|
||||
use super::Payload;
|
||||
use crate::Id;
|
||||
|
||||
use std::convert::TryFrom;
|
||||
|
||||
|
@ -38,7 +39,7 @@ pub(crate) struct DynStatus {
|
|||
pub(crate) boosted_user: Option<Id>,
|
||||
}
|
||||
|
||||
type Result<T> = std::result::Result<T, EventErr>;
|
||||
type Result<T> = std::result::Result<T, err::Event>;
|
||||
|
||||
impl DynEvent {
|
||||
pub(crate) fn set_update(self) -> Result<Self> {
|
||||
|
@ -50,16 +51,13 @@ impl DynEvent {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl DynStatus {
|
||||
pub(crate) fn new(payload: &Value) -> Result<Self> {
|
||||
use EventErr::*;
|
||||
|
||||
Ok(Self {
|
||||
id: Id::try_from(&payload["account"]["id"])?,
|
||||
username: payload["account"]["acct"]
|
||||
.as_str()
|
||||
.ok_or(DynParse)?
|
||||
.ok_or(err::Event::DynParse)?
|
||||
.to_string(),
|
||||
language: payload["language"].as_str().map(String::from),
|
||||
mentioned_users: HashSet::new(),
|
||||
|
@ -67,68 +65,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
|
||||
}
|
||||
}
|
|
@ -1,17 +1,17 @@
|
|||
use std::{fmt, num::ParseIntError};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum EventErr {
|
||||
pub enum Event {
|
||||
SerdeParse(serde_json::Error),
|
||||
NonNumId(ParseIntError),
|
||||
DynParse,
|
||||
}
|
||||
|
||||
impl std::error::Error for EventErr {}
|
||||
impl std::error::Error for Event {}
|
||||
|
||||
impl fmt::Display for EventErr {
|
||||
impl fmt::Display for Event {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
||||
use EventErr::*;
|
||||
use Event::*;
|
||||
match self {
|
||||
SerdeParse(inner) => write!(f, "{}", inner),
|
||||
NonNumId(inner) => write!(f, "ID could not be parsed: {}", inner),
|
||||
|
@ -21,12 +21,12 @@ impl fmt::Display for EventErr {
|
|||
}
|
||||
}
|
||||
|
||||
impl From<ParseIntError> for EventErr {
|
||||
impl From<ParseIntError> for Event {
|
||||
fn from(error: ParseIntError) -> Self {
|
||||
Self::NonNumId(error)
|
||||
}
|
||||
}
|
||||
impl From<serde_json::Error> for EventErr {
|
||||
impl From<serde_json::Error> for Event {
|
||||
fn from(error: serde_json::Error) -> Self {
|
||||
Self::SerdeParse(error)
|
||||
}
|
|
@ -2,18 +2,21 @@ mod connection;
|
|||
mod manager;
|
||||
mod msg;
|
||||
|
||||
pub(crate) use connection::{RedisConn, RedisConnErr};
|
||||
pub(self) use super::{Event, EventErr};
|
||||
pub(self) use connection::RedisConn;
|
||||
pub use manager::Error;
|
||||
pub use manager::Manager;
|
||||
pub(crate) use manager::ManagerErr;
|
||||
pub(crate) use msg::RedisParseErr;
|
||||
|
||||
pub(crate) enum RedisCmd {
|
||||
use connection::RedisConnErr;
|
||||
use msg::RedisParseErr;
|
||||
|
||||
enum RedisCmd {
|
||||
Subscribe,
|
||||
Unsubscribe,
|
||||
}
|
||||
|
||||
impl RedisCmd {
|
||||
pub(crate) fn into_sendable(self, tl: &str) -> (Vec<u8>, Vec<u8>) {
|
||||
fn into_sendable(self, tl: &str) -> (Vec<u8>, Vec<u8>) {
|
||||
match self {
|
||||
RedisCmd::Subscribe => (
|
||||
[
|
||||
|
|
|
@ -2,10 +2,11 @@ mod err;
|
|||
pub(crate) use err::RedisConnErr;
|
||||
|
||||
use super::msg::{RedisParseErr, RedisParseOutput};
|
||||
use super::{ManagerErr, RedisCmd};
|
||||
use super::Error as ManagerErr;
|
||||
use super::Event;
|
||||
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;
|
||||
|
@ -18,7 +19,7 @@ use std::time::Duration;
|
|||
type Result<T> = std::result::Result<T, RedisConnErr>;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct RedisConn {
|
||||
pub(super) struct RedisConn {
|
||||
primary: TcpStream,
|
||||
secondary: TcpStream,
|
||||
redis_namespace: Option<String>,
|
||||
|
@ -29,7 +30,7 @@ pub(crate) struct RedisConn {
|
|||
}
|
||||
|
||||
impl RedisConn {
|
||||
pub(crate) fn new(redis_cfg: &Redis) -> Result<Self> {
|
||||
pub(super) fn new(redis_cfg: &Redis) -> Result<Self> {
|
||||
let addr = [&*redis_cfg.host, ":", &*redis_cfg.port.to_string()].concat();
|
||||
|
||||
let conn = Self::new_connection(&addr, redis_cfg.password.as_ref())?;
|
||||
|
@ -50,7 +51,7 @@ impl RedisConn {
|
|||
Ok(redis_conn)
|
||||
}
|
||||
|
||||
pub(crate) fn poll_redis(&mut self) -> Poll<Option<(Timeline, Event)>, ManagerErr> {
|
||||
pub(super) fn poll_redis(&mut self) -> Poll<Option<(Timeline, Event)>, ManagerErr> {
|
||||
loop {
|
||||
match self.primary.read(&mut self.redis_input[self.cursor..]) {
|
||||
Ok(n) => {
|
||||
|
@ -111,18 +112,15 @@ impl RedisConn {
|
|||
res
|
||||
}
|
||||
|
||||
pub(crate) fn update_cache(&mut self, hashtag: String, id: i64) {
|
||||
pub(super) fn update_cache(&mut self, hashtag: String, id: i64) {
|
||||
self.tag_id_cache.put(hashtag.clone(), id);
|
||||
self.tag_name_cache.put(id, hashtag);
|
||||
}
|
||||
|
||||
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)?;
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use crate::request::TimelineErr;
|
||||
use crate::request;
|
||||
use std::fmt;
|
||||
|
||||
#[derive(Debug)]
|
||||
|
@ -9,11 +9,11 @@ pub enum RedisConnErr {
|
|||
IncorrectPassword(String),
|
||||
MissingPassword,
|
||||
NotRedis(String),
|
||||
TimelineErr(TimelineErr),
|
||||
TimelineErr(request::TimelineErr),
|
||||
}
|
||||
|
||||
impl RedisConnErr {
|
||||
pub(crate) fn with_addr<T: AsRef<str>>(address: T, inner: std::io::Error) -> Self {
|
||||
pub(super) fn with_addr<T: AsRef<str>>(address: T, inner: std::io::Error) -> Self {
|
||||
Self::ConnectionErr {
|
||||
addr: address.as_ref().to_string(),
|
||||
inner,
|
||||
|
@ -57,8 +57,8 @@ impl fmt::Display for RedisConnErr {
|
|||
}
|
||||
}
|
||||
|
||||
impl From<TimelineErr> for RedisConnErr {
|
||||
fn from(e: TimelineErr) -> RedisConnErr {
|
||||
impl From<request::TimelineErr> for RedisConnErr {
|
||||
fn from(e: request::TimelineErr) -> RedisConnErr {
|
||||
RedisConnErr::TimelineErr(e)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,12 +2,14 @@
|
|||
//! polled by the correct `ClientAgent`. Also manages sububscriptions and
|
||||
//! unsubscriptions to/from Redis.
|
||||
mod err;
|
||||
pub(crate) use err::ManagerErr;
|
||||
pub use err::Error;
|
||||
|
||||
use super::Event;
|
||||
use super::{RedisCmd, RedisConn};
|
||||
use crate::config;
|
||||
use crate::event::Event;
|
||||
use crate::request::{Stream, Subscription, Timeline};
|
||||
use crate::request::{Subscription, Timeline};
|
||||
|
||||
pub(self) use super::EventErr;
|
||||
|
||||
use futures::{Async, Stream as _Stream};
|
||||
use hashbrown::HashMap;
|
||||
|
@ -15,7 +17,7 @@ use std::sync::{Arc, Mutex, MutexGuard, PoisonError};
|
|||
use std::time::{Duration, Instant};
|
||||
use tokio::sync::{mpsc, watch};
|
||||
|
||||
type Result<T> = std::result::Result<T, ManagerErr>;
|
||||
type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
/// The item that streams from Redis and is polled by the `ClientAgent`
|
||||
#[derive(Debug)]
|
||||
|
@ -50,7 +52,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);
|
||||
};
|
||||
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
use super::super::{RedisConnErr, RedisParseErr};
|
||||
use crate::event::{Event, EventErr};
|
||||
use super::{Event, EventErr};
|
||||
use crate::request::{Timeline, TimelineErr};
|
||||
|
||||
use std::fmt;
|
||||
#[derive(Debug)]
|
||||
pub enum ManagerErr {
|
||||
pub enum Error {
|
||||
InvalidId,
|
||||
TimelineErr(TimelineErr),
|
||||
EventErr(EventErr),
|
||||
|
@ -13,11 +13,11 @@ pub enum ManagerErr {
|
|||
ChannelSendErr(tokio::sync::watch::error::SendError<(Timeline, Event)>),
|
||||
}
|
||||
|
||||
impl std::error::Error for ManagerErr {}
|
||||
impl std::error::Error for Error {}
|
||||
|
||||
impl fmt::Display for ManagerErr {
|
||||
impl fmt::Display for Error {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
||||
use ManagerErr::*;
|
||||
use Error::*;
|
||||
match self {
|
||||
InvalidId => write!(
|
||||
f,
|
||||
|
@ -33,31 +33,31 @@ impl fmt::Display for ManagerErr {
|
|||
}
|
||||
}
|
||||
|
||||
impl From<tokio::sync::watch::error::SendError<(Timeline, Event)>> for ManagerErr {
|
||||
impl From<tokio::sync::watch::error::SendError<(Timeline, Event)>> for Error {
|
||||
fn from(error: tokio::sync::watch::error::SendError<(Timeline, Event)>) -> Self {
|
||||
Self::ChannelSendErr(error)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<EventErr> for ManagerErr {
|
||||
impl From<EventErr> for Error {
|
||||
fn from(error: EventErr) -> Self {
|
||||
Self::EventErr(error)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<RedisConnErr> for ManagerErr {
|
||||
impl From<RedisConnErr> for Error {
|
||||
fn from(e: RedisConnErr) -> Self {
|
||||
Self::RedisConnErr(e)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<TimelineErr> for ManagerErr {
|
||||
impl From<TimelineErr> for Error {
|
||||
fn from(e: TimelineErr) -> Self {
|
||||
Self::TimelineErr(e)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<RedisParseErr> for ManagerErr {
|
||||
impl From<RedisParseErr> for Error {
|
||||
fn from(e: RedisParseErr) -> Self {
|
||||
Self::RedisParseErr(e)
|
||||
}
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
pub use sse::Sse;
|
||||
pub use ws::Ws;
|
||||
|
||||
pub(self) use super::{Event, Payload};
|
||||
|
||||
mod sse;
|
||||
mod ws;
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use crate::event::Event;
|
||||
use super::{Event, Payload};
|
||||
use crate::request::{Subscription, Timeline};
|
||||
|
||||
use futures::stream::Stream;
|
||||
|
@ -18,41 +18,19 @@ impl Sse {
|
|||
sse_rx: watch::Receiver<(Timeline, Event)>,
|
||||
) -> impl Reply {
|
||||
let target_timeline = subscription.timeline;
|
||||
let allowed_langs = subscription.allowed_langs;
|
||||
let blocks = subscription.blocks;
|
||||
|
||||
let event_stream = sse_rx
|
||||
.filter(move |(timeline, _)| target_timeline == *timeline)
|
||||
.filter_map(move |(timeline, event)| {
|
||||
use crate::event::{
|
||||
CheckedEvent, CheckedEvent::Update, DynEvent, Event::*, EventKind,
|
||||
};
|
||||
|
||||
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,
|
||||
_ => Event::TypeSafe(CheckedEvent::Update { payload, queued_at })
|
||||
.to_warp_reply(),
|
||||
},
|
||||
TypeSafe(non_update) => Event::TypeSafe(non_update).to_warp_reply(),
|
||||
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,
|
||||
_ => Dynamic(DynEvent {
|
||||
kind: EventKind::Update(s),
|
||||
..dyn_event
|
||||
})
|
||||
.to_warp_reply(),
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
.filter_map(move |(_timeline, event)| {
|
||||
match (event.update_payload(), event.dyn_update_payload()) {
|
||||
(Some(update), _) if Sse::update_not_filtered(subscription.clone(), update) => {
|
||||
event.to_warp_reply()
|
||||
}
|
||||
Ping => None, // pings handled automatically
|
||||
(None, None) => event.to_warp_reply(), // send all non-updates
|
||||
(_, Some(update)) if Sse::update_not_filtered(subscription.clone(), update) => {
|
||||
event.to_warp_reply()
|
||||
}
|
||||
(_, _) => None,
|
||||
}
|
||||
})
|
||||
.then(move |res| {
|
||||
|
@ -69,4 +47,23 @@ impl Sse {
|
|||
.stream(event_stream),
|
||||
)
|
||||
}
|
||||
|
||||
fn update_not_filtered(subscription: Subscription, update: &impl Payload) -> bool {
|
||||
let blocks = &subscription.blocks;
|
||||
let allowed_langs = &subscription.allowed_langs;
|
||||
|
||||
match subscription.timeline {
|
||||
tl if tl.is_public()
|
||||
&& !update.language_unset()
|
||||
&& !allowed_langs.is_empty()
|
||||
&& !allowed_langs.contains(&update.language()) =>
|
||||
{
|
||||
false
|
||||
}
|
||||
_ if !blocks.blocked_users.is_disjoint(&update.involved_users()) => false,
|
||||
_ if blocks.blocking_users.contains(update.author()) => false,
|
||||
_ if blocks.blocked_domains.contains(update.sent_from()) => false,
|
||||
_ => true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,10 +1,12 @@
|
|||
use crate::event::Event;
|
||||
use super::{Event, Payload};
|
||||
use crate::request::{Subscription, Timeline};
|
||||
|
||||
use futures::{future::Future, stream::Stream};
|
||||
use tokio::sync::{mpsc, watch};
|
||||
use warp::ws::{Message, WebSocket};
|
||||
|
||||
type Result<T> = std::result::Result<T, ()>;
|
||||
|
||||
pub struct Ws {
|
||||
unsubscribe_tx: mpsc::UnboundedSender<Timeline>,
|
||||
subscription: Subscription,
|
||||
|
@ -52,40 +54,38 @@ impl Ws {
|
|||
|
||||
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)),
|
||||
}
|
||||
} else {
|
||||
self.send_msg(&Dynamic(dyn_event))
|
||||
}
|
||||
}
|
||||
Ping => unreachable!(), // handled pings above
|
||||
match (event.update_payload(), event.dyn_update_payload()) {
|
||||
(Some(update), _) => self.send_or_filter(tl, &event, update)?,
|
||||
(None, None) => self.send_msg(&event)?, // send all non-updates
|
||||
(_, Some(dyn_update)) => self.send_or_filter(tl, &event, dyn_update)?,
|
||||
}
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
|
||||
fn send_msg(&mut self, event: &Event) -> Result<(), ()> {
|
||||
fn send_or_filter(&mut self, tl: Timeline, event: &Event, update: &impl Payload) -> Result<()> {
|
||||
let blocks = &self.subscription.blocks;
|
||||
let allowed_langs = &self.subscription.allowed_langs;
|
||||
const SKIP: Result<()> = Ok(());
|
||||
match tl {
|
||||
tl if tl.is_public()
|
||||
&& !update.language_unset()
|
||||
&& !allowed_langs.is_empty()
|
||||
&& !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,
|
||||
_ => Ok(self.send_msg(&event)?),
|
||||
}
|
||||
}
|
||||
|
||||
fn send_msg(&mut self, event: &Event) -> Result<()> {
|
||||
let txt = &event.to_json_string();
|
||||
let tl = self.subscription.timeline;
|
||||
let mut channel = self.ws_tx.clone().ok_or(())?;
|
||||
|
|
Loading…
Reference in New Issue