Skip to content

Commit

Permalink
Add trap for isPrivate done search and if unpublished dont search'
Browse files Browse the repository at this point in the history
  • Loading branch information
mbround18 committed Aug 21, 2024
1 parent b8136e7 commit fb48584
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 70 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
tmp/
doc.txt

# Created by https://www.toptal.com/developers/gitignore/api/rust,intellij,visualstudiocode,python
# Edit at https://www.toptal.com/developers/gitignore?templates=rust,intellij,visualstudiocode,python
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.0.1
0.0.2
88 changes: 56 additions & 32 deletions engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,22 @@ async function getSearchEngine({
return searchEngine;
}

function rejectIfIsPrivateAndNotPublished(page, action) {
if (!page.isPublished) {
logger.warn(
`(SEARCH/MEILISEARCH) SKIPPING: Page with path ${page.path} is not published.`,
);
return Promise.resolve();
} else if (page.isPrivate) {
logger.warn(
`(SEARCH/MEILISEARCH) SKIPPING: Page with path ${page.path} is private.`,
);
return Promise.resolve();
} else {
return action(page);
}
}

module.exports = {
/**
* ACTIVATE
Expand Down Expand Up @@ -195,59 +211,67 @@ module.exports = {
* @param {Object} page Page to create
*/
async created(page) {
logger.info(
`(SEARCH/MEILISEARCH) Creating search index for page: ${page.path}`,
);
const engine = await getSearchEngine(this.config);
await engine.created(page);
logger.info(
`(SEARCH/MEILISEARCH) Search index created for page: ${page.path}`,
);
await rejectIfIsPrivateAndNotPublished(page, async (page) => {
logger.info(
`(SEARCH/MEILISEARCH) Creating search index for page: ${page.path}`,
);
const engine = await getSearchEngine(this.config);
await engine.created(page);
logger.info(
`(SEARCH/MEILISEARCH) Search index created for page: ${page.path}`,
);
});
},
/**
* UPDATE
*
* @param {Object} page Page to update
*/
async updated(page) {
logger.info(
`(SEARCH/MEILISEARCH) Updating search index for page: ${page.path}`,
);
const engine = await getSearchEngine(this.config);
await engine.updated(page);
logger.info(
`(SEARCH/MEILISEARCH) Search index updated for page: ${page.path}`,
);
await rejectIfIsPrivateAndNotPublished(page, async (page) => {
logger.info(
`(SEARCH/MEILISEARCH) Updating search index for page: ${page.path}`,
);
const engine = await getSearchEngine(this.config);
await engine.updated(page);
logger.info(
`(SEARCH/MEILISEARCH) Search index updated for page: ${page.path}`,
);
});
},
/**
* DELETE
*
* @param {Object} page Page to delete
*/
async deleted(page) {
logger.info(
`(SEARCH/MEILISEARCH) Deleting search index for page: ${page.path}`,
);
const engine = await getSearchEngine(this.config);
await engine.deleted(page);
logger.info(
`(SEARCH/MEILISEARCH) Search index deleted for page: ${page.path}`,
);
await rejectIfIsPrivateAndNotPublished(page, async (page) => {
logger.info(
`(SEARCH/MEILISEARCH) Deleting search index for page: ${page.path}`,
);
const engine = await getSearchEngine(this.config);
await engine.deleted(page);
logger.info(
`(SEARCH/MEILISEARCH) Search index deleted for page: ${page.path}`,
);
});
},
/**
* RENAME
*
* @param {Object} page Page to rename
*/
async renamed(page) {
logger.info(
`(SEARCH/MEILISEARCH) Renaming search index for page: ${page.path}`,
);
const engine = await getSearchEngine(this.config);
await engine.updated(page);
logger.info(
`(SEARCH/MEILISEARCH) Search index renamed for page: ${page.destinationPath}`,
);
await rejectIfIsPrivateAndNotPublished(page, async (page) => {
logger.info(
`(SEARCH/MEILISEARCH) Renaming search index for page: ${page.path}`,
);
const engine = await getSearchEngine(this.config);
await engine.updated(page);
logger.info(
`(SEARCH/MEILISEARCH) Search index renamed for page: ${page.destinationPath}`,
);
});
},
/**
* REBUILD INDEX
Expand Down
11 changes: 1 addition & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
mod logger;
mod page;
mod query;
mod transformers;

use crate::page::RenamedWikiPage;
use crate::query::{PageSearchResponse, SerializableSearchResult};
Expand Down Expand Up @@ -64,15 +63,7 @@ impl WikiSearchEngine {
})
})?;

if page.is_published && !page.is_private {
operation(page).await
} else {
log::warn!(
"Skipping operation on unpublished or private page: {}",
page.id
);
Ok(())
}
operation(page).await
}

async fn add_document(&self, page: WikiPage) -> Result<(), JsValue> {
Expand Down
11 changes: 0 additions & 11 deletions src/page.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::transformers::{bool_from_int, bool_to_int};
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug)]
Expand All @@ -9,10 +8,6 @@ pub struct WikiPage {
pub(crate) hash: String,
title: String,
description: String,
#[serde(deserialize_with = "bool_from_int", serialize_with = "bool_to_int")]
pub(crate) is_private: bool,
#[serde(deserialize_with = "bool_from_int", serialize_with = "bool_to_int")]
pub(crate) is_published: bool,
pub(crate) content: String,
content_type: String,
created_at: String,
Expand All @@ -31,8 +26,6 @@ impl From<RenamedWikiPage> for WikiPage {
hash: renamed.destination_hash,
title: renamed.title,
description: renamed.description,
is_private: renamed.is_private,
is_published: renamed.is_published,
content: renamed.content,
content_type: renamed.content_type,
created_at: renamed.created_at,
Expand All @@ -53,10 +46,6 @@ pub(crate) struct RenamedWikiPage {
hash: String,
title: String,
description: String,
#[serde(deserialize_with = "bool_from_int", serialize_with = "bool_to_int")]
pub(crate) is_private: bool,
#[serde(deserialize_with = "bool_from_int", serialize_with = "bool_to_int")]
pub(crate) is_published: bool,
content: String,
content_type: String,
created_at: String,
Expand Down
16 changes: 0 additions & 16 deletions src/transformers.rs

This file was deleted.

0 comments on commit fb48584

Please sign in to comment.