|
|
|
@@ -23,7 +23,7 @@ use super::analyses::{ANALYSIS_COLUMNS, AnalysisRow, Target, queue};
|
|
|
|
|
use super::charts::{CHART_COLUMNS, ChartRow, calculate, find_chart};
|
|
|
|
|
use super::{Body, IdPath, St, double_option};
|
|
|
|
|
use crate::AppState;
|
|
|
|
|
use crate::auth::{AdminUser, AuthUser};
|
|
|
|
|
use crate::auth::{AuthUser, CelebrityEditor};
|
|
|
|
|
use crate::error::{ApiError, ApiResult};
|
|
|
|
|
use crate::views::{chart_highlights, public_chart, rfc3339, rfc3339_opt};
|
|
|
|
|
|
|
|
|
@@ -43,12 +43,13 @@ struct CelebrityRow {
|
|
|
|
|
focus_x: f32,
|
|
|
|
|
focus_y: f32,
|
|
|
|
|
published_at: Option<OffsetDateTime>,
|
|
|
|
|
created_by: Option<Uuid>,
|
|
|
|
|
created_at: OffsetDateTime,
|
|
|
|
|
updated_at: OffsetDateTime,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const COLUMNS: &str = "id, chart_id, name, subtitle, slug, photo_key, photo_width, photo_height, focus_x, focus_y, \
|
|
|
|
|
published_at, created_at, updated_at";
|
|
|
|
|
published_at, created_by, created_at, updated_at";
|
|
|
|
|
|
|
|
|
|
impl CelebrityRow {
|
|
|
|
|
fn photo(&self) -> Value {
|
|
|
|
@@ -161,6 +162,7 @@ async fn admin_view(db: &PgPool, row: &CelebrityRow) -> Result<Value, ApiError>
|
|
|
|
|
let chart = find_chart(db, row.chart_id).await?.ok_or(ApiError::NotFound("celebrity"))?;
|
|
|
|
|
let analysis = latest_analysis(db, row.chart_id).await?;
|
|
|
|
|
let mut view = row.base();
|
|
|
|
|
view["created_by"] = json!(row.created_by);
|
|
|
|
|
view["birth_data"] = chart.birth_data.clone();
|
|
|
|
|
view["birth"] = public_chart(&chart.result, true)["birth"].clone();
|
|
|
|
|
view["highlights"] = chart_highlights(&chart.result);
|
|
|
|
@@ -174,7 +176,7 @@ async fn admin_view(db: &PgPool, row: &CelebrityRow) -> Result<Value, ApiError>
|
|
|
|
|
type ListRow =
|
|
|
|
|
(Uuid, Uuid, String, Option<String>, String, Option<OffsetDateTime>, Option<String>, Value, Option<String>);
|
|
|
|
|
|
|
|
|
|
pub async fn list(State(state): St, _admin: AdminUser) -> ApiResult<Json<Value>> {
|
|
|
|
|
pub async fn list(State(state): St, _editor: CelebrityEditor) -> ApiResult<Json<Value>> {
|
|
|
|
|
let rows: Vec<ListRow> = sqlx::query_as(
|
|
|
|
|
"SELECT c.id, c.chart_id, c.name, c.subtitle, c.slug, c.published_at, c.photo_key, ch.result, \
|
|
|
|
|
(SELECT a.status FROM analyses a WHERE a.chart_id = c.chart_id AND a.kind = 'celebrity' \
|
|
|
|
@@ -210,7 +212,7 @@ pub struct CreateCelebrity {
|
|
|
|
|
/// Adds a famous person: calculates the chart and queues the analysis.
|
|
|
|
|
pub async fn create(
|
|
|
|
|
State(state): St,
|
|
|
|
|
AdminUser(admin): AdminUser,
|
|
|
|
|
CelebrityEditor(admin): CelebrityEditor,
|
|
|
|
|
WithRejection(Json(req), _): Body<CreateCelebrity>,
|
|
|
|
|
) -> ApiResult<(StatusCode, Json<Value>)> {
|
|
|
|
|
let name = clean_name(&req.name)?;
|
|
|
|
@@ -249,7 +251,7 @@ pub async fn create(
|
|
|
|
|
|
|
|
|
|
pub async fn get_one(
|
|
|
|
|
State(state): St,
|
|
|
|
|
_admin: AdminUser,
|
|
|
|
|
_editor: CelebrityEditor,
|
|
|
|
|
WithRejection(Path(id), _): IdPath,
|
|
|
|
|
) -> ApiResult<Json<Value>> {
|
|
|
|
|
let row = find(&state.db, id).await?;
|
|
|
|
@@ -273,7 +275,7 @@ pub struct UpdateCelebrity {
|
|
|
|
|
|
|
|
|
|
pub async fn update(
|
|
|
|
|
State(state): St,
|
|
|
|
|
AdminUser(admin): AdminUser,
|
|
|
|
|
CelebrityEditor(admin): CelebrityEditor,
|
|
|
|
|
WithRejection(Path(id), _): IdPath,
|
|
|
|
|
WithRejection(Json(req), _): Body<UpdateCelebrity>,
|
|
|
|
|
) -> ApiResult<Json<Value>> {
|
|
|
|
@@ -353,10 +355,10 @@ pub async fn update(
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Queues the analysis again when there is none or the last one failed
|
|
|
|
|
/// (rewriting a finished one: `POST /v1/admin/analyses/{id}/regenerate`).
|
|
|
|
|
/// (rewriting a finished one: [`regenerate_analysis`]).
|
|
|
|
|
pub async fn request_analysis(
|
|
|
|
|
State(state): St,
|
|
|
|
|
AdminUser(admin): AdminUser,
|
|
|
|
|
CelebrityEditor(admin): CelebrityEditor,
|
|
|
|
|
WithRejection(Path(id), _): IdPath,
|
|
|
|
|
) -> ApiResult<Json<Value>> {
|
|
|
|
|
let row = find(&state.db, id).await?;
|
|
|
|
@@ -365,6 +367,17 @@ pub async fn request_analysis(
|
|
|
|
|
Ok(Json(admin_view(&state.db, &row).await?))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Writes the finished (or failed) analysis again with the current prompt.
|
|
|
|
|
pub async fn regenerate_analysis(
|
|
|
|
|
State(state): St,
|
|
|
|
|
_editor: CelebrityEditor,
|
|
|
|
|
WithRejection(Path(id), _): IdPath,
|
|
|
|
|
) -> ApiResult<Json<Value>> {
|
|
|
|
|
let row = find(&state.db, id).await?;
|
|
|
|
|
let analysis = latest_analysis(&state.db, row.chart_id).await?.ok_or(ApiError::NotFound("analysis"))?;
|
|
|
|
|
Ok(Json(super::analyses::regenerate_in_place(&state, analysis.id).await?))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn storage(state: &AppState) -> Result<&crate::storage::Storage, ApiError> {
|
|
|
|
|
state
|
|
|
|
|
.storage
|
|
|
|
@@ -385,7 +398,7 @@ async fn delete_photo(state: &AppState, key: &str) {
|
|
|
|
|
/// WebP); it replaces the previous one. The face point is reset.
|
|
|
|
|
pub async fn upload_photo(
|
|
|
|
|
State(state): St,
|
|
|
|
|
AdminUser(admin): AdminUser,
|
|
|
|
|
CelebrityEditor(admin): CelebrityEditor,
|
|
|
|
|
WithRejection(Path(id), _): IdPath,
|
|
|
|
|
body: Bytes,
|
|
|
|
|
) -> ApiResult<Json<Value>> {
|
|
|
|
@@ -422,7 +435,7 @@ pub async fn upload_photo(
|
|
|
|
|
|
|
|
|
|
pub async fn remove_photo(
|
|
|
|
|
State(state): St,
|
|
|
|
|
_admin: AdminUser,
|
|
|
|
|
_editor: CelebrityEditor,
|
|
|
|
|
WithRejection(Path(id), _): IdPath,
|
|
|
|
|
) -> ApiResult<Json<Value>> {
|
|
|
|
|
let row = find(&state.db, id).await?;
|
|
|
|
@@ -442,10 +455,14 @@ pub async fn remove_photo(
|
|
|
|
|
/// Deletes the famous person with their chart, analysis and photo.
|
|
|
|
|
pub async fn remove(
|
|
|
|
|
State(state): St,
|
|
|
|
|
AdminUser(admin): AdminUser,
|
|
|
|
|
editor: CelebrityEditor,
|
|
|
|
|
WithRejection(Path(id), _): IdPath,
|
|
|
|
|
) -> ApiResult<StatusCode> {
|
|
|
|
|
let row = find(&state.db, id).await?;
|
|
|
|
|
if !editor.is_admin() && row.created_by != Some(editor.0.id()) {
|
|
|
|
|
return Err(ApiError::Forbidden("only an admin may delete someone else's famous person".into()));
|
|
|
|
|
}
|
|
|
|
|
let admin = editor.0;
|
|
|
|
|
sqlx::query("DELETE FROM charts WHERE id = $1").bind(row.chart_id).execute(&state.db).await?;
|
|
|
|
|
if let Some(key) = &row.photo_key {
|
|
|
|
|
delete_photo(&state, key).await;
|
|
|
|
@@ -474,7 +491,7 @@ pub async fn public_list(State(state): St) -> ApiResult<Json<Value>> {
|
|
|
|
|
Ok(Json(json!({ "celebrities": items })))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Public: one famous person's page (a draft only for admins).
|
|
|
|
|
/// Public: one famous person's page (a draft only for admins and creators).
|
|
|
|
|
pub async fn public_view(
|
|
|
|
|
State(state): St,
|
|
|
|
|
auth: Result<AuthUser, ApiError>,
|
|
|
|
@@ -485,7 +502,7 @@ pub async fn public_view(
|
|
|
|
|
.fetch_optional(&state.db)
|
|
|
|
|
.await?
|
|
|
|
|
.ok_or(ApiError::NotFound("celebrity"))?;
|
|
|
|
|
let admin = auth.is_ok_and(|a| a.user.has_role(db::Role::Admin));
|
|
|
|
|
let admin = auth.is_ok_and(|a| a.user.has_role(db::Role::Admin) || a.user.has_role(db::Role::Creator));
|
|
|
|
|
if row.published_at.is_none() && !admin {
|
|
|
|
|
return Err(ApiError::NotFound("celebrity"));
|
|
|
|
|
}
|
|
|
|
|