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.

Narwhal.xm 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #import "Narwhal.h"
  2. #import "assets/TFHelper.h"
  3. static BOOL isNarwhalEnabled;
  4. static BOOL isTFDeletedOnly;
  5. static CGFloat pushshiftRequestTimeoutValue;
  6. %group Narwhal
  7. BOOL shouldHaveUndeleteAction = NO;
  8. id tfComment;
  9. id tfController;
  10. void getUndeleteCommentData(id controller, id comment){
  11. [%c(TFHelper) getUndeleteDataWithID:[[comment fullName] componentsSeparatedByString:@"_"][1] isComment:YES timeout:pushshiftRequestTimeoutValue extraData:@{@"comment" : comment} completionTarget:controller completionSelector:@selector(completeUndeleteCommentAction:)];
  12. }
  13. %hook UIViewController
  14. -(void) presentViewController:(id) arg1 animated:(BOOL) arg2 completion:(id) arg3{
  15. if ([arg1 isKindOfClass:[UIAlertController class]] && shouldHaveUndeleteAction){
  16. UIAlertAction* undeleteAction;
  17. if (tfComment){
  18. undeleteAction = [UIAlertAction actionWithTitle:@"tf did that say?" style:UIAlertActionStyleDefault handler:^(UIAlertAction* action){getUndeleteCommentData(tfController, tfComment);}];
  19. } else {
  20. undeleteAction = [UIAlertAction actionWithTitle:@"tf did that say?" style:UIAlertActionStyleDefault handler:^(UIAlertAction* action){[tfController handleUndeletePostAction];}];
  21. }
  22. [arg1 addAction:undeleteAction];
  23. }
  24. %orig;
  25. }
  26. %end
  27. %hook NRTLinkViewController
  28. -(void) swipeCell:(id) arg1 didEndDragWithState:(NSUInteger) arg2{
  29. if (arg2 == 2){
  30. if ([arg1 isKindOfClass:[%c(NRTCommentTableViewCell) class]]) {
  31. NSString *commentBody = MSHookIvar<NSString*>([arg1 comment], "_body");
  32. if ((isTFDeletedOnly && ([commentBody isEqualToString:@"[deleted]"] || [commentBody isEqualToString:@"[removed]"])) || !isTFDeletedOnly){
  33. tfComment = [arg1 comment];
  34. tfController = self;
  35. shouldHaveUndeleteAction = YES;
  36. }
  37. }
  38. }
  39. %orig;
  40. shouldHaveUndeleteAction = NO;
  41. }
  42. -(void) _dotsButtonTouched:(id) arg1{
  43. if ([self linkTextOffscreenCell]){
  44. NSString *postBody = [[self link] selfText];
  45. if ((isTFDeletedOnly && ([postBody isEqualToString:@"[deleted]"] || [postBody isEqualToString:@"[removed]"])) || !isTFDeletedOnly){
  46. tfController = self;
  47. tfComment = nil;
  48. shouldHaveUndeleteAction = YES;
  49. }
  50. }
  51. %orig;
  52. shouldHaveUndeleteAction = NO;
  53. }
  54. %new
  55. -(void) handleUndeletePostAction{
  56. id post = [self link];
  57. [%c(TFHelper) getUndeleteDataWithID:[[post fullName] componentsSeparatedByString:@"_"][1] isComment:NO timeout:pushshiftRequestTimeoutValue extraData:@{@"post" : post} completionTarget:self completionSelector:@selector(completeUndeletePostAction:)];
  58. }
  59. %new
  60. -(void) completeUndeletePostAction:(NSDictionary *) data{
  61. id post = data[@"post"];
  62. MSHookIvar<NSString*>(post, "_author") = data[@"author"];
  63. NSAttributedString* postBodyAttributedString = [%c(NRTMarkdownManager) attributedStringFromMarkdown:data[@"body"] type:0];
  64. [self setLinkText:postBodyAttributedString];
  65. [[self tableView] reloadData];
  66. }
  67. %new
  68. -(void) completeUndeleteCommentAction:(NSDictionary *) data{
  69. id comment = data[@"comment"];
  70. if (comment){
  71. MSHookIvar<NSString*>(comment, "_author") = data[@"author"];
  72. MSHookIvar<NSString*>(comment, "_body") = data[@"body"];
  73. [[self commentsManager] updateComment:comment fromEdited:comment];
  74. }
  75. }
  76. %end
  77. %hook NRTMediaTableViewDataSource
  78. -(void) swipeCell:(id) arg1 didEndDragWithState:(NSUInteger) arg2{
  79. if (arg2 == 2){
  80. if ([arg1 isKindOfClass:[%c(NRTCommentTableViewCell) class]]) {
  81. NSString *commentBody = MSHookIvar<NSString*>([arg1 comment], "_body");
  82. if ((isTFDeletedOnly && ([commentBody isEqualToString:@"[deleted]"] || [commentBody isEqualToString:@"[removed]"])) || !isTFDeletedOnly){
  83. tfComment = [arg1 comment];
  84. tfController = self;
  85. shouldHaveUndeleteAction = YES;
  86. }
  87. }
  88. }
  89. %orig;
  90. shouldHaveUndeleteAction = NO;
  91. }
  92. %new
  93. -(void) completeUndeleteCommentAction:(NSDictionary *) data{
  94. id comment = data[@"comment"];
  95. if (comment){
  96. MSHookIvar<NSString*>(comment, "_author") = data[@"author"];
  97. MSHookIvar<NSString*>(comment, "_body") = data[@"body"];
  98. [[self commentsManager] updateComment:comment fromEdited:comment];
  99. }
  100. }
  101. %end
  102. %end
  103. static void loadPrefs(){
  104. NSMutableDictionary *prefs = [[NSMutableDictionary alloc] initWithContentsOfFile:@"/User/Library/Preferences/com.lint.undelete.prefs.plist"];
  105. if (prefs){
  106. if ([prefs objectForKey:@"isNarwhalEnabled"] != nil){
  107. isNarwhalEnabled = [[prefs objectForKey:@"isNarwhalEnabled"] boolValue];
  108. } else {
  109. isNarwhalEnabled = YES;
  110. }
  111. if ([prefs objectForKey:@"isTFDeletedOnly"] != nil) {
  112. isTFDeletedOnly = [[prefs objectForKey:@"isTFDeletedOnly"] boolValue];
  113. } else {
  114. isTFDeletedOnly = YES;
  115. }
  116. if ([prefs objectForKey:@"requestTimeoutValue"] != nil){
  117. pushshiftRequestTimeoutValue = [[prefs objectForKey:@"requestTimeoutValue"] doubleValue];
  118. } else {
  119. pushshiftRequestTimeoutValue = 10;
  120. }
  121. } else {
  122. isNarwhalEnabled = 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:@"narwhal"]){
  134. if (isNarwhalEnabled){
  135. CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, prefsChanged, CFSTR("com.lint.undelete.prefs.changed"), NULL, CFNotificationSuspensionBehaviorDeliverImmediately);
  136. %init(Narwhal);
  137. }
  138. }
  139. }