mirror of
https://github.com/darkrenaissance/darkfi.git
synced 2026-04-28 03:00:18 -04:00
bin/darkwikid: create index for title and id
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
pub enum DarkWikiError {
|
||||
#[error("File/Doc not found")]
|
||||
FileNotFound,
|
||||
#[error("Encryption error: `{0}`")]
|
||||
EncryptionError(String),
|
||||
#[error("Json serialization error: `{0}`")]
|
||||
|
||||
@@ -2,7 +2,7 @@ use async_std::sync::{Arc, Mutex};
|
||||
use std::{
|
||||
fs::{create_dir_all, read_dir},
|
||||
mem::discriminant,
|
||||
path::PathBuf,
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
|
||||
use async_executor::Executor;
|
||||
@@ -25,6 +25,7 @@ use darkfi::{
|
||||
cli::{get_log_config, get_log_level, spawn_config},
|
||||
expand_path,
|
||||
file::{load_file, load_json_file, save_file, save_json_file},
|
||||
gen_id,
|
||||
path::get_config_path,
|
||||
},
|
||||
Error, Result,
|
||||
@@ -34,7 +35,7 @@ mod error;
|
||||
mod jsonrpc;
|
||||
mod patch;
|
||||
|
||||
use error::DarkWikiResult;
|
||||
use error::{DarkWikiError, DarkWikiResult};
|
||||
use jsonrpc::JsonRpcInterface;
|
||||
use patch::{OpMethod, Patch};
|
||||
|
||||
@@ -78,6 +79,29 @@ fn str_to_chars(s: &str) -> Vec<&str> {
|
||||
s.graphemes(true).collect::<Vec<&str>>()
|
||||
}
|
||||
|
||||
fn get_from_index(local_path: &Path, title: &str) -> DarkWikiResult<String> {
|
||||
let index = load_json_file::<FxHashMap<String, String>>(&local_path.join("index"))?;
|
||||
|
||||
for (i, t) in index {
|
||||
if t == title {
|
||||
return Ok(i)
|
||||
}
|
||||
}
|
||||
|
||||
Err(DarkWikiError::FileNotFound)
|
||||
}
|
||||
|
||||
fn save_to_index(local_path: &Path, id: &str, title: &str) -> DarkWikiResult<()> {
|
||||
let mut index = load_json_file::<FxHashMap<String, String>>(&local_path.join("index"))?;
|
||||
index.insert(id.to_owned(), title.to_owned());
|
||||
save_json_file(&local_path.join("index"), &index)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn extract_title(content: &str) -> String {
|
||||
str_to_chars(content).into_iter().skip_while(|&c| c == "#").take_while(|&c| c == "\n").collect()
|
||||
}
|
||||
|
||||
fn lcs(a: &str, b: &str) -> Vec<OpMethod> {
|
||||
let a: Vec<_> = str_to_chars(a);
|
||||
let b: Vec<_> = str_to_chars(b);
|
||||
@@ -146,15 +170,25 @@ fn on_receive_update(settings: &DarkWikiSettings) -> DarkWikiResult<Vec<Patch>>
|
||||
// then merged with sync patches if any received
|
||||
let docs = read_dir(&docs_path).map_err(Error::from)?;
|
||||
for doc in docs {
|
||||
let doc_id = doc.as_ref().unwrap().file_name();
|
||||
let doc_path = docs_path.join(&doc_id);
|
||||
let doc_title = doc.as_ref().unwrap().file_name();
|
||||
let doc_title = doc_title.to_str().unwrap();
|
||||
|
||||
let doc_id = match get_from_index(&local_path, doc_title) {
|
||||
Ok(id) => id,
|
||||
Err(_) => {
|
||||
let id = gen_id(30);
|
||||
save_to_index(&local_path, &id, doc_title)?;
|
||||
id
|
||||
}
|
||||
};
|
||||
|
||||
// load doc content
|
||||
let edit = load_file(&doc_path).map_err(Error::from)?;
|
||||
// TODO
|
||||
let edit = load_file(&docs_path.join(doc_title)).map_err(Error::from)?;
|
||||
let edit = edit.trim();
|
||||
|
||||
// create new patch
|
||||
let mut new_patch = Patch::new(doc_id.to_str().unwrap(), &settings.author);
|
||||
let mut new_patch = Patch::new(&doc_id, &settings.author);
|
||||
|
||||
// check for any changes found with local doc and darkwiki doc
|
||||
if let Ok(local_edit) = load_file(&local_path.join(&doc_id)) {
|
||||
@@ -183,7 +217,7 @@ fn on_receive_update(settings: &DarkWikiSettings) -> DarkWikiResult<Vec<Patch>>
|
||||
if sync_patch.to_string() != local_edit {
|
||||
let sync_patch_t = new_patch.transform(&sync_patch);
|
||||
new_patch = new_patch.merge(&sync_patch_t);
|
||||
save_file(&doc_path, &new_patch.to_string())?;
|
||||
save_file(&docs_path.join(doc_title), &new_patch.to_string())?;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
@@ -200,6 +234,7 @@ fn on_receive_update(settings: &DarkWikiSettings) -> DarkWikiResult<Vec<Patch>>
|
||||
let sync_files = read_dir(&sync_path).map_err(Error::from)?;
|
||||
for file in sync_files {
|
||||
let file_id = file.as_ref().unwrap().file_name();
|
||||
let file_id = file_id.to_str().unwrap();
|
||||
let file_path = sync_path.join(&file_id);
|
||||
let sync_patch: Patch = load_json_file(&file_path)?;
|
||||
|
||||
@@ -209,8 +244,12 @@ fn on_receive_update(settings: &DarkWikiSettings) -> DarkWikiResult<Vec<Patch>>
|
||||
}
|
||||
}
|
||||
|
||||
save_file(&docs_path.join(&file_id), &sync_patch.to_string())?;
|
||||
save_file(&local_path.join(file_id), &sync_patch.to_string())?;
|
||||
let sync_patch_str = sync_patch.to_string();
|
||||
let file_title = extract_title(&sync_patch_str);
|
||||
save_to_index(&local_path, file_id, &file_title)?;
|
||||
|
||||
save_file(&docs_path.join(&file_title), &sync_patch_str)?;
|
||||
save_file(&local_path.join(file_id), &sync_patch_str)?;
|
||||
}
|
||||
|
||||
Ok(patches)
|
||||
|
||||
Reference in New Issue
Block a user