Skip to content

Commit

Permalink
Merge pull request #2103 from scpwiki/frontend
Browse files Browse the repository at this point in the history
Frontend things [4]
  • Loading branch information
Zokhoi authored Sep 25, 2024
2 parents 6fd3dd8 + 0869feb commit baa9d5c
Show file tree
Hide file tree
Showing 21 changed files with 640 additions and 55 deletions.
3 changes: 3 additions & 0 deletions deepwell/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ async fn build_module(app_state: ServerState) -> anyhow::Result<RpcModule<Server
register!("page_create", page_create);
register!("page_get", page_get);
register!("page_get_direct", page_get_direct);
register!("page_get_score", page_get_score);
register!("page_edit", page_edit);
register!("page_delete", page_delete);
register!("page_move", page_move);
Expand All @@ -252,6 +253,8 @@ async fn build_module(app_state: ServerState) -> anyhow::Result<RpcModule<Server
register!("parent_get", parent_get);
register!("parent_remove", parent_remove);
register!("parent_relationships_get", parent_relationships_get);
register!("parent_get_all", parent_get_all);
register!("parent_update", parent_update);

// Blob data
register!("blob_get", blob_get);
Expand Down
20 changes: 18 additions & 2 deletions deepwell/src/endpoints/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ use super::prelude::*;
use crate::models::page::Model as PageModel;
use crate::services::page::{
CreatePage, CreatePageOutput, DeletePage, DeletePageOutput, EditPage, EditPageOutput,
GetPageAnyDetails, GetPageDirect, GetPageOutput, GetPageReferenceDetails, MovePage,
MovePageOutput, RestorePage, RestorePageOutput, RollbackPage, SetPageLayout,
GetPageAnyDetails, GetPageDirect, GetPageOutput, GetPageReference,
GetPageReferenceDetails, GetPageScoreOutput, MovePage, MovePageOutput, RestorePage,
RestorePageOutput, RollbackPage, SetPageLayout,
};
use crate::services::{Result, TextService};
use crate::web::{PageDetails, Reference};
Expand Down Expand Up @@ -72,6 +73,21 @@ pub async fn page_get_direct(
}
}

pub async fn page_get_score(
ctx: &ServiceContext<'_>,
params: Params<'static>,
) -> Result<GetPageScoreOutput> {
let GetPageReference {
site_id,
page: reference,
} = params.parse()?;

info!("Getting score for page {reference:?} in site ID {site_id}");
let page_id = PageService::get_id(ctx, site_id, reference).await?;
let score = ScoreService::score(ctx, page_id).await?;
Ok(GetPageScoreOutput { page_id, score })
}

pub async fn page_edit(
ctx: &ServiceContext<'_>,
params: Params<'static>,
Expand Down
5 changes: 2 additions & 3 deletions deepwell/src/endpoints/page_revision.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

use super::prelude::*;
use crate::models::page_revision::Model as PageRevisionModel;
use crate::services::page::GetPageReferenceDetails;
use crate::services::page::GetPageReference;
use crate::services::page_revision::{
GetPageRevision, GetPageRevisionDetails, GetPageRevisionRangeDetails,
PageRevisionCountOutput, PageRevisionModelFiltered, UpdatePageRevisionDetails,
Expand All @@ -32,10 +32,9 @@ pub async fn page_revision_count(
ctx: &ServiceContext<'_>,
params: Params<'static>,
) -> Result<PageRevisionCountOutput> {
let GetPageReferenceDetails {
let GetPageReference {
site_id,
page: reference,
details: _,
} = params.parse()?;

info!("Getting latest revision for page {reference:?} in site ID {site_id}",);
Expand Down
96 changes: 95 additions & 1 deletion deepwell/src/endpoints/parent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@

use super::prelude::*;
use crate::models::page_parent::Model as PageParentModel;
use crate::services::page::GetPageReference;
use crate::services::parent::{
GetParentRelationships, ParentDescription, RemoveParentOutput,
GetParentRelationships, ParentDescription, RemoveParentOutput, UpdateParents,
UpdateParentsOutput,
};
use crate::web::Reference;
use futures::future::try_join_all;

pub async fn parent_relationships_get(
ctx: &ServiceContext<'_>,
Expand Down Expand Up @@ -85,3 +89,93 @@ pub async fn parent_remove(

ParentService::remove(ctx, input).await
}

pub async fn parent_get_all(
ctx: &ServiceContext<'_>,
params: Params<'static>,
) -> Result<Vec<String>> {
let GetPageReference { site_id, page } = params.parse()?;

info!(
"Getting parents for child {:?} in site ID {}",
page, site_id,
);

let parents: Vec<Reference<'_>> = ParentService::get_parents(ctx, site_id, page)
.await?
.iter()
.map(|p| Reference::from(p.parent_page_id))
.collect();

let pages: Vec<String> = PageService::get_pages(ctx, site_id, parents.as_slice())
.await?
.into_iter()
.map(|p| p.slug)
.collect();

Ok(pages)
}

pub async fn parent_update(
ctx: &ServiceContext<'_>,
params: Params<'static>,
) -> Result<UpdateParentsOutput> {
let input: UpdateParents = params.parse()?;

info!(
"Updating multiple parental relationships for child {:?} in site ID {}",
input.child, input.site_id,
);

let creation = match input.add {
Some(parents) => {
let creation = parents.iter().map(|parent| {
ParentService::create(
ctx,
ParentDescription {
site_id: input.site_id,
parent: parent.to_owned(),
child: input.child.clone(),
},
)
});
Some(
try_join_all(creation)
.await?
.iter()
.flatten()
.map(|p| p.parent_page_id)
.collect(),
)
}
None => None,
};

let removal = match input.remove {
Some(parents) => {
let removal = parents.iter().map(|parent| {
ParentService::remove(
ctx,
ParentDescription {
site_id: input.site_id,
parent: parent.to_owned(),
child: input.child.clone(),
},
)
});
Some(
try_join_all(removal)
.await?
.iter()
.map(|p| p.was_deleted)
.collect(),
)
}
None => None,
};

Ok(UpdateParentsOutput {
added: creation,
removed: removal,
})
}
12 changes: 12 additions & 0 deletions deepwell/src/services/page/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ pub struct CreatePageOutput {
pub parser_errors: Vec<ParseError>,
}

#[derive(Deserialize, Debug, Clone)]
pub struct GetPageReference<'a> {
pub site_id: i64,
pub page: Reference<'a>,
}

#[derive(Deserialize, Debug, Clone)]
pub struct GetPageReferenceDetails<'a> {
pub site_id: i64,
Expand Down Expand Up @@ -107,6 +113,12 @@ pub struct GetPageOutput {
pub layout: Layout,
}

#[derive(Serialize, Debug, Clone)]
pub struct GetPageScoreOutput {
pub page_id: i64,
pub score: ScoreValue,
}

#[derive(Deserialize, Debug, Clone)]
pub struct EditPage<'a> {
pub site_id: i64,
Expand Down
4 changes: 2 additions & 2 deletions deepwell/src/services/parent/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ impl ParentService {
let txn = ctx.transaction();
let page_id = PageService::get_id(ctx, site_id, reference).await?;
let column = match relationship_type {
ParentalRelationshipType::Parent => page_parent::Column::ParentPageId,
ParentalRelationshipType::Child => page_parent::Column::ChildPageId,
ParentalRelationshipType::Parent => page_parent::Column::ChildPageId,
ParentalRelationshipType::Child => page_parent::Column::ParentPageId,
};

let models = PageParent::find()
Expand Down
14 changes: 14 additions & 0 deletions deepwell/src/services/parent/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ pub struct ParentDescription<'a> {
pub child: Reference<'a>,
}

#[derive(Deserialize, Debug, Clone)]
pub struct UpdateParents<'a> {
pub site_id: i64,
pub child: Reference<'a>,
pub add: Option<Vec<Reference<'a>>>,
pub remove: Option<Vec<Reference<'a>>>,
}

#[derive(Serialize, Deserialize, Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub enum ParentalRelationshipType {
#[serde(rename = "parents")]
Expand Down Expand Up @@ -70,3 +78,9 @@ pub struct GetParentRelationships<'a> {
pub struct RemoveParentOutput {
pub was_deleted: bool,
}

#[derive(Serialize, Debug, Clone)]
pub struct UpdateParentsOutput {
pub added: Option<Vec<i64>>,
pub removed: Option<Vec<bool>>,
}
22 changes: 11 additions & 11 deletions deepwell/src/services/view/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,17 @@ const PAGE_ARGUMENTS_SCHEMA: ArgumentSchema = ArgumentSchema {
/// pairs, this struct parses them into logical flags to be processed.
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct PageOptions {
edit: bool,
title: Option<String>,
parent: Option<String>,
tags: Option<String>,
no_redirect: bool,
no_render: bool,
rerender: bool,
comments: bool,
history: bool,
offset: Option<i32>,
data: String,
pub edit: bool,
pub title: Option<String>,
pub parent: Option<String>,
pub tags: Option<String>,
pub no_redirect: bool,
pub no_render: bool,
pub rerender: bool,
pub comments: bool,
pub history: bool,
pub offset: Option<i32>,
pub data: String,
}

impl PageOptions {
Expand Down
23 changes: 21 additions & 2 deletions deepwell/src/services/view/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,6 @@ impl ViewService {
{
// This page exists, return its data directly.
Some(page) => {
// TODO determine if page needs rerender?

// Get associated revision
let page_revision =
PageRevisionService::get_latest(ctx, site.site_id, page.page_id)
Expand All @@ -174,6 +172,17 @@ impl ViewService {
if Self::can_access_page(ctx, user_permissions).await? {
debug!("User has page access, return text data");

if options.rerender
&& Self::can_edit_page(ctx, user_permissions).await?
{
info!(
"Re-rendering revision: site ID {} page ID {} revision ID {} (depth {})",
page.site_id, page.page_id, page_revision.revision_id, 0,
);
PageRevisionService::rerender(ctx, page.site_id, page.page_id, 0)
.await?;
};

let (wikitext, compiled_html) = try_join!(
TextService::get(ctx, &page_revision.wikitext_hash),
TextService::get(ctx, &page_revision.compiled_hash),
Expand Down Expand Up @@ -491,6 +500,16 @@ impl ViewService {
Ok(true)
}

async fn can_edit_page(
_ctx: &ServiceContext<'_>,
permissions: UserPermissions,
) -> Result<bool> {
info!("Checking page access: {permissions:?}");
debug!("TODO: stub");
// TODO perform permission checks
Ok(true)
}

fn should_redirect_site(
ctx: &ServiceContext,
site: &SiteModel,
Expand Down
Loading

0 comments on commit baa9d5c

Please sign in to comment.