Skip to content

Commit

Permalink
Updater
Browse files Browse the repository at this point in the history
  • Loading branch information
omznc committed Oct 11, 2024
1 parent 22686ca commit 11269da
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 23 deletions.
1 change: 1 addition & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ jobs:
- uses: tauri-apps/tauri-action@v0
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
TAURI_SIGNING_PRIVATE_KEY: ${{ secrets.TAURI_SIGNING_PRIVATE_KEY }} # Updater private key
with:
tagName: app-v__VERSION__ # the action automatically replaces \_\_VERSION\_\_ with the app version.
releaseName: 'Tauri v__VERSION__'
Expand Down
10 changes: 10 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src-tauri/capabilities/desktop.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"identifier": "desktop-capability",
"platforms": ["macOS", "windows", "linux"],
"permissions": ["updater:default"]
}
74 changes: 51 additions & 23 deletions src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
use reqwest::header::AUTHORIZATION;
use serde::{de, Deserialize, Deserializer, Serialize};
use serde_json::{json, Value};
use std::{
fmt, fs,
path::{Path, PathBuf},
};
use tauri::Manager;
use std::{fmt, fs, path::{Path, PathBuf}};
use reqwest::header::AUTHORIZATION;

#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
tauri::Builder::default()
.plugin(tauri_plugin_updater::Builder::new().build())
.plugin(tauri_plugin_shell::init())
.invoke_handler(tauri::generate_handler![read_discord_backup, delete_message])
.invoke_handler(tauri::generate_handler![
read_discord_backup,
delete_message
])
.run(tauri::generate_context!())
.expect("error while running tauri application");
}



fn string_or_number<'de, D>(deserializer: D) -> Result<String, D::Error>
where
D: Deserializer<'de>,
Expand Down Expand Up @@ -84,11 +89,10 @@ struct DiscordBackup {
guilds: Vec<GuildChat>,
}


fn get_or_create_deleted_messages_path(app_handle: &tauri::AppHandle) -> Result<PathBuf, String> {
// Get the application data directory
let app_data_path = app_handle.path().app_data_dir().unwrap();

// Ensure the directory exists
if !app_data_path.exists() {
fs::create_dir_all(&app_data_path).map_err(|e| e.to_string())?;
Expand All @@ -99,8 +103,16 @@ fn get_or_create_deleted_messages_path(app_handle: &tauri::AppHandle) -> Result<
}

#[tauri::command]
async fn delete_message(app_handle: tauri::AppHandle, token: String, channel_id: String, message_id: String) -> Result<u16, String> {
let url = format!("https://discord.com/api/v9/channels/{}/messages/{}", channel_id, message_id);
async fn delete_message(
app_handle: tauri::AppHandle,
token: String,
channel_id: String,
message_id: String,
) -> Result<u16, String> {
let url = format!(
"https://discord.com/api/v9/channels/{}/messages/{}",
channel_id, message_id
);

let client = reqwest::Client::new();
let response = client
Expand All @@ -119,16 +131,20 @@ async fn delete_message(app_handle: tauri::AppHandle, token: String, channel_id:
if !json_path.exists() {
// Create the JSON file with initial structure
let initial_data = json!({ "deleted_messages": [] });
fs::write(&json_path, serde_json::to_string_pretty(&initial_data).map_err(|e| e.to_string())?)
.map_err(|e| e.to_string())?;
fs::write(
&json_path,
serde_json::to_string_pretty(&initial_data).map_err(|e| e.to_string())?,
)
.map_err(|e| e.to_string())?;
}

// Log deleted message to the JSON file
let mut deleted_messages = vec![];

// Read existing entries
if let Ok(data) = fs::read_to_string(&json_path) {
let json_data: Value = serde_json::from_str(&data).map_err(|e| e.to_string())?;
let json_data: Value =
serde_json::from_str(&data).map_err(|e| e.to_string())?;
if let Some(messages) = json_data["deleted_messages"].as_array() {
deleted_messages.extend(messages.clone());
}
Expand All @@ -142,17 +158,23 @@ async fn delete_message(app_handle: tauri::AppHandle, token: String, channel_id:

// Write back to the JSON file
let json_output = json!({ "deleted_messages": deleted_messages });
fs::write(&json_path, serde_json::to_string_pretty(&json_output).map_err(|e| e.to_string())?)
.map_err(|e| e.to_string())?;
fs::write(
&json_path,
serde_json::to_string_pretty(&json_output).map_err(|e| e.to_string())?,
)
.map_err(|e| e.to_string())?;
}
Ok(status_code) // Return the status code
},
Err(_) => Err("Error deleting the message".into()), // Handle errors
Ok(status_code) // Return the status code
}
Err(_) => Err("Error deleting the message".into()), // Handle errors
}
}

#[tauri::command]
fn read_discord_backup(app_handle: tauri::AppHandle, directory: String) -> Result<DiscordBackup, String> {
fn read_discord_backup(
app_handle: tauri::AppHandle,
directory: String,
) -> Result<DiscordBackup, String> {
let path = Path::new(&directory);
if !path.is_dir() {
return Err("Provided directory is not valid".to_string());
Expand All @@ -161,15 +183,19 @@ fn read_discord_backup(app_handle: tauri::AppHandle, directory: String) -> Resul
// Get the path of the JSON file next to the executable
let json_path = get_or_create_deleted_messages_path(&app_handle)?;


// Load deleted messages from JSON file
let mut deleted_message_ids = vec![];

if json_path.exists() {
let data = fs::read_to_string(&json_path).map_err(|e| e.to_string())?;
let json_data: serde_json::Value = serde_json::from_str(&data).map_err(|e| e.to_string())?;
let json_data: serde_json::Value =
serde_json::from_str(&data).map_err(|e| e.to_string())?;
if let Some(messages) = json_data["deleted_messages"].as_array() {
deleted_message_ids.extend(messages.iter().map(|msg| msg["message_id"].as_str().unwrap_or("").to_string()));
deleted_message_ids.extend(
messages
.iter()
.map(|msg| msg["message_id"].as_str().unwrap_or("").to_string()),
);
}
}

Expand All @@ -186,11 +212,13 @@ fn read_discord_backup(app_handle: tauri::AppHandle, directory: String) -> Resul

// Read and parse channel.json
let channel_data = fs::read_to_string(channel_file).map_err(|e| e.to_string())?;
let channel: Channel = serde_json::from_str(&channel_data).map_err(|e| e.to_string())?;
let channel: Channel =
serde_json::from_str(&channel_data).map_err(|e| e.to_string())?;

// Read and parse messages.json
let messages_data = fs::read_to_string(messages_file).map_err(|e| e.to_string())?;
let mut messages: Vec<Message> = serde_json::from_str(&messages_data).map_err(|e| e.to_string())?;
let mut messages: Vec<Message> =
serde_json::from_str(&messages_data).map_err(|e| e.to_string())?;

// Filter out deleted messages
messages.retain(|msg| !deleted_message_ids.contains(&msg.ID));
Expand Down

0 comments on commit 11269da

Please sign in to comment.