client-api: Use an enum to differentiate when deleting a pusher

This commit is contained in:
Kévin Commaille 2022-11-03 19:07:14 +01:00 committed by Kévin Commaille
parent 5158dbf2eb
commit 801d9bf212
6 changed files with 292 additions and 210 deletions

View File

@ -7,6 +7,9 @@ Breaking changes:
* `fully_read` field in `read_marker::set_read_marker` is no longer required
* Remove the `fully_read` argument from `read_marker::set_read_marker::Request::new`
* Move `message::get_message_events::v3::Direction` to the root of the crate
* Make `push::set_pusher::v3::Request` use an enum to differentiate when deleting a pusher
* Move `push::get_pushers::v3::Pusher` to `push` and make it use the new `PusherIds` type
* Remove `push::set_pusher::v3::Pusher` and use the common type instead
Improvements:

View File

@ -35,6 +35,7 @@ assign = { workspace = true }
bytes = "1.0.1"
http = { workspace = true }
js_int = { workspace = true, features = ["serde"] }
js_option = "0.1.1"
maplit = { workspace = true }
percent-encoding = "2.1.0"
ruma-common = { version = "0.10.5", path = "../ruma-common", features = ["api", "events"] }

View File

@ -204,3 +204,100 @@ pub enum PusherKind {
#[doc(hidden)]
_Custom(PrivOwnedStr),
}
/// Defines a pusher.
///
/// To create an instance of this type, first create a `PusherInit` and convert it via
/// `Pusher::from` / `.into()`.
#[derive(Clone, Debug, Serialize, Deserialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct Pusher {
/// Identifiers for this pusher.
#[serde(flatten)]
pub ids: PusherIds,
/// The kind of the pusher.
pub kind: PusherKind,
/// A string that will allow the user to identify what application owns this pusher.
pub app_display_name: String,
/// A string that will allow the user to identify what device owns this pusher.
pub device_display_name: String,
/// Determines which set of device specific rules this pusher executes.
#[serde(skip_serializing_if = "Option::is_none")]
pub profile_tag: Option<String>,
/// The preferred language for receiving notifications (e.g. 'en' or 'en-US')
pub lang: String,
/// Information for the pusher implementation itself.
pub data: PusherData,
}
/// Initial set of fields of `Pusher`.
///
/// This struct will not be updated even if additional fields are added to `Pusher` in a new
/// (non-breaking) release of the Matrix specification.
#[derive(Debug)]
#[allow(clippy::exhaustive_structs)]
pub struct PusherInit {
/// Identifiers for this pusher.
pub ids: PusherIds,
/// The kind of the pusher.
pub kind: PusherKind,
/// A string that will allow the user to identify what application owns this pusher.
pub app_display_name: String,
/// A string that will allow the user to identify what device owns this pusher.
pub device_display_name: String,
/// Determines which set of device-specific rules this pusher executes.
pub profile_tag: Option<String>,
/// The preferred language for receiving notifications (e.g. 'en' or 'en-US').
pub lang: String,
/// Information for the pusher implementation itself.
pub data: PusherData,
}
impl From<PusherInit> for Pusher {
fn from(init: PusherInit) -> Self {
let PusherInit {
ids,
kind,
app_display_name,
device_display_name,
profile_tag,
lang,
data,
} = init;
Self { ids, kind, app_display_name, device_display_name, profile_tag, lang, data }
}
}
/// Strings to uniquely identify a `Pusher`.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct PusherIds {
/// A unique identifier for the pusher.
///
/// The maximum allowed length is 512 bytes.
pub pushkey: String,
/// A reverse-DNS style identifier for the application.
///
/// The maximum allowed length is 64 bytes.
pub app_id: String,
}
impl PusherIds {
/// Creates a new `PusherIds` with the given pushkey and application ID.
pub fn new(pushkey: String, app_id: String) -> Self {
Self { pushkey, app_id }
}
}

View File

@ -6,9 +6,8 @@ pub mod v3 {
//! [spec]: https://spec.matrix.org/v1.4/client-server-api/#get_matrixclientv3pushers
use ruma_common::api::ruma_api;
use serde::{Deserialize, Serialize};
use crate::push::{PusherData, PusherKind};
use crate::push::Pusher;
ruma_api! {
metadata: {
@ -46,102 +45,4 @@ pub mod v3 {
Self { pushers }
}
}
/// Defines a pusher.
///
/// To create an instance of this type, first create a `PusherInit` and convert it via
/// `Pusher::from` / `.into()`.
#[derive(Clone, Debug, Serialize, Deserialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct Pusher {
/// A unique identifier for this pusher.
///
/// The maximum allowed length is 512 bytes.
pub pushkey: String,
/// The kind of the pusher.
pub kind: PusherKind,
/// A reverse-DNS style identifier for the application.
///
/// The maximum allowed length is 64 bytes.
pub app_id: String,
/// A string that will allow the user to identify what application owns this pusher.
pub app_display_name: String,
/// A string that will allow the user to identify what device owns this pusher.
pub device_display_name: String,
/// Determines which set of device specific rules this pusher executes.
#[serde(skip_serializing_if = "Option::is_none")]
pub profile_tag: Option<String>,
/// The preferred language for receiving notifications (e.g. 'en' or 'en-US')
pub lang: String,
/// Information for the pusher implementation itself.
pub data: PusherData,
}
/// Initial set of fields of `Pusher`.
///
/// This struct will not be updated even if additional fields are added to `Pusher` in a new
/// (non-breaking) release of the Matrix specification.
#[derive(Debug)]
#[allow(clippy::exhaustive_structs)]
pub struct PusherInit {
/// A unique identifier for this pusher.
///
/// The maximum allowed length is 512 bytes.
pub pushkey: String,
/// The kind of the pusher.
pub kind: PusherKind,
/// A reverse-DNS style identifier for the application.
///
/// The maximum allowed length is 64 bytes.
pub app_id: String,
/// A string that will allow the user to identify what application owns this pusher.
pub app_display_name: String,
/// A string that will allow the user to identify what device owns this pusher.
pub device_display_name: String,
/// Determines which set of device-specific rules this pusher executes.
pub profile_tag: Option<String>,
/// The preferred language for receiving notifications (e.g. 'en' or 'en-US').
pub lang: String,
/// Information for the pusher implementation itself.
pub data: PusherData,
}
impl From<PusherInit> for Pusher {
fn from(init: PusherInit) -> Self {
let PusherInit {
pushkey,
kind,
app_id,
app_display_name,
device_display_name,
profile_tag,
lang,
data,
} = init;
Self {
pushkey,
kind,
app_id,
app_display_name,
device_display_name,
profile_tag,
lang,
data,
}
}
}
}

View File

@ -1,5 +1,7 @@
//! `POST /_matrix/client/*/pushers/set`
mod pusher_action_serde;
pub mod v3 {
//! `/v3/` ([spec])
//!
@ -8,7 +10,7 @@ pub mod v3 {
use ruma_common::api::ruma_api;
use serde::{Deserialize, Serialize};
use crate::push::{PusherData, PusherKind};
use crate::push::{Pusher, PusherIds};
ruma_api! {
metadata: {
@ -23,15 +25,9 @@ pub mod v3 {
}
request: {
/// The pusher to configure.
#[serde(flatten)]
pub pusher: Pusher,
/// Controls if another pusher with the same pushkey and app id should be created.
///
/// Defaults to `false`. See the spec for more details.
#[serde(default, skip_serializing_if = "ruma_common::serde::is_default")]
pub append: bool,
/// The action to take.
#[ruma_api(body)]
pub action: PusherAction,
}
#[derive(Default)]
@ -41,9 +37,19 @@ pub mod v3 {
}
impl Request {
/// Creates a new `Request` with the given pusher.
pub fn new(pusher: Pusher) -> Self {
Self { pusher, append: false }
/// Creates a new `Request` for the given action.
pub fn new(action: PusherAction) -> Self {
Self { action }
}
/// Creates a new `Request` to create or update the given pusher.
pub fn post(pusher: Pusher) -> Self {
Self::new(PusherAction::Post(PusherPostData { pusher, append: false }))
}
/// Creates a new `Request` to delete the pusher identified by the given IDs.
pub fn delete(ids: PusherIds) -> Self {
Self::new(PusherAction::Delete(ids))
}
}
@ -54,105 +60,30 @@ pub mod v3 {
}
}
/// Defines a pusher.
///
/// To create an instance of this type, first create a `PusherInit` and convert it via
/// `Pusher::from` / `.into()`.
/// The action to take for the pusher.
#[derive(Clone, Debug)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub enum PusherAction {
/// Create or update the given pusher.
Post(PusherPostData),
/// Delete the pusher identified by the given IDs.
Delete(PusherIds),
}
/// Data necessary to create or update a pusher.
#[derive(Clone, Debug, Serialize, Deserialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct Pusher {
/// A unique identifier for this pusher.
pub struct PusherPostData {
/// The pusher to configure.
#[serde(flatten)]
pub pusher: Pusher,
/// Controls if another pusher with the same pushkey and app id should be created, if there
/// are already others for other users.
///
/// The maximum allowed length is 512 bytes.
pub pushkey: String,
/// The kind of the pusher.
///
/// `None` deletes the pusher.
pub kind: Option<PusherKind>,
/// A reverse-DNS style identifier for the application.
///
/// The maximum allowed length is 64 bytes.
pub app_id: String,
/// A string that will allow the user to identify what application owns this pusher.
pub app_display_name: String,
/// A string that will allow the user to identify what device owns this pusher.
pub device_display_name: String,
/// Determines which set of device specific rules this pusher executes.
#[serde(skip_serializing_if = "Option::is_none")]
pub profile_tag: Option<String>,
/// The preferred language for receiving notifications (e.g. 'en' or 'en-US')
pub lang: String,
/// Information for the pusher implementation itself.
pub data: PusherData,
}
/// Initial set of fields of `Pusher`.
///
/// This struct will not be updated even if additional fields are added to `Pusher` in a new
/// (non-breaking) release of the Matrix specification.
#[derive(Debug)]
#[allow(clippy::exhaustive_structs)]
pub struct PusherInit {
/// A unique identifier for this pusher.
///
/// The maximum allowed length is 512 bytes.
pub pushkey: String,
/// The kind of the pusher.
///
/// `None` deletes the pusher.
pub kind: Option<PusherKind>,
/// A reverse-DNS style identifier for the application.
///
/// The maximum allowed length is 64 bytes.
pub app_id: String,
/// A string that will allow the user to identify what application owns this pusher.
pub app_display_name: String,
/// A string that will allow the user to identify what device owns this pusher.
pub device_display_name: String,
/// Determines which set of device specific rules this pusher executes.
pub profile_tag: Option<String>,
/// The preferred language for receiving notifications (e.g. 'en' or 'en-US')
pub lang: String,
/// Information for the pusher implementation itself.
pub data: PusherData,
}
impl From<PusherInit> for Pusher {
fn from(init: PusherInit) -> Self {
let PusherInit {
pushkey,
kind,
app_id,
app_display_name,
device_display_name,
profile_tag,
lang,
data,
} = init;
Self {
pushkey,
kind,
app_id,
app_display_name,
device_display_name,
profile_tag,
lang,
data,
}
}
/// Defaults to `false`. See the spec for more details.
#[serde(default, skip_serializing_if = "ruma_common::serde::is_default")]
pub append: bool,
}
}

View File

@ -0,0 +1,149 @@
use js_option::JsOption;
use ruma_common::serde::from_raw_json_value;
use serde::{de, ser::SerializeStruct, Deserialize, Serialize};
use serde_json::value::RawValue as RawJsonValue;
use crate::push::PusherKind;
use super::v3::PusherAction;
impl Serialize for PusherAction {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
match self {
PusherAction::Post(pusher) => pusher.serialize(serializer),
PusherAction::Delete(ids) => {
let mut st = serializer.serialize_struct("PusherAction", 3)?;
st.serialize_field("pushkey", &ids.pushkey)?;
st.serialize_field("app_id", &ids.app_id)?;
st.serialize_field("kind", &None::<&str>)?;
st.end()
}
}
}
}
#[derive(Debug, Deserialize)]
struct PusherActionDeHelper {
kind: JsOption<PusherKind>,
}
impl<'de> Deserialize<'de> for PusherAction {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: de::Deserializer<'de>,
{
let json = Box::<RawJsonValue>::deserialize(deserializer)?;
let PusherActionDeHelper { kind } = from_raw_json_value(&json)?;
match kind {
JsOption::Some(_) => Ok(Self::Post(from_raw_json_value(&json)?)),
JsOption::Null => Ok(Self::Delete(from_raw_json_value(&json)?)),
// This is unreachable because we don't use `#[serde(default)]` on the field.
JsOption::Undefined => Err(de::Error::missing_field("kind")),
}
}
}
#[cfg(test)]
mod tests {
use assert_matches::assert_matches;
use ruma_common::push::PusherData;
use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
use super::PusherAction;
use crate::push::{set_pusher::v3::PusherPostData, Pusher, PusherIds, PusherKind};
#[test]
fn serialize_post() {
let action = PusherAction::Post(PusherPostData {
pusher: Pusher {
ids: PusherIds::new("abcdef".to_owned(), "my.matrix.app".to_owned()),
kind: PusherKind::Email,
app_display_name: "My Matrix App".to_owned(),
device_display_name: "My Phone".to_owned(),
profile_tag: None,
lang: "en".to_owned(),
data: PusherData::new(),
},
append: false,
});
assert_eq!(
to_json_value(action).unwrap(),
json!({
"pushkey": "abcdef",
"app_id": "my.matrix.app",
"kind": "email",
"app_display_name": "My Matrix App",
"device_display_name": "My Phone",
"lang": "en",
"data": {}
})
);
}
#[test]
fn serialize_delete() {
let action =
PusherAction::Delete(PusherIds::new("abcdef".to_owned(), "my.matrix.app".to_owned()));
assert_eq!(
to_json_value(action).unwrap(),
json!({
"pushkey": "abcdef",
"app_id": "my.matrix.app",
"kind": null,
})
);
}
#[test]
fn deserialize_post() {
let json = json!({
"pushkey": "abcdef",
"app_id": "my.matrix.app",
"kind": "email",
"app_display_name": "My Matrix App",
"device_display_name": "My Phone",
"lang": "en",
"data": {}
});
let post_data = assert_matches!(
from_json_value(json).unwrap(),
PusherAction::Post(post_data) => post_data
);
assert!(!post_data.append);
let pusher = post_data.pusher;
assert_eq!(pusher.ids.pushkey, "abcdef");
assert_eq!(pusher.ids.app_id, "my.matrix.app");
assert_eq!(pusher.kind, PusherKind::Email);
assert_eq!(pusher.app_display_name, "My Matrix App");
assert_eq!(pusher.device_display_name, "My Phone");
assert_eq!(pusher.profile_tag, None);
assert_eq!(pusher.lang, "en");
assert!(pusher.data.is_empty());
}
#[test]
fn deserialize_delete() {
let json = json!({
"pushkey": "abcdef",
"app_id": "my.matrix.app",
"kind": null,
});
let ids = assert_matches!(
from_json_value(json).unwrap(),
PusherAction::Delete(ids) => ids
);
assert_eq!(ids.pushkey, "abcdef");
assert_eq!(ids.app_id, "my.matrix.app");
}
}