zulip-api 0.0.8

Client library for interacting with the Zulip API
Documentation
use serde::{Deserialize, Serialize};

use crate::response::BaseResponse;
use crate::{field_access, impl_default, impl_display, impl_response, VisibilityPolicy};

/// Represents the response to a `Send Message` API request.
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
pub struct Response {
    #[serde(flatten)]
    base: BaseResponse,
    #[serde(skip_serializing_if = "Option::is_none")]
    id: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    automatic_new_visibility_policy: Option<VisibilityPolicy>,
}

impl_response!(Response, base);

field_access! {
    Response {
        /// Unique ID assigned to the sent message.
        id: option { u64 },
        /// If the message's sender had configured their [visibility policy settings](https://zulip.com/help/mute-a-topic)
        /// to potentially automatically follow or unmute topics when sending messages,
        /// and one of these policies did in fact change the user's visibility policy
        /// for the topic where this message was sent, the new value for that user's
        /// visibility policy for the recipient topic.
        ///
        /// Only present if the sender's visibility was in fact changed.
        ///
        /// The value can be either [unmuted or followed](https://zulip.com/api/update-user-topic#parameter-visibility_policy).
        ///
        /// Clients will also be notified about the change in policy via a
        /// `user_topic` event as usual. This field is intended to be used by clients
        /// to explicitly inform the user when a topic's visibility policy was changed
        /// automatically due to sending a message.
        ///
        /// For example, the Zulip web application uses this field to decide whether
        /// to display a warning or notice suggesting to unmute the topic after
        /// sending a message to a muted channel. Such a notice would be confusing in
        /// the event that the act of sending the message had already resulted in the
        /// user automatically unmuting or following the topic in question.
        ///
        /// **Changes**: New in Zulip 8.0 (feature level 218).
        automatic_new_visibility_policy: option { VisibilityPolicy },
    }
}

impl Response {
    /// Creates a new [Response].
    pub const fn new() -> Self {
        Self {
            base: BaseResponse::new(),
            id: None,
            automatic_new_visibility_policy: None,
        }
    }
}

impl_default!(Response);
impl_display!(Response);

#[cfg(test)]
mod tests {
    use super::*;
    use crate::response::{ApiResult, ErrorCode, Response as _};

    #[test]
    fn test_success_message_response() {
        let msg = "test response";
        let msg_json = serde_json::to_string(&msg).unwrap();

        let result = ApiResult::Success;
        let result_json = serde_json::to_string(&result).unwrap();

        let id = 42;
        let id_json = serde_json::to_string(&id).unwrap();

        let response = Response::new()
            .with_msg(msg)
            .with_result(result)
            .with_id(id);

        let response_json =
            format!(r#"{{"msg":{msg_json},"result":{result_json},"id":{id_json}}}"#);

        assert_eq!(serde_json::to_string(&response).unwrap(), response_json);
        assert_eq!(
            serde_json::from_str::<Response>(&response_json).unwrap(),
            response
        );

        assert_eq!(response.msg(), msg);
        assert_eq!(response.result(), result);
        assert_eq!(response.code(), None);
        assert_eq!(response.id(), Some(id));
    }

    #[test]
    fn test_error_message_response() {
        let msg = "test response";
        let msg_json = serde_json::to_string(&msg).unwrap();

        let result = ApiResult::Error;
        let result_json = serde_json::to_string(&result).unwrap();

        let code = ErrorCode::BadRequest;
        let code_json = serde_json::to_string(&code).unwrap();

        let id = 42;
        let id_json = serde_json::to_string(&id).unwrap();

        let response = Response::new()
            .with_msg(msg)
            .with_result(result)
            .with_code(code)
            .with_id(id);

        let response_json = format!(
            r#"{{"msg":{msg_json},"result":{result_json},"code":{code_json},"id":{id_json}}}"#
        );

        assert_eq!(serde_json::to_string(&response).unwrap(), response_json);
        assert_eq!(
            serde_json::from_str::<Response>(&response_json).unwrap(),
            response
        );

        assert_eq!(response.msg(), msg);
        assert_eq!(response.result(), result);
        assert_eq!(response.code(), Some(code));
        assert_eq!(response.id(), Some(id));
    }
}