use oiseau::cache::Cache;
use crate::model::{
Error, Result,
requests::{ActionRequest, ActionType},
auth::{Notification, User},
guest_logs::GuestLog,
permissions::{SecondaryPermission, FinePermission},
};
use crate::{auto_method, DataManager};
use oiseau::{execute, get, query_rows, params, PostgresRow};
impl DataManager {
pub(crate) fn get_guest_log_from_row(x: &PostgresRow) -> GuestLog {
GuestLog {
id: get!(x->0(i64)) as usize,
created: get!(x->1(i64)) as usize,
owner: get!(x->2(i64)) as usize,
name: get!(x->3(String)),
content: get!(x->4(String)),
waiting_for_review: get!(x->5(i32)) as i8 == 1,
ip: get!(x->6(String)),
}
}
auto_method!(get_guest_log_by_id()@get_guest_log_from_row -> "SELECT * FROM guest_logs WHERE id = $1" --name="guest_log" --returns=GuestLog --cache-key-tmpl="atto.guest_log:{}");
pub async fn get_guest_logs_by_owner(
&self,
owner: usize,
batch: usize,
page: usize,
) -> Result<Vec<GuestLog>> {
let conn = match self.0.connect().await {
Ok(c) => c,
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
};
let res = query_rows!(
&conn,
"SELECT * FROM guest_logs WHERE owner = $1 AND waiting_for_review = 0 ORDER BY created DESC LIMIT $2 OFFSET $3",
&[&(owner as i64), &(batch as i64), &((page * batch) as i64)],
|x| { Self::get_guest_log_from_row(x) }
);
if res.is_err() {
return Err(Error::GeneralNotFound("guest_log".to_string()));
}
Ok(res.unwrap())
}
pub async fn get_guest_logs_by_owner_wfr(
&self,
owner: usize,
batch: usize,
page: usize,
) -> Result<Vec<GuestLog>> {
let conn = match self.0.connect().await {
Ok(c) => c,
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
};
let res = query_rows!(
&conn,
"SELECT * FROM guest_logs WHERE owner = $1 AND waiting_for_review = 1 ORDER BY created DESC LIMIT $2 OFFSET $3",
&[&(owner as i64), &(batch as i64), &((page * batch) as i64)],
|x| { Self::get_guest_log_from_row(x) }
);
if res.is_err() {
return Err(Error::GeneralNotFound("guest_log".to_string()));
}
Ok(res.unwrap())
}
pub async fn create_guest_log(&self, data: GuestLog) -> Result<usize> {
if data.name.len() < 2 {
return Err(Error::DataTooShort("name".to_string()));
}
if data.name.len() > 32 {
return Err(Error::DataTooLong("name".to_string()));
}
if data.content.len() < 2 {
return Err(Error::DataTooShort("content".to_string()));
}
if data.content.len() > 2048 {
return Err(Error::DataTooLong("content".to_string()));
}
let owner = self.get_user_by_id(data.owner).await?;
let conn = match self.0.connect().await {
Ok(c) => c,
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
};
let res = execute!(
&conn,
"INSERT INTO guest_logs VALUES ($1, $2, $3, $4, $5, $6, $7)",
params![
&(data.id as i64),
&(data.created as i64),
&(data.owner as i64),
&data.name,
&data.content,
&{ if data.waiting_for_review { 1 } else { 0 } },
&data.ip,
]
);
if let Err(e) = res {
return Err(Error::DatabaseError(e.to_string()));
}
if data.waiting_for_review {
self.create_request(ActionRequest::with_id(
data.owner,
data.owner,
ActionType::GuestLog,
data.id,
None,
))
.await?;
} else {
self.create_notification(Notification::new(
"New message on your guestbook!".to_string(),
format!(
"You've received a new message in your [guestbook](/@{}/guestbook).",
owner.username
),
data.owner,
))
.await?;
}
Ok(data.id)
}
pub async fn delete_guest_log(&self, id: usize, user: &User) -> Result<()> {
let y = self.get_guest_log_by_id(id).await?;
if user.id != y.owner
&& !user
.secondary_permissions
.check(SecondaryPermission::MANAGE_GUEST_LOGS)
{
return Err(Error::NotAllowed);
}
let conn = match self.0.connect().await {
Ok(c) => c,
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
};
let res = execute!(
&conn,
"DELETE FROM guest_logs WHERE id = $1",
&[&(id as i64)]
);
if let Err(e) = res {
return Err(Error::DatabaseError(e.to_string()));
}
self.0.1.remove(format!("atto.guest_log:{}", id)).await;
if y.waiting_for_review
&& self
.get_request_by_id_linked_asset(y.owner, y.id)
.await
.is_ok()
{
self.delete_request(y.owner, y.id, user, false).await?;
}
Ok(())
}
pub async fn update_guest_log_waiting_for_review(
&self,
id: usize,
new_wfr: bool,
user: &User,
) -> Result<()> {
let y = self.get_guest_log_by_id(id).await?;
if y.owner != user.id && !user.permissions.check(FinePermission::MANAGE_REQUESTS) {
return Err(Error::NotAllowed);
}
let conn = match self.0.connect().await {
Ok(c) => c,
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
};
let res = execute!(
&conn,
"UPDATE guest_logs SET waiting_for_review = $1 WHERE id = $2",
params![&{ if new_wfr { 1 } else { 0 } }, &(id as i64)]
);
if let Err(e) = res {
return Err(Error::DatabaseError(e.to_string()));
}
self.0.1.remove(format!("atto.guest_log:{}", id)).await;
Ok(())
}
}