-
-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathinstance.rs
More file actions
211 lines (196 loc) · 6.6 KB
/
instance.rs
File metadata and controls
211 lines (196 loc) · 6.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
use core::fmt;
use serde::{Deserialize, Serialize};
use sqlx::{query, query_as, SqliteExecutor};
use super::{Id, NoId};
use crate::proto;
#[derive(Serialize, Deserialize, Debug)]
pub struct Instance<I = NoId> {
pub id: I,
pub name: String,
pub uuid: String,
pub url: String,
pub proxy_url: String,
pub username: String,
pub token: Option<String>,
pub disable_all_traffic: bool,
pub enterprise_enabled: bool,
pub use_openid_for_mfa: bool,
pub openid_display_name: Option<String>,
}
impl fmt::Display for Instance<Id> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}(ID: {})", self.name, self.id)
}
}
impl From<proto::InstanceInfo> for Instance<NoId> {
fn from(instance_info: proto::InstanceInfo) -> Self {
Self {
id: NoId,
name: instance_info.name,
uuid: instance_info.id,
url: instance_info.url,
proxy_url: instance_info.proxy_url,
username: instance_info.username,
token: None,
disable_all_traffic: instance_info.disable_all_traffic,
enterprise_enabled: instance_info.enterprise_enabled,
use_openid_for_mfa: instance_info.use_openid_for_mfa,
openid_display_name: instance_info.openid_display_name,
}
}
}
impl Instance<Id> {
pub(crate) async fn save<'e, E>(&mut self, executor: E) -> Result<(), sqlx::Error>
where
E: SqliteExecutor<'e>,
{
query!(
"UPDATE instance SET name = $1, uuid = $2, url = $3, proxy_url = $4, username = $5, \
disable_all_traffic = $6, enterprise_enabled = $7, token = $8, use_openid_for_mfa = $9, openid_display_name = $10 WHERE id = $11;",
self.name,
self.uuid,
self.url,
self.proxy_url,
self.username,
self.disable_all_traffic,
self.enterprise_enabled,
self.token,
self.use_openid_for_mfa,
self.openid_display_name,
self.id
)
.execute(executor)
.await?;
Ok(())
}
pub(crate) async fn all<'e, E>(executor: E) -> Result<Vec<Self>, sqlx::Error>
where
E: SqliteExecutor<'e>,
{
let instances = query_as!(
Self,
"SELECT id \"id: _\", name, uuid, url, proxy_url, username, token \"token?\", \
disable_all_traffic, enterprise_enabled, use_openid_for_mfa, openid_display_name FROM instance;"
)
.fetch_all(executor)
.await?;
Ok(instances)
}
pub(crate) async fn find_by_id<'e, E>(executor: E, id: Id) -> Result<Option<Self>, sqlx::Error>
where
E: SqliteExecutor<'e>,
{
let instance = query_as!(
Self,
"SELECT id \"id: _\", name, uuid, url, proxy_url, username, token \"token?\", \
disable_all_traffic, enterprise_enabled, use_openid_for_mfa, openid_display_name FROM instance WHERE id = $1;",
id
)
.fetch_optional(executor)
.await?;
Ok(instance)
}
pub(crate) async fn delete_by_id<'e, E>(executor: E, id: Id) -> Result<(), sqlx::Error>
where
E: SqliteExecutor<'e>,
{
// delete instance
query!("DELETE FROM instance WHERE id = $1", id)
.execute(executor)
.await?;
Ok(())
}
pub(crate) async fn delete<'e, E>(&self, executor: E) -> Result<(), sqlx::Error>
where
E: SqliteExecutor<'e>,
{
Instance::delete_by_id(executor, self.id).await?;
Ok(())
}
pub(crate) async fn all_with_token<'e, E>(executor: E) -> Result<Vec<Self>, sqlx::Error>
where
E: SqliteExecutor<'e>,
{
let instances = query_as!(
Self,
"SELECT id \"id: _\", name, uuid, url, proxy_url, username, token, \
disable_all_traffic, enterprise_enabled, use_openid_for_mfa, openid_display_name FROM instance
WHERE token IS NOT NULL;"
)
.fetch_all(executor)
.await?;
Ok(instances)
}
}
// This compares proto::InstanceInfo, not to be confused with regular InstanceInfo defined below
impl PartialEq<proto::InstanceInfo> for Instance<Id> {
fn eq(&self, other: &proto::InstanceInfo) -> bool {
self.name == other.name
&& self.uuid == other.id
&& self.url == other.url
&& self.proxy_url == other.proxy_url
&& self.username == other.username
&& self.disable_all_traffic == other.disable_all_traffic
&& self.enterprise_enabled == other.enterprise_enabled
&& self.use_openid_for_mfa == other.use_openid_for_mfa
&& self.openid_display_name == other.openid_display_name
}
}
impl Instance<NoId> {
pub async fn save<'e, E>(self, executor: E) -> Result<Instance<Id>, sqlx::Error>
where
E: SqliteExecutor<'e>,
{
let url = self.url.clone();
let proxy_url = self.proxy_url.clone();
let result = query!(
"INSERT INTO instance (name, uuid, url, proxy_url, username, token, \
disable_all_traffic, enterprise_enabled, use_openid_for_mfa, openid_display_name) \
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) RETURNING id;",
self.name,
self.uuid,
url,
proxy_url,
self.username,
self.token,
self.disable_all_traffic,
self.enterprise_enabled,
self.use_openid_for_mfa,
self.openid_display_name
)
.fetch_one(executor)
.await?;
Ok(Instance::<Id> {
id: result.id,
name: self.name,
uuid: self.uuid,
url: self.url,
proxy_url: self.proxy_url,
username: self.username,
token: self.token,
disable_all_traffic: self.disable_all_traffic,
enterprise_enabled: self.enterprise_enabled,
use_openid_for_mfa: self.use_openid_for_mfa,
openid_display_name: self.openid_display_name,
})
}
}
#[derive(Debug, Serialize, Deserialize)]
pub struct InstanceInfo<I = NoId> {
pub id: I,
pub name: String,
pub uuid: String,
pub url: String,
pub proxy_url: String,
pub active: bool,
pub pubkey: String,
pub disable_all_traffic: bool,
pub enterprise_enabled: bool,
pub use_openid_for_mfa: bool,
pub openid_display_name: Option<String>,
}
impl fmt::Display for InstanceInfo<Id> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}(ID: {})", self.name, self.id)
}
}