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.

228 lines
6.7KB

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