tetratto_core/database/
uploads.rs1use oiseau::cache::Cache;
2use crate::model::auth::User;
3use crate::model::permissions::FinePermission;
4use crate::model::{Error, Result, uploads::MediaUpload};
5use crate::{auto_method, DataManager};
6
7use oiseau::PostgresRow;
8
9use oiseau::{execute, get, query_rows, params};
10
11impl DataManager {
12 pub(crate) fn get_upload_from_row(x: &PostgresRow) -> MediaUpload {
14 MediaUpload {
15 id: get!(x->0(i64)) as usize,
16 created: get!(x->1(i64)) as usize,
17 owner: get!(x->2(i64)) as usize,
18 what: serde_json::from_str(&get!(x->3(String))).unwrap(),
19 alt: get!(x->4(String)),
20 }
21 }
22
23 auto_method!(get_upload_by_id(usize as i64)@get_upload_from_row -> "SELECT * FROM uploads WHERE id = $1" --name="upload" --returns=MediaUpload --cache-key-tmpl="atto.upload:{}");
24
25 pub async fn get_uploads(&self, batch: usize, page: usize) -> Result<Vec<MediaUpload>> {
31 let conn = match self.0.connect().await {
32 Ok(c) => c,
33 Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
34 };
35
36 let res = query_rows!(
37 &conn,
38 "SELECT * FROM uploads ORDER BY created DESC LIMIT $1 OFFSET $2",
39 &[&(batch as i64), &((page * batch) as i64)],
40 |x| { Self::get_upload_from_row(x) }
41 );
42
43 if res.is_err() {
44 return Err(Error::GeneralNotFound("upload".to_string()));
45 }
46
47 Ok(res.unwrap())
48 }
49
50 pub async fn get_uploads_by_owner(
57 &self,
58 owner: usize,
59 batch: usize,
60 page: usize,
61 ) -> Result<Vec<MediaUpload>> {
62 let conn = match self.0.connect().await {
63 Ok(c) => c,
64 Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
65 };
66
67 let res = query_rows!(
68 &conn,
69 "SELECT * FROM uploads WHERE owner = $1 ORDER BY created DESC LIMIT $2 OFFSET $3",
70 &[&(owner as i64), &(batch as i64), &((page * batch) as i64)],
71 |x| { Self::get_upload_from_row(x) }
72 );
73
74 if res.is_err() {
75 return Err(Error::GeneralNotFound("upload".to_string()));
76 }
77
78 Ok(res.unwrap())
79 }
80
81 pub async fn get_uploads_by_owner_all(&self, owner: usize) -> Result<Vec<MediaUpload>> {
86 let conn = match self.0.connect().await {
87 Ok(c) => c,
88 Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
89 };
90
91 let res = query_rows!(
92 &conn,
93 "SELECT * FROM uploads WHERE owner = $1 ORDER BY created DESC",
94 &[&(owner as i64)],
95 |x| { Self::get_upload_from_row(x) }
96 );
97
98 if res.is_err() {
99 return Err(Error::GeneralNotFound("upload".to_string()));
100 }
101
102 Ok(res.unwrap())
103 }
104
105 pub async fn create_upload(&self, data: MediaUpload) -> Result<MediaUpload> {
110 let conn = match self.0.connect().await {
111 Ok(c) => c,
112 Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
113 };
114
115 let res = execute!(
116 &conn,
117 "INSERT INTO uploads VALUES ($1, $2, $3, $4, $5)",
118 params![
119 &(data.id as i64),
120 &(data.created as i64),
121 &(data.owner as i64),
122 &serde_json::to_string(&data.what).unwrap().as_str(),
123 &data.alt,
124 ]
125 );
126
127 if let Err(e) = res {
128 return Err(Error::DatabaseError(e.to_string()));
129 }
130
131 Ok(data)
133 }
134
135 pub async fn delete_upload(&self, id: usize) -> Result<()> {
136 let upload = self.get_upload_by_id(id).await?;
146 upload.remove(&self.0.0)?;
147
148 let conn = match self.0.connect().await {
150 Ok(c) => c,
151 Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
152 };
153
154 let res = execute!(&conn, "DELETE FROM uploads WHERE id = $1", &[&(id as i64)]);
155
156 if let Err(e) = res {
157 return Err(Error::DatabaseError(e.to_string()));
158 }
159
160 self.0.1.remove(format!("atto.upload:{}", id)).await;
161
162 Ok(())
164 }
165
166 pub async fn delete_upload_checked(&self, id: usize, user: &User) -> Result<()> {
167 let upload = self.get_upload_by_id(id).await?;
168
169 if user.id != upload.owner && !user.permissions.check(FinePermission::MANAGE_UPLOADS) {
171 return Err(Error::NotAllowed);
172 }
173
174 upload.remove(&self.0.0)?;
176
177 let conn = match self.0.connect().await {
179 Ok(c) => c,
180 Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
181 };
182
183 let res = execute!(&conn, "DELETE FROM uploads WHERE id = $1", &[&(id as i64)]);
184
185 if let Err(e) = res {
186 return Err(Error::DatabaseError(e.to_string()));
187 }
188
189 self.0.1.remove(format!("atto.upload:{}", id)).await;
190 Ok(())
191 }
192
193 auto_method!(update_upload_alt(&str)@get_upload_by_id:FinePermission::MANAGE_UPLOADS; -> "UPDATE uploads SET alt = $1 WHERE id = $2" --cache-key-tmpl="atto.upload:{}");
194}