1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
use std::ops::Not;

use crate::requests::*;
use crate::types::*;

/// Use this method to forward messages of any kind.
#[derive(Debug, Clone, PartialEq, PartialOrd, Serialize)]
#[must_use = "requests do nothing unless sent"]
pub struct ForwardMessage {
    chat_id: ChatRef,
    from_chat_id: ChatRef,
    #[serde(skip_serializing_if = "Not::not")]
    disable_notification: bool,
    message_id: MessageId,
}

impl Request for ForwardMessage {
    type Type = JsonRequestType<Self>;
    type Response = JsonIdResponse<Message>;

    fn serialize(&self) -> Result<HttpRequest, Error> {
        Self::Type::serialize(RequestUrl::method("forwardMessage"), self)
    }
}

impl ForwardMessage {
    pub fn new<M, F, T>(message: M, from: F, to: T) -> Self
    where
        M: ToMessageId,
        F: ToChatRef,
        T: ToChatRef,
    {
        ForwardMessage {
            chat_id: to.to_chat_ref(),
            from_chat_id: from.to_chat_ref(),
            disable_notification: false,
            message_id: message.to_message_id(),
        }
    }

    pub fn disable_notification(&mut self) -> &mut Self {
        self.disable_notification = true;
        self
    }
}

/// Forward message.
pub trait CanForwardMessage {
    fn forward<T>(&self, to: T) -> ForwardMessage
    where
        T: ToChatRef;
}

impl<M> CanForwardMessage for M
where
    M: ToMessageId + ToSourceChat,
{
    fn forward<T>(&self, to: T) -> ForwardMessage
    where
        T: ToChatRef,
    {
        ForwardMessage::new(self.to_message_id(), self.to_source_chat(), to)
    }
}