desmos_bindings/reactions/
msg.rs

1//! Contains the messages that can be sent to the chain to interact with the x/reactions module.
2
3use crate::reactions::types::*;
4use cosmwasm_std::Addr;
5
6/// ReactionsMsg is the builder to generate Desmos x/reactions messages.
7pub struct ReactionsMsg {}
8
9impl ReactionsMsg {
10    /// Creates a new instance of [`MsgAddReaction`].
11    ///
12    /// * `subspace_id` - Id of the subspace inside which the post to react to is.
13    /// * `post_id` - Id of the post to react to.
14    /// * `value` - Value of the reaction.
15    /// * `user` - User reacting to the post.
16    pub fn add_reaction(
17        subspace_id: u64,
18        post_id: u64,
19        value: ReactionValue,
20        user: Addr,
21    ) -> MsgAddReaction {
22        MsgAddReaction {
23            subspace_id,
24            post_id,
25            value: Some(value.into()),
26            user: user.into(),
27        }
28    }
29    /// Creates a new instance of [`MsgRemoveReaction`].
30    ///
31    /// * `subspace_id` - Id of the subspace inside which the reaction to remove is.
32    /// * `post_id` - Id of the post from which to remove the reaction.
33    /// * `reaction_id` - Id of the reaction to be removed.
34    /// * `user` - User removing the reaction.
35    pub fn remove_reaction(
36        subspace_id: u64,
37        post_id: u64,
38        reaction_id: u32,
39        user: Addr,
40    ) -> MsgRemoveReaction {
41        MsgRemoveReaction {
42            subspace_id,
43            post_id,
44            reaction_id,
45            user: user.into(),
46        }
47    }
48    /// Creates a new instance of [`MsgAddRegisteredReaction`].
49    ///
50    /// * `subspace_id` - Id of the subspace inside which this reaction should be registered.
51    /// * `shorthand_code` - Shorthand code of the reaction.
52    /// * `display_value` - Display value of the reaction.
53    /// * `user` - User adding the supported reaction.
54    pub fn add_registered_reaction(
55        subspace_id: u64,
56        shorthand_code: &str,
57        display_value: &str,
58        user: Addr,
59    ) -> MsgAddRegisteredReaction {
60        MsgAddRegisteredReaction {
61            subspace_id,
62            shorthand_code: shorthand_code.into(),
63            display_value: display_value.into(),
64            user: user.into(),
65        }
66    }
67    /// Creates a new instance of [`MsgEditRegisteredReaction`].
68    ///
69    /// * `subspace_id` - Id of the subspace inside which the reaction to edit is.
70    /// * `registered_reaction_id` - Id of the registered reaction to edit.
71    /// * `shorthand_code` - New shorthand code to be set.
72    /// * `display_value` - Display value to be set.
73    /// * `user` - User editing the registered reaction.
74    pub fn edit_registered_reaction(
75        subspace_id: u64,
76        registered_reaction_id: u32,
77        shorthand_code: &str,
78        display_value: &str,
79        user: Addr,
80    ) -> MsgEditRegisteredReaction {
81        MsgEditRegisteredReaction {
82            subspace_id,
83            registered_reaction_id,
84            shorthand_code: shorthand_code.into(),
85            display_value: display_value.into(),
86            user: user.into(),
87        }
88    }
89    /// Creates a new instance of [`MsgRemoveRegisteredReaction`].
90    ///
91    /// * `subspace_id` - Id of the registered reaction to be removed.
92    /// * `registered_reaction_id` - Id of the registered reaction to be removed.
93    /// * `user` - User removing the registered reaction.
94    pub fn remove_registered_reaction(
95        subspace_id: u64,
96        registered_reaction_id: u32,
97        user: Addr,
98    ) -> MsgRemoveRegisteredReaction {
99        MsgRemoveRegisteredReaction {
100            subspace_id,
101            registered_reaction_id,
102            user: user.into(),
103        }
104    }
105    /// Creates a new instance of [`MsgSetReactionsParams`].
106    ///
107    /// * `subspace_id` - Id of the subspace for which to set the params.
108    /// * `registered_reaction` - Params related to [`FreeTextValue`].
109    /// * `free_text` - Params related to [`RegisteredReactionValue`].
110    /// * `user` - User setting the params.
111    pub fn set_reactions_params(
112        subspace_id: u64,
113        registered_reaction: Option<RegisteredReactionValueParams>,
114        free_text: Option<FreeTextValueParams>,
115        user: Addr,
116    ) -> MsgSetReactionsParams {
117        MsgSetReactionsParams {
118            subspace_id: subspace_id.into(),
119            registered_reaction,
120            free_text,
121            user: user.into(),
122        }
123    }
124}
125
126#[cfg(test)]
127mod tests {
128    use super::*;
129
130    #[test]
131    fn test_add_reaction() {
132        let msg = ReactionsMsg::add_reaction(
133            1,
134            1,
135            ReactionValue::Registered(RegisteredReactionValue {
136                registered_reaction_id: 1,
137            })
138            .into(),
139            Addr::unchecked("user"),
140        );
141
142        let expected = MsgAddReaction {
143            subspace_id: 1,
144            post_id: 1,
145            value: Some(
146                ReactionValue::Registered(RegisteredReactionValue {
147                    registered_reaction_id: 1,
148                })
149                .into(),
150            ),
151            user: "user".into(),
152        };
153
154        assert_eq!(expected, msg)
155    }
156
157    #[test]
158    fn test_remove_reaction() {
159        let msg = ReactionsMsg::remove_reaction(1, 1, 1, Addr::unchecked("user"));
160
161        let expected = MsgRemoveReaction {
162            subspace_id: 1,
163            post_id: 1,
164            reaction_id: 1,
165            user: "user".into(),
166        };
167
168        assert_eq!(expected, msg)
169    }
170
171    #[test]
172    fn test_add_registered_reaction() {
173        let msg = ReactionsMsg::add_registered_reaction(
174            1,
175            "test_code",
176            "test_value",
177            Addr::unchecked("user"),
178        );
179
180        let expected = MsgAddRegisteredReaction {
181            subspace_id: 1,
182            shorthand_code: "test_code".into(),
183            display_value: "test_value".into(),
184            user: "user".into(),
185        };
186
187        assert_eq!(expected, msg)
188    }
189
190    #[test]
191    fn test_edit_registered_reaction() {
192        let msg = ReactionsMsg::edit_registered_reaction(
193            1,
194            1,
195            "test_code",
196            "test_value",
197            Addr::unchecked("user"),
198        );
199
200        let expected = MsgEditRegisteredReaction {
201            subspace_id: 1,
202            registered_reaction_id: 1,
203            shorthand_code: "test_code".into(),
204            display_value: "test_value".into(),
205            user: "user".into(),
206        };
207
208        assert_eq!(expected, msg)
209    }
210
211    #[test]
212    fn test_remove_registered_reaction() {
213        let msg = ReactionsMsg::remove_registered_reaction(1, 1, Addr::unchecked("user"));
214
215        let expected = MsgRemoveRegisteredReaction {
216            subspace_id: 1,
217            registered_reaction_id: 1,
218            user: "user".into(),
219        };
220
221        assert_eq!(expected, msg)
222    }
223
224    #[test]
225    fn test_set_reactions_params() {
226        let msg = ReactionsMsg::set_reactions_params(
227            1,
228            Some(RegisteredReactionValueParams { enabled: true }),
229            Some(FreeTextValueParams {
230                enabled: true,
231                max_length: 100,
232                reg_ex: "".into(),
233            }),
234            Addr::unchecked("user"),
235        );
236
237        let expected = MsgSetReactionsParams {
238            subspace_id: 1,
239            registered_reaction: Some(RegisteredReactionValueParams { enabled: true }),
240            free_text: Some(FreeTextValueParams {
241                enabled: true,
242                max_length: 100,
243                reg_ex: "".into(),
244            }),
245            user: "user".into(),
246        };
247
248        assert_eq!(expected, msg)
249    }
250}