This commit is contained in:
Junko 2024-04-04 21:57:29 +02:00
parent c1788330f5
commit 7698732472
13 changed files with 315 additions and 51 deletions

17
.envrc Normal file
View File

@ -0,0 +1,17 @@
#!/usr/bin/env bash
# ^ make editor happy
#
# Use https://direnv.net/ to automatically load the dev shell.
#
if ! has nix_direnv_version || ! nix_direnv_version 3.0.4; then
source_url "https://raw.githubusercontent.com/nix-community/nix-direnv/3.0.4/direnvrc" "sha256-DzlYZ33mWF/Gs8DDeyjr8mnVmQGx7ASYqA5WlxwvBG4="
fi
watch_file nix/**
watch_file -- **/*.nix
# Adding files to git includes them in a flake
# But it is also a bit much reloading.
# watch_file .git/index .git/HEAD
use flake . --show-trace

45
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,45 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug executable 'pluralsync'",
"cargo": {
"args": [
"build",
"--bin=pluralsync",
"--package=pluralsync"
],
"filter": {
"name": "pluralsync",
"kind": "bin"
}
},
"args": ["add","aki"],
"cwd": "${workspaceFolder}"
},
{
"type": "lldb",
"request": "launch",
"name": "Debug unit tests in executable 'pluralsync'",
"cargo": {
"args": [
"test",
"--no-run",
"--bin=pluralsync",
"--package=pluralsync"
],
"filter": {
"name": "pluralsync",
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}"
}
]
}

2
Cargo.lock generated
View File

@ -870,7 +870,7 @@ checksum = "d231b230927b5e4ad203db57bbcbee2802f6bce620b1e4a9024a07d94e2907ec"
[[package]] [[package]]
name = "pluralsync" name = "pluralsync"
version = "1.2.0" version = "1.4.0"
dependencies = [ dependencies = [
"clap", "clap",
"color-eyre", "color-eyre",

View File

@ -1,6 +1,6 @@
[package] [package]
name = "pluralsync" name = "pluralsync"
version = "1.2.0" version = "1.4.0"
edition = "2021" edition = "2021"
authors = ["jvnkyard@ak.lavenderfield.xyz"] authors = ["jvnkyard@ak.lavenderfield.xyz"]
@ -18,7 +18,7 @@ crossterm = "0.27.0"
color-eyre = "0.6.2" color-eyre = "0.6.2"
[features] [features]
default = ["avatar"] default = ["fedi","discord"]#,"jlog"]
avatar = [] avatar = []
discord = ["avatar"] discord = ["avatar"]
fedi = ["avatar"] fedi = ["avatar"]

View File

@ -36,9 +36,9 @@ On the first run, if it does not exist, PluralSync will create a configuration d
Just fill it with PluralKit and SimplyPlural tokens associated to your account. Just fill it with PluralKit and SimplyPlural tokens associated to your account.
** Commands ** Commands
- *Sync* - Once the configuration is ready, you can start by calling the =sync= command, which will get the list of members of the system and place their data in a =system.json= file inside the configuration folder. Running commands like =set= or =add= without a present =system.json= will automatically fetch the data for you. - *Update* - Once the configuration is ready, you can start by calling the =update= command, which will get the list of members of the system and place their data in a =system.json= file inside the configuration folder. Running commands like =set= or =add= without a present =system.json= will automatically fetch the data for you.
*Remember to sync whenever you change the information of the system! i.e. adding a new member, rename...* *Remember to update whenever you change the information of the system! i.e. adding a new member, rename...*
- *Set* - Takes multiple arguments. Sets the current front to the selected list of members. - *Set* - Takes multiple arguments. Sets the current front to the selected list of members.

130
flake.lock Normal file
View File

@ -0,0 +1,130 @@
{
"nodes": {
"flake-utils": {
"inputs": {
"systems": "systems"
},
"locked": {
"lastModified": 1710146030,
"narHash": "sha256-SZ5L6eA7HJ/nmkzGG7/ISclqe6oZdOZTNoesiInkXPQ=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "b1d9ab70662946ef0850d488da1c9019f3a9752a",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"flake-utils_2": {
"inputs": {
"systems": "systems_2"
},
"locked": {
"lastModified": 1705309234,
"narHash": "sha256-uNRRNRKmJyCRC/8y1RqBkqWBLM034y4qN7EprSdmgyA=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "1ef2e671c3b0c19053962c07dbda38332dcebf26",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1712163089,
"narHash": "sha256-Um+8kTIrC19vD4/lUCN9/cU9kcOsD1O1m+axJqQPyMM=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "fd281bd6b7d3e32ddfa399853946f782553163b5",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixos-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"nixpkgs_2": {
"locked": {
"lastModified": 1706487304,
"narHash": "sha256-LE8lVX28MV2jWJsidW13D2qrHU/RUUONendL2Q/WlJg=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "90f456026d284c22b3e3497be980b2e47d0b28ac",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixpkgs-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"flake-utils": "flake-utils",
"nixpkgs": "nixpkgs",
"rust-overlay": "rust-overlay"
}
},
"rust-overlay": {
"inputs": {
"flake-utils": "flake-utils_2",
"nixpkgs": "nixpkgs_2"
},
"locked": {
"lastModified": 1712196778,
"narHash": "sha256-SOiwCr2HtmYpw8OvQQVRPtiCBWwndbIoPqtsamZK3J8=",
"owner": "oxalica",
"repo": "rust-overlay",
"rev": "20e7895d1873cc64c14a9f024a8e04f5824bed28",
"type": "github"
},
"original": {
"owner": "oxalica",
"repo": "rust-overlay",
"type": "github"
}
},
"systems": {
"locked": {
"lastModified": 1681028828,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
"owner": "nix-systems",
"repo": "default",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
"type": "github"
},
"original": {
"owner": "nix-systems",
"repo": "default",
"type": "github"
}
},
"systems_2": {
"locked": {
"lastModified": 1681028828,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
"owner": "nix-systems",
"repo": "default",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
"type": "github"
},
"original": {
"owner": "nix-systems",
"repo": "default",
"type": "github"
}
}
},
"root": "root",
"version": 7
}

43
flake.nix Normal file
View File

@ -0,0 +1,43 @@
{
description = "rust";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
rust-overlay.url = "github:oxalica/rust-overlay";
flake-utils.url = "github:numtide/flake-utils";
};
outputs = { nixpkgs, rust-overlay, flake-utils, ... }:
flake-utils.lib.eachDefaultSystem (system:
let
overlays = [ (import rust-overlay) ];
pkgs = import nixpkgs {
inherit system overlays;
};
bi = with pkgs; [
openssl
pkg-config
rust-bin.nightly.latest.default
];
in
with pkgs;
{
packages.default = rustPlatform.buildRustPackage {
pname = "pluralsync";
version = "1.4.0";
src = ./.;
cargoLock.lockFile = ./Cargo.lock;
checkType = "release";
nativeBuildInputs = [ pkgs.pkg-config ];
buildNoDefaultFeatures = true;
buildFeatures = ["discord" "fedi"];
buildInputs = bi;
};
devShells.default = mkShell {
name = "rust";
buildInputs = bi;
shellHook = ''
'';
};
}
);
}

1
result Symbolic link
View File

@ -0,0 +1 @@
/nix/store/5h8kyc2m1gs0lyscwrsbvijp9l7b2pym-pluralsync-1.4.0

View File

@ -163,8 +163,8 @@ pub mod sp {
let datas: Vec<Value> = serde_json::from_str(&result)?; let datas: Vec<Value> = serde_json::from_str(&result)?;
for data in datas { for data in datas {
let sp_f_id = &data["id"].to_string(); let sp_f_id = &data["id"].as_str().unwrap().to_string();
let sp_id = &data["content"]["member"].to_string(); let sp_id = &data["content"]["member"].as_str().unwrap().to_string();
if sp_id.to_string() == to_id.sp_id { if sp_id.to_string() == to_id.sp_id {
return Ok(sp_f_id.to_string()); return Ok(sp_f_id.to_string());
} }
@ -256,12 +256,40 @@ pub mod sp {
} }
} }
#[cfg(feature = "fedi")]
pub mod fedi {
use std::fs;
use reqwest::header::{USER_AGENT, AUTHORIZATION};
use reqwest::multipart::{Part, Form};
use color_eyre::eyre::{eyre, Result};
use crate::configdata::Config;
use crate::api::request::http_request;
pub fn fedi_request(config: &Config) -> Result<()> {
let client = reqwest::Client::new();
let form = Form::new().part("avatar", Part::bytes(fs::read(config.avatar_module.avatar_output_path.clone()).unwrap()).file_name("face.png").mime_str("image/png").unwrap());
let rb = client
.patch(config.fedi_module.instance.clone() + "/api/v1/accounts/update_credentials")
.multipart(form)
.header(USER_AGENT, "Pluralsync")
.header(AUTHORIZATION, format!("Bearer {}", &config.fedi_module.token).as_str());
println!("Fedi module finished");
match http_request(rb) {
Ok(_) => Ok(()),
Err(e) => Err(eyre!("{}", e.to_string())),
}
}
}
pub mod request { pub mod request {
use reqwest::RequestBuilder; use reqwest::RequestBuilder;
use reqwest::header::{USER_AGENT, AUTHORIZATION}; use reqwest::header::{USER_AGENT, AUTHORIZATION};
#[cfg(feature = "fedi")]
use reqwest::multipart::{Part, Form};
use color_eyre::eyre::Result; use color_eyre::eyre::Result;

View File

@ -6,6 +6,7 @@ use color_eyre::eyre::{eyre, Result};
pub enum CurrentScreen { pub enum CurrentScreen {
Dash, Dash,
Member,
Configuration, Configuration,
Exit, Exit,
} }
@ -94,22 +95,24 @@ impl App {
fn add(&mut self, to_front: Vec<Member>) -> Result<()> { fn add(&mut self, to_front: Vec<Member>) -> Result<()> {
let aux_fronters: Fronters = Fronters::get(self, ForceFrom::None)?; self.get(ForceFrom::None)?;
if let Some(aux_fronters) = &self.fronters {
let mut addfronters: Vec<Member> = Vec::new();
addfronters.append(&mut aux_fronters.pk.clone());
addfronters.append(&mut to_front.clone());
let mut addfronters: Vec<Member> = Vec::new(); self.fronters = Some(Fronters::set(self, addfronters)?);
addfronters.append(&mut aux_fronters.pk.clone());
addfronters.append(&mut to_front.clone());
self.fronters = Some(Fronters::set(self, addfronters)?); return Ok(());
}
Ok(()) Err(eyre!("Could not fetch the current front"))
} }
pub fn memberlist(&self) -> Result<()> { pub fn memberlist(&self) -> Result<()> {
if let Some(sys) = &self.sys { if let Some(sys) = &self.sys {
for member in &sys.members { for member in &sys.members {
println!("{} / {}", member.name, member.aliases.join(" | ")); println!("{} [ {} ]", member.name, member.aliases.join(" | "));
} }
} }
@ -135,15 +138,15 @@ impl App {
if to_front.len() == tf_members.len() { if to_front.len() == tf_members.len() {
if !append { if !append {
self.set(to_front)?; self.set(to_front.clone())?;
} else { } else {
self.add(to_front)?; self.add(to_front.clone())?;
} }
} else { } else {
println!("One or more members were not found. Known members:\n--------------------------"); println!("One or more members were not found. Known members:\n--------------------------");
self.memberlist()?; self.memberlist()?;
println!("--------------------------\nIf a member is missing from the system try running \"pluralsync update\" to refresh the local database"); println!("--------------------------\nIf a member is missing from the system try running \"pluralsync update\" to refresh the local database");
return Err(eyre!("Missing member")); return Ok(());
} }
#[cfg(feature = "jlog")] { #[cfg(feature = "jlog")] {

View File

@ -4,7 +4,7 @@ use clap::{Parser, Subcommand, ValueEnum};
#[clap(arg_required_else_help = true, verbatim_doc_comment, author)] #[clap(arg_required_else_help = true, verbatim_doc_comment, author)]
#[command(name = "PluralSync")] #[command(name = "PluralSync")]
#[command(author = "jvnkyard <lafresita@protonmail.com>")] #[command(author = "jvnkyard <lafresita@protonmail.com>")]
#[command(version = "1.2")] #[command(version = "1.4")]
#[command( #[command(
help_template = "{name} Version {version} \n {author} \n {about-section} \n {usage-heading} {usage} \n \n {all-args} {tab}" help_template = "{name} Version {version} \n {author} \n {about-section} \n {usage-heading} {usage} \n \n {all-args} {tab}"
)] )]
@ -67,6 +67,7 @@ pub enum Commands {
fedi: bool, fedi: bool,
}, },
/// Displays a list with all members /// Displays a list with all members
#[clap(aliases = &["memberlist", "list"])]
Members, Members,
} }

View File

@ -96,7 +96,7 @@ impl System {
} }
} }
#[derive(Debug)] #[derive(Debug, Clone)]
pub struct Fronters { pub struct Fronters {
pub sp: Vec<Member>, pub sp: Vec<Member>,
pub pk: Vec<Member>, pub pk: Vec<Member>,
@ -120,9 +120,12 @@ impl Fronters {
ForceFrom::SP => { ForceFrom::SP => {
fronters.pk = fronters.sp.clone(); fronters.pk = fronters.sp.clone();
} }
_ => { ForceFrom::PK => {
fronters.sp = fronters.pk.clone(); fronters.sp = fronters.pk.clone();
} }
ForceFrom::None => {
}
} }
} }

View File

@ -17,6 +17,9 @@ use {
color_eyre::eyre::eyre, color_eyre::eyre::eyre,
}; };
#[cfg(feature = "fedi")]
use crate::api::fedi::*;
pub fn load_json(file_path: &str) -> Result<Value> { pub fn load_json(file_path: &str) -> Result<Value> {
let file_data = fs::read_to_string(&file_path)?; let file_data = fs::read_to_string(&file_path)?;
@ -28,18 +31,18 @@ pub fn load_json(file_path: &str) -> Result<Value> {
#[cfg(feature = "avatar")] #[cfg(feature = "avatar")]
pub fn update_avatars(app: &mut App, #[cfg(feature = "discord")] discord: bool, #[cfg(feature = "fedi")] fedi: bool) -> Result<()>{ pub fn update_avatars(app: &mut App, #[cfg(feature = "discord")] discord: bool, #[cfg(feature = "fedi")] fedi: bool) -> Result<()>{
if let Some(cfg) = app.cfg.clone() { if let Some(mut cfg) = app.cfg.clone() {
let names = &app.get_front(ForceFrom::None)?; // TODO CHANGE INTO USING LOCAL FRONT let names = &app.get_front(ForceFrom::None)?; // TODO CHANGE INTO USING LOCAL FRONT
match avatar_module(&cfg, &names) { match avatar_module(&cfg, &names) {
Ok(_) => { Ok(_) => {
#[cfg(feature = "discord")] { #[cfg(feature = "discord")] {
config.disc_module.enabled |= discord; cfg.disc_module.enabled |= discord;
discord_module(&config); discord_module(&cfg)?;
} }
#[cfg(feature = "fedi")] { #[cfg(feature = "fedi")] {
config.fedi_module.enabled |= fedi; cfg.fedi_module.enabled |= fedi;
fedi_module(&config); fedi_module(&cfg)?;
} }
}, },
Err(e) => println!("{}", e), Err(e) => println!("{}", e),
@ -50,7 +53,7 @@ pub fn update_avatars(app: &mut App, #[cfg(feature = "discord")] discord: bool,
} }
#[cfg(feature = "avatar")] #[cfg(feature = "avatar")]
fn avatar_module(config: &Config, names: &Vec<String>) -> Result<(), &'static str>{ fn avatar_module(config: &Config, names: &Vec<String>) -> Result<()>{
if config.avatar_module.enabled { if config.avatar_module.enabled {
let mut whitelisted_names: Vec<String> = names.iter().map(|i| i.to_lowercase()).collect(); let mut whitelisted_names: Vec<String> = names.iter().map(|i| i.to_lowercase()).collect();
let blacklist: Vec<String> = config.avatar_module.blacklist.iter().map(|i| i.to_lowercase()).collect(); let blacklist: Vec<String> = config.avatar_module.blacklist.iter().map(|i| i.to_lowercase()).collect();
@ -78,43 +81,33 @@ fn avatar_module(config: &Config, names: &Vec<String>) -> Result<(), &'static st
println!("Avatar module finished"); println!("Avatar module finished");
Ok(()) Ok(())
} else { } else {
Err("Avatar module failed") Err(eyre!("Avatar module failed"))
} }
} else { } else {
Err("Avatar mode disabled") Err(eyre!("Avatar mode disabled"))
} }
} }
#[cfg(feature = "discord")] #[cfg(feature = "discord")]
fn discord_module(config: &Config) { fn discord_module(config: &Config) -> Result<()> {
if config.disc_module.enabled { if config.disc_module.enabled {
if cfg!(target_os = "windows") { if cfg!(target_os = "windows") {
let mut c = Command::new("cmd").args(["/C", format!("{} {}", &config.disc_module.python_path, &config.disc_module.script_path).as_str()]).spawn().expect("Discord module error"); let mut c = Command::new("cmd").args(["/C", format!("{} {}", &config.disc_module.python_path, &config.disc_module.script_path).as_str()]).spawn()?;
let _ = c.wait().expect("Error"); let _ = c.wait()?;
} else { } else {
let mut c = Command::new("sh").arg("-c").arg(format!("{} {}", &config.disc_module.python_path, &config.disc_module.script_path)).spawn().expect("Discord module error"); let mut c = Command::new("sh").arg("-c").arg(format!("{} {}", &config.disc_module.python_path, &config.disc_module.script_path)).spawn()?;
let _ = c.wait().expect("Error"); let _ = c.wait()?;
} }
} }
Ok(())
} }
#[cfg(feature = "fedi")] #[cfg(feature = "fedi")]
fn fedi_module(config: &Config) { fn fedi_module(config: &Config) -> Result<()> {
if config.fedi_module.enabled { if config.fedi_module.enabled {
fedi_request(config)?;
let client = reqwest::Client::new();
let form = Form::new().part("avatar", Part::bytes(fs::read(config.avatar_module.avatar_output_path.clone()).unwrap()).file_name("face.png").mime_str("image/png").unwrap());
let rb = client
.patch(config.fedi_module.instance.clone() + "/api/v1/accounts/update_credentials")
.multipart(form)
.header(USER_AGENT, "Pluralsync")
.header(AUTHORIZATION, format!("Bearer {}", &config.fedi_module.token).as_str());
println!("Fedi module finished");
match http_request(rb) {
Ok(_) => (),
Err(e) => println!("{}", e.to_string()),
}
} }
Ok(())
} }