use super::community_plugin::CommunityPlugin;
use super::error::ObsidianError;
use super::utils;
use serde::{Deserialize, Serialize};
use std::fs::{create_dir_all, remove_file, File};
use std::io::copy;
use std::path::PathBuf;
use std::time::UNIX_EPOCH;
#[derive(Serialize, Deserialize)]
pub struct ObsidianReleases {
pub community_plugins: Vec<CommunityPlugin>,
}
impl Default for ObsidianReleases {
fn default() -> Self {
Self::new()
}
}
impl ObsidianReleases {
pub fn new() -> Self {
let mut s = Self {
community_plugins: Vec::new(),
};
s.refresh_community_plugins().unwrap();
s
}
fn get_config_path(&self) -> PathBuf {
PathBuf::from(
shellexpand::tilde("~/.md2ms/obsidian/")
.to_string()
.to_owned(),
)
}
fn refresh_community_plugins(&mut self) -> Result<(), ObsidianError> {
let config = self.get_config_path();
if create_dir_all(config).is_err() {
return Err(ObsidianError::DirectoryCreationError);
}
let cache = self.get_config_path().join("community-plugins.json");
if cache.exists() && cache.is_file() {
if let Ok(file) = File::open(&cache) {
let seconds = file
.metadata()
.unwrap()
.created()
.unwrap()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs();
let now = UNIX_EPOCH.elapsed().unwrap().as_secs();
let age = now - seconds;
if age > 3600 {
let _ = remove_file(&cache);
} else {
let contents = utils::slurp(&cache);
if let Ok(p) = serde_json::from_str(&contents) {
self.community_plugins = p;
return Ok(());
} else {
return Err(ObsidianError::ParseError);
}
}
}
}
let data = utils::slurp_url("https://raw.githubusercontent.com/obsidianmd/obsidian-releases/refs/heads/master/community-plugins.json".to_string());
if let Ok(mut out) = File::create(cache) {
if copy(&mut data.as_bytes(), &mut out).is_ok() {
let p: Vec<CommunityPlugin> = serde_json::from_str(&data).unwrap();
self.community_plugins = p;
return Ok(());
}
}
Err(ObsidianError::OtherError)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_obsidian_releases_refresh_community_plugins() {
let mut or = ObsidianReleases::new();
let _ = or.refresh_community_plugins();
assert!(or.community_plugins.len() > 0);
}
}