diff --git a/Makefile b/Makefile index a67c768..cbdf696 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ ARCHS = armv7 armv7s arm64 arm64e include $(THEOS)/makefiles/common.mk TWEAK_NAME = tfdidthatsay -tfdidthatsay_FILES = $(wildcard tweak/*.xm tweak/assets/*.m) +tfdidthatsay_FILES = $(wildcard tweak/*.xm tweak/assets/*.m tweak/assets/MMMarkdown/*.m) tfdidthatsay_CFLAGS = -fobjc-arc tweak/Reddit.xm_CFLAGS = -fno-objc-arc tweak/Apollo.xm_CFLAGS = -fno-objc-arc diff --git a/tweak/AlienBlue.xm b/tweak/AlienBlue.xm index aa00663..f3c68d7 100644 --- a/tweak/AlienBlue.xm +++ b/tweak/AlienBlue.xm @@ -1,6 +1,7 @@ #import "AlienBlue.h" -#import "assets/MMMarkdown.h" +#import "assets/TFHelper.h" +#import "assets/MMMarkdown/MMMarkdown.h" static BOOL isAlienBlueEnabled; static BOOL isTFDeletedOnly; @@ -56,43 +57,25 @@ static CGFloat pushshiftRequestTimeoutValue; id comment = [[self node] comment]; - NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; - NSOperationQueue *queue = [[NSOperationQueue alloc] init]; + [%c(TFHelper) getUndeleteDataWithID:[comment ident] isComment:YES timeout:pushshiftRequestTimeoutValue extraData:@{@"sender" : sender} completionTarget:self completionSelector:@selector(completeUndeleteCommentAction:)]; +} - [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://api.pushshift.io/reddit/search/comment/?ids=%@&fields=author,body",[comment ident]]]]; - [request setHTTPMethod:@"GET"]; - [request setTimeoutInterval:pushshiftRequestTimeoutValue]; - - [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { +%new +-(void) completeUndeleteCommentAction:(NSDictionary *) data{ - 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 = [NSString stringWithFormat:@"[an error occured while attempting to contact pushshift api (%@)]", [error localizedDescription]]; - } - - NSString *bodyHTML = [%c(MMMarkdown) HTMLStringWithMarkdown:body extensions:MMMarkdownExtensionsGitHubFlavored error:nil]; - - [comment setAuthor:author]; - [comment setBody:body]; - [comment setBodyHTML:bodyHTML]; - - [[self delegate] performSelectorOnMainThread:@selector(respondToStyleChange) withObject:nil waitUntilDone:NO]; - - [sender setEnabled:YES]; - }]; + id comment = [[self node] comment]; + + NSString *body = data[@"body"]; + + NSString *bodyHTML = [%c(MMMarkdown) HTMLStringWithMarkdown:body extensions:MMMarkdownExtensionsGitHubFlavored error:nil]; + + [comment setAuthor:data[@"author"]]; + [comment setBody:body]; + [comment setBodyHTML:bodyHTML]; + + [[self delegate] respondToStyleChange]; + + [data[@"sender"] setEnabled:YES]; } %new @@ -100,46 +83,28 @@ static CGFloat pushshiftRequestTimeoutValue; [sender setEnabled:NO]; + id post = [[self node] post]; + + [%c(TFHelper) getUndeleteDataWithID:[post ident] isComment:NO timeout:pushshiftRequestTimeoutValue extraData:@{@"sender" : sender} completionTarget:self completionSelector:@selector(completeUndeletePostAction:)]; +} + +%new +-(void) completeUndeletePostAction:(NSDictionary *) data{ + id post = [[self node] post]; id postComment = [[self node] comment]; //Don't know why he used a comment to store info about a post, but it exists - 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 ident]]]]; - [request setHTTPMethod:@"GET"]; - [request setTimeoutInterval:pushshiftRequestTimeoutValue]; - - [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 = [NSString stringWithFormat:@"[an error occured while attempting to contact pushshift api (%@)]", [error localizedDescription]]; - } - - NSString *bodyHTML = [%c(MMMarkdown) HTMLStringWithMarkdown:body extensions:MMMarkdownExtensionsGitHubFlavored error:nil]; - - [post setAuthor:author]; - [post setSelftext:body]; - [postComment setBodyHTML:bodyHTML]; - - [[self delegate] performSelectorOnMainThread:@selector(respondToStyleChange) withObject:nil waitUntilDone:NO]; - - [sender setEnabled:YES]; - }]; + NSString *body = data[@"body"]; + + NSString *bodyHTML = [%c(MMMarkdown) HTMLStringWithMarkdown:body extensions:MMMarkdownExtensionsGitHubFlavored error:nil]; + + [post setAuthor:data[@"author"]]; + [post setSelftext:body]; + [postComment setBodyHTML:bodyHTML]; + + [[self delegate] respondToStyleChange]; + + [data[@"sender"] setEnabled:YES]; } %end @@ -196,4 +161,3 @@ static void prefsChanged(CFNotificationCenterRef center, void *observer, CFStrin } } } - diff --git a/tweak/Antenna.xm b/tweak/Antenna.xm index 1e77ec3..c8c69a6 100644 --- a/tweak/Antenna.xm +++ b/tweak/Antenna.xm @@ -1,6 +1,7 @@ #import "Antenna.h" -#import "assets/MMMarkdown.h" +#import "assets/TFHelper.h" +#import "assets/MMMarkdown/MMMarkdown.h" static BOOL isAntennaEnabled; static BOOL isTFDeletedOnly; @@ -48,48 +49,28 @@ id tfAntennaCommentCell; %new -(void) handleUndeleteCommentAction{ + [%c(TFHelper) getUndeleteDataWithID:[[tfAntennaCommentCell comment] itemId] isComment:YES timeout:pushshiftRequestTimeoutValue extraData:nil completionTarget:self completionSelector:@selector(completeUndeleteCommentAction:)]; +} + +%new +-(void) completeUndeleteCommentAction:(NSDictionary *) data{ + id comment = [tfAntennaCommentCell comment]; id commentText = [comment commentText]; - 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 itemId]]]]; - [request setHTTPMethod:@"GET"]; - [request setTimeoutInterval:pushshiftRequestTimeoutValue]; - - [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { + NSString *body = data[@"body"]; - 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 = [NSString stringWithFormat:@"[an error occured while attempting to contact pushshift api (%@)]", [error localizedDescription]]; - } - - [comment setAuthor:author]; - [commentText setBody:body]; - [commentText setBodyHTML:[%c(MMMarkdown) HTMLStringWithMarkdown:body extensions:MMMarkdownExtensionsGitHubFlavored error:nil]]; - - [commentText setBodyAttributedString:nil]; - [commentText setBodyAttributedStringForPreview:nil]; - [commentText setTextHeightCache:nil]; - [self setCommentHeightCache:nil]; - - [tfAntennaCommentCell performSelectorOnMainThread:@selector(updateWithModelObject:) withObject:comment waitUntilDone:YES]; - [[[self delegate] tableView] performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO]; - }]; + [comment setAuthor:data[@"author"]]; + [commentText setBody:body]; + [commentText setBodyHTML:[%c(MMMarkdown) HTMLStringWithMarkdown:body extensions:MMMarkdownExtensionsGitHubFlavored error:nil]]; + + [commentText setBodyAttributedString:nil]; + [commentText setBodyAttributedStringForPreview:nil]; + [commentText setTextHeightCache:nil]; + [self setCommentHeightCache:nil]; + + [tfAntennaCommentCell updateWithModelObject:comment]; + [[[self delegate] tableView] reloadData]; } %end @@ -178,49 +159,29 @@ id tfAntennaCommentCell; [sender setEnabled:NO]; + [%c(TFHelper) getUndeleteDataWithID:[[self post] itemId] isComment:NO timeout:pushshiftRequestTimeoutValue extraData:@{@"sender" : sender} completionTarget:self completionSelector:@selector(completeUndeletePostAction:)]; +} + +%new +-(void) completeUndeletePostAction:(NSDictionary *) data{ + id post = [self post]; id postText = [post selfCommentText]; - 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 itemId]]]]; - [request setHTTPMethod:@"GET"]; - [request setTimeoutInterval:pushshiftRequestTimeoutValue]; - - [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 = [NSString stringWithFormat:@"[an error occured while attempting to contact pushshift api (%@)]", [error localizedDescription]]; - } - - [post setAuthor:author]; - [postText setBody:body]; - [postText setBodyHTML:[%c(MMMarkdown) HTMLStringWithMarkdown:body extensions:MMMarkdownExtensionsGitHubFlavored error:nil]]; - - [postText setBodyAttributedString:nil]; - [postText setBodyAttributedStringForPreview:nil]; - [postText setTextHeightCache:nil]; - - [self performSelectorOnMainThread:@selector(loadView) withObject:nil waitUntilDone:YES]; - [[self tableView] performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO]; - - [sender setEnabled:YES]; - }]; + NSString *body = data[@"body"]; + + [post setAuthor:data[@"author"]]; + [postText setBody:body]; + [postText setBodyHTML:[%c(MMMarkdown) HTMLStringWithMarkdown:body extensions:MMMarkdownExtensionsGitHubFlavored error:nil]]; + + [postText setBodyAttributedString:nil]; + [postText setBodyAttributedStringForPreview:nil]; + [postText setTextHeightCache:nil]; + + [self loadView]; + [[self tableView] reloadData]; + + [data[@"sender"] setEnabled:YES]; } %end diff --git a/tweak/Apollo.xm b/tweak/Apollo.xm index b183996..aafe372 100644 --- a/tweak/Apollo.xm +++ b/tweak/Apollo.xm @@ -1,5 +1,6 @@ #import "Apollo.h" +#import "assets/TFHelper.h" static BOOL isTFDeletedOnly; static BOOL isApolloEnabled; @@ -38,59 +39,6 @@ NSDictionary* apolloBodyAttributes = nil; %hook ApolloCommentCellNode %property(strong,nonatomic) id undeleteButton; -%new --(void) didTapUndeleteButton:(id) sender{ - - [sender setEnabled:NO]; - - id bodyNode = MSHookIvar(self, "bodyNode"); - id authorNode = MSHookIvar(self, "authorNode"); - id authorTextNode = [authorNode subnodes][0]; - id comment = MSHookIvar(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 fullName] componentsSeparatedByString:@"_"][1]]]]; - [request setHTTPMethod:@"GET"]; - [request setTimeoutInterval:pushshiftRequestTimeoutValue]; - - [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 = [NSString stringWithFormat:@"[an error occured while attempting to contact pushshift api (%@)]", [error localizedDescription]]; - } - - id prevAuthorAttributedString = [authorTextNode attributedString]; - NSDictionary *authorStringAttributes = [prevAuthorAttributedString attributesAtIndex:0 longestEffectiveRange:nil inRange:NSMakeRange(0, [prevAuthorAttributedString length])]; - NSAttributedString* newAuthorAttributedString = [[NSAttributedString alloc] initWithString:author attributes:authorStringAttributes]; - - [authorTextNode setAttributedText:newAuthorAttributedString]; - [authorTextNode setAttributedString:newAuthorAttributedString]; - - [comment setAuthor:author]; - - [bodyNode setAttributedString:[%c(MarkdownRenderer) attributedStringFromMarkdown:body withAttributes:apolloBodyAttributes]]; - - [sender setEnabled:YES]; - - }]; -} - -(void) didLoad { %orig; @@ -128,71 +76,49 @@ NSDictionary* apolloBodyAttributes = nil; CGFloat nodeSpacing =[ageNode frame].origin.x - nodeFrame.origin.x - nodeFrame.size.width; [[self undeleteButton] setFrame:CGRectMake(nodeFrame.origin.x - imageSize - nodeSpacing, centerHeight - (imageSize / 2), imageSize, imageSize)]; - } } +%new +-(void) didTapUndeleteButton:(id) sender{ + + [sender setEnabled:NO]; + + id comment = MSHookIvar(self, "comment"); + + [%c(TFHelper) getUndeleteDataWithID:[[comment fullName] componentsSeparatedByString:@"_"][1] isComment:YES timeout:pushshiftRequestTimeoutValue extraData:@{@"sender" : sender} completionTarget:self completionSelector:@selector(completeUndeleteCommentAction:)]; +} + +%new +-(void) completeUndeleteCommentAction:(NSDictionary *) data{ + + id comment = MSHookIvar(self, "comment"); + id bodyNode = MSHookIvar(self, "bodyNode"); + id authorNode = MSHookIvar(self, "authorNode"); + id authorTextNode = [authorNode subnodes][0]; + + NSString *author = data[@"author"]; + + id prevAuthorAttributedString = [authorTextNode attributedString]; + NSDictionary *authorStringAttributes = [prevAuthorAttributedString attributesAtIndex:0 longestEffectiveRange:nil inRange:NSMakeRange(0, [prevAuthorAttributedString length])]; + NSAttributedString *newAuthorAttributedString = [[NSAttributedString alloc] initWithString:author attributes:authorStringAttributes]; + + [authorTextNode setAttributedText:newAuthorAttributedString]; + [authorTextNode setAttributedString:newAuthorAttributedString]; + + [comment setAuthor:author]; + + [bodyNode setAttributedString:[%c(MarkdownRenderer) attributedStringFromMarkdown:data[@"body"] withAttributes:apolloBodyAttributes]]; + + [data[@"sender"] setEnabled:YES]; +} + %end %hook ApolloCommentsHeaderCellNode %property(strong, nonatomic) id undeleteButton; -%new --(void) didTapUndeleteButton:(id) sender{ - - [sender setEnabled:NO]; - - id bodyNode = MSHookIvar(self, "bodyNode"); - id postInfoNode = MSHookIvar(self, "postInfoNode"); - id authorNode = MSHookIvar(postInfoNode, "authorButtonNode"); - id authorTextNode = [authorNode subnodes][0]; - id post = MSHookIvar(self, "link"); - - 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 fullName] componentsSeparatedByString:@"_"][1]]]]; - [request setHTTPMethod:@"GET"]; - [request setTimeoutInterval:pushshiftRequestTimeoutValue]; - - [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 = [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. - - author = [NSString stringWithFormat:@"by %@", author]; - - id prevAuthorAttributedString = [authorTextNode attributedString]; - NSDictionary *authorStringAttributes = [prevAuthorAttributedString attributesAtIndex:0 longestEffectiveRange:nil inRange:NSMakeRange(0, [prevAuthorAttributedString length])]; - NSAttributedString* newAuthorAttributedString = [[NSAttributedString alloc] initWithString:author attributes:authorStringAttributes]; - - [authorTextNode setAttributedText:newAuthorAttributedString]; - [authorTextNode setAttributedString:newAuthorAttributedString]; - - [bodyNode setAttributedString:[%c(MarkdownRenderer) attributedStringFromMarkdown:body withAttributes:apolloBodyAttributes]]; - - [sender setEnabled:YES]; - }]; -} - -(void) didLoad{ %orig; @@ -234,6 +160,43 @@ NSDictionary* apolloBodyAttributes = nil; } } +%new +-(void) didTapUndeleteButton:(id) sender{ + + [sender setEnabled:NO]; + + id post = MSHookIvar(self, "link"); + + [%c(TFHelper) getUndeleteDataWithID:[[post fullName] componentsSeparatedByString:@"_"][1] isComment:NO timeout:pushshiftRequestTimeoutValue extraData:@{@"sender" : sender} completionTarget:self completionSelector:@selector(completeUndeletePostAction:)]; +} + +%new +-(void) completeUndeletePostAction:(NSDictionary *) data{ + + id bodyNode = MSHookIvar(self, "bodyNode"); + id postInfoNode = MSHookIvar(self, "postInfoNode"); + id authorNode = MSHookIvar(postInfoNode, "authorButtonNode"); + id authorTextNode = [authorNode subnodes][0]; + + NSString *author = data[@"author"]; + + //id post = MSHookIvar(self, "link"); + //MSHookIvar(post, "_author") = author; //Crashes when clicking on author name. You will have to search the author name to go find the profile. + + author = [NSString stringWithFormat:@"by %@", author]; + + id prevAuthorAttributedString = [authorTextNode attributedString]; + NSDictionary *authorStringAttributes = [prevAuthorAttributedString attributesAtIndex:0 longestEffectiveRange:nil inRange:NSMakeRange(0, [prevAuthorAttributedString length])]; + NSAttributedString* newAuthorAttributedString = [[NSAttributedString alloc] initWithString:author attributes:authorStringAttributes]; + + [authorTextNode setAttributedText:newAuthorAttributedString]; + [authorTextNode setAttributedString:newAuthorAttributedString]; + + [bodyNode setAttributedString:[%c(MarkdownRenderer) attributedStringFromMarkdown:data[@"body"] withAttributes:apolloBodyAttributes]]; + + [data[@"sender"] setEnabled:YES]; +} + %end %end diff --git a/tweak/BaconReader.xm b/tweak/BaconReader.xm index 2666c38..ed78949 100644 --- a/tweak/BaconReader.xm +++ b/tweak/BaconReader.xm @@ -1,6 +1,7 @@ #import "BaconReader.h" -#import "assets/MMMarkdown.h" +#import "assets/TFHelper.h" +#import "assets/MMMarkdown/MMMarkdown.h" static BOOL isBaconReaderEnabled; static BOOL isTFDeletedOnly; @@ -126,85 +127,40 @@ id tfStoryController; %new -(void) handleUndeleteCommentAction{ + [%c(TFHelper) getUndeleteDataWithID:[[tfCommentCellView comment] serverID] isComment:YES timeout:pushshiftRequestTimeoutValue extraData:nil completionTarget:self completionSelector:@selector(completeUndeleteCommentAction:)]; +} + +%new +-(void) completeUndeleteCommentAction:(NSDictionary *) data{ + id comment = [tfCommentCellView 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 serverID]]]]; - [request setHTTPMethod:@"GET"]; - [request setTimeoutInterval:pushshiftRequestTimeoutValue]; - - [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { + NSString *body = data[@"body"]; - 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 = [NSString stringWithFormat:@"[an error occured while attempting to contact pushshift api (%@)]", [error localizedDescription]]; - } - - [comment setAuthor:author]; - [comment setBody:body]; - [comment setBody_html:[%c(MMMarkdown) HTMLStringWithMarkdown:body extensions:MMMarkdownExtensionsGitHubFlavored error:nil]]; - [comment setAttributedDescriptionString:nil]; - - NSAttributedString *commentAttrString = [%c(BRUtils) attributedDescriptionForComment:comment]; - [comment setAttributedDescriptionString:commentAttrString]; - - [[[self detailPage] tableView] performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO]; - }]; + [comment setAuthor:data[@"author"]]; + [comment setBody:body]; + [comment setBody_html:[%c(MMMarkdown) HTMLStringWithMarkdown:body extensions:MMMarkdownExtensionsGitHubFlavored error:nil]]; + [comment setAttributedDescriptionString:nil]; + + NSAttributedString *commentAttrString = [%c(BRUtils) attributedDescriptionForComment:comment]; + [comment setAttributedDescriptionString:commentAttrString]; + + [[[self detailPage] tableView] reloadData]; } %new -(void) handleUndeletePostAction{ - id post = [self story]; + [%c(TFHelper) getUndeleteDataWithID:[[self story] serverID] isComment:NO timeout:pushshiftRequestTimeoutValue extraData:nil completionTarget:self completionSelector:@selector(completeUndeletePostAction:)]; +} + +%new +-(void) completeUndeletePostAction:(NSDictionary *) data{ - 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 serverID]]]]; - [request setHTTPMethod:@"GET"]; - [request setTimeoutInterval:pushshiftRequestTimeoutValue]; - - [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 = [NSString stringWithFormat:@"[an error occured while attempting to contact pushshift api (%@)]", [error localizedDescription]]; - } - - tfPostAuthor = author; - tfPostSelftext = [%c(MMMarkdown) HTMLStringWithMarkdown:body extensions:MMMarkdownExtensionsGitHubFlavored error:nil]; + tfPostAuthor = data[@"author"]; + tfPostSelftext = [%c(MMMarkdown) HTMLStringWithMarkdown:data[@"body"] extensions:MMMarkdownExtensionsGitHubFlavored error:nil]; - [[self detailPage] performSelectorOnMainThread:@selector(refreshTouched) withObject:nil waitUntilDone:NO]; - - }]; + [[self detailPage] refreshTouched]; } %end diff --git a/tweak/Beam.xm b/tweak/Beam.xm index 42475d3..7737200 100644 --- a/tweak/Beam.xm +++ b/tweak/Beam.xm @@ -1,5 +1,6 @@ #import "Beam.h" +#import "assets/TFHelper.h" static BOOL isBeamEnabled; static BOOL isTFDeletedOnly; @@ -25,8 +26,6 @@ static CGFloat pushshiftRequestTimeoutValue; NSString *commentBody = [[self comment] content]; - HBLogDebug(@"body: %@", commentBody); - if ((isTFDeletedOnly && ([commentBody isEqualToString:@"[deleted]"] || [commentBody isEqualToString:@"[removed]"])) || !isTFDeletedOnly){ CGFloat authorTextHeight = [[self authorButton] frame].size.height; @@ -51,45 +50,23 @@ static CGFloat pushshiftRequestTimeoutValue; [sender setEnabled:NO]; + [%c(TFHelper) getUndeleteDataWithID:[[self comment] identifier] isComment:YES timeout:pushshiftRequestTimeoutValue extraData:@{@"sender" : sender} completionTarget:self completionSelector:@selector(completeUndeleteCommentAction:)]; +} + +%new +-(void) completeUndeleteCommentAction:(NSDictionary *) data{ + id 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 identifier]]]]; - [request setHTTPMethod:@"GET"]; - [request setTimeoutInterval:pushshiftRequestTimeoutValue]; - - [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 = [NSString stringWithFormat:@"[an error occured while attempting to contact pushshift api (%@)]", [error localizedDescription]]; - } - - [comment setAuthor:author]; - [comment setContent:body]; - [comment setMarkdownString:nil]; - - [self setCommentDidChange:YES]; - [self performSelectorOnMainThread:@selector(reloadContents) withObject:nil waitUntilDone:YES]; - [[MSHookIvar(self, "delegate") tableView] performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO]; - - [sender setEnabled:YES]; - }]; + [comment setAuthor:data[@"author"]]; + [comment setContent:data[@"body"]]; + [comment setMarkdownString:nil]; + + [self setCommentDidChange:YES]; + [self reloadContents]; + [[MSHookIvar(self, "delegate") tableView] reloadData]; + + [data[@"sender"] setEnabled:YES]; } %end @@ -173,53 +150,33 @@ static CGFloat pushshiftRequestTimeoutValue; id post = [self post]; if (post){ - - 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 identifier]]]]; - [request setHTTPMethod:@"GET"]; - [request setTimeoutInterval:pushshiftRequestTimeoutValue]; - - [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 = [NSString stringWithFormat:@"[an error occured while attempting to contact pushshift api (%@)]", [error localizedDescription]]; - } - - [post setAuthor:author]; - [post setContent:body]; - [post setMarkdownString:nil]; - - if ([self selfTextView]){ - [[self selfTextView] performSelectorOnMainThread:@selector(reloadContents) withObject:nil waitUntilDone:YES]; - } - - if ([self metadataView]){ - [[self metadataView] performSelectorOnMainThread:@selector(setPost:) withObject:post waitUntilDone:YES]; - } - - [[self tableView] performSelectorOnMainThread:@selector(reloadData) withObject:post waitUntilDone:NO]; - - [sender setEnabled:YES]; - }]; + + [%c(TFHelper) getUndeleteDataWithID:[post identifier] isComment:NO timeout:pushshiftRequestTimeoutValue extraData:@{@"sender" : sender} completionTarget:self completionSelector:@selector(completeUndeletePostAction:)]; } } +%new +-(void) completeUndeletePostAction:(NSDictionary *) data{ + + id post = [self post]; + + [post setAuthor:data[@"author"]]; + [post setContent:data[@"body"]]; + [post setMarkdownString:nil]; + + if ([self selfTextView]){ + [[self selfTextView] reloadContents]; + } + + if ([self metadataView]){ + [[self metadataView] setPost:post]; + } + + [[self tableView] reloadData]; + + [data[@"sender"] setEnabled:YES]; +} + %end %end @@ -274,4 +231,3 @@ static void prefsChanged(CFNotificationCenterRef center, void *observer, CFStrin } } } - diff --git a/tweak/Narwhal.xm b/tweak/Narwhal.xm index 9a3b76a..4c59aa2 100644 --- a/tweak/Narwhal.xm +++ b/tweak/Narwhal.xm @@ -1,5 +1,6 @@ #import "Narwhal.h" +#import "assets/TFHelper.h" static BOOL isNarwhalEnabled; static BOOL isTFDeletedOnly; @@ -11,38 +12,8 @@ BOOL shouldHaveUndeleteAction = NO; id tfComment; id tfController; -void getUndeleteCommentData(id controller, id 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 fullName] componentsSeparatedByString:@"_"][1]]]]; - [request setHTTPMethod:@"GET"]; - [request setTimeoutInterval:pushshiftRequestTimeoutValue]; - - [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 = [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]; - }]; - +void getUndeleteCommentData(id controller, id comment){ + [%c(TFHelper) getUndeleteDataWithID:[[comment fullName] componentsSeparatedByString:@"_"][1] isComment:YES timeout:pushshiftRequestTimeoutValue extraData:@{@"comment" : comment} completionTarget:controller completionSelector:@selector(completeUndeleteCommentAction:)]; } @@ -61,7 +32,6 @@ void getUndeleteCommentData(id controller, id comment){ } [arg1 addAction:undeleteAction]; - } %orig; @@ -70,71 +40,7 @@ void getUndeleteCommentData(id controller, id comment){ %end -%hook NRTLinkViewController - -%new --(void) completeUndeleteComment:(id) data{ - - id comment = data[@"comment"]; - - if (comment){ - - MSHookIvar(comment, "_author") = data[@"author"]; - MSHookIvar(comment, "_body") = data[@"body"]; - - [[self commentsManager] updateComment:comment fromEdited:comment]; - } -} - -%new --(void) completeUndeletePost:(id) data{ - - id post = data[@"post"]; - - MSHookIvar(post, "_author") = data[@"author"]; - - NSAttributedString* postBodyAttributedString = [%c(NRTMarkdownManager) attributedStringFromMarkdown:data[@"body"] type:0]; - [self setLinkText:postBodyAttributedString]; - - [[self tableView] reloadData]; -} - -%new --(void) handleUndeletePostAction{ - - id post = [self link]; - - 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 fullName] componentsSeparatedByString:@"_"][1]]]]; - [request setHTTPMethod:@"GET"]; - [request setTimeoutInterval:pushshiftRequestTimeoutValue]; - - [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 = [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]; - - }]; -} +%hook NRTLinkViewController -(void) swipeCell:(id) arg1 didEndDragWithState:(NSUInteger) arg2{ @@ -174,24 +80,45 @@ void getUndeleteCommentData(id controller, id comment){ shouldHaveUndeleteAction = NO; } +%new +-(void) handleUndeletePostAction{ + + id post = [self link]; + + [%c(TFHelper) getUndeleteDataWithID:[[post fullName] componentsSeparatedByString:@"_"][1] isComment:NO timeout:pushshiftRequestTimeoutValue extraData:@{@"post" : post} completionTarget:self completionSelector:@selector(completeUndeletePostAction:)]; +} + +%new +-(void) completeUndeletePostAction:(NSDictionary *) data{ + + id post = data[@"post"]; + + MSHookIvar(post, "_author") = data[@"author"]; + + NSAttributedString* postBodyAttributedString = [%c(NRTMarkdownManager) attributedStringFromMarkdown:data[@"body"] type:0]; + [self setLinkText:postBodyAttributedString]; + + [[self tableView] reloadData]; +} + +%new +-(void) completeUndeleteCommentAction:(NSDictionary *) data{ + + id comment = data[@"comment"]; + + if (comment){ + + MSHookIvar(comment, "_author") = data[@"author"]; + MSHookIvar(comment, "_body") = data[@"body"]; + + [[self commentsManager] updateComment:comment fromEdited:comment]; + } +} + %end %hook NRTMediaTableViewDataSource - -%new --(void) completeUndeleteComment:(id) data{ - - id comment = data[@"comment"]; - - if (comment){ - - MSHookIvar(comment, "_author") = data[@"author"]; - MSHookIvar(comment, "_body") = data[@"body"]; - - [[self commentsManager] updateComment:comment fromEdited:comment]; - } -} -(void) swipeCell:(id) arg1 didEndDragWithState:(NSUInteger) arg2{ @@ -213,6 +140,20 @@ void getUndeleteCommentData(id controller, id comment){ shouldHaveUndeleteAction = NO; } +%new +-(void) completeUndeleteCommentAction:(NSDictionary *) data{ + + id comment = data[@"comment"]; + + if (comment){ + + MSHookIvar(comment, "_author") = data[@"author"]; + MSHookIvar(comment, "_body") = data[@"body"]; + + [[self commentsManager] updateComment:comment fromEdited:comment]; + } +} + %end %end diff --git a/tweak/Reddit.h b/tweak/Reddit.h index 4867f73..78808eb 100644 --- a/tweak/Reddit.h +++ b/tweak/Reddit.h @@ -21,6 +21,10 @@ -(void) reloadPostSection:(BOOL) arg1; -(void) feedPostViewDidUpdatePost:(id) arg1 shouldReloadFeed:(BOOL) arg2; -(void) updateFloatingViews; + +//custom elements +-(void) updateComments; +-(void) updatePostText; @end @interface CommentActionSheetViewController : UIViewController diff --git a/tweak/Reddit.xm b/tweak/Reddit.xm index a0b1fa5..3210323 100644 --- a/tweak/Reddit.xm +++ b/tweak/Reddit.xm @@ -1,5 +1,6 @@ #import "Reddit.h" +#import "assets/TFHelper.h" static BOOL isRedditEnabled; static BOOL isTFDeletedOnly; @@ -93,98 +94,80 @@ int getRedditVersionPart(int index){ id commentTreeNode = [self commentTreeNode]; Comment *comment = [commentTreeNode 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"]; - [request setTimeoutInterval:pushshiftRequestTimeoutValue]; - - [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 = [NSString stringWithFormat:@"[an error occured while attempting to contact pushshift api (%@)]", [error localizedDescription]]; - } - - NSMutableAttributedString *bodyMutableAttributedText; - - id themeManager; - id isNightMode; - id textColor; - - if (getRedditVersionPart(1) >= 45){ - themeManager = [[%c(ThemeManager) alloc] initWithAppSettings:[%c(AppSettings) sharedSettings]]; - isNightMode = [[[%c(AccountManager) sharedManager] defaults] objectForKey:@"kUseNightKey"]; - - if (isNightMode) { - textColor = [[themeManager darkTheme] bodyTextColor]; - } else{ - textColor = [[themeManager lightTheme] bodyTextColor]; - } - - [themeManager release]; - - } else if (getRedditVersionPart(1) >= 37){ - themeManager = [[%c(ThemeManager) alloc] initWithTraitCollection:nil appSettings:[%c(AppSettings) sharedSettings]]; - isNightMode = [[[%c(AccountManager) sharedManager] defaults] objectForKey:@"kUseNightKey"]; - - if (isNightMode) { - textColor = [[themeManager nightTheme] bodyTextColor]; - } else{ - textColor = [[themeManager dayTheme] bodyTextColor]; - } - - [themeManager release]; - - } else { - themeManager = [%c(ThemeManager) sharedManager]; - isNightMode = [[[%c(AccountManager) sharedManager] defaults] objectForKey:@"kUseNightKey"]; - - if (isNightMode) { - textColor = [[themeManager nightTheme] bodyTextColor]; - } else{ - textColor = [[themeManager dayTheme] bodyTextColor]; - } - } - - bodyMutableAttributedText = [[NSMutableAttributedString alloc] initWithAttributedString:[%c(NSAttributedStringMarkdownParser) attributedStringUsingCurrentConfig:body]]; - - [bodyMutableAttributedText beginEditing]; - [bodyMutableAttributedText enumerateAttribute:NSForegroundColorAttributeName inRange:NSMakeRange(0, bodyMutableAttributedText.length) options:0 usingBlock:^(id _Nullable value, NSRange range, BOOL * _Nonnull stop) { - [bodyMutableAttributedText removeAttribute:NSForegroundColorAttributeName range:range]; - [bodyMutableAttributedText addAttribute:NSForegroundColorAttributeName value:textColor range:range]; - }]; - [bodyMutableAttributedText endEditing]; - - [comment setAuthor:author]; - [comment setBodyText:body]; - [comment setBodyRichTextAttributed:bodyMutableAttributedText]; - [comment setBodyAttributedText:bodyMutableAttributedText]; - - [[commentTreeNode commentTreeHeaderNode] performSelectorOnMainThread:@selector(updateContentViewsForData:) withObject:comment waitUntilDone:NO]; - - [request release]; - [queue release]; - [bodyMutableAttributedText release]; - - }]; + [%c(TFHelper) getUndeleteDataWithID:[[comment pk] componentsSeparatedByString:@"_"][1] isComment:YES timeout:pushshiftRequestTimeoutValue extraData:nil completionTarget:self completionSelector:@selector(completeUndeleteCommentAction:)]; } } + +%new +-(void) completeUndeleteCommentAction:(NSDictionary *) data{ + + id commentTreeNode = [self commentTreeNode]; + Comment *comment = [commentTreeNode comment]; + + NSString *author = data[@"author"]; + NSString *body = data[@"body"]; + + NSMutableAttributedString *bodyMutableAttributedText; + + id themeManager; + id isNightMode; + id textColor; + + if (getRedditVersionPart(1) >= 45){ + themeManager = [[%c(ThemeManager) alloc] initWithAppSettings:[%c(AppSettings) sharedSettings]]; + isNightMode = [[[%c(AccountManager) sharedManager] defaults] objectForKey:@"kUseNightKey"]; + + if (isNightMode) { + textColor = [[themeManager darkTheme] bodyTextColor]; + } else{ + textColor = [[themeManager lightTheme] bodyTextColor]; + } + + [themeManager release]; + + } else if (getRedditVersionPart(1) >= 37){ + themeManager = [[%c(ThemeManager) alloc] initWithTraitCollection:nil appSettings:[%c(AppSettings) sharedSettings]]; + isNightMode = [[[%c(AccountManager) sharedManager] defaults] objectForKey:@"kUseNightKey"]; + + if (isNightMode) { + textColor = [[themeManager nightTheme] bodyTextColor]; + } else{ + textColor = [[themeManager dayTheme] bodyTextColor]; + } + + [themeManager release]; + + } else { + themeManager = [%c(ThemeManager) sharedManager]; + isNightMode = [[[%c(AccountManager) sharedManager] defaults] objectForKey:@"kUseNightKey"]; + + if (isNightMode) { + textColor = [[themeManager nightTheme] bodyTextColor]; + } else{ + textColor = [[themeManager dayTheme] bodyTextColor]; + } + } + + bodyMutableAttributedText = [[NSMutableAttributedString alloc] initWithAttributedString:[%c(NSAttributedStringMarkdownParser) attributedStringUsingCurrentConfig:body]]; + + [bodyMutableAttributedText beginEditing]; + [bodyMutableAttributedText enumerateAttribute:NSForegroundColorAttributeName inRange:NSMakeRange(0, bodyMutableAttributedText.length) options:0 usingBlock:^(id _Nullable value, NSRange range, BOOL * _Nonnull stop) { + [bodyMutableAttributedText removeAttribute:NSForegroundColorAttributeName range:range]; + [bodyMutableAttributedText addAttribute:NSForegroundColorAttributeName value:textColor range:range]; + }]; + [bodyMutableAttributedText endEditing]; + + [comment setAuthor:author]; + [comment setBodyText:body]; + [comment setBodyRichTextAttributed:bodyMutableAttributedText]; + [comment setBodyAttributedText:bodyMutableAttributedText]; + + [[commentTreeNode commentTreeHeaderNode] updateContentViewsForData:comment]; + + [bodyMutableAttributedText release]; +} + %end @@ -242,102 +225,83 @@ int getRedditVersionPart(int index){ 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"]; - [request setTimeoutInterval:pushshiftRequestTimeoutValue]; - - [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 = [NSString stringWithFormat:@"[an error occured while attempting to contact pushshift api (%@)]", [error localizedDescription]]; - } - - id themeManager; - id isNightMode; - id textColor; - - if (getRedditVersionPart(1) >= 45){ - themeManager = [[%c(ThemeManager) alloc] initWithAppSettings:[%c(AppSettings) sharedSettings]]; - isNightMode = [[[%c(AccountManager) sharedManager] defaults] objectForKey:@"kUseNightKey"]; - - if (isNightMode) { - textColor = [[themeManager darkTheme] bodyTextColor]; - } else{ - textColor = [[themeManager lightTheme] bodyTextColor]; - } - - [themeManager release]; - - } else if (getRedditVersionPart(1) >= 37){ - themeManager = [[%c(ThemeManager) alloc] initWithTraitCollection:nil appSettings:[%c(AppSettings) sharedSettings]]; - isNightMode = [[[%c(AccountManager) sharedManager] defaults] objectForKey:@"kUseNightKey"]; - - if (isNightMode) { - textColor = [[themeManager nightTheme] bodyTextColor]; - } else{ - textColor = [[themeManager dayTheme] bodyTextColor]; - } - - [themeManager release]; - - } else { - themeManager = [%c(ThemeManager) sharedManager]; - isNightMode = [[[%c(AccountManager) sharedManager] defaults] objectForKey:@"kUseNightKey"]; - - if (isNightMode) { - textColor = [[themeManager nightTheme] bodyTextColor]; - } else{ - textColor = [[themeManager dayTheme] bodyTextColor]; - } - } - - NSMutableAttributedString *bodyMutableAttributedText = [[NSMutableAttributedString alloc] initWithAttributedString:[%c(NSAttributedStringMarkdownParser) attributedStringUsingCurrentConfig:body]]; - - [bodyMutableAttributedText beginEditing]; - [bodyMutableAttributedText enumerateAttribute:NSForegroundColorAttributeName inRange:NSMakeRange(0, bodyMutableAttributedText.length) options:0 usingBlock:^(id _Nullable value, NSRange range, BOOL * _Nonnull stop) { - [bodyMutableAttributedText removeAttribute:NSForegroundColorAttributeName range:range]; - [bodyMutableAttributedText addAttribute:NSForegroundColorAttributeName value:textColor range:range]; - }]; - [bodyMutableAttributedText endEditing]; - - [post setSelfText:body]; - [post setAuthor:author]; - [post setSelfPostRichTextAttributed:bodyMutableAttributedText]; - [post setPreviewFeedPostTextString:bodyMutableAttributedText]; - - if (getRedditVersionPart(1) >= 44){ - [[[[[self postActionSheetDelegate] controller] feedPostDetailCellNode] contentNode] configureSelfTextNode]; - } else if (getRedditVersionPart(1) >= 38) { - [[[[self postActionSheetDelegate] controller] feedPostDetailCellNode] configureSelfTextNode]; - } else { - [[[[self postActionSheetDelegate] controller] feedPostDetailCellNode] configureSelfTextNode]; - [[[[[self postActionSheetDelegate] controller] feedPostDetailCellNode] titleNode] configureNodes]; - } - - [request release]; - [queue release]; - [bodyMutableAttributedText release]; - }]; + [%c(TFHelper) getUndeleteDataWithID:[[post pk] componentsSeparatedByString:@"_"][1] isComment:NO timeout:pushshiftRequestTimeoutValue extraData:nil completionTarget:self completionSelector:@selector(completeUndeletePostAction:)]; } } } + +%new +-(void) completeUndeletePostAction:(NSDictionary *) data{ + Post *post = [self post]; + + NSString *author = data[@"author"]; + NSString *body = data[@"body"]; + + id themeManager; + id isNightMode; + id textColor; + + if (getRedditVersionPart(1) >= 45){ + themeManager = [[%c(ThemeManager) alloc] initWithAppSettings:[%c(AppSettings) sharedSettings]]; + isNightMode = [[[%c(AccountManager) sharedManager] defaults] objectForKey:@"kUseNightKey"]; + + if (isNightMode) { + textColor = [[themeManager darkTheme] bodyTextColor]; + } else{ + textColor = [[themeManager lightTheme] bodyTextColor]; + } + + [themeManager release]; + + } else if (getRedditVersionPart(1) >= 37){ + themeManager = [[%c(ThemeManager) alloc] initWithTraitCollection:nil appSettings:[%c(AppSettings) sharedSettings]]; + isNightMode = [[[%c(AccountManager) sharedManager] defaults] objectForKey:@"kUseNightKey"]; + + if (isNightMode) { + textColor = [[themeManager nightTheme] bodyTextColor]; + } else{ + textColor = [[themeManager dayTheme] bodyTextColor]; + } + + [themeManager release]; + + } else { + themeManager = [%c(ThemeManager) sharedManager]; + isNightMode = [[[%c(AccountManager) sharedManager] defaults] objectForKey:@"kUseNightKey"]; + + if (isNightMode) { + textColor = [[themeManager nightTheme] bodyTextColor]; + } else{ + textColor = [[themeManager dayTheme] bodyTextColor]; + } + } + + NSMutableAttributedString *bodyMutableAttributedText = [[NSMutableAttributedString alloc] initWithAttributedString:[%c(NSAttributedStringMarkdownParser) attributedStringUsingCurrentConfig:body]]; + + [bodyMutableAttributedText beginEditing]; + [bodyMutableAttributedText enumerateAttribute:NSForegroundColorAttributeName inRange:NSMakeRange(0, bodyMutableAttributedText.length) options:0 usingBlock:^(id _Nullable value, NSRange range, BOOL * _Nonnull stop) { + [bodyMutableAttributedText removeAttribute:NSForegroundColorAttributeName range:range]; + [bodyMutableAttributedText addAttribute:NSForegroundColorAttributeName value:textColor range:range]; + }]; + [bodyMutableAttributedText endEditing]; + + [post setSelfText:body]; + [post setAuthor:author]; + [post setSelfPostRichTextAttributed:bodyMutableAttributedText]; + [post setPreviewFeedPostTextString:bodyMutableAttributedText]; + + if (getRedditVersionPart(1) >= 44){ + [[[[[self postActionSheetDelegate] controller] feedPostDetailCellNode] contentNode] configureSelfTextNode]; + } else if (getRedditVersionPart(1) >= 38) { + [[[[self postActionSheetDelegate] controller] feedPostDetailCellNode] configureSelfTextNode]; + } else { + [[[[self postActionSheetDelegate] controller] feedPostDetailCellNode] configureSelfTextNode]; + [[[[[self postActionSheetDelegate] controller] feedPostDetailCellNode] titleNode] configureNodes]; + } + + [bodyMutableAttributedText release]; +} + %end %end @@ -404,47 +368,8 @@ int getRedditVersionPart(int index){ [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"]; - [request setTimeoutInterval:pushshiftRequestTimeoutValue]; - - [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 = [NSString stringWithFormat:@"[an error occured while attempting to contact pushshift api (%@)]", [error localizedDescription]]; - } - - NSMutableAttributedString *bodyMutableAttributedText = [[NSMutableAttributedString alloc] initWithAttributedString:[%c(NSAttributedStringMarkdownParser) attributedStringUsingCurrentConfig:body]]; - - [comment setAuthor:author]; - [comment setBodyText:body]; - [comment setBodyRichTextAttributed:bodyMutableAttributedText]; - [comment setBodyAttributedText:bodyMutableAttributedText]; - - [[self commentActionSheetDelegate] performSelectorOnMainThread:@selector(updateComments) withObject:nil waitUntilDone:NO]; - - [request release]; - [queue release]; - [bodyMutableAttributedText release]; - }]; + [%c(TFHelper) getUndeleteDataWithID:[[comment pk] componentsSeparatedByString:@"_"][1] isComment:YES timeout:pushshiftRequestTimeoutValue extraData:nil completionTarget:self completionSelector:@selector(completeUndeleteCommentAction:)]; } } @@ -457,52 +382,32 @@ int getRedditVersionPart(int index){ [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"]; - [request setTimeoutInterval:pushshiftRequestTimeoutValue]; - - [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 = [NSString stringWithFormat:@"[an error occured while attempting to contact pushshift api (%@)]", [error localizedDescription]]; - } - - NSMutableAttributedString *bodyMutableAttributedText = [[NSMutableAttributedString alloc] initWithAttributedString:[%c(NSAttributedStringMarkdownParser) attributedStringUsingCurrentConfig:body]]; - - [comment setAuthor:author]; - [comment setBodyText:body]; - [comment setBodyAttributedText:bodyMutableAttributedText]; - - if (getRedditVersionPart(1) >= 12) { - [comment setBodyRichTextAttributed:bodyMutableAttributedText]; - } - - [[self commentActionSheetDelegate] performSelectorOnMainThread:@selector(updateComments) withObject:nil waitUntilDone:NO]; - - [request release]; - [queue release]; - [bodyMutableAttributedText release]; - }]; + [%c(TFHelper) getUndeleteDataWithID:[[comment pk] componentsSeparatedByString:@"_"][1] isComment:YES timeout:pushshiftRequestTimeoutValue extraData:nil completionTarget:self completionSelector:@selector(completeUndeleteCommentAction:)]; } } + +%new +-(void) completeUndeleteCommentAction:(NSDictionary *) data{ + + Comment *comment = [self comment]; + + NSString *body = data[@"body"]; + + NSMutableAttributedString *bodyMutableAttributedText = [[NSMutableAttributedString alloc] initWithAttributedString:[%c(NSAttributedStringMarkdownParser) attributedStringUsingCurrentConfig:body]]; + + [comment setAuthor:data[@"author"]]; + [comment setBodyText:body]; + [comment setBodyAttributedText:bodyMutableAttributedText]; + + if (getRedditVersionPart(1) >= 12) { + [comment setBodyRichTextAttributed:bodyMutableAttributedText]; + } + + [[self commentActionSheetDelegate] updateComments]; + + [bodyMutableAttributedText release]; +} %end @@ -552,46 +457,7 @@ int getRedditVersionPart(int index){ 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"]; - [request setTimeoutInterval:pushshiftRequestTimeoutValue]; - - [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 = [NSString stringWithFormat:@"[an error occured while attempting to contact pushshift api (%@)]", [error localizedDescription]]; - } - - NSMutableAttributedString *bodyMutableAttributedText = [[NSMutableAttributedString alloc] initWithAttributedString:[%c(NSAttributedStringMarkdownParser) attributedStringUsingCurrentConfig:body]]; - - [post setSelfText:body]; - [post setAuthor:author]; - [post setSelfPostRichTextAttributed:bodyMutableAttributedText]; - [post setPreviewFeedPostTextString:bodyMutableAttributedText]; - - [[self postActionSheetDelegate] performSelectorOnMainThread:@selector(updatePostText) withObject:nil waitUntilDone:NO]; - - [request release]; - [queue release]; - [bodyMutableAttributedText release]; - }]; + [%c(TFHelper) getUndeleteDataWithID:[[post pk] componentsSeparatedByString:@"_"][1] isComment:NO timeout:pushshiftRequestTimeoutValue extraData:nil completionTarget:self completionSelector:@selector(completeUndeletePostAction:)]; } } } @@ -608,56 +474,37 @@ int getRedditVersionPart(int index){ 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"]; - [request setTimeoutInterval:pushshiftRequestTimeoutValue]; - - [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 = [NSString stringWithFormat:@"[an error occured while attempting to contact pushshift api (%@)]", [error localizedDescription]]; - } - - NSMutableAttributedString *bodyMutableAttributedText = [[NSMutableAttributedString alloc] initWithAttributedString:[%c(NSAttributedStringMarkdownParser) attributedStringUsingCurrentConfig:body]]; - - [post setAuthor:author]; - [post setSelfText:body]; - [post setSelfTextAttributed:bodyMutableAttributedText]; - - if (getRedditVersionPart(1) >= 8) { - [post setSelfPostRichTextAttributed:bodyMutableAttributedText]; - } - - if (getRedditVersionPart(1) >= 15) { - [post setPreviewFeedPostTextString:bodyMutableAttributedText]; - } - - [[self postActionSheetDelegate] performSelectorOnMainThread:@selector(updatePostText) withObject:nil waitUntilDone:NO]; - - [request release]; - [queue release]; - [bodyMutableAttributedText release]; - }]; + [%c(TFHelper) getUndeleteDataWithID:[[post pk] componentsSeparatedByString:@"_"][1] isComment:NO timeout:pushshiftRequestTimeoutValue extraData:nil completionTarget:self completionSelector:@selector(completeUndeletePostAction:)]; } } } + +%new +-(void) completeUndeletePostAction:(NSDictionary *) data{ + + Post *post = [self post]; + + NSString *body = data[@"body"]; + + NSMutableAttributedString *bodyMutableAttributedText = [[NSMutableAttributedString alloc] initWithAttributedString:[%c(NSAttributedStringMarkdownParser) attributedStringUsingCurrentConfig:body]]; + + [post setAuthor:data[@"author"]]; + [post setSelfText:body]; + [post setSelfTextAttributed:bodyMutableAttributedText]; + + if (getRedditVersionPart(1) >= 8) { + [post setSelfPostRichTextAttributed:bodyMutableAttributedText]; + } + + if (getRedditVersionPart(1) >= 15) { + [post setPreviewFeedPostTextString:bodyMutableAttributedText]; + } + + [[self postActionSheetDelegate] updatePostText]; + + [bodyMutableAttributedText release]; +} + %end %end diff --git a/tweak/Slide.xm b/tweak/Slide.xm index bb0e51d..133e448 100644 --- a/tweak/Slide.xm +++ b/tweak/Slide.xm @@ -1,6 +1,7 @@ #import "Slide.h" -#import "assets/MMMarkdown.h" +#import "assets/TFHelper.h" +#import "assets/MMMarkdown/MMMarkdown.h" static BOOL isSlideEnabled; static BOOL isTFDeletedOnly; @@ -288,129 +289,112 @@ static UIButton * createUndeleteButton(){ -(void) handleUndeleteComment:(id) sender{ [sender setEnabled:NO]; + + id comment = MSHookIvar(self, "comment"); + + [%c(TFHelper) getUndeleteDataWithID:[[comment id] componentsSeparatedByString:@"_"][1] isComment:YES timeout:pushshiftRequestTimeoutValue extraData:@{@"sender" : sender} completionTarget:self completionSelector:@selector(completeUndeleteCommentAction:)]; +} + +%new +-(void) completeUndeleteCommentAction:(NSDictionary *) data{ NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; id textStackDisplayView = MSHookIvar(self, "commentBody"); id comment = MSHookIvar(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 id] componentsSeparatedByString:@"_"][1]]]]; - [request setHTTPMethod:@"GET"]; - [request setTimeoutInterval:pushshiftRequestTimeoutValue]; - - [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { + NSString *author = data[@"author"]; + NSString *body = data[@"body"]; - 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 = [NSString stringWithFormat:@"[an error occured while attempting to contact pushshift api (%@)]", [error localizedDescription]]; - } + //Attributed string generation rewrote from Slide_for_Reddit.TextDisplayStackView.createAttributedChunk(...) + + UIFont *font = [%c(FontGenerator) fontOfSize:MSHookIvar(textStackDisplayView, "fontSize") submission:NO willOffset:YES]; - //Attributed string generation rewrote from Slide_for_Reddit.TextDisplayStackView.createAttributedChunk(...) - - UIFont *font = [%c(FontGenerator) fontOfSize:MSHookIvar(textStackDisplayView, "fontSize") submission:NO willOffset:YES]; + NSString *themeName = [userDefaults stringForKey:@"theme"]; + UIColor *fontColor = [%c(ColorUtil) fontColorForTheme:themeName]; + UIColor *accentColor = [%c(ColorUtil) accentColorForSub:[comment subreddit]]; + + NSString *html = [%c(MMMarkdown) HTMLStringWithMarkdown:body extensions:MMMarkdownExtensionsGitHubFlavored error:nil]; + html = [[html stringByReplacingOccurrencesOfString:@"" withString:@""] stringByReplacingOccurrencesOfString:@"" withString:@""]; + html = [[html stringByReplacingOccurrencesOfString:@"" withString:@""] stringByReplacingOccurrencesOfString:@"" withString:@""]; + html = [[html stringByReplacingOccurrencesOfString:@"" withString:@""] stringByReplacingOccurrencesOfString:@"" withString:@""]; + html = [html stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; + + DTHTMLAttributedStringBuilder *dthtmlBuilder = [[%c(DTHTMLAttributedStringBuilder) alloc] initWithHTML:[html dataUsingEncoding:NSUTF8StringEncoding] options:@{@"DTUseiOS6Attributes": @YES, @"DTDefaultTextColor": fontColor, @"DTDefaultFontSize": @([font pointSize])} documentAttributes:nil]; + + NSMutableAttributedString *htmlAttributedString = [[NSMutableAttributedString alloc] initWithAttributedString:[dthtmlBuilder generatedAttributedString]]; + NSRange htmlStringRange = NSMakeRange(0, [htmlAttributedString length]); + + [[htmlAttributedString mutableString] replaceOccurrencesOfString:@"\t•\t" withString:@" • " options:0 range: htmlStringRange]; + [[htmlAttributedString mutableString] replaceOccurrencesOfString:@"\t◦\t" withString:@"  ◦ " options:0 range: htmlStringRange]; + [[htmlAttributedString mutableString] replaceOccurrencesOfString:@"\t▪\t" withString:@" ▪ " options:0 range: htmlStringRange]; + + [htmlAttributedString removeAttribute:@"CTForegroundColorFromContext" range:htmlStringRange]; + + [htmlAttributedString enumerateAttributesInRange:htmlStringRange options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired usingBlock:^(NSDictionary *attributes, NSRange range, BOOL *stop) { + for (NSString *key in attributes){ - NSString *themeName = [userDefaults stringForKey:@"theme"]; - UIColor *fontColor = [%c(ColorUtil) fontColorForTheme:themeName]; - UIColor *accentColor = [%c(ColorUtil) accentColorForSub:[comment subreddit]]; - - NSString *html = [%c(MMMarkdown) HTMLStringWithMarkdown:body extensions:MMMarkdownExtensionsGitHubFlavored error:nil]; - html = [[html stringByReplacingOccurrencesOfString:@"" withString:@""] stringByReplacingOccurrencesOfString:@"" withString:@""]; - html = [[html stringByReplacingOccurrencesOfString:@"" withString:@""] stringByReplacingOccurrencesOfString:@"" withString:@""]; - html = [[html stringByReplacingOccurrencesOfString:@"" withString:@""] stringByReplacingOccurrencesOfString:@"" withString:@""]; - html = [html stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; - - DTHTMLAttributedStringBuilder *dthtmlBuilder = [[%c(DTHTMLAttributedStringBuilder) alloc] initWithHTML:[html dataUsingEncoding:NSUTF8StringEncoding] options:@{@"DTUseiOS6Attributes": @YES, @"DTDefaultTextColor": fontColor, @"DTDefaultFontSize": @([font pointSize])} documentAttributes:nil]; - - NSMutableAttributedString *htmlAttributedString = [[NSMutableAttributedString alloc] initWithAttributedString:[dthtmlBuilder generatedAttributedString]]; - NSRange htmlStringRange = NSMakeRange(0, [htmlAttributedString length]); - - [[htmlAttributedString mutableString] replaceOccurrencesOfString:@"\t•\t" withString:@" • " options:0 range: htmlStringRange]; - [[htmlAttributedString mutableString] replaceOccurrencesOfString:@"\t◦\t" withString:@"  ◦ " options:0 range: htmlStringRange]; - [[htmlAttributedString mutableString] replaceOccurrencesOfString:@"\t▪\t" withString:@" ▪ " options:0 range: htmlStringRange]; - - [htmlAttributedString removeAttribute:@"CTForegroundColorFromContext" range:htmlStringRange]; - - [htmlAttributedString enumerateAttributesInRange:htmlStringRange options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired usingBlock:^(NSDictionary *attributes, NSRange range, BOOL *stop) { - for (NSString *key in attributes){ - - if ([(UIColor *) attributes[key] isKindOfClass:[UIColor class]]){ - UIColor *attrColor = (UIColor *) attributes[key]; - if ([[attrColor hexString] isEqualToString:@"#0000FF"]){ - UIFont *tempFont = [UIFont fontWithName:@"Courier" size:font.pointSize]; - - [htmlAttributedString setAttributes:@{NSForegroundColorAttributeName: accentColor, NSBackgroundColorAttributeName: [%c(ColorUtil) backgroundColorForTheme:themeName], NSFontAttributeName: (tempFont ? tempFont : font)} range:range]; - } else if ([[attrColor hexString] isEqualToString:@"#008000"]) { - [htmlAttributedString setAttributes:@{NSForegroundColorAttributeName: fontColor, NSFontAttributeName:font} range:range]; - } - } else if ([(NSURL *) attributes[key] isKindOfClass:[NSURL class]]){ - NSURL *attrUrl = (NSURL *)attributes[key]; - - if (([userDefaults objectForKey:@"ENLARGE_LINKS"] == nil) ? YES : [userDefaults boolForKey:@"ENLARGE_LINKS"]){ - [htmlAttributedString addAttribute:NSFontAttributeName value:[%c(FontGenerator) boldFontOfSize:18 submission:NO willOffset:YES] range:range]; - } + if ([(UIColor *) attributes[key] isKindOfClass:[UIColor class]]){ + UIColor *attrColor = (UIColor *) attributes[key]; + if ([[attrColor hexString] isEqualToString:@"#0000FF"]){ + UIFont *tempFont = [UIFont fontWithName:@"Courier" size:font.pointSize]; - [htmlAttributedString addAttribute:NSForegroundColorAttributeName value:accentColor range:range]; - [htmlAttributedString addAttribute:NSUnderlineColorAttributeName value:[UIColor clearColor] range:range]; - - //skipping showLinkContentType b/c not necessary and spoilers b/c MMMarkdown doesn't support them + [htmlAttributedString setAttributes:@{NSForegroundColorAttributeName: accentColor, NSBackgroundColorAttributeName: [%c(ColorUtil) backgroundColorForTheme:themeName], NSFontAttributeName: (tempFont ? tempFont : font)} range:range]; + } else if ([[attrColor hexString] isEqualToString:@"#008000"]) { + [htmlAttributedString setAttributes:@{NSForegroundColorAttributeName: fontColor, NSFontAttributeName:font} range:range]; + } + } else if ([(NSURL *) attributes[key] isKindOfClass:[NSURL class]]){ + NSURL *attrUrl = (NSURL *)attributes[key]; - [htmlAttributedString yy_setTextHighlightRange:range color: accentColor backgroundColor:nil userInfo:@{@"url": attrUrl}]; - break; - } - } - }]; - - [htmlAttributedString beginEditing]; - [htmlAttributedString enumerateAttribute:NSFontAttributeName inRange:NSMakeRange(0, [htmlAttributedString length]) options:0 usingBlock:^(id value, NSRange range, BOOL *stop){ - - UIFont *attrFont = (UIFont *)value; - - BOOL isBold = (attrFont.fontDescriptor.symbolicTraits & UIFontDescriptorTraitBold) != 0; - BOOL isItalic = (attrFont.fontDescriptor.symbolicTraits & UIFontDescriptorTraitItalic) != 0; - - UIFont *newFont = font; - - if (isBold){ - newFont = [%c(FontGenerator) boldFontOfSize:attrFont.pointSize submission:NO willOffset:NO]; - } else if (isItalic){ - newFont = [%c(FontGenerator) italicFontOfSize:attrFont.pointSize submission:NO willOffset:NO]; - } - - [htmlAttributedString removeAttribute:NSFontAttributeName range:range]; - [htmlAttributedString addAttribute:NSFontAttributeName value:newFont range:range]; - - }]; - [htmlAttributedString endEditing]; - - NSMutableAttributedString *newCommentText = [MSHookIvar(self, "cellContent") initWithAttributedString:htmlAttributedString]; - NSAttributedString *tempAttributedString = [[NSAttributedString alloc] initWithString:@""]; - [newCommentText appendAttributedString:tempAttributedString]; //to keep the compiler happy - - [comment setAuthor:author]; - [comment setBody:body]; - - id controller = MSHookIvar(self, "parent"); - - [self performSelectorOnMainThread:@selector(showMenu:) withObject:nil waitUntilDone:YES]; - [MSHookIvar(controller, "tableView") performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO]; - - [sender setEnabled:YES]; + if (([userDefaults objectForKey:@"ENLARGE_LINKS"] == nil) ? YES : [userDefaults boolForKey:@"ENLARGE_LINKS"]){ + [htmlAttributedString addAttribute:NSFontAttributeName value:[%c(FontGenerator) boldFontOfSize:18 submission:NO willOffset:YES] range:range]; + } + + [htmlAttributedString addAttribute:NSForegroundColorAttributeName value:accentColor range:range]; + [htmlAttributedString addAttribute:NSUnderlineColorAttributeName value:[UIColor clearColor] range:range]; + + //skipping showLinkContentType b/c not necessary and spoilers b/c MMMarkdown doesn't support them + + [htmlAttributedString yy_setTextHighlightRange:range color: accentColor backgroundColor:nil userInfo:@{@"url": attrUrl}]; + break; + } + } }]; + + [htmlAttributedString beginEditing]; + [htmlAttributedString enumerateAttribute:NSFontAttributeName inRange:NSMakeRange(0, [htmlAttributedString length]) options:0 usingBlock:^(id value, NSRange range, BOOL *stop){ + + UIFont *attrFont = (UIFont *)value; + + BOOL isBold = (attrFont.fontDescriptor.symbolicTraits & UIFontDescriptorTraitBold) != 0; + BOOL isItalic = (attrFont.fontDescriptor.symbolicTraits & UIFontDescriptorTraitItalic) != 0; + + UIFont *newFont = font; + + if (isBold){ + newFont = [%c(FontGenerator) boldFontOfSize:attrFont.pointSize submission:NO willOffset:NO]; + } else if (isItalic){ + newFont = [%c(FontGenerator) italicFontOfSize:attrFont.pointSize submission:NO willOffset:NO]; + } + + [htmlAttributedString removeAttribute:NSFontAttributeName range:range]; + [htmlAttributedString addAttribute:NSFontAttributeName value:newFont range:range]; + + }]; + [htmlAttributedString endEditing]; + + NSMutableAttributedString *newCommentText = [MSHookIvar(self, "cellContent") initWithAttributedString:htmlAttributedString]; + NSAttributedString *tempAttributedString = [[NSAttributedString alloc] initWithString:@""]; + [newCommentText appendAttributedString:tempAttributedString]; //to keep the compiler happy + + [comment setAuthor:author]; + [comment setBody:body]; + + id controller = MSHookIvar(self, "parent"); + + [self showMenu:nil]; + [MSHookIvar(controller, "tableView") reloadData]; + + [data[@"sender"] setEnabled:YES]; } %end diff --git a/tweak/assets/MMDocument.h b/tweak/assets/MMMarkdown/MMDocument.h similarity index 100% rename from tweak/assets/MMDocument.h rename to tweak/assets/MMMarkdown/MMDocument.h diff --git a/tweak/assets/MMDocument.m b/tweak/assets/MMMarkdown/MMDocument.m similarity index 100% rename from tweak/assets/MMDocument.m rename to tweak/assets/MMMarkdown/MMDocument.m diff --git a/tweak/assets/MMDocument_Private.h b/tweak/assets/MMMarkdown/MMDocument_Private.h similarity index 100% rename from tweak/assets/MMDocument_Private.h rename to tweak/assets/MMMarkdown/MMDocument_Private.h diff --git a/tweak/assets/MMElement.h b/tweak/assets/MMMarkdown/MMElement.h similarity index 100% rename from tweak/assets/MMElement.h rename to tweak/assets/MMMarkdown/MMElement.h diff --git a/tweak/assets/MMElement.m b/tweak/assets/MMMarkdown/MMElement.m similarity index 100% rename from tweak/assets/MMElement.m rename to tweak/assets/MMMarkdown/MMElement.m diff --git a/tweak/assets/MMGenerator.h b/tweak/assets/MMMarkdown/MMGenerator.h similarity index 100% rename from tweak/assets/MMGenerator.h rename to tweak/assets/MMMarkdown/MMGenerator.h diff --git a/tweak/assets/MMGenerator.m b/tweak/assets/MMMarkdown/MMGenerator.m similarity index 100% rename from tweak/assets/MMGenerator.m rename to tweak/assets/MMMarkdown/MMGenerator.m diff --git a/tweak/assets/MMHTMLParser.h b/tweak/assets/MMMarkdown/MMHTMLParser.h similarity index 100% rename from tweak/assets/MMHTMLParser.h rename to tweak/assets/MMMarkdown/MMHTMLParser.h diff --git a/tweak/assets/MMHTMLParser.m b/tweak/assets/MMMarkdown/MMHTMLParser.m similarity index 100% rename from tweak/assets/MMHTMLParser.m rename to tweak/assets/MMMarkdown/MMHTMLParser.m diff --git a/tweak/assets/MMMarkdown-Prefix.pch b/tweak/assets/MMMarkdown/MMMarkdown-Prefix.pch similarity index 100% rename from tweak/assets/MMMarkdown-Prefix.pch rename to tweak/assets/MMMarkdown/MMMarkdown-Prefix.pch diff --git a/tweak/assets/MMMarkdown.h b/tweak/assets/MMMarkdown/MMMarkdown.h similarity index 100% rename from tweak/assets/MMMarkdown.h rename to tweak/assets/MMMarkdown/MMMarkdown.h diff --git a/tweak/assets/MMMarkdown.m b/tweak/assets/MMMarkdown/MMMarkdown.m similarity index 100% rename from tweak/assets/MMMarkdown.m rename to tweak/assets/MMMarkdown/MMMarkdown.m diff --git a/tweak/assets/MMParser.h b/tweak/assets/MMMarkdown/MMParser.h similarity index 100% rename from tweak/assets/MMParser.h rename to tweak/assets/MMMarkdown/MMParser.h diff --git a/tweak/assets/MMParser.m b/tweak/assets/MMMarkdown/MMParser.m similarity index 100% rename from tweak/assets/MMParser.m rename to tweak/assets/MMMarkdown/MMParser.m diff --git a/tweak/assets/MMScanner.h b/tweak/assets/MMMarkdown/MMScanner.h similarity index 100% rename from tweak/assets/MMScanner.h rename to tweak/assets/MMMarkdown/MMScanner.h diff --git a/tweak/assets/MMScanner.m b/tweak/assets/MMMarkdown/MMScanner.m similarity index 100% rename from tweak/assets/MMScanner.m rename to tweak/assets/MMMarkdown/MMScanner.m diff --git a/tweak/assets/MMSpanParser.h b/tweak/assets/MMMarkdown/MMSpanParser.h similarity index 100% rename from tweak/assets/MMSpanParser.h rename to tweak/assets/MMMarkdown/MMSpanParser.h diff --git a/tweak/assets/MMSpanParser.m b/tweak/assets/MMMarkdown/MMSpanParser.m similarity index 100% rename from tweak/assets/MMSpanParser.m rename to tweak/assets/MMMarkdown/MMSpanParser.m diff --git a/tweak/assets/TFHelper.h b/tweak/assets/TFHelper.h new file mode 100644 index 0000000..7694e00 --- /dev/null +++ b/tweak/assets/TFHelper.h @@ -0,0 +1,6 @@ + +@interface TFHelper : NSObject + ++(void) getUndeleteDataWithID:(NSString *) ident isComment:(BOOL) isComment timeout:(CGFloat) timeout extraData:(NSDictionary *) extra completionTarget:(id) target completionSelector:(SEL) sel; + +@end diff --git a/tweak/assets/TFHelper.m b/tweak/assets/TFHelper.m new file mode 100644 index 0000000..e2cffc2 --- /dev/null +++ b/tweak/assets/TFHelper.m @@ -0,0 +1,50 @@ + +#import "TFHelper.h" + +@implementation TFHelper + ++(void) getUndeleteDataWithID:(NSString *) ident isComment:(BOOL) isComment timeout:(CGFloat) timeout extraData:(NSDictionary *) extra completionTarget:(id) target completionSelector:(SEL) sel{ + + NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; + NSOperationQueue *queue = [[NSOperationQueue alloc] init]; + + if (isComment){ + [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://api.pushshift.io/reddit/search/comment/?ids=%@&fields=author,body", ident]]]; + } else { + [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://api.pushshift.io/reddit/search/submission/?ids=%@&fields=author,selftext", ident]]]; + } + + [request setHTTPMethod:@"GET"]; + [request setTimeoutInterval:timeout]; + + [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] objectForKey:@"data"]; + if ([jsonData count] != 0){ + author = [jsonData[0] objectForKey:@"author"]; + body = isComment ? [jsonData[0] objectForKey:@"body"] : [jsonData[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 = [NSString stringWithFormat:@"[an error occured while attempting to contact pushshift api (%@)]", [error localizedDescription]]; + } + + NSMutableDictionary *result = [@{@"author" : author, @"body" : body} mutableCopy]; + + if (extra){ + [result addEntriesFromDictionary:extra]; + } + + [target performSelectorOnMainThread:sel withObject:result waitUntilDone:NO]; + }]; +} + +@end \ No newline at end of file