add: mail ui
This commit is contained in:
parent
2e60cbc464
commit
b2a73d286b
24 changed files with 993 additions and 259 deletions
|
@ -1,16 +1,27 @@
|
|||
use axum::{response::IntoResponse, Extension, Json, extract::Path};
|
||||
use axum::{
|
||||
extract::{Path, Query},
|
||||
response::IntoResponse,
|
||||
Extension, Json,
|
||||
};
|
||||
use tetratto_core::model::{auth::Notification, mail::Letter, oauth, ApiReturn, Error};
|
||||
use crate::{get_user_from_token, State, cookie::CookieJar};
|
||||
use crate::{cookie::CookieJar, get_user_from_token, routes::pages::PaginatedQuery, State};
|
||||
use super::CreateLetter;
|
||||
|
||||
pub async fn list_received_request(jar: CookieJar, data: Extension<State>) -> impl IntoResponse {
|
||||
pub async fn list_received_request(
|
||||
jar: CookieJar,
|
||||
data: Extension<State>,
|
||||
Query(props): Query<PaginatedQuery>,
|
||||
) -> impl IntoResponse {
|
||||
let data = &(data.read().await).0;
|
||||
let user = match get_user_from_token!(jar, data, oauth::AppScope::UserReadLetters) {
|
||||
Some(ua) => ua,
|
||||
None => return Json(Error::NotAllowed.into()),
|
||||
};
|
||||
|
||||
let letters = match data.get_received_letters_by_user(user.id).await {
|
||||
let letters = match data
|
||||
.get_received_letters_by_user(user.id, 12, props.page)
|
||||
.await
|
||||
{
|
||||
Ok(l) => l,
|
||||
Err(e) => return Json(e.into()),
|
||||
};
|
||||
|
@ -22,14 +33,18 @@ pub async fn list_received_request(jar: CookieJar, data: Extension<State>) -> im
|
|||
})
|
||||
}
|
||||
|
||||
pub async fn list_sent_request(jar: CookieJar, data: Extension<State>) -> impl IntoResponse {
|
||||
pub async fn list_sent_request(
|
||||
jar: CookieJar,
|
||||
data: Extension<State>,
|
||||
Query(props): Query<PaginatedQuery>,
|
||||
) -> impl IntoResponse {
|
||||
let data = &(data.read().await).0;
|
||||
let user = match get_user_from_token!(jar, data, oauth::AppScope::UserReadLetters) {
|
||||
Some(ua) => ua,
|
||||
None => return Json(Error::NotAllowed.into()),
|
||||
};
|
||||
|
||||
let letters = match data.get_letters_by_user(user.id).await {
|
||||
let letters = match data.get_letters_by_user(user.id, 12, props.page).await {
|
||||
Ok(l) => l,
|
||||
Err(e) => return Json(e.into()),
|
||||
};
|
||||
|
@ -92,7 +107,7 @@ pub async fn delete_request(
|
|||
pub async fn create_request(
|
||||
jar: CookieJar,
|
||||
data: Extension<State>,
|
||||
Json(props): Json<CreateLetter>,
|
||||
Json(mut props): Json<CreateLetter>,
|
||||
) -> impl IntoResponse {
|
||||
let data = &(data.read().await).0;
|
||||
let user = match get_user_from_token!(jar, data, oauth::AppScope::UserCreateLetters) {
|
||||
|
@ -100,10 +115,51 @@ pub async fn create_request(
|
|||
None => return Json(Error::NotAllowed.into()),
|
||||
};
|
||||
|
||||
// check receivers
|
||||
props.receivers.dedup();
|
||||
let mut receivers = Vec::new();
|
||||
|
||||
if props.receivers.len() < 1 {
|
||||
return Json(Error::DataTooShort("receivers".to_string()).into());
|
||||
} else if props.receivers.len() > 10 {
|
||||
return Json(Error::DataTooLong("receivers".to_string()).into());
|
||||
}
|
||||
|
||||
for receiver in &props.receivers {
|
||||
let other_user = match if receiver.starts_with("id:") {
|
||||
data.get_user_by_id(match receiver.replace("id:", "").parse() {
|
||||
Ok(x) => x,
|
||||
Err(_) => continue,
|
||||
})
|
||||
.await
|
||||
} else {
|
||||
data.get_user_by_username(receiver).await
|
||||
} {
|
||||
Ok(ua) => ua,
|
||||
Err(e) => return Json(e.into()),
|
||||
};
|
||||
|
||||
if crate::check_user_is_blocked!(data, user, other_user) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if other_user.settings.private_mails
|
||||
&& data
|
||||
.get_userfollow_by_initiator_receiver(other_user.id, user.id)
|
||||
.await
|
||||
.is_err()
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
receivers.push(other_user.id);
|
||||
}
|
||||
|
||||
// ...
|
||||
match data
|
||||
.create_letter(Letter::new(
|
||||
user.id,
|
||||
props.receivers,
|
||||
receivers,
|
||||
props.subject,
|
||||
props.content,
|
||||
match props.replying_to.parse() {
|
||||
|
@ -120,7 +176,7 @@ pub async fn create_request(
|
|||
.create_notification(Notification::new(
|
||||
"You've got mail!".to_string(),
|
||||
format!(
|
||||
"[@{}](/api/v1/auth/user/find/{}) has sent you a [letter](/mail/{}).",
|
||||
"[@{}](/api/v1/auth/user/find/{}) has sent you a [letter](/mail/letter/{}).",
|
||||
user.username, user.id, l.id
|
||||
),
|
||||
*x,
|
||||
|
@ -135,7 +191,7 @@ pub async fn create_request(
|
|||
Json(ApiReturn {
|
||||
ok: true,
|
||||
message: "Success".to_string(),
|
||||
payload: Some(l),
|
||||
payload: Some(l.id.to_string()),
|
||||
})
|
||||
}
|
||||
Err(e) => return Json(e.into()),
|
||||
|
@ -163,7 +219,11 @@ pub async fn add_read_request(
|
|||
}
|
||||
|
||||
if letter.read_by.contains(&user.id) {
|
||||
return Json(Error::MiscError("Already marked as read".to_string()).into());
|
||||
return Json(ApiReturn {
|
||||
ok: true,
|
||||
message: "Already marked as read".to_string(),
|
||||
payload: (),
|
||||
});
|
||||
}
|
||||
|
||||
letter.read_by.push(user.id);
|
||||
|
|
|
@ -1219,7 +1219,7 @@ pub struct QueryAppData {
|
|||
|
||||
#[derive(Deserialize)]
|
||||
pub struct CreateLetter {
|
||||
pub receivers: Vec<usize>,
|
||||
pub receivers: Vec<String>,
|
||||
pub subject: String,
|
||||
pub content: String,
|
||||
pub replying_to: String,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue