Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

207 lines
5.6KB

  1. #import "BaconReader.h"
  2. #import "assets/TFHelper.h"
  3. #import "assets/MMMarkdown/MMMarkdown.h"
  4. static BOOL isEnabled;
  5. static BOOL isBaconReaderEnabled;
  6. static BOOL isTFDeletedOnly;
  7. static CGFloat pushshiftRequestTimeoutValue;
  8. %group BaconReader
  9. BOOL shouldHaveBRUndeleteAction = NO;
  10. NSString *tfPostSelftext;
  11. NSString *tfPostAuthor;
  12. id tfCommentCellView;
  13. id tfStoryController;
  14. %hook CommentCell
  15. %end
  16. %hook CommentCellView
  17. %end
  18. %hook StoryDetailView
  19. %end
  20. %hook BRComment
  21. - (BOOL)contains_htmlValue {
  22. return YES;
  23. }
  24. - (BOOL)primitiveContains_htmlValue {
  25. return YES;
  26. }
  27. - (BOOL)is_deletedValue {
  28. return NO;
  29. }
  30. - (BOOL)primitiveIs_deletedValue {
  31. return NO;
  32. }
  33. %end
  34. %hook BRStory
  35. + (id)storyWithDictionary:(id)arg1 inContext:(id)arg2 {
  36. id orig = %orig;
  37. if (tfPostSelftext) {
  38. [orig setSelftext_html:tfPostSelftext];
  39. [orig setAuthor:tfPostAuthor];
  40. tfPostSelftext = nil;
  41. tfPostAuthor = nil;
  42. }
  43. return orig;
  44. }
  45. %end
  46. %hook UIViewController
  47. - (void)presentViewController:(id)arg1 animated:(BOOL)arg2 completion:(id)arg3 {
  48. if ([arg1 isKindOfClass:[UIAlertController class]] && shouldHaveBRUndeleteAction) {
  49. UIAlertAction *undeleteAction;
  50. if (tfCommentCellView) {
  51. undeleteAction = [UIAlertAction actionWithTitle:@"TF Did That Say?" style:UIAlertActionStyleDefault handler:^(UIAlertAction* action){[tfStoryController handleUndeleteCommentAction];}];
  52. } else {
  53. undeleteAction = [UIAlertAction actionWithTitle:@"TF Did That Say?" style:UIAlertActionStyleDefault handler:^(UIAlertAction* action){[tfStoryController handleUndeletePostAction];}];
  54. }
  55. [arg1 addAction:undeleteAction];
  56. }
  57. %orig;
  58. }
  59. %end
  60. %hook StoryDetailViewController
  61. - (void)showMoreCommentActions:(id)arg1 showAll:(BOOL)arg2 {
  62. NSString *commentBody = [[arg1 comment] body];
  63. if ([%c(TFHelper) shouldShowUndeleteButtonWithInfo:commentBody isDeletedOnly:isTFDeletedOnly]) {
  64. shouldHaveBRUndeleteAction = YES;
  65. tfCommentCellView = arg1;
  66. tfStoryController = self;
  67. tfPostSelftext = nil;
  68. }
  69. %orig;
  70. shouldHaveBRUndeleteAction = NO;
  71. }
  72. - (void)menuTouchedWithSender:(id)arg1 {
  73. if ([[self story] is_selfValue]) {
  74. NSString *postBody = [[self story] selftext];
  75. if ([%c(TFHelper) shouldShowUndeleteButtonWithInfo:postBody isDeletedOnly:isTFDeletedOnly]) {
  76. shouldHaveBRUndeleteAction = YES;
  77. tfCommentCellView = nil;
  78. tfStoryController = self;
  79. tfPostSelftext = nil;
  80. }
  81. }
  82. %orig;
  83. shouldHaveBRUndeleteAction = NO;
  84. }
  85. %new
  86. - (void)handleUndeleteCommentAction {
  87. [%c(TFHelper) getUndeleteDataWithID:[[tfCommentCellView comment] serverID] isComment:YES timeout:pushshiftRequestTimeoutValue extraData:nil completionTarget:self completionSelector:@selector(completeUndeleteCommentAction:)];
  88. }
  89. %new
  90. - (void)completeUndeleteCommentAction:(NSDictionary *)data {
  91. id comment = [tfCommentCellView comment];
  92. NSString *body = data[@"body"];
  93. [comment setAuthor:data[@"author"]];
  94. [comment setBody:body];
  95. [comment setBody_html:[%c(MMMarkdown) HTMLStringWithMarkdown:body extensions:MMMarkdownExtensionsGitHubFlavored error:nil]];
  96. [comment setAttributedDescriptionString:nil];
  97. NSAttributedString *commentAttrString = [%c(BRUtils) attributedDescriptionForComment:comment];
  98. [comment setAttributedDescriptionString:commentAttrString];
  99. [[[self detailPage] tableView] reloadData];
  100. }
  101. %new
  102. - (void)handleUndeletePostAction {
  103. [%c(TFHelper) getUndeleteDataWithID:[[self story] serverID] isComment:NO timeout:pushshiftRequestTimeoutValue extraData:nil completionTarget:self completionSelector:@selector(completeUndeletePostAction:)];
  104. }
  105. %new
  106. - (void)completeUndeletePostAction:(NSDictionary *)data {
  107. tfPostAuthor = data[@"author"];
  108. tfPostSelftext = [%c(MMMarkdown) HTMLStringWithMarkdown:data[@"body"] extensions:MMMarkdownExtensionsGitHubFlavored error:nil];
  109. [[self detailPage] refreshTouched];
  110. }
  111. %end
  112. %end
  113. static void loadPrefs(){
  114. NSMutableDictionary *prefs = [[NSMutableDictionary alloc] initWithContentsOfFile:@"/User/Library/Preferences/com.lint.undelete.prefs.plist"];
  115. if (prefs){
  116. isEnabled = [prefs objectForKey:@"isEnabled"] ? [[prefs objectForKey:@"isEnabled"] boolValue] : YES;
  117. isBaconReaderEnabled = [prefs objectForKey:@"isBaconReaderEnabled"] ? [[prefs objectForKey:@"isBaconReaderEnabled"] boolValue] : YES;
  118. isTFDeletedOnly = [prefs objectForKey:@"isTFDeletedOnly"] ? [[prefs objectForKey:@"isTFDeletedOnly"] boolValue] : YES;
  119. pushshiftRequestTimeoutValue = [prefs objectForKey:@"requestTimeoutValue"] ? [[prefs objectForKey:@"requestTimeoutValue"] doubleValue] : 10;
  120. } else {
  121. isEnabled = YES;
  122. isBaconReaderEnabled = YES;
  123. isTFDeletedOnly = YES;
  124. pushshiftRequestTimeoutValue = 10;
  125. }
  126. }
  127. static void prefsChanged(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) {
  128. loadPrefs();
  129. }
  130. %ctor {
  131. loadPrefs();
  132. NSString* processName = [[NSProcessInfo processInfo] processName];
  133. if ([processName isEqualToString:@"BaconReader"]){
  134. if (isBaconReaderEnabled && isEnabled){
  135. CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, (CFNotificationCallback)prefsChanged, CFSTR("com.lint.undelete.prefs.changed"), NULL, CFNotificationSuspensionBehaviorCoalesce);
  136. %init(BaconReader, StoryDetailView = objc_getClass("BaconReader.StoryDetailView"), StoryDetailViewController = objc_getClass("BaconReader.StoryDetailViewController"), CommentCellView = objc_getClass("BaconReader.CommentCellView"), CommentCell = objc_getClass("BaconReader.CommentCell"));
  137. }
  138. }
  139. }