Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

227 rindas
6.3KB

  1. #import <Cephei/HBPreferences.h>
  2. #import "Narwhal.h"
  3. HBPreferences *narwhalPrefs;
  4. CGFloat narwhalRequestTimeoutValue;
  5. %group Narwhal
  6. BOOL shouldHaveUndeleteAction = NO;
  7. id tfComment;
  8. id tfController;
  9. void getUndeleteCommentData(id controller, id comment){
  10. NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
  11. NSOperationQueue *queue = [[NSOperationQueue alloc] init];
  12. [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://api.pushshift.io/reddit/search/comment/?ids=%@&fields=author,body",[[comment fullName] componentsSeparatedByString:@"_"][1]]]];
  13. [request setHTTPMethod:@"GET"];
  14. [request setTimeoutInterval:[narwhalPrefs doubleForKey:@"requestTimeoutValue" default:10]];
  15. [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
  16. NSString *author = @"[author]";
  17. NSString *body = @"[body]";
  18. if (data != nil && error == nil){
  19. id jsonData = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
  20. if ([[jsonData objectForKey:@"data"] count] != 0){
  21. author = [[jsonData objectForKey:@"data"][0] objectForKey:@"author"];
  22. body = [[jsonData objectForKey:@"data"][0] objectForKey:@"body"];
  23. if ([body isEqualToString:@"[deleted]"] || [body isEqualToString:@"[removed]"]){
  24. body = @"[pushshift was unable to archive this]";
  25. }
  26. } else {
  27. body = @"[pushshift has not archived this yet]";
  28. }
  29. } else if (error != nil || data == nil){
  30. body = [NSString stringWithFormat:@"[an error occured while attempting to contact pushshift api (%@)]", [error localizedDescription]];
  31. }
  32. [controller performSelectorOnMainThread:@selector(completeUndeleteComment:) withObject:@{@"body":body, @"author":author, @"comment":comment} waitUntilDone:NO];
  33. }];
  34. }
  35. %hook UIViewController
  36. -(void) presentViewController:(id) arg1 animated:(BOOL) arg2 completion:(id) arg3{
  37. if ([arg1 isKindOfClass:[UIAlertController class]] && shouldHaveUndeleteAction){
  38. UIAlertAction* undeleteAction;
  39. if (tfComment){
  40. undeleteAction = [UIAlertAction actionWithTitle:@"tf did that say?" style:nil handler:^(UIAlertAction* action){getUndeleteCommentData(tfController, tfComment);}];
  41. } else {
  42. undeleteAction = [UIAlertAction actionWithTitle:@"tf did that say?" style:nil handler:^(UIAlertAction* action){[tfController handleUndeletePostAction];}];
  43. }
  44. [arg1 addAction:undeleteAction];
  45. }
  46. %orig;
  47. }
  48. %end
  49. %hook NRTLinkViewController
  50. %new
  51. -(void) completeUndeleteComment:(id) data{
  52. id comment = data[@"comment"];
  53. if (comment){
  54. MSHookIvar<NSString*>(comment, "_author") = data[@"author"];
  55. MSHookIvar<NSString*>(comment, "_body") = data[@"body"];
  56. [[self commentsManager] updateComment:comment fromEdited:comment];
  57. }
  58. }
  59. %new
  60. -(void) completeUndeletePost:(id) data{
  61. id post = data[@"post"];
  62. MSHookIvar<NSString*>(post, "_author") = data[@"author"];
  63. NSAttributedString* postBodyAttributedString = [%c(NRTMarkdownManager) attributedStringFromMarkdown:data[@"body"] type:0];
  64. [self setLinkText:postBodyAttributedString];
  65. [[self tableView] reloadData];
  66. }
  67. %new
  68. -(void) handleUndeletePostAction{
  69. id post = [self link];
  70. NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
  71. NSOperationQueue *queue = [[NSOperationQueue alloc] init];
  72. [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://api.pushshift.io/reddit/search/submission/?ids=%@&fields=author,selftext",[[post fullName] componentsSeparatedByString:@"_"][1]]]];
  73. [request setHTTPMethod:@"GET"];
  74. [request setTimeoutInterval:[narwhalPrefs doubleForKey:@"requestTimeoutValue" default:10]];
  75. [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
  76. NSString *author = @"[author]";
  77. NSString *body = @"[body]";
  78. if (data != nil && error == nil){
  79. id jsonData = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
  80. if ([[jsonData objectForKey:@"data"] count] != 0){
  81. author = [[jsonData objectForKey:@"data"][0] objectForKey:@"author"];
  82. body = [[jsonData objectForKey:@"data"][0] objectForKey:@"selftext"];
  83. if ([body isEqualToString:@"[deleted]"] || [body isEqualToString:@"[removed]"]){
  84. body = @"[pushshift was unable to archive this]";
  85. }
  86. } else {
  87. body = @"[pushshift has not archived this yet]";
  88. }
  89. } else if (error != nil || data == nil){
  90. body = [NSString stringWithFormat:@"[an error occured while attempting to contact pushshift api (%@)]", [error localizedDescription]];
  91. }
  92. [self performSelectorOnMainThread:@selector(completeUndeletePost:) withObject:@{@"body":body, @"author":author, @"post":post} waitUntilDone:NO];
  93. }];
  94. }
  95. -(void) swipeCell:(id) arg1 didEndDragWithState:(NSUInteger) arg2{
  96. if (arg2 == 2){
  97. if ([arg1 isKindOfClass:[%c(NRTCommentTableViewCell) class]]) {
  98. tfComment = [arg1 comment];
  99. tfController = self;
  100. shouldHaveUndeleteAction = YES;
  101. }
  102. }
  103. %orig;
  104. shouldHaveUndeleteAction = NO;
  105. }
  106. -(void) _dotsButtonTouched:(id) arg1{
  107. if ([self linkTextOffscreenCell]){
  108. tfController = self;
  109. tfComment = nil;
  110. shouldHaveUndeleteAction = YES;
  111. }
  112. %orig;
  113. shouldHaveUndeleteAction = NO;
  114. }
  115. %end
  116. %hook NRTMediaTableViewDataSource
  117. %new
  118. -(void) completeUndeleteComment:(id) data{
  119. id comment = data[@"comment"];
  120. if (comment){
  121. MSHookIvar<NSString*>(comment, "_author") = data[@"author"];
  122. MSHookIvar<NSString*>(comment, "_body") = data[@"body"];
  123. [[self commentsManager] updateComment:comment fromEdited:comment];
  124. }
  125. }
  126. -(void) swipeCell:(id) arg1 didEndDragWithState:(NSUInteger) arg2{
  127. if (arg2 == 2){
  128. if ([arg1 isKindOfClass:[%c(NRTCommentTableViewCell) class]]) {
  129. tfComment = [arg1 comment];
  130. tfController = self;
  131. shouldHaveUndeleteAction = YES;
  132. }
  133. }
  134. %orig;
  135. shouldHaveUndeleteAction = NO;
  136. }
  137. %end
  138. %end
  139. %ctor {
  140. narwhalPrefs = [[HBPreferences alloc] initWithIdentifier:@"com.lint.undelete.prefs"];
  141. [narwhalPrefs registerDouble:&narwhalRequestTimeoutValue default:10 forKey:@"requestTimeoutValue"];
  142. NSString* processName = [[NSProcessInfo processInfo] processName];
  143. if ([processName isEqualToString:@"narwhal"]){
  144. %init(Narwhal);
  145. }
  146. }