diff --git a/shell.nix b/shell.nix deleted file mode 100644 index 42abca8..0000000 --- a/shell.nix +++ /dev/null @@ -1,25 +0,0 @@ -{pkgs ? import {}}: let - rust-toolchain = pkgs.symlinkJoin { - name = "rust-toolchain"; - paths = with pkgs; [rustc cargo rustPlatform.rustcSrc rustfmt clippy]; - }; -in - pkgs.mkShell { - nativeBuildInputs = with pkgs.buildPackages; [ - rust-toolchain - - pkg-config - xe - xz - - cargo-tarpaulin - openssl - sqlite - gcc - gnumake - - # dotnet-sdk_8 - ]; - - RUST_SRC_PATH = "${pkgs.rust.packages.stable.rustPlatform.rustLibSrc}"; - } diff --git a/src/hedgedoc.rs b/src/hedgedoc.rs index b28ae7b..514f081 100644 --- a/src/hedgedoc.rs +++ b/src/hedgedoc.rs @@ -1,4 +1,5 @@ use crate::config_spec::{CfgField, CfgGroup}; +use crate::key_value::KeyValueStore; use ollama_rs; use ollama_rs::generation::completion::request::GenerationRequest; use regex::Regex; @@ -152,56 +153,64 @@ impl HedgeDoc { Err(format!("Failed to import note: {}", res.status()).into()) } } -} -pub fn extract_metadata(pad_content: String) -> String { - let re_yaml = Regex::new(r"(?s)---\s*(.*?)\s*(?:\.\.\.|---)").unwrap(); - re_yaml.captures_iter(&pad_content).map(|c| c[1].to_string()).collect::>().join("\n") -} - -pub fn strip_metadata(pad_content: String) -> String { - let re_yaml = Regex::new(r"(?s)---\s*.*?\s*(?:\.\.\.|---)").unwrap(); - let pad_content = re_yaml.replace_all(&pad_content, "").to_string(); - let re_comment = Regex::new(r"(?s)").unwrap(); - let content_without_comments = re_comment.replace_all(&pad_content, "").to_string(); - content_without_comments.trim().to_string() -} - -pub fn summarize(pad_content: String) -> String { - // 1. remove HTML comments - let pad_content = strip_metadata(pad_content); - // 2. accumulate topic lines - let re_header = Regex::new(r"^\s*##(#*) TOP ([\d.]+\s*.*?)\s*#*$").unwrap(); - let mut result: Vec = Vec::new(); - for line in pad_content.lines() { - if let Some(captures) = re_header.captures(line) { - let indent = " ".repeat(captures.get(1).unwrap().as_str().len()); - let title = captures.get(2).unwrap().as_str(); - result.push(format!("{}{}", indent, title)); - } + pub fn rotate(config: &KeyValueStore, is_dry_run: bool) -> Result> { + let hold_pad_id = &config["hedgedoc-next-id"]; + config.set("hedgedoc-last-id", hold_pad_id); + let new_id = HedgeDoc::new(&config.get("hedgedoc-server-url").unwrap(), is_dry_run).create_pad()?; + config.set("hedgedoc-next-id", &new_id); + Ok(new_id) } - result.join("\n") -} -pub fn summarize_with_ollama( - pad_content: &str, ollama_pre_prompt: &str, ollama_address: &str, ollama_port: &u16, -) -> Result> { - let ollama = ollama_rs::Ollama::new(ollama_address, ollama_port.clone()); - let model = "qwen2.5:32b".to_string(); - let prompt = ollama_pre_prompt.to_string() + pad_content; - let rt = Runtime::new().unwrap(); + pub fn extract_metadata(pad_content: String) -> String { + let re_yaml = Regex::new(r"(?s)---\s*(.*?)\s*(?:\.\.\.|---)").unwrap(); + re_yaml.captures_iter(&pad_content).map(|c| c[1].to_string()).collect::>().join("\n") + } - let result = - rt.block_on(async { ollama.generate(GenerationRequest::new(model, prompt)).await }); - match result { - Ok(res) => return Ok(res.response), - Err(err) => return Err(err.into()), + pub fn strip_metadata(pad_content: String) -> String { + let re_yaml = Regex::new(r"(?s)---\s*.*?\s*(?:\.\.\.|---)").unwrap(); + let pad_content = re_yaml.replace_all(&pad_content, "").to_string(); + let re_comment = Regex::new(r"(?s)").unwrap(); + let content_without_comments = re_comment.replace_all(&pad_content, "").to_string(); + content_without_comments.trim().to_string() + } + + pub fn summarize(&self, pad_content: String) -> String { + // 1. remove HTML comments + let pad_content = Self::strip_metadata(pad_content); + // 2. accumulate topic lines + let re_header = Regex::new(r"^\s*##(#*) TOP ([\d.]+\s*.*?)\s*#*$").unwrap(); + let mut result: Vec = Vec::new(); + for line in pad_content.lines() { + if let Some(captures) = re_header.captures(line) { + let indent = " ".repeat(captures.get(1).unwrap().as_str().len()); + let title = captures.get(2).unwrap().as_str(); + result.push(format!("{}{}", indent, title)); + } + } + result.join("\n") + } + + pub fn summarize_with_ollama( + pad_content: &str, ollama_pre_prompt: &str, ollama_address: &str, ollama_port: &u16, + ) -> Result> { + let ollama = ollama_rs::Ollama::new(ollama_address, ollama_port.clone()); + let model = "qwen2.5:32b".to_string(); + let prompt = ollama_pre_prompt.to_string() + pad_content; + let rt = Runtime::new().unwrap(); + + let result = + rt.block_on(async { ollama.generate(GenerationRequest::new(model, prompt)).await }); + match result { + Ok(res) => return Ok(res.response), + Err(err) => return Err(err.into()), + } } } /// For the config, make a new pad ID (by actually making a pad.) -fn make_pad_id( +pub fn make_pad_id( _key: &str, config: &crate::key_value::KeyValueStore, is_dry_run: bool, ) -> Result> { HedgeDoc::new(&config.get("hedgedoc-server-url").unwrap(), is_dry_run).create_pad() -} +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index ab30b7d..f237b4f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -263,21 +263,6 @@ fn main() -> Result<(), Box> { } } -fn rotate(future_pad_id: &str, kv: &KV) { - let next_plenum_pad = kv.get("zukünftiges-plenumspad").ok(); - if let Some(next_plenum_pad) = next_plenum_pad { - kv.set("aktuelles-plenumspad", &next_plenum_pad) - .expect("Fehler beim Beschreiben der Datenbank mit neuem Plenumslink!"); // Beispiel: aktuelles-plenumspad: Ok(Some("eCH24zXGS9S8Stg5xI3aRg")) - kv.set("zukünftiges-plenumspad", future_pad_id) - .expect("Fehler beim Beschreiben der Datenbank mit neuem Plenumslink!"); - // Beispiel: aktuelles-plenumspad: Ok(Some("eCH24zXGS9S8Stg5xI3aRg")) - } else { - kv.set("zukünftiges-plenumspad", future_pad_id) - .expect("Fehler beim Beschreiben der Datenbank mit neuem Plenumslink!"); - // Beispiel: aktuelles-plenumspad: Ok(Some("eCH24zXGS9S8Stg5xI3aRg")) - } -} - /* ***** formatting helpers ***** */ fn relative_date(ttp: i64) -> String { @@ -497,6 +482,8 @@ fn do_protocol( ); let _message_id = send_email(&subject, &body, email, config)?; mediawiki::pad_ins_wiki(pad_content, wiki, plenum_day)?; + let new_id = hedgedoc::rotate(config, is_dry_run())?; + println!("Generated new pad with id '{}'", new_id); config.set("state-name", &ProgramState::Logged.to_string()).ok(); } else { verboseln!("There were no TOPs on this Plenum"); diff --git a/systemd/plenumbot.service b/systemd/plenumbot.service index d172b17..5968755 100644 --- a/systemd/plenumbot.service +++ b/systemd/plenumbot.service @@ -3,7 +3,6 @@ Description=CCCB Plenum-Bot After=network-online.target [Service] -WorkingDirectory=/home/$User/plenum-bot -ExecStart=./util/run_release.sh -Type=simple -User=root \ No newline at end of file +WorkingDirectory=%h +ExecStart=.cargo/bin/Plenum-Bot -f .config/Plenum-Bot.sqlite +Type=simple \ No newline at end of file diff --git a/util/change-config.sh b/util/change-config.sh index 252b850..6262605 100644 --- a/util/change-config.sh +++ b/util/change-config.sh @@ -1,7 +1,5 @@ #!/bin/sh -# This script is being ran every time the systemd job is triggered +# This script can be run in order to change the Plenum-Bot config -u=$USER - -../target/release/Plenum-Bot -f /home/$u/.config/plenum-bot/config.sqlite -c \ No newline at end of file +$HOME/.cargo/bin/Plenum-Bot -f .config/Plenum-Bot.sqlite -c \ No newline at end of file diff --git a/util/run_release.sh b/util/run_release.sh deleted file mode 100644 index ac6f6ee..0000000 --- a/util/run_release.sh +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/sh - -# This script is being ran every time the systemd job is triggered - -u=$USER - -../target/release/Plenum-Bot -f /home/$u/.config/plenum-bot/config.sqlite \ No newline at end of file diff --git a/util/update_or_create_systemd_service.sh b/util/update_or_create_systemd_service.sh index 596fb37..218007b 100644 --- a/util/update_or_create_systemd_service.sh +++ b/util/update_or_create_systemd_service.sh @@ -3,9 +3,9 @@ # This script can be run to update or newly install # the systemd services needed for sudo -i -cat ../systemd/plenumbot.service > /etc/systemd/system/plenumsbot.service -cat ../systemd/plenumbot.timer > /etc/systemd/system/plenumsbot.timer -chmod 755 /etc/systemd/system/plenumsbot.timer -chmod 755 /etc/systemd/system/plenumsbot.service +cat ../systemd/plenumbot.service > $HOME/.config/systemd/user/Plenum-Bot.service +cat ../systemd/plenumbot.timer > $HOME/.config/systemd/user/Plenum-Bot.timer +chmod 755 $HOME/.config/systemd/user/Plenum-Bot.timer +chmod 755 $HOME/.config/systemd/user/Plenum-Bot.service -systemctl daemon-reload \ No newline at end of file +systemctl daemon-reload --user \ No newline at end of file