Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File actions for frontend #2214

Merged
merged 12 commits into from
Dec 15, 2024
1 change: 1 addition & 0 deletions deepwell/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ async fn build_module(app_state: ServerState) -> anyhow::Result<RpcModule<Server
register!("page_get_direct", page_get_direct);
register!("page_get_deleted", page_get_deleted);
register!("page_get_score", page_get_score);
register!("page_get_files", page_get_files);
register!("page_edit", page_edit);
register!("page_delete", page_delete);
register!("page_move", page_move);
Expand Down
4 changes: 2 additions & 2 deletions deepwell/src/endpoints/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ pub async fn file_move(
let input: MoveFile = params.parse()?;

info!(
"Moving file ID {} from page ID {} to page ID {} in site ID {}",
input.file_id, input.current_page_id, input.destination_page_id, input.site_id,
"Moving file ID {} from page ID {} to page {:?} in site ID {}",
input.file_id, input.current_page_id, input.destination_page, input.site_id,
);

FileService::r#move(ctx, input).await
Expand Down
66 changes: 65 additions & 1 deletion deepwell/src/endpoints/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@
*/

use super::prelude::*;
use crate::models::file::Model as FileModel;
use crate::models::page::Model as PageModel;
use crate::services::file::{GetFileOutput, GetPageFiles};
use crate::services::page::{
CreatePage, CreatePageOutput, DeletePage, DeletePageOutput, EditPage, EditPageOutput,
GetDeletedPageOutput, GetPageAnyDetails, GetPageDirect, GetPageOutput,
GetPageReference, GetPageReferenceDetails, GetPageScoreOutput, GetPageSlug, MovePage,
MovePageOutput, RestorePage, RestorePageOutput, RollbackPage, SetPageLayout,
};
use crate::services::{Result, TextService};
use crate::types::{PageDetails, Reference};
use crate::types::{FileOrder, PageDetails, Reference};
use futures::future::try_join_all;

pub async fn page_create(
Expand Down Expand Up @@ -110,6 +112,37 @@ pub async fn page_get_score(
Ok(GetPageScoreOutput { page_id, score })
}

pub async fn page_get_files(
ctx: &ServiceContext<'_>,
params: Params<'static>,
) -> Result<Vec<GetFileOutput>> {
let GetPageFiles {
page_id,
site_id,
deleted,
} = params.parse()?;

info!("Getting files for page ID {page_id} in site ID {site_id}");
let get_page_files = FileService::get_all(
ctx,
site_id,
page_id,
deleted.to_option().copied(),
FileOrder::default(),
)
.await?
.into_iter()
.map(|file| build_page_file_output(ctx, file));

let result = try_join_all(get_page_files)
.await?
.into_iter()
.flatten()
.collect();

Ok(result)
}

pub async fn page_edit(
ctx: &ServiceContext<'_>,
params: Params<'static>,
Expand Down Expand Up @@ -286,3 +319,34 @@ async fn build_page_deleted_output(
rating,
}))
}

async fn build_page_file_output(
ctx: &ServiceContext<'_>,
file: FileModel,
) -> Result<Option<GetFileOutput>> {
// Get file revision
let revision =
FileRevisionService::get_latest(ctx, file.site_id, file.page_id, file.file_id)
.await?;

// Build result struct
Ok(Some(GetFileOutput {
file_id: file.file_id,
file_created_at: file.created_at,
file_updated_at: file.updated_at,
file_deleted_at: file.deleted_at,
page_id: file.page_id,
revision_id: revision.revision_id,
revision_type: revision.revision_type,
revision_created_at: revision.created_at,
revision_number: revision.revision_number,
revision_user_id: revision.user_id,
name: file.name,
data: None,
mime: revision.mime_hint,
size: revision.size_hint,
licensing: revision.licensing,
revision_comments: revision.comments,
hidden_fields: revision.hidden,
}))
}
19 changes: 12 additions & 7 deletions deepwell/src/services/file/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use crate::services::file_revision::{
GetFileRevision,
};
use crate::services::filter::{FilterClass, FilterType};
use crate::services::{BlobService, FileRevisionService, FilterService};
use crate::services::{BlobService, FileRevisionService, FilterService, PageService};
use crate::types::FileOrder;
use crate::utils::regex_replace_in_place;
use once_cell::sync::Lazy;
Expand Down Expand Up @@ -248,12 +248,12 @@ impl FileService {
name,
site_id,
current_page_id,
destination_page_id,
destination_page,
file_id,
user_id,
last_revision_id,
revision_comments,
}: MoveFile,
}: MoveFile<'_>,
) -> Result<Option<MoveFileOutput>> {
let txn = ctx.transaction();
let last_revision =
Expand All @@ -262,6 +262,10 @@ impl FileService {

check_last_revision(&last_revision, last_revision_id)?;

// Get destination page id
let destination_page_id =
PageService::get_id(ctx, site_id, destination_page).await?;

// Get destination filename
let mut name = name.unwrap_or_else(|| last_revision.name.clone());

Expand Down Expand Up @@ -402,18 +406,20 @@ impl FileService {
pub async fn restore(
ctx: &ServiceContext<'_>,
RestoreFile {
new_page_id,
new_page,
new_name,
site_id,
page_id,
file_id,
user_id,
revision_comments,
}: RestoreFile,
}: RestoreFile<'_>,
) -> Result<RestoreFileOutput> {
let txn = ctx.transaction();
let file = Self::get_direct(ctx, file_id, true).await?;
let new_page_id = new_page_id.unwrap_or(page_id);
let new_page_id =
PageService::get_id(ctx, site_id, new_page.unwrap_or(Reference::Id(page_id)))
.await?;
let new_name = new_name.unwrap_or(file.name);

// Do page checks:
Expand Down Expand Up @@ -625,7 +631,6 @@ impl FileService {
/// * If it is `Some(false)`, then it only returns pages which are extant.
/// * If it is `None`, then it returns all pages regardless of deletion status.
// TODO add pagination
#[allow(dead_code)] // TEMP
pub async fn get_all(
ctx: &ServiceContext<'_>,
site_id: i64,
Expand Down
15 changes: 11 additions & 4 deletions deepwell/src/services/file/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ pub struct GetFileOutput {
pub hidden_fields: Vec<String>,
}

#[derive(Deserialize, Debug, Clone)]
pub struct GetPageFiles {
pub site_id: i64,
pub page_id: i64,
pub deleted: Maybe<bool>,
}

#[derive(Deserialize, Debug, Clone)]
pub struct EditFile {
pub site_id: i64,
Expand Down Expand Up @@ -129,15 +136,15 @@ pub struct EditFileBody {
pub type EditFileOutput = CreateFileRevisionOutput;

#[derive(Deserialize, Debug, Clone)]
pub struct MoveFile {
pub struct MoveFile<'a> {
pub revision_comments: String,
pub site_id: i64,
pub file_id: i64,
pub user_id: i64,
pub last_revision_id: i64,
pub name: Option<String>,
pub current_page_id: i64,
pub destination_page_id: i64,
pub destination_page: Reference<'a>,
}

pub type MoveFileOutput = CreateFileRevisionOutput;
Expand All @@ -160,9 +167,9 @@ pub struct DeleteFileOutput {
}

#[derive(Deserialize, Debug, Clone)]
pub struct RestoreFile {
pub struct RestoreFile<'a> {
pub revision_comments: String,
pub new_page_id: Option<i64>,
pub new_page: Option<Reference<'a>>,
pub new_name: Option<String>,
pub site_id: i64,
pub page_id: i64,
Expand Down
Loading
Loading