Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. #import "Narwhal.h"
  2. static BOOL isNarwhalEnabled;
  3. static BOOL isTFDeletedOnly;
  4. static CGFloat pushshiftRequestTimeoutValue;
  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:pushshiftRequestTimeoutValue];
  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:UIAlertActionStyleDefault handler:^(UIAlertAction* action){getUndeleteCommentData(tfController, tfComment);}];
  41. } else {
  42. undeleteAction = [UIAlertAction actionWithTitle:@"tf did that say?" style:UIAlertActionStyleDefault 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:pushshiftRequestTimeoutValue];
  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. NSString *commentBody = MSHookIvar<NSString*>([arg1 comment], "_body");
  99. if ((isTFDeletedOnly && ([commentBody isEqualToString:@"[deleted]"] || [commentBody isEqualToString:@"[removed]"])) || !isTFDeletedOnly){
  100. tfComment = [arg1 comment];
  101. tfController = self;
  102. shouldHaveUndeleteAction = YES;
  103. }
  104. }
  105. }
  106. %orig;
  107. shouldHaveUndeleteAction = NO;
  108. }
  109. -(void) _dotsButtonTouched:(id) arg1{
  110. if ([self linkTextOffscreenCell]){
  111. NSString *postBody = [[self link] selfText];
  112. if ((isTFDeletedOnly && ([postBody isEqualToString:@"[deleted]"] || [postBody isEqualToString:@"[removed]"])) || !isTFDeletedOnly){
  113. tfController = self;
  114. tfComment = nil;
  115. shouldHaveUndeleteAction = YES;
  116. }
  117. }
  118. %orig;
  119. shouldHaveUndeleteAction = NO;
  120. }
  121. %end
  122. %hook NRTMediaTableViewDataSource
  123. %new
  124. -(void) completeUndeleteComment:(id) data{
  125. id comment = data[@"comment"];
  126. if (comment){
  127. MSHookIvar<NSString*>(comment, "_author") = data[@"author"];
  128. MSHookIvar<NSString*>(comment, "_body") = data[@"body"];
  129. [[self commentsManager] updateComment:comment fromEdited:comment];
  130. }
  131. }
  132. -(void) swipeCell:(id) arg1 didEndDragWithState:(NSUInteger) arg2{
  133. if (arg2 == 2){
  134. if ([arg1 isKindOfClass:[%c(NRTCommentTableViewCell) class]]) {
  135. NSString *commentBody = MSHookIvar<NSString*>([arg1 comment], "_body");
  136. if ((isTFDeletedOnly && ([commentBody isEqualToString:@"[deleted]"] || [commentBody isEqualToString:@"[removed]"])) || !isTFDeletedOnly){
  137. tfComment = [arg1 comment];
  138. tfController = self;
  139. shouldHaveUndeleteAction = YES;
  140. }
  141. }
  142. }
  143. %orig;
  144. shouldHaveUndeleteAction = NO;
  145. }
  146. %end
  147. %end
  148. static void loadPrefs(){
  149. NSMutableDictionary *prefs = [[NSMutableDictionary alloc] initWithContentsOfFile:@"/User/Library/Preferences/com.lint.undelete.prefs.plist"];
  150. if (prefs){
  151. if ([prefs objectForKey:@"isNarwhalEnabled"] != nil){
  152. isNarwhalEnabled = [[prefs objectForKey:@"isNarwhalEnabled"] boolValue];
  153. } else {
  154. isNarwhalEnabled = YES;
  155. }
  156. if ([prefs objectForKey:@"isTFDeletedOnly"] != nil) {
  157. isTFDeletedOnly = [[prefs objectForKey:@"isTFDeletedOnly"] boolValue];
  158. } else {
  159. isTFDeletedOnly = YES;
  160. }
  161. if ([prefs objectForKey:@"requestTimeoutValue"] != nil){
  162. pushshiftRequestTimeoutValue = [[prefs objectForKey:@"requestTimeoutValue"] doubleValue];
  163. } else {
  164. pushshiftRequestTimeoutValue = 10;
  165. }
  166. } else {
  167. isNarwhalEnabled = YES;
  168. isTFDeletedOnly = YES;
  169. pushshiftRequestTimeoutValue = 10;
  170. }
  171. }
  172. static void prefsChanged(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) {
  173. loadPrefs();
  174. }
  175. %ctor {
  176. loadPrefs();
  177. NSString* processName = [[NSProcessInfo processInfo] processName];
  178. if ([processName isEqualToString:@"narwhal"]){
  179. if (isNarwhalEnabled){
  180. CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, prefsChanged, CFSTR("com.lint.undelete.prefs.changed"), NULL, CFNotificationSuspensionBehaviorDeliverImmediately);
  181. %init(Narwhal);
  182. }
  183. }
  184. }