From bb602d11215e2d9a1bef901dd0ca4986dd3dd6b8 Mon Sep 17 00:00:00 2001 From: lint <47455468+lint@users.noreply.github.com> Date: Sun, 10 Nov 2019 18:00:26 -0500 Subject: [PATCH] Add request timeout, switch to Cephei prefs --- Makefile | 2 + control | 4 +- prefs/Resources/Root.plist | 26 ++++++++++++ tweak/Apollo.xm | 25 +++++++---- tweak/Narwhal.xm | 22 ++++++---- tweak/Reddit.xm | 85 +++++++++++++++++++------------------- 6 files changed, 105 insertions(+), 59 deletions(-) diff --git a/Makefile b/Makefile index 19b277b..3403953 100644 --- a/Makefile +++ b/Makefile @@ -5,6 +5,8 @@ include $(THEOS)/makefiles/common.mk TWEAK_NAME = tfdidthatsay tfdidthatsay_FILES = $(wildcard tweak/*.xm) tweak/Narwhal.xm_CFLAGS = -fobjc-arc +tfdidthatsay_EXTRA_FRAMEWORKS += Cephei + include $(THEOS_MAKE_PATH)/tweak.mk diff --git a/control b/control index c4dcab1..9b9262a 100644 --- a/control +++ b/control @@ -1,7 +1,7 @@ Package: com.lint.undelete Name: TFDidThatSay? -Depends: mobilesubstrate -Version: 1.2.6 +Depends: mobilesubstrate, ws.hbang.common +Version: 1.2.7 Architecture: iphoneos-arm Description: See "[deleted]" comments and posts without leaving Reddit! Maintainer: lint diff --git a/prefs/Resources/Root.plist b/prefs/Resources/Root.plist index c39d9d3..8d91249 100644 --- a/prefs/Resources/Root.plist +++ b/prefs/Resources/Root.plist @@ -22,6 +22,32 @@ label Only display eye on deleted comments + + cell + PSGroupCell + footerText + The number of seconds before the request to pushshift will timeout and return an error. + isStaticText + + label + Request timeout (seconds) + + + cell + PSSliderCell + default + 10 + defaults + com.lint.undelete.prefs + key + requestTimeoutValue + max + 60 + min + 5 + showValue + + title TFDidThatSay? diff --git a/tweak/Apollo.xm b/tweak/Apollo.xm index b953be2..571e05e 100644 --- a/tweak/Apollo.xm +++ b/tweak/Apollo.xm @@ -1,9 +1,12 @@ +#import #import "Apollo.h" -%group Apollo +HBPreferences *apolloPrefs; +BOOL isApolloDeletedCommentsOnly; +CGFloat apolloRequestTimeoutValue; -const NSDictionary* settings = [[NSDictionary alloc] initWithContentsOfFile:@"/var/mobile/Library/Preferences/com.lint.undelete.prefs.plist"]; +%group Apollo NSDictionary* apolloBodyAttributes = nil; @@ -50,7 +53,8 @@ NSDictionary* apolloBodyAttributes = nil; NSOperationQueue *queue = [[NSOperationQueue alloc] init]; [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://api.pushshift.io/reddit/search/comment/?ids=%@&fields=author,body",[[comment fullName] componentsSeparatedByString:@"_"][1]]]]; - [request setHTTPMethod:@"GET"]; + [request setHTTPMethod:@"GET"]; + [request setTimeoutInterval:[apolloPrefs doubleForKey:@"requestTimeoutValue" default:10]]; [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { @@ -69,7 +73,7 @@ NSDictionary* apolloBodyAttributes = nil; body = @"[pushshift has not archived this yet]"; } } else if (error != nil || data == nil){ - body = @"[an error occured]"; + body = [NSString stringWithFormat:@"[an error occured while attempting to contact pushshift api (%@)]", [error localizedDescription]]; } id prevAuthorAttributedString = [authorTextNode attributedString]; @@ -93,9 +97,9 @@ NSDictionary* apolloBodyAttributes = nil; id commentBody = [MSHookIvar(self, "comment") body]; - id isDeletedOnly = [settings valueForKey:@"isApolloDeletedCommentsOnly"]; + BOOL isDeletedOnly = [apolloPrefs boolForKey:@"isApolloDeletedCommentsOnly"]; - if (([isDeletedOnly isEqual:@1] && ([commentBody isEqualToString:@"[deleted]"] || [commentBody isEqualToString:@"[removed]"])) || [isDeletedOnly isEqual:@0] ) { + if ((isDeletedOnly && ([commentBody isEqualToString:@"[deleted]"] || [commentBody isEqualToString:@"[removed]"])) || !isDeletedOnly) { CGFloat imageSize = 20.0f; @@ -152,7 +156,8 @@ NSDictionary* apolloBodyAttributes = nil; NSOperationQueue *queue = [[NSOperationQueue alloc] init]; [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://api.pushshift.io/reddit/search/submission/?ids=%@&fields=author,selftext",[[post fullName] componentsSeparatedByString:@"_"][1]]]]; - [request setHTTPMethod:@"GET"]; + [request setHTTPMethod:@"GET"]; + [request setTimeoutInterval:[apolloPrefs doubleForKey:@"requestTimeoutValue" default:10]]; [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { @@ -171,7 +176,7 @@ NSDictionary* apolloBodyAttributes = nil; body = @"[pushshift has not archived this yet]"; } } else if (error != nil || data == nil){ - body = @"[an error occured]"; + body = [NSString stringWithFormat:@"[an error occured while attempting to contact pushshift api (%@)]", [error localizedDescription]]; } //MSHookIvar(post, "_author") = author; //Crashes when clicking on author name. You will have to search the author name to go find the profile. @@ -235,6 +240,10 @@ NSDictionary* apolloBodyAttributes = nil; %ctor { + apolloPrefs = [[HBPreferences alloc] initWithIdentifier:@"com.lint.undelete.prefs"]; + [apolloPrefs registerBool:&isApolloDeletedCommentsOnly default:YES forKey:@"isApolloDeletedCommentsOnly"]; + [apolloPrefs registerDouble:&apolloRequestTimeoutValue default:10 forKey:@"requestTimeoutValue"]; + NSString* processName = [[NSProcessInfo processInfo] processName]; if ([processName isEqualToString:@"Apollo"]){ diff --git a/tweak/Narwhal.xm b/tweak/Narwhal.xm index 6a3657a..5234891 100644 --- a/tweak/Narwhal.xm +++ b/tweak/Narwhal.xm @@ -1,5 +1,9 @@ +#import #import "Narwhal.h" + +HBPreferences *narwhalPrefs; +CGFloat narwhalRequestTimeoutValue; %group Narwhal @@ -13,7 +17,8 @@ void getUndeleteCommentData(id controller, id comment){ NSOperationQueue *queue = [[NSOperationQueue alloc] init]; [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://api.pushshift.io/reddit/search/comment/?ids=%@&fields=author,body",[[comment fullName] componentsSeparatedByString:@"_"][1]]]]; - [request setHTTPMethod:@"GET"]; + [request setHTTPMethod:@"GET"]; + [request setTimeoutInterval:[narwhalPrefs doubleForKey:@"requestTimeoutValue" default:10]]; [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { @@ -32,7 +37,7 @@ void getUndeleteCommentData(id controller, id comment){ body = @"[pushshift has not archived this yet]"; } } else if (error != nil || data == nil){ - body = @"[an error occured]"; + body = [NSString stringWithFormat:@"[an error occured while attempting to contact pushshift api (%@)]", [error localizedDescription]]; } [controller performSelectorOnMainThread:@selector(completeUndeleteComment:) withObject:@{@"body":body, @"author":author, @"comment":comment} waitUntilDone:NO]; @@ -106,7 +111,8 @@ void getUndeleteCommentData(id controller, id comment){ NSOperationQueue *queue = [[NSOperationQueue alloc] init]; [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://api.pushshift.io/reddit/search/submission/?ids=%@&fields=author,selftext",[[post fullName] componentsSeparatedByString:@"_"][1]]]]; - [request setHTTPMethod:@"GET"]; + [request setHTTPMethod:@"GET"]; + [request setTimeoutInterval:[narwhalPrefs doubleForKey:@"requestTimeoutValue" default:10]]; [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { @@ -125,7 +131,7 @@ void getUndeleteCommentData(id controller, id comment){ body = @"[pushshift has not archived this yet]"; } } else if (error != nil || data == nil){ - body = @"[an error occured]"; + body = [NSString stringWithFormat:@"[an error occured while attempting to contact pushshift api (%@)]", [error localizedDescription]]; } [self performSelectorOnMainThread:@selector(completeUndeletePost:) withObject:@{@"body":body, @"author":author, @"post":post} waitUntilDone:NO]; @@ -142,7 +148,6 @@ void getUndeleteCommentData(id controller, id comment){ tfComment = [arg1 comment]; tfController = self; shouldHaveUndeleteAction = YES; - } } @@ -159,7 +164,6 @@ void getUndeleteCommentData(id controller, id comment){ tfController = self; tfComment = nil; shouldHaveUndeleteAction = YES; - } %orig; @@ -208,7 +212,11 @@ void getUndeleteCommentData(id controller, id comment){ %end -%ctor { +%ctor { + + narwhalPrefs = [[HBPreferences alloc] initWithIdentifier:@"com.lint.undelete.prefs"]; + [narwhalPrefs registerDouble:&narwhalRequestTimeoutValue default:10 forKey:@"requestTimeoutValue"]; + NSString* processName = [[NSProcessInfo processInfo] processName]; if ([processName isEqualToString:@"narwhal"]){ diff --git a/tweak/Reddit.xm b/tweak/Reddit.xm index 8eeaeb8..9140e0e 100644 --- a/tweak/Reddit.xm +++ b/tweak/Reddit.xm @@ -1,6 +1,12 @@ +#import #import "Reddit.h" +HBPreferences *redditPrefs; +CGFloat redditRequestTimeoutValue; + +NSArray *redditVersion; + %group Reddit_v4_current %hook CommentTreeNode @@ -71,7 +77,8 @@ NSOperationQueue *queue = [[NSOperationQueue alloc] init]; [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://api.pushshift.io/reddit/search/comment/?ids=%@&fields=author,body",[[comment pk] componentsSeparatedByString:@"_"][1]]]]; - [request setHTTPMethod:@"GET"]; + [request setHTTPMethod:@"GET"]; + [request setTimeoutInterval:[redditPrefs doubleForKey:@"requestTimeoutValue" default:10]]; [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { @@ -90,18 +97,16 @@ body = @"[pushshift has not archived this yet]"; } } else if (error != nil || data == nil){ - body = @"[an error occured]"; + body = [NSString stringWithFormat:@"[an error occured while attempting to contact pushshift api (%@)]", [error localizedDescription]]; } - NSArray* appVersion = [[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"] componentsSeparatedByString:@"."]; - NSMutableAttributedString *bodyMutableAttributedText; id themeManager; id isNightMode; id textColor; - if ([appVersion[1] integerValue] >= 45){ + if ([redditVersion[1] integerValue] >= 45){ themeManager = [[%c(ThemeManager) alloc] initWithAppSettings:[%c(AppSettings) sharedSettings]]; isNightMode = [[[%c(AccountManager) sharedManager] defaults] objectForKey:@"kUseNightKey"]; @@ -113,8 +118,7 @@ [themeManager release]; - - } else if ([appVersion[1] integerValue] >= 37){ + } else if ([redditVersion[1] integerValue] >= 37){ themeManager = [[%c(ThemeManager) alloc] initWithTraitCollection:nil appSettings:[%c(AppSettings) sharedSettings]]; isNightMode = [[[%c(AccountManager) sharedManager] defaults] objectForKey:@"kUseNightKey"]; @@ -218,7 +222,8 @@ NSOperationQueue *queue = [[NSOperationQueue alloc] init]; [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://api.pushshift.io/reddit/search/submission/?ids=%@&fields=author,selftext",[[post pk] componentsSeparatedByString:@"_"][1]]]]; - [request setHTTPMethod:@"GET"]; + [request setHTTPMethod:@"GET"]; + [request setTimeoutInterval:[redditPrefs doubleForKey:@"requestTimeoutValue" default:10]]; [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { @@ -237,16 +242,14 @@ body = @"[pushshift has not archived this yet]"; } } else if (error != nil || data == nil){ - body = @"[an error occured]"; + body = [NSString stringWithFormat:@"[an error occured while attempting to contact pushshift api (%@)]", [error localizedDescription]]; } - NSArray* appVersion = [[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"] componentsSeparatedByString:@"."]; - id themeManager; id isNightMode; id textColor; - if ([appVersion[1] integerValue] >= 45){ + if ([redditVersion[1] integerValue] >= 45){ themeManager = [[%c(ThemeManager) alloc] initWithAppSettings:[%c(AppSettings) sharedSettings]]; isNightMode = [[[%c(AccountManager) sharedManager] defaults] objectForKey:@"kUseNightKey"]; @@ -258,7 +261,7 @@ [themeManager release]; - } else if ([appVersion[1] integerValue] >= 37){ + } else if ([redditVersion[1] integerValue] >= 37){ themeManager = [[%c(ThemeManager) alloc] initWithTraitCollection:nil appSettings:[%c(AppSettings) sharedSettings]]; isNightMode = [[[%c(AccountManager) sharedManager] defaults] objectForKey:@"kUseNightKey"]; @@ -295,9 +298,9 @@ [post setSelfPostRichTextAttributed:bodyMutableAttributedText]; [post setPreviewFeedPostTextString:bodyMutableAttributedText]; - if ([appVersion[1] integerValue] >= 44){ + if ([redditVersion[1] integerValue] >= 44){ [[[[[self postActionSheetDelegate] controller] feedPostDetailCellNode] contentNode] configureSelfTextNode]; - } else if ([appVersion[1] integerValue] >= 38) { + } else if ([redditVersion[1] integerValue] >= 38) { [[[[self postActionSheetDelegate] controller] feedPostDetailCellNode] configureSelfTextNode]; } else { [[[[self postActionSheetDelegate] controller] feedPostDetailCellNode] configureSelfTextNode]; @@ -327,9 +330,8 @@ %new -(void) updatePostText{ - NSArray* appVersion = [[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"] componentsSeparatedByString:@"."]; - if ([appVersion[1] integerValue] >= 2){ + if ([redditVersion[1] integerValue] >= 2){ [self reloadPostSection:YES]; } else { [self feedPostViewDidUpdatePost:[self postData] shouldReloadFeed:NO]; @@ -351,9 +353,7 @@ id undeleteItem; - NSArray* appVersion = [[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"] componentsSeparatedByString:@"."]; - - if ([appVersion[1] integerValue] >= 18) { + if ([redditVersion[1] integerValue] >= 18) { undeleteItem = [[%c(RUIActionSheetItem) alloc] initWithLeftIconImage:newImage text:@"TF did that say?" identifier:@"undeleteItemIdentifier" context:[self comment]]; } else { undeleteItem = [[%c(ActionSheetItem) alloc] initWithLeftIconImage:newImage text:@"TF did that say?" identifier:@"undeleteItemIdentifier" context:[self comment]]; @@ -378,7 +378,8 @@ NSOperationQueue *queue = [[NSOperationQueue alloc] init]; [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://api.pushshift.io/reddit/search/comment/?ids=%@&fields=author,body",[[comment pk] componentsSeparatedByString:@"_"][1]]]]; - [request setHTTPMethod:@"GET"]; + [request setHTTPMethod:@"GET"]; + [request setTimeoutInterval:[redditPrefs doubleForKey:@"requestTimeoutValue" default:10]]; [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { @@ -397,7 +398,7 @@ body = @"[pushshift has not archived this yet]"; } } else if (error != nil || data == nil){ - body = @"[an error occured]"; + body = [NSString stringWithFormat:@"[an error occured while attempting to contact pushshift api (%@)]", [error localizedDescription]]; } NSMutableAttributedString *bodyMutableAttributedText = [[NSMutableAttributedString alloc] initWithAttributedString:[%c(NSAttributedStringMarkdownParser) attributedStringUsingCurrentConfig:body]]; @@ -430,7 +431,8 @@ NSOperationQueue *queue = [[NSOperationQueue alloc] init]; [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://api.pushshift.io/reddit/search/comment/?ids=%@&fields=author,body",[[comment pk] componentsSeparatedByString:@"_"][1]]]]; - [request setHTTPMethod:@"GET"]; + [request setHTTPMethod:@"GET"]; + [request setTimeoutInterval:[redditPrefs doubleForKey:@"requestTimeoutValue" default:10]]; [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { @@ -449,18 +451,16 @@ body = @"[pushshift has not archived this yet]"; } } else if (error != nil || data == nil){ - body = @"[an error occured]"; + body = [NSString stringWithFormat:@"[an error occured while attempting to contact pushshift api (%@)]", [error localizedDescription]]; } - NSArray* appVersion = [[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"] componentsSeparatedByString:@"."]; - NSMutableAttributedString *bodyMutableAttributedText = [[NSMutableAttributedString alloc] initWithAttributedString:[%c(NSAttributedStringMarkdownParser) attributedStringUsingCurrentConfig:body]]; [comment setAuthor:author]; [comment setBodyText:body]; [comment setBodyAttributedText:bodyMutableAttributedText]; - if ([appVersion[1] integerValue] >= 12) { + if ([redditVersion[1] integerValue] >= 12) { [comment setBodyRichTextAttributed:bodyMutableAttributedText]; } @@ -491,10 +491,8 @@ UIImage *newImage = [UIImage imageWithCGImage:[origImage CGImage] scale:scale orientation:origImage.imageOrientation]; id undeleteItem; - - NSArray* appVersion = [[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"] componentsSeparatedByString:@"."]; - if ([appVersion[1] integerValue] >= 18) { + if ([redditVersion[1] integerValue] >= 18) { undeleteItem = [[%c(RUIActionSheetItem) alloc] initWithLeftIconImage:newImage text:@"TF did that say?" identifier:@"undeleteItemIdentifier" context:[self post]]; } else { undeleteItem = [[%c(ActionSheetItem) alloc] initWithLeftIconImage:newImage text:@"TF did that say?" identifier:@"undeleteItemIdentifier" context:[self post]]; @@ -524,7 +522,8 @@ NSOperationQueue *queue = [[NSOperationQueue alloc] init]; [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://api.pushshift.io/reddit/search/submission/?ids=%@&fields=author,selftext",[[post pk] componentsSeparatedByString:@"_"][1]]]]; - [request setHTTPMethod:@"GET"]; + [request setHTTPMethod:@"GET"]; + [request setTimeoutInterval:[redditPrefs doubleForKey:@"requestTimeoutValue" default:10]]; [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { @@ -543,7 +542,7 @@ body = @"[pushshift has not archived this yet]"; } } else if (error != nil || data == nil){ - body = @"[an error occured]"; + body = [NSString stringWithFormat:@"[an error occured while attempting to contact pushshift api (%@)]", [error localizedDescription]]; } NSMutableAttributedString *bodyMutableAttributedText = [[NSMutableAttributedString alloc] initWithAttributedString:[%c(NSAttributedStringMarkdownParser) attributedStringUsingCurrentConfig:body]]; @@ -579,7 +578,8 @@ NSOperationQueue *queue = [[NSOperationQueue alloc] init]; [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://api.pushshift.io/reddit/search/submission/?ids=%@&fields=author,selftext",[[post pk] componentsSeparatedByString:@"_"][1]]]]; - [request setHTTPMethod:@"GET"]; + [request setHTTPMethod:@"GET"]; + [request setTimeoutInterval:[redditPrefs doubleForKey:@"requestTimeoutValue" default:10]]; [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { @@ -598,22 +598,20 @@ body = @"[pushshift has not archived this yet]"; } } else if (error != nil || data == nil){ - body = @"[an error occured]"; + body = [NSString stringWithFormat:@"[an error occured while attempting to contact pushshift api (%@)]", [error localizedDescription]]; } - NSArray* appVersion = [[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"] componentsSeparatedByString:@"."]; - NSMutableAttributedString *bodyMutableAttributedText = [[NSMutableAttributedString alloc] initWithAttributedString:[%c(NSAttributedStringMarkdownParser) attributedStringUsingCurrentConfig:body]]; [post setAuthor:author]; [post setSelfText:body]; [post setSelfTextAttributed:bodyMutableAttributedText]; - if ([appVersion[1] integerValue] >= 8) { + if ([redditVersion[1] integerValue] >= 8) { [post setSelfPostRichTextAttributed:bodyMutableAttributedText]; } - if ([appVersion[1] integerValue] >= 15) { + if ([redditVersion[1] integerValue] >= 15) { [post setPreviewFeedPostTextString:bodyMutableAttributedText]; } @@ -718,17 +716,20 @@ %ctor{ + redditPrefs = [[HBPreferences alloc] initWithIdentifier:@"com.lint.undelete.prefs"]; + [redditPrefs registerDouble:&redditRequestTimeoutValue default:10 forKey:@"requestTimeoutValue"]; + NSString* processName = [[NSProcessInfo processInfo] processName]; - NSArray* appVersion = [[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"] componentsSeparatedByString:@"."]; + redditVersion = [[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"] componentsSeparatedByString:@"."]; if ([processName isEqualToString:@"Reddit"]){ - if ([appVersion[0] isEqualToString:@"4"]){ - if ([appVersion[1] integerValue] <= 32){ + if ([redditVersion[0] isEqualToString:@"4"]){ + if ([redditVersion[1] integerValue] <= 32){ %init(Reddit_v4_ios10); } else{ %init(Reddit_v4_current); } - } else if ([appVersion[0] isEqualToString:@"3"]) { + } else if ([redditVersion[0] isEqualToString:@"3"]) { %init(Reddit_v3); } }