hi_push/service/
model.rs

1use std::collections::HashMap;
2
3use derive_builder::Builder;
4use mongodb::bson::oid::ObjectId;
5use serde::{Deserialize, Serialize};
6use serde_repr::{Deserialize_repr, Serialize_repr};
7
8#[derive(Debug, Serialize_repr, Deserialize_repr, Default, Clone)]
9#[repr(i32)]
10pub enum ChannelType {
11    #[default]
12    Unknown = 0,
13    #[cfg(feature = "xiaomi")]
14    Mi = 1,
15    #[cfg(feature = "huawei")]
16    Huawei = 2,
17    #[cfg(feature = "fcm")]
18    Fcm = 3,
19    #[cfg(feature = "wecom")]
20    Wecom = 4,
21    #[cfg(feature = "apns")]
22    Apns = 5,
23    #[cfg(feature = "email")]
24    Email = 6,
25    #[cfg(feature = "rtm")]
26    Rtm = 7,
27}
28
29/*
30    App
31*/
32#[derive(Debug, Serialize, Deserialize, Clone)]
33#[serde(rename_all = "camelCase")]
34pub struct App {
35    #[cfg(feature = "mongo")]
36    #[serde(rename = "_id")]
37    pub id: ObjectId,
38    pub name: String,
39    pub client_id: String,
40    pub client_secret: String,
41
42    pub create_ts: i64,
43    pub update_ts: i64,
44}
45
46/*
47    Channel
48    key_type: todo!(),
49    project_id: Some(conf.project_id.to_string()),
50    private_key_id: todo!(),
51    private_key: todo!(),
52    client_email: todo!(),
53    client_id: todo!(),
54    auth_uri: todo!(),
55    token_uri: todo!(),
56    auth_provider_x509_cert_url: todo!(),
57    client_x509_cert_url: todo!(),
58*/
59#[derive(Debug, Serialize, Deserialize, Clone)]
60#[serde(rename_all = "camelCase")]
61pub struct Channel {
62    #[cfg(feature = "mongo")]
63    #[serde(rename = "_id")]
64    pub id: ObjectId,
65    pub ch_id: String,
66    pub app_id: String,
67    #[serde(rename = "type")]
68    pub _type: ChannelType,
69    #[serde(skip_serializing_if = "Option::is_none")]
70    pub client_id: Option<String>,
71    #[serde(skip_serializing_if = "Option::is_none")]
72    pub client_secret: Option<String>,
73    //
74    #[serde(skip_serializing_if = "Option::is_none")]
75    pub project_id: Option<String>,
76    // apple
77    #[serde(skip_serializing_if = "Option::is_none")]
78    pub certs: Option<Vec<u8>>,
79    // wecom
80    #[serde(skip_serializing_if = "Option::is_none")]
81    pub agentid: Option<i64>,
82    #[serde(skip_serializing_if = "Option::is_none")]
83    pub key_type: Option<String>,
84    #[serde(skip_serializing_if = "Option::is_none")]
85    pub private_key_id: Option<String>,
86    #[serde(skip_serializing_if = "Option::is_none")]
87    pub private_key: Option<String>,
88    #[serde(skip_serializing_if = "Option::is_none")]
89    pub client_email: Option<String>,
90    #[serde(skip_serializing_if = "Option::is_none")]
91    pub auth_uri: Option<String>,
92    #[serde(skip_serializing_if = "Option::is_none")]
93    pub token_uri: Option<String>,
94    #[serde(skip_serializing_if = "Option::is_none")]
95    pub auth_provider_x509_cert_url: Option<String>,
96    #[serde(skip_serializing_if = "Option::is_none")]
97    pub client_x509_cert_url: Option<String>,
98    // email
99    #[serde(skip_serializing_if = "Option::is_none")]
100    pub addr: Option<String>,
101
102    pub create_ts: i64,
103    pub update_ts: i64,
104}
105
106impl Default for Channel {
107    fn default() -> Self {
108        Self {
109            id: Default::default(),
110            ch_id: uuid::Uuid::new_v4().to_string(),
111            app_id: "".to_string(),
112            _type: Default::default(),
113            client_id: None,
114            client_secret: None,
115            project_id: None,
116            certs: None,
117            agentid: None,
118            key_type: None,
119            private_key_id: None,
120            private_key: None,
121            client_email: None,
122            auth_uri: None,
123            token_uri: None,
124            auth_provider_x509_cert_url: None,
125            client_x509_cert_url: None,
126            addr: None,
127            create_ts: chrono::Utc::now().timestamp(),
128            update_ts: chrono::Utc::now().timestamp(),
129        }
130    }
131}
132
133/*
134    Token
135*/
136#[derive(Debug, Serialize, Deserialize, Clone)]
137#[serde(rename_all = "camelCase")]
138pub struct Token {
139    #[cfg(feature = "mongo")]
140    #[serde(rename = "_id")]
141    pub id: ObjectId,
142    pub app_id: String,
143    pub ch_id: String,
144    pub group: String,
145    pub token: String,
146    pub valid: bool,
147    pub create_ts: i64,
148    pub update_ts: i64,
149}
150
151impl Default for Token {
152    fn default() -> Self {
153        Self {
154            id: Default::default(),
155            ch_id: Default::default(),
156            token: Default::default(),
157            valid: true,
158            create_ts: chrono::Utc::now().timestamp(),
159            update_ts: chrono::Utc::now().timestamp(),
160            group: Default::default(),
161            app_id: Default::default(),
162        }
163    }
164}
165
166/**
167 *
168 * Data transfer object
169 */
170#[derive(Debug, Serialize_repr, Clone, Default)]
171#[repr(u8)]
172pub enum Code {
173    #[default]
174    Ok,
175    Err,
176}
177
178#[derive(Debug, Serialize, Builder, Clone, Default)]
179pub struct Response<T = String> {
180    #[builder(default)]
181    pub code: Code,
182    #[builder(default)]
183    #[serde(skip_serializing_if = "Option::is_none")]
184    pub data: Option<T>,
185    #[builder(default)]
186    pub msg: String,
187    #[builder(default)]
188    #[serde(skip_serializing_if = "Option::is_none")]
189    pub errors: Option<Vec<String>>,
190}
191
192impl<T> From<anyhow::Error> for Response<T> {
193    fn from(e: anyhow::Error) -> Self {
194        Self {
195            code: Code::Err,
196            data: Option::<T>::None,
197            msg: e.to_string(),
198            errors: None,
199        }
200    }
201}
202
203#[derive(Serialize, Clone)]
204#[serde(rename_all = "camelCase")]
205pub struct RegisterTokenResp {
206    pub(crate) success: u64,
207    pub(crate) failure: u64,
208    pub failure_tokens: Vec<String>,
209}
210
211#[derive(Deserialize)]
212#[serde(rename_all = "camelCase")]
213pub struct RegisterTokenParams {
214    pub group: String,
215    pub token: String,
216    pub ch_id: String,
217    #[serde(rename = "override")]
218    pub _override: Option<bool>,
219}
220
221#[derive(Deserialize)]
222#[serde(rename_all = "camelCase")]
223pub struct RevokeTokenParams {
224    pub group: String,
225    pub token: String,
226    pub ch_id: String,
227}
228
229#[derive(Serialize, Clone)]
230#[serde(rename_all = "camelCase")]
231pub struct RevokeTokenResp {
232    pub success: u64,
233    pub failure: u64,
234    pub failure_tokens: Vec<String>,
235}
236
237#[derive(Deserialize)]
238#[serde(rename_all = "camelCase")]
239pub struct Condition {
240    pub channels: Vec<String>,
241}
242
243#[derive(Deserialize)]
244#[serde(rename_all = "camelCase")]
245pub struct Options {
246    pub condition: Condition,
247}
248
249#[derive(Deserialize)]
250pub enum Body {
251    Json(serde_json::Map<String, serde_json::Value>),
252    Text(String),
253}
254
255#[derive(Deserialize)]
256pub struct PushTransparentParams {
257    pub groups: Vec<String>,
258    pub channels: Vec<String>,
259    #[serde(flatten)]
260    pub body: Body,
261    #[serde(flatten)]
262    pub platform_extra: PlatformParams,
263}
264
265#[derive(Deserialize)]
266#[serde(rename_all = "camelCase")]
267pub struct PushNotificationParams {
268    pub groups: Vec<String>,
269    pub channels: Vec<String>,
270    pub title: String,
271    pub body: String,
272    #[serde(flatten)]
273    pub platform_extra: Option<PlatformParams>,
274}
275
276#[derive(Deserialize)]
277#[serde(rename_all = "camelCase")]
278pub struct PlatformParams {
279    pub android: String,
280    pub apns: String,
281    pub wecom: String,
282    pub rtm: String,
283}
284
285#[derive(Deserialize)]
286#[serde(rename_all = "camelCase")]
287pub enum PublicChannel {
288    #[serde(rename_all = "camelCase")]
289    Wecom {
290        client_id: String,
291        client_secret: String,
292        agentid: i64,
293    },
294    #[serde(rename_all = "camelCase")]
295    Fcm {
296        key_type: String,
297        private_key_id: String,
298        private_key: String,
299        client_email: String,
300        auth_uri: String,
301        token_uri: String,
302        auth_provider_x509_cert_url: String,
303        client_x509_cert_url: String,
304    },
305    #[serde(rename_all = "camelCase")]
306    Email {
307        client_id: String,
308        client_secret: String,
309        addr: String,
310    },
311    #[serde(rename_all = "camelCase")]
312    Xiaomi {
313        client_id: String,
314        client_secret: String,
315    },
316    #[serde(rename_all = "camelCase")]
317    Apns {
318        client_id: String,
319        client_secret: String,
320    },
321    #[serde(rename_all = "camelCase")]
322    Huawei {
323        client_id: String,
324        client_secret: String,
325    },
326}
327
328#[derive(Debug, Serialize, Clone)]
329#[serde(rename_all = "camelCase")]
330pub struct Running {
331    pub ch_ids: Vec<String>,
332}
333
334#[derive(Debug, Deserialize, Clone)]
335#[serde(rename_all = "camelCase")]
336pub struct CreateAppParams {
337    pub name: String,
338}
339
340#[derive(Debug, Deserialize, Clone)]
341#[serde(rename_all = "camelCase")]
342pub struct DeleteChannelParams {
343    pub ch_id: String,
344}
345
346#[derive(Debug, Serialize)]
347#[serde(rename_all = "camelCase")]
348pub struct PushResp {
349    pub success: i64,
350    pub failure: i64,
351    pub results: HashMap<String, crate::PushResults>,
352}