client-api: Update tag endpoints to the new API standards

This commit is contained in:
Jonas Platte 2020-08-30 22:20:38 +02:00
parent 532e7a7233
commit 5407a95a99
No known key found for this signature in database
GPG Key ID: 7D261D771D915378
4 changed files with 87 additions and 13 deletions

View File

@ -14,25 +14,42 @@ ruma_api! {
requires_authentication: true, requires_authentication: true,
} }
#[non_exhaustive]
request: { request: {
/// The ID of the user creating the tag. /// The ID of the user creating the tag.
#[ruma_api(path)] #[ruma_api(path)]
pub user_id: UserId, pub user_id: &'a UserId,
/// The room to tag. /// The room to tag.
#[ruma_api(path)] #[ruma_api(path)]
pub room_id: RoomId, pub room_id: &'a RoomId,
/// The name of the tag to create. /// The name of the tag to create.
#[ruma_api(path)] #[ruma_api(path)]
pub tag: String, pub tag: &'a str,
/// Info about the tag. /// Info about the tag.
#[ruma_api(body)] #[ruma_api(body)]
pub tag_info: TagInfo, pub tag_info: TagInfo,
} }
#[derive(Default)]
#[non_exhaustive]
response: {} response: {}
error: crate::Error error: crate::Error
} }
impl<'a> Request<'a> {
/// Creates a new `Request` with the given user ID, room ID, tag and tag info.
pub fn new(user_id: &'a UserId, room_id: &'a RoomId, tag: &'a str, tag_info: TagInfo) -> Self {
Self { user_id, room_id, tag, tag_info }
}
}
impl Response {
/// Creates an empty `Response`.
pub fn new() -> Self {
Self
}
}

View File

@ -13,21 +13,38 @@ ruma_api! {
requires_authentication: true, requires_authentication: true,
} }
#[non_exhaustive]
request: { request: {
/// The user whose tag will be deleted. /// The user whose tag will be deleted.
#[ruma_api(path)] #[ruma_api(path)]
pub user_id: UserId, pub user_id: &'a UserId,
/// The tagged room. /// The tagged room.
#[ruma_api(path)] #[ruma_api(path)]
pub room_id: RoomId, pub room_id: &'a RoomId,
/// The name of the tag to delete. /// The name of the tag to delete.
#[ruma_api(path)] #[ruma_api(path)]
pub tag: String, pub tag: &'a str,
} }
#[derive(Default)]
#[non_exhaustive]
response: {} response: {}
error: crate::Error error: crate::Error
} }
impl<'a> Request<'a> {
/// Creates a new `Request` with the given user ID, room ID and tag
pub fn new(user_id: &'a UserId, room_id: &'a RoomId, tag: &'a str) -> Self {
Self { user_id, room_id, tag }
}
}
impl Response {
/// Creates an empty `Response`.
pub fn new() -> Self {
Self
}
}

View File

@ -14,16 +14,18 @@ ruma_api! {
requires_authentication: true, requires_authentication: true,
} }
#[non_exhaustive]
request: { request: {
/// The user whose tags will be retrieved. /// The user whose tags will be retrieved.
#[ruma_api(path)] #[ruma_api(path)]
pub user_id: UserId, pub user_id: &'a UserId,
/// The room from which tags will be retrieved. /// The room from which tags will be retrieved.
#[ruma_api(path)] #[ruma_api(path)]
pub room_id: RoomId, pub room_id: &'a RoomId,
} }
#[non_exhaustive]
response: { response: {
/// The user's tags for the room. /// The user's tags for the room.
pub tags: Tags, pub tags: Tags,
@ -32,19 +34,35 @@ ruma_api! {
error: crate::Error error: crate::Error
} }
impl<'a> Request<'a> {
/// Creates a new `Request` with the given user ID and room ID.
pub fn new(user_id: &'a UserId, room_id: &'a RoomId) -> Self {
Self { user_id, room_id }
}
}
impl Response {
/// Creates a new `Response` with the given tags.
pub fn new(tags: Tags) -> Self {
Self { tags }
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::convert::TryFrom;
use assign::assign;
use ruma_events::tag::{TagInfo, Tags};
use serde_json::json; use serde_json::json;
use super::Response; use super::Response;
use ruma_events::tag::{TagInfo, Tags};
use std::convert::TryFrom;
#[test] #[test]
fn test_serializing_get_tags_response() { fn test_serializing_get_tags_response() {
let mut tags = Tags::new(); let mut tags = Tags::new();
tags.insert("m.favourite".into(), TagInfo { order: Some(0.25) }); tags.insert("m.favourite".into(), assign!(TagInfo::new(), { order: Some(0.25) }));
tags.insert("u.user_tag".into(), TagInfo { order: Some(0.11) }); tags.insert("u.user_tag".into(), assign!(TagInfo::new(), { order: Some(0.11) }));
let response = Response { tags }; let response = Response { tags };
let http_response = http::Response::<Vec<u8>>::try_from(response).unwrap(); let http_response = http::Response::<Vec<u8>>::try_from(response).unwrap();

View File

@ -15,16 +15,38 @@ pub type Tags = BTreeMap<String, TagInfo>;
/// The payload for `TagEvent`. /// The payload for `TagEvent`.
#[derive(Clone, Debug, Deserialize, Serialize, BasicEventContent)] #[derive(Clone, Debug, Deserialize, Serialize, BasicEventContent)]
#[non_exhaustive]
#[ruma_event(type = "m.tag")] #[ruma_event(type = "m.tag")]
pub struct TagEventContent { pub struct TagEventContent {
/// A map of tag names to tag info. /// A map of tag names to tag info.
pub tags: Tags, pub tags: Tags,
} }
impl TagEventContent {
/// Creates a new `TagEventContent` with the given `Tags`.
pub fn new(tags: Tags) -> Self {
Self { tags }
}
}
impl From<Tags> for TagEventContent {
fn from(tags: Tags) -> Self {
Self::new(tags)
}
}
/// Information about a tag. /// Information about a tag.
#[derive(Clone, Debug, Deserialize, Serialize)] #[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[non_exhaustive]
pub struct TagInfo { pub struct TagInfo {
/// Value to use for lexicographically ordering rooms with this tag. /// Value to use for lexicographically ordering rooms with this tag.
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
pub order: Option<f64>, pub order: Option<f64>,
} }
impl TagInfo {
/// Creates an empty `TagInfo`.
pub fn new() -> Self {
Default::default()
}
}