You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

352 lines
11KB

  1. #import "Reddit.h"
  2. %group Redditv4
  3. %hook CommentTreeNode
  4. %property(assign,nonatomic)id commentTreeHeaderNode;
  5. %property(assign,nonatomic)id commentTreeCommandBarNode;
  6. %end
  7. %hook CommentTreeHeaderNode
  8. -(void) didLoad{
  9. %orig;
  10. [[self commentTreeNode] setCommentTreeHeaderNode:self];
  11. }
  12. %end
  13. %hook CommentTreeCommandBarNode
  14. %property(assign,nonatomic) id activityIndicator;
  15. %property(assign,nonatomic) id undeleteButton;
  16. -(void) didLoad{
  17. %orig;
  18. [[self commentTreeNode] setCommentTreeCommandBarNode:self];
  19. }
  20. %end
  21. %hook CommentActionSheetViewController
  22. -(void) setItems:(id) arg1{
  23. UIImage* origImage = [UIImage imageWithContentsOfFile:@"/var/mobile/Library/Application Support/TFDidThatSay/eye160dark.png"];
  24. CGSize existingImageSize = [[arg1[0] leftIconImage] size];
  25. CGFloat scale = origImage.size.width / existingImageSize.width;
  26. UIImage *newImage = [UIImage imageWithCGImage:[origImage CGImage] scale:scale orientation:origImage.imageOrientation];
  27. id undeleteItem = [[%c(RUIActionSheetItem) alloc] initWithLeftIconImage:newImage text:@"TF did that say?" identifier:@"undeleteItemIdentifier" context:[self comment]];
  28. %orig([arg1 arrayByAddingObject:undeleteItem]);
  29. [undeleteItem release];
  30. }
  31. -(void) handleDidSelectActionSheetItem:(id) arg1{
  32. %orig;
  33. if ([[arg1 identifier] isEqualToString:@"undeleteItemIdentifier"]){
  34. [self dismissViewControllerAnimated:YES completion:nil];
  35. id commentTreeNode = [self commentTreeNode];
  36. id comment = [commentTreeNode comment];
  37. NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
  38. NSOperationQueue *queue = [[NSOperationQueue alloc] init];
  39. [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://api.pushshift.io/reddit/search/comment/?ids=%@&fields=author,body",[[comment pk] componentsSeparatedByString:@"_"][1]]]];
  40. [request setHTTPMethod:@"GET"];
  41. [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
  42. NSString *author = @"[author]";
  43. NSString *body = @"[body]";
  44. if (data != nil && error == nil){
  45. id jsonData = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
  46. author = [[jsonData objectForKey:@"data"][0] objectForKey:@"author"];
  47. body = [[jsonData objectForKey:@"data"][0] objectForKey:@"body"];
  48. if ([body isEqualToString:@"[deleted]"] || [body isEqualToString:@"[removed]"]){
  49. body = @"[comment was unable to be archived]";
  50. }
  51. } else if (error != nil || data == nil){
  52. body = @"[an error occured]";
  53. }
  54. id themeManager = [[%c(ThemeManager) alloc] initWithTraitCollection:nil appSettings:[%c(AppSettings) sharedSettings]];
  55. id isNightMode = [[[%c(AccountManager) sharedManager] defaults] objectForKey:@"kUseNightKey"];
  56. id textColor;
  57. if (isNightMode) {
  58. textColor = [[themeManager nightTheme] bodyTextColor];
  59. } else{
  60. textColor = [[themeManager dayTheme] bodyTextColor];
  61. }
  62. NSMutableAttributedString *bodyMutableAttributedText = [[NSMutableAttributedString alloc] initWithAttributedString:[%c(NSAttributedStringMarkdownParser) attributedStringUsingCurrentConfig:body]];
  63. [bodyMutableAttributedText beginEditing];
  64. [bodyMutableAttributedText enumerateAttribute:NSForegroundColorAttributeName inRange:NSMakeRange(0, bodyMutableAttributedText.length) options:0 usingBlock:^(id _Nullable value, NSRange range, BOOL * _Nonnull stop) {
  65. [bodyMutableAttributedText removeAttribute:NSForegroundColorAttributeName range:range];
  66. [bodyMutableAttributedText addAttribute:NSForegroundColorAttributeName value:textColor range:range];
  67. }];
  68. [bodyMutableAttributedText endEditing];
  69. [comment setValue:bodyMutableAttributedText forKey:@"bodyRichTextAttributed"];
  70. [comment setValue:author forKey:@"author"];
  71. [comment setValue:body forKey:@"bodyText"];
  72. [comment setValue:bodyMutableAttributedText forKey:@"bodyAttributedText"];
  73. [[commentTreeNode commentTreeHeaderNode] updateContentViewsForData:comment];
  74. [request release];
  75. [queue release];
  76. [bodyMutableAttributedText release];
  77. [themeManager release];
  78. }];
  79. }
  80. }
  81. %end
  82. %hook PostDetailViewController
  83. %property(assign,nonatomic) id feedPostTextWithThumbnailNode;
  84. %property(assign,nonatomic) id feedPostDetailCellNode;
  85. %end
  86. %hook FeedPostDetailCellNode
  87. -(void) didLoad{
  88. %orig;
  89. [[[self delegate] viewController] setFeedPostDetailCellNode:self];
  90. }
  91. %end
  92. %hook PostActionSheetViewController
  93. -(void) setItems:(id) arg1{
  94. id post = [self post];
  95. if ([post isSelfPost]){
  96. UIImage* origImage = [UIImage imageWithContentsOfFile:@"/var/mobile/Library/Application Support/TFDidThatSay/eye160dark.png"];
  97. CGSize existingImageSize = [[arg1[0] leftIconImage] size];
  98. CGFloat scale = origImage.size.width / existingImageSize.width;
  99. UIImage *newImage = [UIImage imageWithCGImage:[origImage CGImage] scale:scale orientation:origImage.imageOrientation];
  100. id undeleteItem = [[%c(RUIActionSheetItem) alloc] initWithLeftIconImage:newImage text:@"TF did that say?" identifier:@"undeleteItemIdentifier" context:[self post]];
  101. arg1 = [arg1 arrayByAddingObject:undeleteItem];
  102. [undeleteItem release];
  103. }
  104. %orig;
  105. }
  106. -(void) handleDidSelectActionSheetItem:(id) arg1{
  107. %orig;
  108. if ([[arg1 identifier] isEqualToString:@"undeleteItemIdentifier"]){
  109. [self dismissViewControllerAnimated:YES completion:nil];
  110. id post = [self post];
  111. if ([post isSelfPost]){
  112. NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
  113. NSOperationQueue *queue = [[NSOperationQueue alloc] init];
  114. [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://api.pushshift.io/reddit/search/submission/?ids=%@&fields=author,selftext",[[post pk] componentsSeparatedByString:@"_"][1]]]];
  115. [request setHTTPMethod:@"GET"];
  116. [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
  117. NSString *author = @"[author]";
  118. NSString *body = @"[body]";
  119. if (data != nil && error == nil){
  120. id jsonData = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
  121. author = [[jsonData objectForKey:@"data"][0] objectForKey:@"author"];
  122. body = [[jsonData objectForKey:@"data"][0] objectForKey:@"selftext"];
  123. if ([body isEqualToString:@"[deleted]"] || [body isEqualToString:@"[removed]"]){
  124. body = @"[comment was unable to be archived]";
  125. }
  126. } else if (error != nil || data == nil){
  127. body = @"[an error occured]";
  128. }
  129. id themeManager = [[%c(ThemeManager) alloc] initWithTraitCollection:nil appSettings:[%c(AppSettings) sharedSettings]];
  130. id isNightMode = [[[%c(AccountManager) sharedManager] defaults] objectForKey:@"kUseNightKey"];
  131. id textColor;
  132. if (isNightMode) {
  133. textColor = [[themeManager nightTheme] bodyTextColor];
  134. } else{
  135. textColor = [[themeManager dayTheme] bodyTextColor];
  136. }
  137. NSMutableAttributedString *bodyMutableAttributedText = [[NSMutableAttributedString alloc] initWithAttributedString:[%c(NSAttributedStringMarkdownParser) attributedStringUsingCurrentConfig:body]];
  138. [bodyMutableAttributedText beginEditing];
  139. [bodyMutableAttributedText enumerateAttribute:NSForegroundColorAttributeName inRange:NSMakeRange(0, bodyMutableAttributedText.length) options:0 usingBlock:^(id _Nullable value, NSRange range, BOOL * _Nonnull stop) {
  140. [bodyMutableAttributedText removeAttribute:NSForegroundColorAttributeName range:range];
  141. [bodyMutableAttributedText addAttribute:NSForegroundColorAttributeName value:textColor range:range];
  142. }];
  143. [bodyMutableAttributedText endEditing];
  144. [post setValue:bodyMutableAttributedText forKey:@"selfPostRichTextAttributed"];
  145. [post setValue:bodyMutableAttributedText forKey:@"previewFeedPostTextString"];
  146. [post setAuthor:author];
  147. [post setValue:body forKey:@"selfText"];
  148. [[[[self postActionSheetDelegate] controller] feedPostDetailCellNode] configureSelfTextNode];
  149. [request release];
  150. [queue release];
  151. [bodyMutableAttributedText release];
  152. [themeManager release];
  153. }];
  154. }
  155. }
  156. }
  157. %end
  158. %end
  159. %group Redditv3
  160. %hook CommentView
  161. %new
  162. -(void) buttonAction {
  163. id commentsViewController = [self delegate];
  164. id comment = [self comment];
  165. NSError* error;
  166. NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
  167. [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://api.pushshift.io/reddit/search/comment/?ids=%@&fields=author,body",[comment pkWithoutPrefix]]]];
  168. [request setHTTPMethod:@"GET"];
  169. NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
  170. NSString *author = @"[author]";
  171. NSString *body = @"[body]";
  172. if (data != nil && error == nil){
  173. id jsonData = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
  174. author = [[jsonData objectForKey:@"data"][0] objectForKey:@"author"];
  175. body = [[jsonData objectForKey:@"data"][0] objectForKey:@"body"];
  176. if ([body isEqualToString:@"[deleted]"] || [body isEqualToString:@"[removed]"]){
  177. body = @"[comment was unable to be archived]";
  178. }
  179. } else if (error != nil || data == nil){
  180. body = @"[an error occured]";
  181. }
  182. [comment setValue:author forKey:@"author"];
  183. [comment setValue:[%c(MarkDownParser) attributedStringFromMarkdownString: body] forKey:@"bodyAttributedText"];
  184. [comment setValue:body forKey:@"bodyText"];
  185. [commentsViewController reloadCommentsWithNewCommentsHighlight:NO autoScroll:NO animated:NO];
  186. }
  187. -(id) initWithFrame:(id)arg1{
  188. id orig = %orig;
  189. id commandView = [self commandView];
  190. UIButton *undeleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
  191. [undeleteButton addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
  192. UIImage* undeleteImage = [UIImage imageWithContentsOfFile:@"/var/mobile/Library/Application Support/TFDidThatSay/eye160dark.png"];
  193. [undeleteButton setImage:undeleteImage forState:UIControlStateNormal];
  194. [commandView setUndeleteButton:undeleteButton];
  195. [commandView addSubview:undeleteButton];
  196. return orig;
  197. }
  198. %end
  199. %hook CommentCommandView
  200. %property (assign, nonatomic) id undeleteButton;
  201. -(void) layoutSubviews{
  202. %orig;
  203. UIButton *button = [self undeleteButton];
  204. button.frame = CGRectMake([[self overflowButton ] frame].origin.x - 32, 0, 32, 32);
  205. }
  206. %end
  207. %end
  208. %ctor{
  209. NSString* processName = [[NSProcessInfo processInfo] processName];
  210. NSString* version = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
  211. NSArray* versionArray = [version componentsSeparatedByString:@"."];
  212. if ([processName isEqualToString:@"Reddit"]){
  213. if ([versionArray[0] isEqualToString:@"4"]){
  214. %init(Redditv4);
  215. } else if ([versionArray[0] isEqualToString:@"3"]) {
  216. %init(Redditv3);
  217. }
  218. }
  219. }