From f8dafd94671063633380bc395666fb11661afa3c Mon Sep 17 00:00:00 2001 From: lint <47455468+lint@users.noreply.github.com> Date: Sat, 9 Nov 2019 16:20:52 -0500 Subject: [PATCH] Add more Reddit support (4.0 ->) for some reason --- control | 2 +- tweak/Reddit.h | 8 +++ tweak/Reddit.xm | 165 ++++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 161 insertions(+), 14 deletions(-) diff --git a/control b/control index c96ec87..c4dcab1 100644 --- a/control +++ b/control @@ -1,7 +1,7 @@ Package: com.lint.undelete Name: TFDidThatSay? Depends: mobilesubstrate -Version: 1.2.5 +Version: 1.2.6 Architecture: iphoneos-arm Description: See "[deleted]" comments and posts without leaving Reddit! Maintainer: lint diff --git a/tweak/Reddit.h b/tweak/Reddit.h index 831446e..4867f73 100644 --- a/tweak/Reddit.h +++ b/tweak/Reddit.h @@ -15,6 +15,7 @@ @end @interface CommentsViewController : NSObject +@property(strong,nonatomic) id postData; -(void) reloadCommentsWithNewCommentsHighlight:(BOOL) arg1 autoScroll:(BOOL) arg2 animated:(BOOL) arg3; -(void) reloadCommentsSection:(BOOL) arg1; -(void) reloadPostSection:(BOOL) arg1; @@ -128,6 +129,13 @@ -(id) initWithLeftIconImage:(id) arg1 text:(id) arg2 identifier:(id) arg3 context:(id) arg4; @end + +@interface ActionSheetItem : NSObject +// <= 4.17 +@property(strong,nonatomic) id leftIconImage; +-(id) initWithLeftIconImage:(id) arg1 text:(id) arg2 identifier:(id) arg3 context:(id) arg4; +@end + @interface RUITheme @property(strong,nonatomic) id bodyTextColor; @end diff --git a/tweak/Reddit.xm b/tweak/Reddit.xm index 05787bd..8eeaeb8 100644 --- a/tweak/Reddit.xm +++ b/tweak/Reddit.xm @@ -316,7 +316,6 @@ %end - %group Reddit_v4_ios10 %hook CommentsViewController @@ -328,7 +327,13 @@ %new -(void) updatePostText{ - [self reloadPostSection:YES]; + NSArray* appVersion = [[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"] componentsSeparatedByString:@"."]; + + if ([appVersion[1] integerValue] >= 2){ + [self reloadPostSection:YES]; + } else { + [self feedPostViewDidUpdatePost:[self postData] shouldReloadFeed:NO]; + } } %end @@ -343,14 +348,23 @@ CGFloat scale = origImage.size.width / existingImageSize.width; UIImage *newImage = [UIImage imageWithCGImage:[origImage CGImage] scale:scale orientation:origImage.imageOrientation]; - - id undeleteItem = [[%c(RUIActionSheetItem) alloc] initWithLeftIconImage:newImage text:@"TF did that say?" identifier:@"undeleteItemIdentifier" context:[self comment]]; + + id undeleteItem; + + NSArray* appVersion = [[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"] componentsSeparatedByString:@"."]; + + if ([appVersion[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]]; + } %orig([arg1 arrayByAddingObject:undeleteItem]); [undeleteItem release]; } +// >= 4.21 -(void) handleDidSelectActionSheetItem:(id) arg1{ %orig; @@ -401,6 +415,63 @@ }]; } } + +// <= 4.20 +-(void) actionSheetViewController:(id) arg1 didSelectItem:(id) arg2{ + %orig; + + if ([[arg2 identifier] isEqualToString:@"undeleteItemIdentifier"]){ + + [self dismissViewControllerAnimated:YES completion:nil]; + + Comment *comment = [self comment]; + + NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; + 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"]; + + [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { + + NSString *author = @"[author]"; + NSString *body = @"[body]"; + + if (data != nil && error == nil){ + id jsonData = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error]; + if ([[jsonData objectForKey:@"data"] count] != 0){ + author = [[jsonData objectForKey:@"data"][0] objectForKey:@"author"]; + body = [[jsonData objectForKey:@"data"][0] objectForKey:@"body"]; + if ([body isEqualToString:@"[deleted]"] || [body isEqualToString:@"[removed]"]){ + body = @"[pushshift was unable to archive this]"; + } + } else { + body = @"[pushshift has not archived this yet]"; + } + } else if (error != nil || data == nil){ + body = @"[an error occured]"; + } + + 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) { + [comment setBodyRichTextAttributed:bodyMutableAttributedText]; + } + + [[self commentActionSheetDelegate] performSelectorOnMainThread:@selector(updateComments) withObject:nil waitUntilDone:NO]; + + [request release]; + [queue release]; + [bodyMutableAttributedText release]; + }]; + } +} %end @@ -418,8 +489,16 @@ CGFloat scale = origImage.size.width / existingImageSize.width; UIImage *newImage = [UIImage imageWithCGImage:[origImage CGImage] scale:scale orientation:origImage.imageOrientation]; - - id undeleteItem = [[%c(RUIActionSheetItem) alloc] initWithLeftIconImage:newImage text:@"TF did that say?" identifier:@"undeleteItemIdentifier" context:[self post]]; + + id undeleteItem; + + NSArray* appVersion = [[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"] componentsSeparatedByString:@"."]; + + if ([appVersion[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]]; + } arg1 = [arg1 arrayByAddingObject:undeleteItem]; @@ -429,7 +508,7 @@ %orig; } - +// >= 4.21 -(void) handleDidSelectActionSheetItem:(id) arg1{ %orig; @@ -483,12 +562,77 @@ } } } + +// <= 4.20 +-(void) actionSheetViewController:(id) arg1 didSelectItem:(id) arg2{ + %orig; + + if ([[arg2 identifier] isEqualToString:@"undeleteItemIdentifier"]){ + + [self dismissViewControllerAnimated:YES completion:nil]; + + Post *post = [self post]; + + if ([post isSelfPost]){ + + NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; + 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"]; + + [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { + + NSString *author = @"[author]"; + NSString *body = @"[body]"; + + if (data != nil && error == nil){ + id jsonData = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error]; + if ([[jsonData objectForKey:@"data"] count] != 0){ + author = [[jsonData objectForKey:@"data"][0] objectForKey:@"author"]; + body = [[jsonData objectForKey:@"data"][0] objectForKey:@"selftext"]; + if ([body isEqualToString:@"[deleted]"] || [body isEqualToString:@"[removed]"]){ + body = @"[pushshift was unable to archive this]"; + } + } else { + body = @"[pushshift has not archived this yet]"; + } + } else if (error != nil || data == nil){ + body = @"[an error occured]"; + } + + 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) { + [post setSelfPostRichTextAttributed:bodyMutableAttributedText]; + } + + if ([appVersion[1] integerValue] >= 15) { + [post setPreviewFeedPostTextString:bodyMutableAttributedText]; + } + + [[self postActionSheetDelegate] performSelectorOnMainThread:@selector(updatePostText) withObject:nil waitUntilDone:NO]; + + [request release]; + [queue release]; + [bodyMutableAttributedText release]; + }]; + } + } +} %end %end - +//outdated and unchanged from first version of this tweak... +//TODO: move button to menu, add post support, make async requests once I feel like doing it %group Reddit_v3 %hook CommentView @@ -589,8 +733,3 @@ } } } - - - - -