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.

BaconReader.xm 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 *author = [[arg1 comment] author];
  63. if ([%c(TFHelper) shouldShowUndeleteButtonWithInfo:author 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 *author = [[self story] author];
  75. if ([%c(TFHelper) shouldShowUndeleteButtonWithInfo:author 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. }