From 4a80f0a30f9528924c3c688429383b8a60ce065c Mon Sep 17 00:00:00 2001 From: Robert Brenckman <20431767+RFBomb@users.noreply.github.com> Date: Sat, 24 Jun 2023 23:16:59 -0400 Subject: [PATCH] Add edit-only option (#66) --- README.md | 3 +++ shreddit.env.example | 1 + src/cli.rs | 25 +++++++++++++++++++++++++ src/things/comment.rs | 6 +----- src/things/friend.rs | 2 +- src/things/post.rs | 6 +----- src/things/saved_comment.rs | 2 +- src/things/saved_post.rs | 2 +- 8 files changed, 34 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 6d35d95..b34e18f 100644 --- a/README.md +++ b/README.md @@ -76,6 +76,9 @@ Options: The User-Agent for Reddit API requests [env: SHREDDIT_USER_AGENT=ShredditRustClient] [default: ShredditRustClient] --gdpr-export-dir The path of the directory of the unzipped GDPR export data. If set, `shreddit` will use the GDPR export folder instead of Reddit's APIs for discovering your data [env: SHREDDIT_GDPR_EXPORT_DIR=/home/you/Downloads/export_yourusername_20230101] + --edit_only + When set 'true', no items will be deleted. Items (comments) will still be modified though. + [env = SHREDDIT_EDIT_ONLY=true/false, default_value = false] -h, --help Print help -V, --version diff --git a/shreddit.env.example b/shreddit.env.example index ae68528..6e36662 100644 --- a/shreddit.env.example +++ b/shreddit.env.example @@ -7,4 +7,5 @@ SHREDDIT_THING_TYPES=posts,comments SHREDDIT_BEFORE='2023-01-01T00:00:00Z' SHREDDIT_MAX_SCORE=100 SHREDDIT_USER_AGENT='ShredditRustClient' +SHREDDIT_EDIT_ONLY=false SHREDDIT_GDPR_EXPORT_DIR='/home/you/Downloads/export_yourusername_20230101' diff --git a/src/cli.rs b/src/cli.rs index 84c0973..56dad48 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -2,6 +2,7 @@ use crate::things::ThingType; use chrono::{DateTime, Utc}; use clap::Parser; use std::path::PathBuf; +use tracing::{debug, warn}; #[derive(Debug, Parser)] #[clap(author, version, about)] @@ -52,4 +53,28 @@ pub struct Config { /// Reddit's APIs for discovering your data. #[clap(long, env = "SHREDDIT_GDPR_EXPORT_DIR")] pub gdpr_export_dir: Option, + + /// If specified, comments will only be edited, not deleted. - Requires gdpr_export + #[clap(long, env = "SHREDDIT_EDIT_ONLY")] + pub edit_only: bool, +} + +impl Config { + /// Return TRUE if either edit_only or dr_run + pub fn should_prevent_deletion(&self) -> bool { + if self.edit_only { + debug!( + "Skipping DELETION due to `edit_only` filter ({})", + self.edit_only + ); + if self.gdpr_export_dir.is_none() { + // As of this writing, there is an approx 1000 comment limit when pulling from JSON. Only reliable way to reach all data is via GDPR. + // See issue #35: https://github.com/andrewbanchich/shreddit/issues/35 + warn!("Because you are not using a GDPR export, not all data will be reached.\nFor info on how to use a GDPR export, see: {}", r##"https://github.com/andrewbanchich/shreddit#delete-all-your-data-using-gdpr-export"##); + } + } else if self.dry_run { + debug!("Skipping DELETION due to 'dry run' filter"); + } + return self.edit_only | self.dry_run; + } } diff --git a/src/things/comment.rs b/src/things/comment.rs index 1d6cd46..1157547 100644 --- a/src/things/comment.rs +++ b/src/things/comment.rs @@ -56,11 +56,7 @@ impl Shred for Comment { async fn delete(&self, client: &Client, access_token: &str, config: &Config) { info!("Deleting..."); - if self.should_skip(config) { - return; - } - - if config.dry_run { + if self.should_skip(config) || config.should_prevent_deletion() { return; } diff --git a/src/things/friend.rs b/src/things/friend.rs index 1b358dd..e06a07a 100644 --- a/src/things/friend.rs +++ b/src/things/friend.rs @@ -17,7 +17,7 @@ impl Shred for Friend { async fn delete(&self, client: &Client, access_token: &str, config: &Config) { info!("Deleting..."); - if config.dry_run { + if config.should_prevent_deletion() { return; } diff --git a/src/things/post.rs b/src/things/post.rs index 523d8e8..6504912 100644 --- a/src/things/post.rs +++ b/src/things/post.rs @@ -110,11 +110,7 @@ impl Shred for Post { async fn delete(&self, client: &Client, access_token: &str, config: &Config) { info!("Deleting..."); - if self.should_skip(config) { - return; - } - - if config.dry_run { + if self.should_skip(config) || config.should_prevent_deletion() { return; } diff --git a/src/things/saved_comment.rs b/src/things/saved_comment.rs index fba91a6..c059e49 100644 --- a/src/things/saved_comment.rs +++ b/src/things/saved_comment.rs @@ -37,7 +37,7 @@ impl Shred for SavedComment { // return; // } - if config.dry_run { + if config.should_prevent_deletion() { return; } diff --git a/src/things/saved_post.rs b/src/things/saved_post.rs index 45f45eb..65eb5e1 100644 --- a/src/things/saved_post.rs +++ b/src/things/saved_post.rs @@ -37,7 +37,7 @@ impl Shred for SavedPost { // return; // } - if config.dry_run { + if config.should_prevent_deletion() { return; }