onechatsocial_models/v0/
channels.rs

1use super::File;
2
3use onechatsocial_permissions::OverrideField;
4use std::collections::{HashMap, HashSet};
5
6auto_derived!(
7    /// Channel
8    #[serde(tag = "channel_type")]
9    pub enum Channel {
10        /// Personal "Saved Notes" channel which allows users to save messages
11        SavedMessages {
12            /// Unique Id
13            #[cfg_attr(feature = "serde", serde(rename = "_id"))]
14            id: String,
15            /// Id of the user this channel belongs to
16            user: String,
17        },
18        /// Direct message channel between two users
19        DirectMessage {
20            /// Unique Id
21            #[cfg_attr(feature = "serde", serde(rename = "_id"))]
22            id: String,
23
24            /// Whether this direct message channel is currently open on both sides
25            active: bool,
26            /// 2-tuple of user ids participating in direct message
27            recipients: Vec<String>,
28            /// Id of the last message sent in this channel
29            #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
30            last_message_id: Option<String>,
31        },
32        /// Group channel between 1 or more participants
33        Group {
34            /// Unique Id
35            #[cfg_attr(feature = "serde", serde(rename = "_id"))]
36            id: String,
37
38            /// Display name of the channel
39            name: String,
40            /// User id of the owner of the group
41            owner: String,
42            /// Channel description
43            #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
44            description: Option<String>,
45            /// Array of user ids participating in channel
46            recipients: Vec<String>,
47
48            /// Custom icon attachment
49            #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
50            icon: Option<File>,
51            /// Id of the last message sent in this channel
52            #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
53            last_message_id: Option<String>,
54
55            /// Permissions assigned to members of this group
56            /// (does not apply to the owner of the group)
57            #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
58            permissions: Option<i64>,
59
60            /// Whether this group is marked as not safe for work
61            #[cfg_attr(
62                feature = "serde",
63                serde(skip_serializing_if = "crate::if_false", default)
64            )]
65            nsfw: bool,
66        },
67        /// Text channel belonging to a server
68        TextChannel {
69            /// Unique Id
70            #[cfg_attr(feature = "serde", serde(rename = "_id"))]
71            id: String,
72            /// Id of the server this channel belongs to
73            server: String,
74
75            /// Display name of the channel
76            name: String,
77            /// Channel description
78            #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
79            description: Option<String>,
80
81            /// Custom icon attachment
82            #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
83            icon: Option<File>,
84            /// Id of the last message sent in this channel
85            #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
86            last_message_id: Option<String>,
87
88            /// Default permissions assigned to users in this channel
89            #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
90            default_permissions: Option<OverrideField>,
91            /// Permissions assigned based on role to this channel
92            #[cfg_attr(
93                feature = "serde",
94                serde(
95                    default = "HashMap::<String, OverrideField>::new",
96                    skip_serializing_if = "HashMap::<String, OverrideField>::is_empty"
97                )
98            )]
99            role_permissions: HashMap<String, OverrideField>,
100
101            /// Whether this channel is marked as not safe for work
102            #[cfg_attr(
103                feature = "serde",
104                serde(skip_serializing_if = "crate::if_false", default)
105            )]
106            nsfw: bool,
107        },
108        /// Voice channel belonging to a server
109        VoiceChannel {
110            /// Unique Id
111            #[cfg_attr(feature = "serde", serde(rename = "_id"))]
112            id: String,
113            /// Id of the server this channel belongs to
114            server: String,
115
116            /// Display name of the channel
117            name: String,
118            #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
119            /// Channel description
120            description: Option<String>,
121            /// Custom icon attachment
122            #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
123            icon: Option<File>,
124
125            /// Default permissions assigned to users in this channel
126            #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
127            default_permissions: Option<OverrideField>,
128            /// Permissions assigned based on role to this channel
129            #[cfg_attr(
130                feature = "serde",
131                serde(
132                    default = "HashMap::<String, OverrideField>::new",
133                    skip_serializing_if = "HashMap::<String, OverrideField>::is_empty"
134                )
135            )]
136            role_permissions: HashMap<String, OverrideField>,
137
138            /// Whether this channel is marked as not safe for work
139            #[cfg_attr(
140                feature = "serde",
141                serde(skip_serializing_if = "crate::if_false", default)
142            )]
143            nsfw: bool,
144        },
145    }
146
147    /// Partial representation of a channel
148    #[derive(Default)]
149    pub struct PartialChannel {
150        #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
151        pub name: Option<String>,
152        #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
153        pub owner: Option<String>,
154        #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
155        pub description: Option<String>,
156        #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
157        pub icon: Option<File>,
158        #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
159        pub nsfw: Option<bool>,
160        #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
161        pub active: Option<bool>,
162        #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
163        pub permissions: Option<i64>,
164        #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
165        pub role_permissions: Option<HashMap<String, OverrideField>>,
166        #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
167        pub default_permissions: Option<OverrideField>,
168        #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
169        pub last_message_id: Option<String>,
170    }
171
172    /// Optional fields on channel object
173    pub enum FieldsChannel {
174        Description,
175        Icon,
176        DefaultPermissions,
177    }
178
179    /// New webhook information
180    #[cfg_attr(feature = "validator", derive(validator::Validate))]
181    pub struct DataEditChannel {
182        /// Channel name
183        #[cfg_attr(feature = "validator", validate(length(min = 1, max = 32)))]
184        pub name: Option<String>,
185
186        /// Channel description
187        #[cfg_attr(feature = "validator", validate(length(min = 0, max = 1024)))]
188        pub description: Option<String>,
189
190        /// Group owner
191        pub owner: Option<String>,
192
193        /// Icon
194        ///
195        /// Provide an Autumn attachment Id.
196        #[cfg_attr(feature = "validator", validate(length(min = 1, max = 128)))]
197        pub icon: Option<String>,
198
199        /// Whether this channel is age-restricted
200        pub nsfw: Option<bool>,
201
202        /// Whether this channel is archived
203        pub archived: Option<bool>,
204
205        /// Fields to remove from channel
206        #[cfg_attr(feature = "serde", serde(default))]
207        pub remove: Option<Vec<FieldsChannel>>,
208    }
209
210    /// Create new group
211    #[derive(Default)]
212    #[cfg_attr(feature = "validator", derive(validator::Validate))]
213    pub struct DataCreateGroup {
214        /// Group name
215        #[cfg_attr(feature = "validator", validate(length(min = 1, max = 32)))]
216        pub name: String,
217        /// Group description
218        #[cfg_attr(feature = "validator", validate(length(min = 0, max = 1024)))]
219        pub description: Option<String>,
220        /// Group icon
221        #[cfg_attr(feature = "validator", validate(length(min = 1, max = 128)))]
222        pub icon: Option<String>,
223        /// Array of user IDs to add to the group
224        ///
225        /// Must be friends with these users.
226        #[cfg_attr(feature = "validator", validate(length(min = 0, max = 49)))]
227        #[serde(default)]
228        pub users: HashSet<String>,
229        /// Whether this group is age-restricted
230        #[serde(skip_serializing_if = "Option::is_none")]
231        pub nsfw: Option<bool>,
232    }
233
234    /// Server Channel Type
235    #[derive(Default)]
236    pub enum LegacyServerChannelType {
237        /// Text Channel
238        #[default]
239        Text,
240        /// Voice Channel
241        Voice,
242    }
243
244    /// Create new server channel
245    #[derive(Default)]
246    #[cfg_attr(feature = "validator", derive(validator::Validate))]
247    pub struct DataCreateServerChannel {
248        /// Channel type
249        #[serde(rename = "type", default = "LegacyServerChannelType::default")]
250        pub channel_type: LegacyServerChannelType,
251        /// Channel name
252        #[cfg_attr(feature = "validator", validate(length(min = 1, max = 32)))]
253        pub name: String,
254        /// Channel description
255        #[cfg_attr(feature = "validator", validate(length(min = 0, max = 1024)))]
256        pub description: Option<String>,
257        /// Whether this channel is age restricted
258        #[serde(skip_serializing_if = "Option::is_none")]
259        pub nsfw: Option<bool>,
260    }
261);
262
263impl Channel {
264    /// Get a reference to this channel's id
265    pub fn id(&self) -> &str {
266        match self {
267            Channel::DirectMessage { id, .. }
268            | Channel::Group { id, .. }
269            | Channel::SavedMessages { id, .. }
270            | Channel::TextChannel { id, .. }
271            | Channel::VoiceChannel { id, .. } => id,
272        }
273    }
274}