Add support for announcement event type (#96)

This commit is contained in:
Daniel Sockwell 2020-03-19 13:20:48 -04:00 committed by GitHub
parent dc6256d521
commit 2096e1348b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 49 additions and 9 deletions

2
Cargo.lock generated
View File

@ -440,7 +440,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "flodgatt" name = "flodgatt"
version = "0.6.1" version = "0.6.2"
dependencies = [ dependencies = [
"criterion 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "criterion 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"dotenv 0.14.0 (registry+https://github.com/rust-lang/crates.io-index)", "dotenv 0.14.0 (registry+https://github.com/rust-lang/crates.io-index)",

View File

@ -1,7 +1,7 @@
[package] [package]
name = "flodgatt" name = "flodgatt"
description = "A blazingly fast drop-in replacement for the Mastodon streaming api server" description = "A blazingly fast drop-in replacement for the Mastodon streaming api server"
version = "0.6.1" version = "0.6.2"
authors = ["Daniel Long Sockwell <daniel@codesections.com", "Julian Laubstein <contact@julianlaubstein.de>"] authors = ["Daniel Long Sockwell <daniel@codesections.com", "Julian Laubstein <contact@julianlaubstein.de>"]
edition = "2018" edition = "2018"

View File

@ -117,6 +117,8 @@ impl futures::stream::Stream for ClientAgent {
Conversation(notification) => send(Conversation(notification)), Conversation(notification) => send(Conversation(notification)),
Delete(status_id) => send(Delete(status_id)), Delete(status_id) => send(Delete(status_id)),
FiltersChanged => send(FiltersChanged), FiltersChanged => send(FiltersChanged),
Announcement(content) => send(Announcement(content)),
UnknownEvent(event, payload) => send(UnknownEvent(event, payload)),
}, },
Ok(Ready(None)) => Ok(Ready(None)), Ok(Ready(None)) => Ok(Ready(None)),
Ok(NotReady) => Ok(NotReady), Ok(NotReady) => Ok(NotReady),

View File

@ -11,13 +11,23 @@ pub enum Message {
Notification(Value), Notification(Value),
Delete(String), Delete(String),
FiltersChanged, FiltersChanged,
Announcement(AnnouncementType),
UnknownEvent(String, Value),
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Status(Value); pub struct Status(Value);
#[derive(Debug, Clone)]
pub enum AnnouncementType {
New(Value),
Delete(String),
Reaction(Value),
}
impl Message { impl Message {
pub fn from_json(json: Value) -> Self { pub fn from_json(json: Value) -> Self {
use AnnouncementType::*;
let event = json["event"] let event = json["event"]
.as_str() .as_str()
.unwrap_or_else(|| log_fatal!("Could not process `event` in {:?}", json)); .unwrap_or_else(|| log_fatal!("Could not process `event` in {:?}", json));
@ -32,20 +42,48 @@ impl Message {
.to_string(), .to_string(),
), ),
"filters_changed" => Self::FiltersChanged, "filters_changed" => Self::FiltersChanged,
unsupported_event => log_fatal!( "announcement" => Self::Announcement(New(json["payload"].clone())),
"Received an unsupported `event` type from Redis: {}", "announcement.reaction" => Self::Announcement(Reaction(json["payload"].clone())),
unsupported_event "announcement.delete" => Self::Announcement(Delete(
), json["payload"]
.as_str()
.unwrap_or_else(|| log_fatal!("Could not process `payload` in {:?}", json))
.to_string(),
)),
unexpected_event => {
log::warn!(
"Received an unexpected `event` type from Redis: {}",
unexpected_event
);
Self::UnknownEvent(event.to_string(), json["payload"].clone())
}
} }
} }
pub fn event(&self) -> String { pub fn event(&self) -> String {
format!("{}", self).to_lowercase() use AnnouncementType::*;
match self {
Self::Update(_) => "update",
Self::Conversation(_) => "conversation",
Self::Notification(_) => "notification",
Self::Announcement(New(_)) => "announcement",
Self::Announcement(Reaction(_)) => "announcement.reaction",
Self::UnknownEvent(event, _) => &event,
Self::Delete(_) => "delete",
Self::Announcement(Delete(_)) => "announcement.delete",
Self::FiltersChanged => "filters_changed",
}
.to_string()
} }
pub fn payload(&self) -> String { pub fn payload(&self) -> String {
use AnnouncementType::*;
match self { match self {
Self::Delete(id) => id.clone(),
Self::Update(status) => status.0.to_string(), Self::Update(status) => status.0.to_string(),
Self::Conversation(value) | Self::Notification(value) => value.to_string(), Self::Conversation(value)
| Self::Notification(value)
| Self::Announcement(New(value))
| Self::Announcement(Reaction(value))
| Self::UnknownEvent(_, value) => value.to_string(),
Self::Delete(id) | Self::Announcement(Delete(id)) => id.clone(),
Self::FiltersChanged => "".to_string(), Self::FiltersChanged => "".to_string(),
} }
} }