Skip to content

Commit

Permalink
Add setting page parents
Browse files Browse the repository at this point in the history
  • Loading branch information
Zokhoi committed Sep 24, 2024
1 parent 973e564 commit d22fbbf
Show file tree
Hide file tree
Showing 10 changed files with 244 additions and 4 deletions.
2 changes: 2 additions & 0 deletions deepwell/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,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_modify", parent_modify);

// Blob data
register!("blob_get", blob_get);
Expand Down
92 changes: 91 additions & 1 deletion deepwell/src/endpoints/parent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@

use super::prelude::*;
use crate::models::page_parent::Model as PageParentModel;
use crate::services::page::GetPageReference;
use crate::services::parent::{
GetParentRelationships, ParentDescription, RemoveParentOutput,
GetParentRelationships, ModifyParentOutput, ParentDescription,
ParentModifyDescription, RemoveParentOutput,
};
use crate::web::Reference;

pub async fn parent_relationships_get(
ctx: &ServiceContext<'_>,
Expand Down Expand Up @@ -85,3 +88,90 @@ 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?
.iter()
.map(|p| p.slug.clone())
.collect();

Ok(pages)
}

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

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

let creation = match input.added {
Some(parents) => {
let mut creation = Vec::new();
for parent in parents {
if let Ok(Some(model)) = ParentService::create(
ctx,
ParentDescription {
site_id: input.site_id.clone(),
parent: parent.clone(),
child: input.child.clone(),
},
)
.await
{
creation.push(model.parent_page_id);
};
}
Some(creation)
}
None => None,
};

let removal = match input.removed {
Some(parents) => {
let mut removal = Vec::new();
for parent in parents {
if let Ok(res) = ParentService::remove(
ctx,
ParentDescription {
site_id: input.site_id.clone(),
parent: parent.to_owned(),
child: input.child.clone(),
},
)
.await
{
removal.push(res.was_deleted);
};
}
Some(removal)
}
None => None,
};

Ok(ModifyParentOutput {
added: creation,
removed: removal,
})
}
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 ParentModifyDescription<'a> {
pub site_id: i64,
pub child: Reference<'a>,
pub added: Option<Vec<Reference<'a>>>,
pub removed: 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 ModifyParentOutput {
pub added: Option<Vec<i64>>,
pub removed: Option<Vec<bool>>,
}
27 changes: 27 additions & 0 deletions framerail/src/lib/server/deepwell/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,33 @@ export async function pageLayout(
})
}

export async function pageParentModify(
siteId: number,
pageId: number,
userId: number,
removed: string[],
added: string[]
): Promise<object> {
return client.request("parent_modify", {
site_id: siteId,
child: pageId,
user_id: userId,
removed,
added
})
}

export async function pageParentGet(
siteId: number,
pageId: Optional<number>,
slug: string
): Promise<object> {
return client.request("parent_get_all", {
site_id: siteId,
page: pageId ?? slug
})
}

export async function pageScore(
siteId: number,
pageId: Optional<number>,
Expand Down
1 change: 1 addition & 0 deletions framerail/src/lib/server/load/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export async function loadPage(
"view": {},
"vote": {},
"layout": {},
"parents": {},

// Page history
"wiki-page-revision": {
Expand Down
19 changes: 19 additions & 0 deletions framerail/src/routes/[slug]/[...extra]/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,25 @@ export async function POST(event) {
let layout = data.get("layout")?.toString().trim() ?? null

res = await page.pageLayout(siteId, pageId, session?.user_id, layout)
} else if (extra.includes("parentset")) {
let oldParentStr = data.get("old-parents")?.toString().trim() ?? ""
let oldParent = oldParentStr.split(" ")
let newParentStr = data.get("new-parents")?.toString().trim() ?? ""
let newParent = newParentStr.split(" ")
let removed: string[] = []
let common: string[] = []
let added: string[] = []
for (let i = 0; i < oldParent.length; i++) {
if (!newParent.includes(oldParent[i])) removed.push(oldParent[i])
else common.push(oldParent[i])
}
for (let i = 0; i < newParent.length; i++) {
if (!common.includes(newParent[i])) added.push(newParent[i])
}

res = await page.pageParentModify(siteId, pageId, session?.user_id, removed, added)
} else if (extra.includes("parentget")) {
res = await page.pageParentGet(siteId, pageId, slug)
} else if (extra.includes("score")) {
res = await page.pageScore(siteId, pageId, slug)
}
Expand Down
87 changes: 86 additions & 1 deletion framerail/src/routes/[slug]/[...extra]/page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
let showMoveAction = false
let showLayoutAction = false
let showParentAction = false
let showHistory = false
let showSource = false
let showRevision = false
Expand All @@ -19,6 +20,7 @@
let revision: Record<string, any> = {}
let voteMap: Map<number, Record<string, any>> = new Map()
let voteRating: number
let parents = ""
async function handleDelete() {
let fdata = new FormData()
Expand Down Expand Up @@ -124,6 +126,46 @@
}
}
async function getParents() {
let fdata = new FormData()
fdata.set("site-id", $page.data.site.site_id)
fdata.set("page-id", $page.data.page.page_id)
let res = await fetch(`/${$page.data.page.slug}/parentget`, {
method: "POST",
body: fdata
}).then((res) => res.json())
if (res?.message) {
showErrorPopup.set({
state: true,
message: res.message
})
} else {
parents = res.join(" ")
showParentAction = true
}
}
async function setParents() {
let form = document.getElementById("page-parent")
let fdata = new FormData(form)
fdata.set("site-id", $page.data.site.site_id)
fdata.set("page-id", $page.data.page.page_id)
fdata.set("old-parents", parents)
let res = await fetch(`/${$page.data.page.slug}/parentset`, {
method: "POST",
body: fdata
}).then((res) => res.json())
if (res?.message) {
showErrorPopup.set({
state: true,
message: res.message
})
} else {
showParentAction = false
invalidateAll()
}
}
async function handleHistory() {
let fdata = new FormData()
fdata.set("site-id", $page.data.site.site_id)
Expand Down Expand Up @@ -395,6 +437,13 @@
>
{$page.data.internationalization?.layout}
</button>
<button
class="action-button editor-button button-parents clickable"
type="button"
on:click={getParents}
>
{$page.data.internationalization?.parents}
</button>
<button
class="action-button editor-button button-delete clickable"
type="button"
Expand Down Expand Up @@ -517,6 +566,41 @@
</form>
{/if}

{#if showParentAction}
<form
id="page-parent"
class="page-parent"
method="POST"
on:submit|preventDefault={setParents}
>
<input
name="new-parents"
class="page-parent-new-parents"
placeholder={$page.data.internationalization?.parents}
type="text"
value={parents}
/>
<div class="action-row page-parent-actions">
<button
class="action-button page-parent-button button-cancel clickable"
type="button"
on:click|stopPropagation={() => {
showParentAction = false
}}
>
{$page.data.internationalization?.cancel}
</button>
<button
class="action-button page-parent-button button-save clickable"
type="submit"
on:click|stopPropagation
>
{$page.data.internationalization?.save}
</button>
</div>
</form>
{/if}

{#if showHistory}
<div class="revision-list">
<div class="revision-header">
Expand Down Expand Up @@ -692,7 +776,8 @@
.editor,
.page-move,
.page-layout {
.page-layout,
.page-parent {
display: flex;
flex-direction: column;
gap: 15px;
Expand Down
1 change: 1 addition & 0 deletions locales/fluent/base/en.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ messages = Messages
move = Move
navigation = Navigation
notifications = Notifications
parents = Parents
preview = Preview
privacy = Privacy
profile = Profile
Expand Down
1 change: 1 addition & 0 deletions locales/fluent/base/zh_Hans.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ messages = 信息
move = 移动
navigation = 导航
notifications = 通知
parents = 父页面
preview = 预览
privacy = 隐私
profile = 个人简介
Expand Down

0 comments on commit d22fbbf

Please sign in to comment.