Skip to content

Commit

Permalink
chore: rename payload names (#284)
Browse files Browse the repository at this point in the history
  • Loading branch information
chris13524 authored and geekbrother committed Nov 21, 2023
1 parent 4b37a80 commit ff634de
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 53 deletions.
40 changes: 20 additions & 20 deletions src/handlers/push_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use {
increment_counter,
log::prelude::*,
middleware::validate_signature::RequireValidSignature,
providers::{NewPushMessage, OldPushMessage, Provider, PushMessage, PushProvider},
providers::{LegacyPushMessage, Provider, PushMessage, PushProvider, RawPushMessage},
request_id::get_req_id,
state::AppState,
stores::StoreError,
Expand All @@ -30,11 +30,11 @@ use {
#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq)]
pub struct PushMessageBody {
#[serde(flatten)]
pub new: Option<NewPushMessage>,
pub raw: Option<RawPushMessage>,

// Legacy (deprecating) fields
#[serde(flatten)]
pub old: Option<OldPushMessage>,
pub legacy: Option<LegacyPushMessage>,
}

pub async fn handler(
Expand Down Expand Up @@ -141,11 +141,11 @@ pub async fn handler_internal(
#[cfg(feature = "analytics")]
Some(MessageInfo {
msg_id: body
.new
.raw
.as_ref()
.map(|msg| relay_rpc::rpc::msg_id::get_message_id(&msg.message).into())
.unwrap_or(
body.old
body.legacy
.as_ref()
.map(|msg| msg.id.clone())
.unwrap_or("error: no message id".to_owned().into()),
Expand All @@ -155,20 +155,20 @@ pub async fn handler_internal(
continent: None,
project_id: tenant_id.clone().into(),
client_id: client_id.clone().into(),
topic: body.new.as_ref().map(|m| m.topic.clone()).unwrap_or(
body.old
topic: body.raw.as_ref().map(|m| m.topic.clone()).unwrap_or(
body.legacy
.as_ref()
.map(|m| m.payload.topic.clone())
.unwrap_or("error: no topic".to_owned().into()),
),
push_provider: "unknown".into(),
always_encrypted: body.new.is_some(),
always_encrypted: body.raw.is_some(),
encrypted: body
.old
.legacy
.as_ref()
.map(|m| m.payload.is_encrypted())
.unwrap_or(false),
flags: body.old.as_ref().map(|m| m.payload.flags).unwrap_or(0),
flags: body.legacy.as_ref().map(|m| m.payload.flags).unwrap_or(0),
status: 0,
response_message: None,
received_at: wc::analytics::time::now(),
Expand All @@ -180,8 +180,8 @@ pub async fn handler_internal(

let cloned_body = body.clone();
let push_message = if client.always_raw {
if let Some(body) = body.new {
PushMessage::NewPushMessage(body)
if let Some(body) = body.raw {
PushMessage::RawPushMessage(body)
} else {
return Err((
Error::EmptyField("missing topic, tag, or message field".to_string()),
Expand All @@ -190,8 +190,8 @@ pub async fn handler_internal(
}
} else {
#[allow(clippy::collapsible_else_if)]
if let Some(body) = body.old {
PushMessage::OldPushMessage(body)
if let Some(body) = body.legacy {
PushMessage::LegacyPushMessage(body)
} else {
return Err((
Error::EmptyField("missing id or payload field".to_string()),
Expand All @@ -213,16 +213,16 @@ pub async fn handler_internal(
topic: push_message.topic(),
push_provider: client.push_type.as_str().into(),
always_encrypted: match push_message {
PushMessage::NewPushMessage(_) => true,
PushMessage::OldPushMessage(_) => false,
PushMessage::RawPushMessage(_) => true,
PushMessage::LegacyPushMessage(_) => false,
},
encrypted: match push_message {
PushMessage::NewPushMessage(_) => false,
PushMessage::OldPushMessage(ref msg) => msg.payload.is_encrypted(),
PushMessage::RawPushMessage(_) => false,
PushMessage::LegacyPushMessage(ref msg) => msg.payload.is_encrypted(),
},
flags: match push_message {
PushMessage::NewPushMessage(_) => 0,
PushMessage::OldPushMessage(ref msg) => msg.payload.flags,
PushMessage::RawPushMessage(_) => 0,
PushMessage::LegacyPushMessage(ref msg) => msg.payload.flags,
},
status: 0,
response_message: None,
Expand Down
6 changes: 3 additions & 3 deletions src/providers/apns.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use {
super::{NewPushMessage, OldPushMessage, PushMessage},
super::{LegacyPushMessage, PushMessage, RawPushMessage},
crate::{blob::DecryptedPayloadBlob, error::Error, providers::PushProvider},
a2::{ErrorReason, NotificationBuilder, NotificationOptions},
async_trait::async_trait,
Expand Down Expand Up @@ -63,7 +63,7 @@ impl PushProvider for ApnsProvider {
};

let result = match body {
PushMessage::NewPushMessage(NewPushMessage {
PushMessage::RawPushMessage(RawPushMessage {
topic,
tag,
message,
Expand All @@ -82,7 +82,7 @@ impl PushProvider for ApnsProvider {

self.client.send(notification_payload).await
}
PushMessage::OldPushMessage(OldPushMessage { id: _, payload }) => {
PushMessage::LegacyPushMessage(LegacyPushMessage { id: _, payload }) => {
// TODO tidy after https://github.com/WalletConnect/a2/issues/67 is closed
if payload.is_encrypted() {
info!("Sending legacy `is_encrypted` message");
Expand Down
6 changes: 3 additions & 3 deletions src/providers/fcm.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use {
super::{OldPushMessage, PushMessage},
super::{LegacyPushMessage, PushMessage},
crate::{blob::DecryptedPayloadBlob, error::Error, providers::PushProvider},
async_trait::async_trait,
fcm::{ErrorReason, FcmError, FcmResponse, MessageBuilder, NotificationBuilder, Priority},
Expand Down Expand Up @@ -32,15 +32,15 @@ impl PushProvider for FcmProvider {
let mut message_builder = MessageBuilder::new(self.api_key.as_str(), token.as_str());

let result = match body {
PushMessage::NewPushMessage(message) => {
PushMessage::RawPushMessage(message) => {
// Sending `always_raw` encrypted message
info!("Sending raw encrypted message");
message_builder.data(&message)?;
set_message_priority_high(&mut message_builder);
let fcm_message = message_builder.finalize();
self.client.send(fcm_message).await
}
PushMessage::OldPushMessage(OldPushMessage { id: _, payload }) => {
PushMessage::LegacyPushMessage(LegacyPushMessage { id: _, payload }) => {
if payload.is_encrypted() {
info!("Sending legacy `is_encrypted` message");
message_builder.data(&payload)?;
Expand Down
16 changes: 8 additions & 8 deletions src/providers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,28 @@ use crate::providers::noop::NoopProvider;

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum PushMessage {
OldPushMessage(OldPushMessage),
NewPushMessage(NewPushMessage),
LegacyPushMessage(LegacyPushMessage),
RawPushMessage(RawPushMessage),
}

impl PushMessage {
pub fn message_id(&self) -> Arc<str> {
match self {
Self::NewPushMessage(msg) => get_message_id(&msg.message).into(),
Self::OldPushMessage(msg) => msg.id.clone(),
Self::RawPushMessage(msg) => get_message_id(&msg.message).into(),
Self::LegacyPushMessage(msg) => msg.id.clone(),
}
}

pub fn topic(&self) -> Arc<str> {
match self {
Self::NewPushMessage(msg) => msg.topic.clone(),
Self::OldPushMessage(msg) => msg.payload.topic.clone(),
Self::RawPushMessage(msg) => msg.topic.clone(),
Self::LegacyPushMessage(msg) => msg.payload.topic.clone(),
}
}
}

#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq)]
pub struct OldPushMessage {
pub struct LegacyPushMessage {
pub id: Arc<str>,
pub payload: MessagePayload,
}
Expand All @@ -64,7 +64,7 @@ impl MessagePayload {
}

#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq)]
pub struct NewPushMessage {
pub struct RawPushMessage {
/// Topic is used by the SDKs to decrypt
/// encrypted payloads on the client side
pub topic: Arc<str>,
Expand Down
18 changes: 9 additions & 9 deletions tests/functional/singletenant/push.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use {
crate::context::EchoServerContext,
echo_server::{
handlers::{push_message::PushMessageBody, register_client::RegisterBody},
providers::{MessagePayload, NewPushMessage, OldPushMessage},
providers::{LegacyPushMessage, MessagePayload, RawPushMessage},
},
hyper::StatusCode,
relay_rpc::{
Expand Down Expand Up @@ -85,8 +85,8 @@ async fn test_push(ctx: &mut EchoServerContext) {
flags: 0,
};
let payload = PushMessageBody {
new: None,
old: Some(OldPushMessage {
raw: None,
legacy: Some(LegacyPushMessage {
id: push_message_id,
payload: push_message_payload,
}),
Expand Down Expand Up @@ -147,8 +147,8 @@ async fn test_push_multiple_clients(ctx: &mut EchoServerContext) {
flags: 0,
};
let payload = PushMessageBody {
new: None,
old: Some(OldPushMessage {
raw: None,
legacy: Some(LegacyPushMessage {
id: push_message_id.clone(),
payload: push_message_payload,
}),
Expand Down Expand Up @@ -207,8 +207,8 @@ async fn test_push_always_raw(ctx: &mut EchoServerContext) {

// Send push with WRONG payload without necessary fields for always_raw
let wrong_payload = PushMessageBody {
new: None,
old: Some(OldPushMessage {
raw: None,
legacy: Some(LegacyPushMessage {
id: push_message_id,
payload: push_message_payload,
}),
Expand All @@ -227,12 +227,12 @@ async fn test_push_always_raw(ctx: &mut EchoServerContext) {

// Send push with good payload without necessary fields for always_raw
let good_payload = PushMessageBody {
new: Some(NewPushMessage {
raw: Some(RawPushMessage {
topic,
tag: 1100,
message: blob,
}),
old: None,
legacy: None,
};
let response = client
.post(format!(
Expand Down
12 changes: 6 additions & 6 deletions tests/functional/stores/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ async fn client_upsert_token(ctx: &mut StoreContext) {

ctx.notifications
.create_or_update_notification(&notification_id, TENANT_ID, &client_id, &PushMessageBody {
new: None,
old: None,
raw: None,
legacy: None,
})
.await
.unwrap();
Expand Down Expand Up @@ -149,8 +149,8 @@ async fn client_upsert_id(ctx: &mut StoreContext) {

ctx.notifications
.create_or_update_notification(&notification_id, TENANT_ID, &client_id, &PushMessageBody {
new: None,
old: None,
raw: None,
legacy: None,
})
.await
.unwrap();
Expand Down Expand Up @@ -211,8 +211,8 @@ async fn client_create_same_id_and_token(ctx: &mut StoreContext) {

ctx.notifications
.create_or_update_notification(&notification_id, TENANT_ID, &client_id, &PushMessageBody {
new: None,
old: None,
raw: None,
legacy: None,
})
.await
.unwrap();
Expand Down
8 changes: 4 additions & 4 deletions tests/functional/stores/notification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ async fn notification(ctx: &mut StoreContext) {
let res = ctx
.notifications
.create_or_update_notification(&gen_id(), TENANT_ID, &client_id, &PushMessageBody {
new: None,
old: None,
raw: None,
legacy: None,
})
.await;

Expand All @@ -50,8 +50,8 @@ async fn notification(ctx: &mut StoreContext) {
async fn notification_multiple_clients_same_payload(ctx: &mut StoreContext) {
let message_id = gen_id();
let payload = PushMessageBody {
new: None,
old: None,
raw: None,
legacy: None,
};

let client_id1 = create_client(&ctx.clients).await;
Expand Down

0 comments on commit ff634de

Please sign in to comment.