No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

290 líneas
11KB

  1. #import "Narwhal.h"
  2. %group Narwhal
  3. UIAlertController* recreateActionSheet(id controller, id comment, NSInteger commentIndex){
  4. UIAlertController* alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:nil];
  5. UIAlertAction* pmAction = [UIAlertAction actionWithTitle:[NSString stringWithFormat:@"private message %@", [comment author]] style:nil handler:^(UIAlertAction* action){[controller _handleActionSheetPrivateMessage:comment];}];
  6. UIAlertAction* viewProfileAction = [UIAlertAction actionWithTitle:@"view profile" style:nil handler:^(UIAlertAction* action){[controller _handleActionSheetViewProfile:comment];}];
  7. UIAlertAction* shareAction = [UIAlertAction actionWithTitle:@"share comment" style:nil handler:^(UIAlertAction* action){[controller _handleActionSheetShareComment:comment];}];
  8. UIAlertAction* copyAction = [UIAlertAction actionWithTitle:@"copy text" style:nil handler:^(UIAlertAction* action){[controller _handleActionSheetCopyCommentText:comment];}];
  9. UIAlertAction* reportAction = [UIAlertAction actionWithTitle:@"report comment" style:nil handler:^(UIAlertAction* action){[controller _handleActionSheetReportComment:comment];}];
  10. UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"cancel" style:UIAlertActionStyleCancel handler:nil];
  11. [alert addAction:pmAction];
  12. [alert addAction:viewProfileAction];
  13. [alert addAction:shareAction];
  14. [alert addAction:copyAction];
  15. if ([comment isSaved]){
  16. UIAlertAction* unsaveAction = [UIAlertAction actionWithTitle:@"unsave comment" style:nil handler:^(UIAlertAction* action){[controller _handleActionSheetUnsaveComment:comment index:commentIndex];}];
  17. [alert addAction:unsaveAction];
  18. } else {
  19. UIAlertAction* saveAction = [UIAlertAction actionWithTitle:@"save comment" style:nil handler:^(UIAlertAction* action){[controller _handleActionSheetSaveComment:comment index:commentIndex];}];
  20. [alert addAction:saveAction];
  21. }
  22. if ([[[comment parentID] componentsSeparatedByString:@"_"][0] isEqualToString:@"t1"]){
  23. UIAlertAction* viewParentAction = [UIAlertAction actionWithTitle:@"view parent" style: nil handler:^(UIAlertAction* action){[controller _handleActionSheetViewParent:comment];}];
  24. [alert addAction:viewParentAction];
  25. }
  26. if ([[comment author] isEqualToString:[[%c(NRTAuthManager) sharedManager] currentUsername]]) {
  27. UIAlertAction* editAction = [UIAlertAction actionWithTitle:@"edit comment" style:nil handler:^(UIAlertAction* action){[controller _handleActionSheetEditComment:comment];}];
  28. UIAlertAction* deleteAction = [UIAlertAction actionWithTitle:@"delete comment" style:nil handler:^(UIAlertAction* action){[controller _handleActionSheetDeleteComment:comment];}];
  29. [alert addAction:editAction];
  30. [alert addAction:deleteAction];
  31. }
  32. [alert addAction:reportAction];
  33. [alert addAction:cancelAction];
  34. UIAlertAction* undeleteAction = [UIAlertAction actionWithTitle:@"tf did that say?" style:nil handler:^(UIAlertAction* action){[controller handleUndeleteCommentAction:comment];}];
  35. [alert addAction:undeleteAction];
  36. return alert;
  37. }
  38. void getUndeleteCommentData(id controller, id comment){
  39. NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
  40. NSOperationQueue *queue = [[NSOperationQueue alloc] init];
  41. [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://api.pushshift.io/reddit/search/comment/?ids=%@&fields=author,body",[[comment fullName] componentsSeparatedByString:@"_"][1]]]];
  42. [request setHTTPMethod:@"GET"];
  43. [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
  44. NSString *author = @"[author]";
  45. NSString *body = @"[body]";
  46. if (data != nil && error == nil){
  47. id jsonData = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
  48. if ([[jsonData objectForKey:@"data"] count] != 0){
  49. author = [[jsonData objectForKey:@"data"][0] objectForKey:@"author"];
  50. body = [[jsonData objectForKey:@"data"][0] objectForKey:@"body"];
  51. if ([body isEqualToString:@"[deleted]"] || [body isEqualToString:@"[removed]"]){
  52. body = @"[pushshift was unable to archive this]";
  53. }
  54. if (!body){
  55. body = @"[wtf]";
  56. }
  57. if (!author){
  58. author = @"[wtf]";
  59. }
  60. } else {
  61. body = @"[pushshift has not archived this yet]";
  62. }
  63. } else if (error != nil || data == nil){
  64. body = @"[an error occured]";
  65. }
  66. [controller performSelectorOnMainThread:@selector(completeUndeleteComment:) withObject:@{@"body":body, @"author":author, @"comment":comment} waitUntilDone:NO];
  67. }];
  68. }
  69. %hook NRTLinkViewController
  70. %new
  71. -(void) completeUndeleteComment:(id) data{
  72. id comment = data[@"comment"];
  73. if (comment){
  74. MSHookIvar<NSString*>(comment, "_author") = data[@"author"];
  75. MSHookIvar<NSString*>(comment, "_body") = data[@"body"];
  76. [[self commentsManager] updateComment:comment fromEdited:comment];
  77. }
  78. }
  79. %new
  80. -(void) completeUndeletePost:(id) data{
  81. id post = data[@"post"];
  82. MSHookIvar<NSString*>(post, "_author") = data[@"author"];
  83. NSAttributedString* postBodyAttributedString = [%c(NRTMarkdownManager) attributedStringFromMarkdown:data[@"body"] type:0];
  84. [self setLinkText:postBodyAttributedString];
  85. [[self tableView] reloadData];
  86. }
  87. %new
  88. -(void) handleUndeleteCommentAction:(id) comment{
  89. getUndeleteCommentData(self, comment);
  90. }
  91. %new
  92. -(void) handleUndeletePostAction{
  93. id post = [self link];
  94. NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
  95. NSOperationQueue *queue = [[NSOperationQueue alloc] init];
  96. [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://api.pushshift.io/reddit/search/submission/?ids=%@&fields=author,selftext",[[post fullName] componentsSeparatedByString:@"_"][1]]]];
  97. [request setHTTPMethod:@"GET"];
  98. [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
  99. NSString *author = @"[author]";
  100. NSString *body = @"[body]";
  101. if (data != nil && error == nil){
  102. id jsonData = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
  103. if ([[jsonData objectForKey:@"data"] count] != 0){
  104. author = [[jsonData objectForKey:@"data"][0] objectForKey:@"author"];
  105. body = [[jsonData objectForKey:@"data"][0] objectForKey:@"selftext"];
  106. if ([body isEqualToString:@"[deleted]"] || [body isEqualToString:@"[removed]"]){
  107. body = @"[pushshift was unable to archive this]";
  108. }
  109. } else {
  110. body = @"[pushshift has not archived this yet]";
  111. }
  112. } else if (error != nil || data == nil){
  113. body = @"[an error occured]";
  114. }
  115. [self performSelectorOnMainThread:@selector(completeUndeletePost:) withObject:@{@"body":body, @"author":author, @"post":post} waitUntilDone:NO];
  116. }];
  117. }
  118. -(void) swipeCell:(id) arg1 didEndDragWithState:(NSUInteger) arg2{
  119. if (arg2 == 2){
  120. if ([arg1 isKindOfClass:[%c(NRTCommentTableViewCell) class]]) {
  121. id comment = [arg1 comment];
  122. NSInteger commentIndex = [[[self commentsManager] comments] indexOfObject:comment];
  123. UIAlertController* alert = recreateActionSheet(self, comment, commentIndex);
  124. [self presentViewController:alert animated:YES completion:nil];
  125. } else {
  126. %orig;
  127. }
  128. } else {
  129. %orig;
  130. }
  131. }
  132. -(void) _dotsButtonTouched:(id) arg1{
  133. id post = [self link];
  134. BOOL shouldHaveUndeleteAction = NO;
  135. UIAlertController* alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:nil];
  136. UIAlertAction* undeletePostAction;
  137. UIAlertAction* sharePostAction = [UIAlertAction actionWithTitle:@"share reddit post" style:nil handler:^(UIAlertAction* action){[self _handleActionSheetSharePost];}];
  138. UIAlertAction* sortCommentsAction = [UIAlertAction actionWithTitle:@"sort comments" style:nil handler:^(UIAlertAction* action){[self _handleActionSheetSortComments];}];
  139. UIAlertAction* refreshCommentsAction = [UIAlertAction actionWithTitle:@"refresh comments" style:nil handler:^(UIAlertAction* action){[self _handleActionSheetRefreshComments];}];
  140. UIAlertAction* reportPostAction = [UIAlertAction actionWithTitle:@"report post" style:nil handler:^(UIAlertAction* action){[self _handleActionSheetReportPost];}];
  141. UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"cancel" style:UIAlertActionStyleCancel handler:nil];
  142. [alert addAction:sharePostAction];
  143. [alert addAction:sortCommentsAction];
  144. [alert addAction:refreshCommentsAction];
  145. if ([self linkTextOffscreenCell]){
  146. UIAlertAction* refreshPostAction = [UIAlertAction actionWithTitle:@"refresh post" style:nil handler:^(UIAlertAction* action){[self _handleActionSheetRefreshPost];}];
  147. [alert addAction:refreshPostAction];
  148. undeletePostAction = [UIAlertAction actionWithTitle:@"tf did that say?" style:nil handler:^(UIAlertAction* action){[self handleUndeletePostAction];}];
  149. shouldHaveUndeleteAction = YES;
  150. }
  151. if ([[post author] isEqualToString:[[%c(NRTAuthManager) sharedManager] currentUsername]]){
  152. UIAlertAction* editPostAction = [UIAlertAction actionWithTitle:@"edit post" style:nil handler:^(UIAlertAction* action){[self _handleActionSheetEditPost];}];
  153. UIAlertAction* deletePostAction = [UIAlertAction actionWithTitle:@"delete post" style:nil handler:^(UIAlertAction* action){[self _handleActionSheetDeletePost];}];
  154. [alert addAction:editPostAction];
  155. [alert addAction:deletePostAction];
  156. }
  157. [alert addAction:reportPostAction];
  158. [alert addAction:cancelAction];
  159. if (shouldHaveUndeleteAction){
  160. [alert addAction:undeletePostAction];
  161. }
  162. [self presentViewController:alert animated:YES completion:nil];
  163. }
  164. %end
  165. %hook NRTMediaTableViewDataSource
  166. %new
  167. -(void) completeUndeleteComment:(id) data{
  168. id comment = data[@"comment"];
  169. if (comment){
  170. MSHookIvar<NSString*>(comment, "_author") = data[@"author"];
  171. MSHookIvar<NSString*>(comment, "_body") = data[@"body"];
  172. [[self commentsManager] updateComment:comment fromEdited:comment];
  173. }
  174. }
  175. %new
  176. -(void) handleUndeleteCommentAction:(id) comment{
  177. getUndeleteCommentData(self, comment);
  178. }
  179. -(void) swipeCell:(id) arg1 didEndDragWithState:(NSUInteger) arg2{
  180. if (arg2 == 2){
  181. if ([arg1 isKindOfClass:[%c(NRTCommentTableViewCell) class]]) {
  182. id comment = [arg1 comment];
  183. NSInteger commentIndex = [[[self commentsManager] comments] indexOfObject:comment];
  184. UIAlertController* alert = recreateActionSheet(self, comment, commentIndex);
  185. [[self parentController] presentViewController:alert animated:YES completion:nil];
  186. } else {
  187. %orig;
  188. }
  189. } else {
  190. %orig;
  191. }
  192. }
  193. %end
  194. %end
  195. %ctor {
  196. NSString* processName = [[NSProcessInfo processInfo] processName];
  197. if ([processName isEqualToString:@"narwhal"]){
  198. %init(Narwhal);
  199. }
  200. }