Skip to content
Clone

feat(export): add space spaces export

zhenyi committed
parent a16141a commit 35fb61c Browse files

1 file changed

+151 −0

Added lib/export/space/spaces.rs View lib/export/space/spaces.rs
@@ -0,0 +1,151 @@
1use serde::{Deserialize, Serialize};
2use time::OffsetDateTime;
3use time::format_description::well_known::Rfc3339;
4use utoipa::ToSchema;
5use uuid::Uuid;
6
7use db::space::spaces as space_entity;
8
9fn fmt_ts(ts: OffsetDateTime) -> String {
10 ts.format(&Rfc3339).unwrap_or_default()
11}
12
13#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, ToSchema)]
14#[serde(rename_all = "snake_case")]
15pub enum SpaceAllowJoinLevel {
16 None,
17 NeedMailActive,
18 NeedMailActiveAndLastOneMid,
19 NeedMailActiveAndLastOneHours,
20 NeedMailActiveAndPhoneActive,
21}
22
23impl From<space_entity::AllowJoinLevel> for SpaceAllowJoinLevel {
24 fn from(v: space_entity::AllowJoinLevel) -> Self {
25 match v {
26 space_entity::AllowJoinLevel::None => SpaceAllowJoinLevel::None,
27 space_entity::AllowJoinLevel::NeedMailActive => SpaceAllowJoinLevel::NeedMailActive,
28 space_entity::AllowJoinLevel::NeedMailActiveAndLastOneMid => {
29 SpaceAllowJoinLevel::NeedMailActiveAndLastOneMid
30 }
31 space_entity::AllowJoinLevel::NeedMailActiveAndLastOneHours => {
32 SpaceAllowJoinLevel::NeedMailActiveAndLastOneHours
33 }
34 space_entity::AllowJoinLevel::NeedMailActiveAndPhoneActive => {
35 SpaceAllowJoinLevel::NeedMailActiveAndPhoneActive
36 }
37 }
38 }
39}
40
41impl From<SpaceAllowJoinLevel> for space_entity::AllowJoinLevel {
42 fn from(v: SpaceAllowJoinLevel) -> Self {
43 match v {
44 SpaceAllowJoinLevel::None => space_entity::AllowJoinLevel::None,
45 SpaceAllowJoinLevel::NeedMailActive => space_entity::AllowJoinLevel::NeedMailActive,
46 SpaceAllowJoinLevel::NeedMailActiveAndLastOneMid => {
47 space_entity::AllowJoinLevel::NeedMailActiveAndLastOneMid
48 }
49 SpaceAllowJoinLevel::NeedMailActiveAndLastOneHours => {
50 space_entity::AllowJoinLevel::NeedMailActiveAndLastOneHours
51 }
52 SpaceAllowJoinLevel::NeedMailActiveAndPhoneActive => {
53 space_entity::AllowJoinLevel::NeedMailActiveAndPhoneActive
54 }
55 }
56 }
57}
58
59#[derive(Debug, Clone, Serialize, ToSchema)]
60pub struct SpaceView {
61 pub id: Uuid,
62 pub name: String,
63 pub description: Option<String>,
64 pub owner_id: Uuid,
65 pub avatar_url: Option<String>,
66 pub public: bool,
67 pub is_active: bool,
68 pub allow_join: bool,
69 pub allow_invite: bool,
70 pub member_count: i64,
71 pub welcome_message: Option<String>,
72 pub allow_join_level: SpaceAllowJoinLevel,
73 pub member_must_enable_2fa: bool,
74 pub created_at: String,
75 pub updated_at: String,
76}
77
78impl From<space_entity::Model> for SpaceView {
79 fn from(m: space_entity::Model) -> Self {
80 Self {
81 id: m.id,
82 name: m.name,
83 description: m.description,
84 owner_id: m.owner_id,
85 avatar_url: m.avatar_url,
86 public: m.public,
87 is_active: m.is_active,
88 allow_join: m.allow_join,
89 allow_invite: m.allow_invite,
90 member_count: m.member_count,
91 welcome_message: m.welcome_message,
92 allow_join_level: m.allow_join_level.into(),
93 member_must_enable_2fa: m.member_must_enable_2fa,
94 created_at: fmt_ts(m.created_at),
95 updated_at: fmt_ts(m.updated_at),
96 }
97 }
98}
99
100#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
101pub struct CreateSpaceCmd {
102 pub name: String,
103 #[serde(default)]
104 pub description: Option<String>,
105 #[serde(default)]
106 pub avatar_url: Option<String>,
107 #[serde(default = "default_bool_true")]
108 pub public: bool,
109 #[serde(default = "default_bool_true")]
110 pub allow_join: bool,
111 #[serde(default = "default_bool_true")]
112 pub allow_invite: bool,
113 #[serde(default)]
114 pub welcome_message: Option<String>,
115 #[serde(default)]
116 pub allow_join_level: Option<SpaceAllowJoinLevel>,
117 #[serde(default)]
118 pub member_must_enable_2fa: bool,
119}
120
121fn default_bool_true() -> bool {
122 true
123}
124
125#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
126pub struct UpdateSpaceCmd {
127 #[serde(default)]
128 pub name: Option<String>,
129 #[serde(default)]
130 pub description: Option<Option<String>>,
131 #[serde(default)]
132 pub avatar_url: Option<Option<String>>,
133 #[serde(default)]
134 pub public: Option<bool>,
135 #[serde(default)]
136 pub allow_join: Option<bool>,
137 #[serde(default)]
138 pub allow_invite: Option<bool>,
139 #[serde(default)]
140 pub welcome_message: Option<Option<String>>,
141 #[serde(default)]
142 pub allow_join_level: Option<SpaceAllowJoinLevel>,
143 #[serde(default)]
144 pub member_must_enable_2fa: Option<bool>,
145}
146
147#[derive(Debug, Clone, Serialize, ToSchema)]
148pub struct SpacePage {
149 pub items: Vec<SpaceView>,
150 pub total: i64,
151}
No newline at end of file