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.

241 líneas
6.9KB

  1. #import "Antenna.h"
  2. #import "assets/TFHelper.h"
  3. #import "assets/MMMarkdown/MMMarkdown.h"
  4. static BOOL isAntennaEnabled;
  5. static BOOL isTFDeletedOnly;
  6. static CGFloat pushshiftRequestTimeoutValue;
  7. %group Antenna
  8. BOOL shouldHaveAntennaUndeleteAction = NO;
  9. id tfAntennaController;
  10. id tfAntennaCommentCell;
  11. %hook RCPostSwift
  12. %end
  13. %hook RCCommentTextSwift
  14. %end
  15. %hook RCCommentSwift
  16. -(BOOL) isCommentDeleted{
  17. return NO;
  18. }
  19. %end
  20. %hook RCPostCommentsController
  21. -(void) didLongPressCell:(id) arg1 gesture:(id) arg2 {
  22. NSString *commentBody = [[[arg1 comment] commentText] body];
  23. if ((isTFDeletedOnly && ([commentBody isEqualToString:@"[deleted]"] || [commentBody isEqualToString:@"[removed]"])) || !isTFDeletedOnly){
  24. tfAntennaController = self;
  25. tfAntennaCommentCell = arg1;
  26. shouldHaveAntennaUndeleteAction = YES;
  27. }
  28. %orig;
  29. shouldHaveAntennaUndeleteAction = NO;
  30. }
  31. %new
  32. -(void) handleUndeleteCommentAction{
  33. [%c(TFHelper) getUndeleteDataWithID:[[tfAntennaCommentCell comment] itemId] isComment:YES timeout:pushshiftRequestTimeoutValue extraData:nil completionTarget:self completionSelector:@selector(completeUndeleteCommentAction:)];
  34. }
  35. %new
  36. -(void) completeUndeleteCommentAction:(NSDictionary *) data{
  37. id comment = [tfAntennaCommentCell comment];
  38. id commentText = [comment commentText];
  39. NSString *body = data[@"body"];
  40. [comment setAuthor:data[@"author"]];
  41. [commentText setBody:body];
  42. [commentText setBodyHTML:[%c(MMMarkdown) HTMLStringWithMarkdown:body extensions:MMMarkdownExtensionsGitHubFlavored error:nil]];
  43. [commentText setBodyAttributedString:nil];
  44. [commentText setBodyAttributedStringForPreview:nil];
  45. [commentText setTextHeightCache:nil];
  46. [self setCommentHeightCache:nil];
  47. [tfAntennaCommentCell updateWithModelObject:comment];
  48. [[[self delegate] tableView] reloadData];
  49. }
  50. %end
  51. %hook AHKActionSheet
  52. -(void)show{
  53. if (shouldHaveAntennaUndeleteAction){
  54. UIImage *undeleteImage = [UIImage imageWithContentsOfFile:@"/var/mobile/Library/Application Support/TFDidThatSay/eye160dark.png"];
  55. AHKActionSheetItem *actionItem = [self items][0];
  56. CGSize actionItemImageSize = [[actionItem image] size];
  57. CGSize newUndeleteImageSize = CGSizeMake(actionItemImageSize.width * 2, actionItemImageSize.height * 2);
  58. UIGraphicsBeginImageContext(newUndeleteImageSize);
  59. [undeleteImage drawInRect:CGRectMake(0, 0, newUndeleteImageSize.width, newUndeleteImageSize.height)];
  60. undeleteImage = UIGraphicsGetImageFromCurrentImageContext();
  61. UIGraphicsEndImageContext();
  62. undeleteImage = [[UIImage alloc] initWithCGImage:[undeleteImage CGImage] scale:2 orientation:UIImageOrientationUp];
  63. [self addButtonWithTitle:@"TF did that say?" image:undeleteImage type:0 handler:^{[tfAntennaController handleUndeleteCommentAction];}];
  64. }
  65. %orig;
  66. }
  67. %end
  68. %hook RCPostActionsSectionHeader
  69. %property(strong, nonatomic) UIButton *undeleteButton;
  70. -(void) layoutSubviews{
  71. BOOL isAbleToUndeletePost = NO;
  72. id post = [[self delegate] postInternal];
  73. NSString *postBody = [[post selfCommentText] body];
  74. if ([post isSelfPost]){
  75. if ((isTFDeletedOnly && ([postBody isEqualToString:@"[deleted]"] || [postBody isEqualToString:@"[removed]"])) || !isTFDeletedOnly) {
  76. isAbleToUndeletePost = YES;
  77. NSMutableArray *barButtons = [self defaultHeaderButtons];
  78. if ([barButtons count] <= 5){
  79. UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
  80. [barButtons addObject:tempView];
  81. }
  82. %orig;
  83. if (![self undeleteButton]){
  84. UIButton *undeleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
  85. [undeleteButton addTarget:[[self delegate] headerCellController] action:@selector(handleUndeletePostAction:) forControlEvents:UIControlEventTouchUpInside];
  86. [undeleteButton setImage:[[UIImage imageWithContentsOfFile:@"/var/mobile/Library/Application Support/TFDidThatSay/eye160dark.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal];
  87. undeleteButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
  88. undeleteButton.tintColor = [barButtons[4] defaultColor];
  89. [self addSubview:undeleteButton];
  90. [self setUndeleteButton:undeleteButton];
  91. }
  92. [[self undeleteButton] setFrame:[barButtons[[barButtons count] - 1] frame]];
  93. }
  94. }
  95. if (!isAbleToUndeletePost){
  96. %orig;
  97. }
  98. }
  99. %end
  100. %hook RCPostHeaderCellController
  101. %new
  102. -(void) handleUndeletePostAction:(id) sender{
  103. [sender setEnabled:NO];
  104. [%c(TFHelper) getUndeleteDataWithID:[[self post] itemId] isComment:NO timeout:pushshiftRequestTimeoutValue extraData:@{@"sender" : sender} completionTarget:self completionSelector:@selector(completeUndeletePostAction:)];
  105. }
  106. %new
  107. -(void) completeUndeletePostAction:(NSDictionary *) data{
  108. id post = [self post];
  109. id postText = [post selfCommentText];
  110. NSString *body = data[@"body"];
  111. [post setAuthor:data[@"author"]];
  112. [postText setBody:body];
  113. [postText setBodyHTML:[%c(MMMarkdown) HTMLStringWithMarkdown:body extensions:MMMarkdownExtensionsGitHubFlavored error:nil]];
  114. [postText setBodyAttributedString:nil];
  115. [postText setBodyAttributedStringForPreview:nil];
  116. [postText setTextHeightCache:nil];
  117. [self loadView];
  118. [[self tableView] reloadData];
  119. [data[@"sender"] setEnabled:YES];
  120. }
  121. %end
  122. %end
  123. static void loadPrefs(){
  124. NSMutableDictionary *prefs = [[NSMutableDictionary alloc] initWithContentsOfFile:@"/User/Library/Preferences/com.lint.undelete.prefs.plist"];
  125. if (prefs){
  126. if ([prefs objectForKey:@"isAntennaEnabled"] != nil){
  127. isAntennaEnabled = [[prefs objectForKey:@"isAntennaEnabled"] boolValue];
  128. } else {
  129. isAntennaEnabled = YES;
  130. }
  131. if ([prefs objectForKey:@"isTFDeletedOnly"] != nil){
  132. isTFDeletedOnly = [[prefs objectForKey:@"isTFDeletedOnly"] boolValue];
  133. } else {
  134. isTFDeletedOnly = YES;
  135. }
  136. if ([prefs objectForKey:@"requestTimeoutValue"] != nil){
  137. pushshiftRequestTimeoutValue = [[prefs objectForKey:@"requestTimeoutValue"] doubleValue];
  138. } else {
  139. pushshiftRequestTimeoutValue = 10;
  140. }
  141. } else {
  142. isAntennaEnabled = YES;
  143. isTFDeletedOnly = YES;
  144. pushshiftRequestTimeoutValue = 10;
  145. }
  146. }
  147. static void prefsChanged(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) {
  148. loadPrefs();
  149. }
  150. %ctor{
  151. loadPrefs();
  152. NSString* processName = [[NSProcessInfo processInfo] processName];
  153. if ([processName isEqualToString:@"amrc"]){
  154. if (isAntennaEnabled){
  155. CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, prefsChanged, CFSTR("com.lint.undelete.prefs.changed"), NULL, CFNotificationSuspensionBehaviorDeliverImmediately);
  156. %init(Antenna, RCCommentSwift = objc_getClass("amrc.RCCommentSwift"), RCPostSwift = objc_getClass("amrc.RCPostSwift"), RCCommentTextSwift = objc_getClass("amrc.RCCommentTextSwift"));
  157. }
  158. }
  159. }