feat(export): add space members export
zhenyi committed
Added lib/export/space/members.rs lib/export/space/members.rs +158 −0 View lib/export/space/members.rs
@@ -0,0 +1,158 @@
1use serde::{Deserialize, Serialize};
2use time::OffsetDateTime;
3use time::format_description::well_known::Rfc3339;
4use utoipa::ToSchema;
5use uuid::Uuid;
6
7use db::space::space_members as member_entity;
8
9fn fmt_ts(ts: OffsetDateTime) -> String {
10 ts.format(&Rfc3339).unwrap_or_default()
11}
12
13fn fmt_ts_opt(ts: Option<OffsetDateTime>) -> Option<String> {
14 ts.map(fmt_ts)
15}
16
17#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize, ToSchema)]
18#[serde(rename_all = "snake_case")]
19pub enum SpaceMemberStatus {
20 #[default]
21 Active,
22 Inactive,
23 Left,
24 Removed,
25}
26
27impl From<member_entity::MemberStatus> for SpaceMemberStatus {
28 fn from(v: member_entity::MemberStatus) -> Self {
29 match v {
30 member_entity::MemberStatus::Active => Self::Active,
31 member_entity::MemberStatus::Inactive => Self::Inactive,
32 member_entity::MemberStatus::Left => Self::Left,
33 member_entity::MemberStatus::Removed => Self::Removed,
34 }
35 }
36}
37
38impl From<SpaceMemberStatus> for member_entity::MemberStatus {
39 fn from(v: SpaceMemberStatus) -> Self {
40 match v {
41 SpaceMemberStatus::Active => Self::Active,
42 SpaceMemberStatus::Inactive => Self::Inactive,
43 SpaceMemberStatus::Left => Self::Left,
44 SpaceMemberStatus::Removed => Self::Removed,
45 }
46 }
47}
48
49#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize, ToSchema)]
50#[serde(rename_all = "snake_case")]
51pub enum MessageNotifications {
52 #[default]
53 All,
54 Mentions,
55 Nothing,
56}
57
58impl From<member_entity::MessageNotifications> for MessageNotifications {
59 fn from(v: member_entity::MessageNotifications) -> Self {
60 match v {
61 member_entity::MessageNotifications::All => Self::All,
62 member_entity::MessageNotifications::Mentions => Self::Mentions,
63 member_entity::MessageNotifications::Nothing => Self::Nothing,
64 }
65 }
66}
67
68impl From<MessageNotifications> for member_entity::MessageNotifications {
69 fn from(v: MessageNotifications) -> Self {
70 match v {
71 MessageNotifications::All => Self::All,
72 MessageNotifications::Mentions => Self::Mentions,
73 MessageNotifications::Nothing => Self::Nothing,
74 }
75 }
76}
77
78#[derive(Debug, Clone, Serialize, ToSchema)]
79pub struct SpaceMemberView {
80 pub space_id: Uuid,
81 pub user_id: Uuid,
82 pub alias_name: Option<String>,
83 pub alias_avatar_url: Option<String>,
84 pub status: SpaceMemberStatus,
85 pub last_active_at: Option<String>,
86 pub left_at: Option<String>,
87 pub invited_at: Option<String>,
88 pub invited_by: Option<Uuid>,
89 pub joined_at: String,
90 pub created_at: String,
91 pub updated_at: Option<String>,
92 pub message_notifications: MessageNotifications,
93 pub muted: bool,
94 pub mute_until: Option<String>,
95 pub mobile_push: bool,
96 pub suppress_everyone: bool,
97 pub suppress_roles: bool,
98 pub hide_muted_channels: bool,
99}
100
101impl From<member_entity::Model> for SpaceMemberView {
102 fn from(m: member_entity::Model) -> Self {
103 Self {
104 space_id: m.space_id,
105 user_id: m.user_id,
106 alias_name: m.alias_name,
107 alias_avatar_url: m.alias_avatar_url,
108 status: m.status.into(),
109 last_active_at: fmt_ts_opt(m.last_active_at),
110 left_at: fmt_ts_opt(m.left_at),
111 invited_at: fmt_ts_opt(m.invited_at),
112 invited_by: m.invited_by,
113 joined_at: fmt_ts(m.joined_at),
114 created_at: fmt_ts(m.created_at),
115 updated_at: fmt_ts_opt(m.updated_at),
116 message_notifications: m.message_notifications.into(),
117 muted: m.muted,
118 mute_until: fmt_ts_opt(m.mute_until),
119 mobile_push: m.mobile_push,
120 suppress_everyone: m.suppress_everyone,
121 suppress_roles: m.suppress_roles,
122 hide_muted_channels: m.hide_muted_channels,
123 }
124 }
125}
126
127#[derive(Debug, Clone, Deserialize, ToSchema)]
128pub struct AddMemberCmd {
129 pub user_id: Uuid,
130 pub alias_name: Option<String>,
131 pub alias_avatar_url: Option<String>,
132 pub message_notifications: Option<MessageNotifications>,
133 pub muted: Option<bool>,
134 pub mute_until: Option<String>,
135 pub mobile_push: Option<bool>,
136 pub suppress_everyone: Option<bool>,
137 pub suppress_roles: Option<bool>,
138 pub hide_muted_channels: Option<bool>,
139}
140
141#[derive(Debug, Clone, Deserialize, ToSchema)]
142pub struct UpdateMemberCmd {
143 pub alias_name: Option<Option<String>>,
144 pub alias_avatar_url: Option<Option<String>>,
145 pub message_notifications: Option<MessageNotifications>,
146 pub muted: Option<bool>,
147 pub mute_until: Option<Option<String>>,
148 pub mobile_push: Option<bool>,
149 pub suppress_everyone: Option<bool>,
150 pub suppress_roles: Option<bool>,
151 pub hide_muted_channels: Option<bool>,
152}
153
154#[derive(Debug, Clone, Serialize, ToSchema)]
155pub struct SpaceMemberPage {
156 pub items: Vec<SpaceMemberView>,
157 pub total: i64,
158}
No newline at end of file