improvements for fedigroups

master
Ondřej Hruška 3 years ago
parent 9f491d2d29
commit 54a0e55964
Signed by: MightyPork
GPG Key ID: 2C5FD5035250423D
  1. 8
      src/debug.rs
  2. 2
      src/entities/event.rs
  3. 30
      src/entities/notification.rs
  4. 19
      src/lib.rs
  5. 5
      src/macros.rs
  6. 18
      src/streaming.rs

@ -8,7 +8,7 @@ pub struct NotificationDisplay<'a>(pub &'a Notification);
impl<'a> Display for NotificationDisplay<'a> { impl<'a> Display for NotificationDisplay<'a> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let n = self.0; let n = self.0;
match n.notification_type { match &n.notification_type {
NotificationType::Follow => { NotificationType::Follow => {
write!(f, "Follow {{ #{}, @{} }}", n.id, n.account.acct ) write!(f, "Follow {{ #{}, @{} }}", n.id, n.account.acct )
} }
@ -33,6 +33,9 @@ impl<'a> Display for NotificationDisplay<'a> {
write!(f, "Reblog {{ #{}, acct: @{}, status: -- }}", n.id, n.account.acct ) write!(f, "Reblog {{ #{}, acct: @{}, status: -- }}", n.id, n.account.acct )
} }
} }
NotificationType::Other(other) => {
f.write_str(&other)
}
} }
} }
} }
@ -52,6 +55,9 @@ impl<'a> Display for EventDisplay<'a> {
Event::FiltersChanged => { Event::FiltersChanged => {
write!(f, "FiltersChanged") write!(f, "FiltersChanged")
} }
Event::Heartbeat => {
write!(f, "Heartbeat")
}
Event::Update(s) => { Event::Update(s) => {
StatusDisplay(s).fmt(f) StatusDisplay(s).fmt(f)
} }

@ -12,4 +12,6 @@ pub enum Event {
Delete(String), Delete(String),
/// FiltersChanged event /// FiltersChanged event
FiltersChanged, FiltersChanged,
/// Indication that the socket works (ping/pong)
Heartbeat,
} }

@ -21,8 +21,10 @@ pub struct Notification {
} }
/// The type of notification. /// The type of notification.
#[derive(Debug, Clone, Copy, Deserialize, PartialEq)] #[derive(Debug, Clone, Deserialize, PartialEq)]
#[serde(rename_all = "lowercase")] #[serde(rename_all = "lowercase")]
// Deserialize "Other" trick from https://github.com/serde-rs/serde/issues/912#issuecomment-803097289
#[serde(remote = "NotificationType")]
pub enum NotificationType { pub enum NotificationType {
/// Someone mentioned the application client in another status. /// Someone mentioned the application client in another status.
Mention, Mention,
@ -32,4 +34,30 @@ pub enum NotificationType {
Favourite, Favourite,
/// Someone followed the application client. /// Someone followed the application client.
Follow, Follow,
/// Anything else
#[serde(skip_deserializing)]
Other(String),
}
use std::str::FromStr;
use serde::de::{value, Deserializer, IntoDeserializer};
impl FromStr for NotificationType {
type Err = value::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Self::deserialize(s.into_deserializer())
}
}
impl<'de> Deserialize<'de> for NotificationType {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>
{
let s = String::deserialize(deserializer)?;
let deserialized = Self::from_str(&s).unwrap_or_else(|_| {
Self::Other(s)
});
Ok(deserialized)
}
} }

@ -19,6 +19,7 @@ use reqwest::multipart;
use entities::prelude::*; use entities::prelude::*;
pub use errors::{Error, Result}; pub use errors::{Error, Result};
use helpers::deserialise_response; use helpers::deserialise_response;
use serde::{Serialize,Deserialize};
use requests::{ use requests::{
AddFilterRequest, AddFilterRequest,
AddPushRequest, AddPushRequest,
@ -87,6 +88,14 @@ impl From<AppData> for FediClient {
} }
} }
#[derive(Serialize,Deserialize,Clone,Copy,PartialEq)]
#[serde(rename_all="snake_case")]
pub enum SearchType {
Accounts,
Hashtags,
Statuses
}
impl FediClient { impl FediClient {
methods![get, put, post, delete,]; methods![get, put, post, delete,];
@ -154,7 +163,15 @@ impl FediClient {
route_v1!((get) get_filters: "filters" => Vec<Filter>); route_v1!((get) get_filters: "filters" => Vec<Filter>);
route_v1!((get) get_follow_suggestions: "suggestions" => Vec<Account>); route_v1!((get) get_follow_suggestions: "suggestions" => Vec<Account>);
route_v2!((get (q: &'a str, resolve: bool,)) search_v2: "search" => SearchResultV2); route_v2!((get (
q: &'a str,
resolve: bool,
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(rename = "type")]
type_: Option<SearchType>,
limit: Option<i32>,
following: bool,
)) search_v2: "search" => SearchResultV2);
route_v1_id!((get) get_account: "accounts/{}" => Account); route_v1_id!((get) get_account: "accounts/{}" => Account);
route_v1_id!((post) follow: "accounts/{}/follow" => Relationship); route_v1_id!((post) follow: "accounts/{}/follow" => Relationship);

@ -78,7 +78,7 @@ macro_rules! route_v1_paged {
} }
macro_rules! route_v2 { macro_rules! route_v2 {
((get ($($param:ident: $typ:ty,)*)) $name:ident: $url:expr => $ret:ty) => { ((get ($($(#[$m:meta])* $param:ident: $typ:ty,)*)) $name:ident: $url:expr => $ret:ty) => {
doc_comment::doc_comment! { doc_comment::doc_comment! {
concat!( concat!(
"Equivalent to `get /api/v2/",$url,"`\n", "Equivalent to `get /api/v2/",$url,"`\n",
@ -92,6 +92,9 @@ macro_rules! route_v2 {
#[derive(Serialize)] #[derive(Serialize)]
struct Data<'a> { struct Data<'a> {
$( $(
$(
#[$m]
)*
$param: $typ, $param: $typ,
)* )*
#[serde(skip)] #[serde(skip)]

@ -91,6 +91,7 @@ impl EventReader {
} }
pub async fn send_ping(&mut self) -> std::result::Result<(), tokio_tungstenite::tungstenite::Error> { pub async fn send_ping(&mut self) -> std::result::Result<(), tokio_tungstenite::tungstenite::Error> {
trace!("Sending ping");
self.stream.send(Message::Ping("pleroma groups".as_bytes().to_vec())).await self.stream.send(Message::Ping("pleroma groups".as_bytes().to_vec())).await
} }
} }
@ -111,20 +112,23 @@ impl Stream for EventReader {
if let Ok(event) = self.make_event(&self.lines) { if let Ok(event) = self.make_event(&self.lines) {
trace!("Parsed event"); trace!("Parsed event");
self.lines.clear(); self.lines.clear();
return Poll::Ready(Some(event)); Poll::Ready(Some(event))
} else { } else {
trace!("Failed to parse"); trace!("Failed to parse");
return Poll::Pending; Poll::Pending
} }
} }
Poll::Ready(Some(Ok(Message::Ping(_)))) | Poll::Ready(Some(Ok(Message::Pong(_)))) => { Poll::Ready(Some(Ok(Message::Ping(_)))) => {
// Discard trace!("Ping");
trace!("Ping/Pong, discard"); Poll::Ready(Some(Event::Heartbeat))
Poll::Pending }
Poll::Ready(Some(Ok(Message::Pong(_)))) => {
trace!("Pong");
Poll::Ready(Some(Event::Heartbeat))
} }
Poll::Ready(Some(Ok(Message::Binary(_)))) => { Poll::Ready(Some(Ok(Message::Binary(_)))) => {
warn!("Unexpected binary msg"); warn!("Unexpected binary msg");
Poll::Pending Poll::Ready(Some(Event::Heartbeat))
} }
Poll::Ready(Some(Ok(Message::Close(_)))) => { Poll::Ready(Some(Ok(Message::Close(_)))) => {
warn!("Websocket close frame!"); warn!("Websocket close frame!");

Loading…
Cancel
Save