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.

200 lines
6.7KB

  1. #import "AlienBlue.h"
  2. #import "assets/MMMarkdown.h"
  3. static BOOL isAlienBlueEnabled;
  4. static BOOL isAlienBlueDeletedOnly;
  5. static CGFloat pushshiftRequestTimeoutValue;
  6. %group AlienBlue
  7. %hook CommentOptionsDrawerView
  8. -(id) initWithNode:(id) arg1 {
  9. id orig = %orig;
  10. NSString *body;
  11. if ([self isPostHeader]) {
  12. body = [[arg1 post] selftext];
  13. } else {
  14. body = [[(CommentNode *)arg1 comment] body];
  15. }
  16. if ((isAlienBlueDeletedOnly && ([body isEqualToString:@"[deleted]"] || [body isEqualToString:@"[removed]"])) || !isAlienBlueDeletedOnly) {
  17. CGSize refSize = [[self buttons][0] frame].size;
  18. UIButton *undeleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
  19. [undeleteButton setFrame:CGRectMake(0, 0, refSize.width, refSize.height)];
  20. if ([self isPostHeader]){
  21. [undeleteButton addTarget:self action:@selector(didTapPostUndeleteButton:) forControlEvents:UIControlEventTouchUpInside];
  22. } else {
  23. [undeleteButton addTarget:self action:@selector(didTapCommentUndeleteButton:) forControlEvents:UIControlEventTouchUpInside];
  24. }
  25. if ([%c(Resources) isNight]) {
  26. [undeleteButton setImage:[UIImage imageWithContentsOfFile:@"/var/mobile/Library/Application Support/TFDidThatSay/eye160dark.png"] forState:UIControlStateNormal];
  27. } else {
  28. [undeleteButton setImage:[UIImage imageWithContentsOfFile:@"/var/mobile/Library/Application Support/TFDidThatSay/eye160light.png"] forState:UIControlStateNormal];
  29. }
  30. undeleteButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
  31. undeleteButton.imageEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10);
  32. [self addButton:undeleteButton];
  33. }
  34. return orig;
  35. }
  36. %new
  37. -(void) didTapCommentUndeleteButton:(id) sender {
  38. [sender setEnabled:NO];
  39. id comment = [[self node] comment];
  40. NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
  41. NSOperationQueue *queue = [[NSOperationQueue alloc] init];
  42. [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://api.pushshift.io/reddit/search/comment/?ids=%@&fields=author,body",[comment ident]]]];
  43. [request setHTTPMethod:@"GET"];
  44. [request setTimeoutInterval:pushshiftRequestTimeoutValue];
  45. [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
  46. NSString *author = @"[author]";
  47. NSString *body = @"[body]";
  48. if (data != nil && error == nil){
  49. id jsonData = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
  50. if ([[jsonData objectForKey:@"data"] count] != 0){
  51. author = [[jsonData objectForKey:@"data"][0] objectForKey:@"author"];
  52. body = [[jsonData objectForKey:@"data"][0] objectForKey:@"body"];
  53. if ([body isEqualToString:@"[deleted]"] || [body isEqualToString:@"[removed]"]){
  54. body = @"[pushshift was unable to archive this]";
  55. }
  56. } else {
  57. body = @"[pushshift has not archived this yet]";
  58. }
  59. } else if (error != nil || data == nil){
  60. body = [NSString stringWithFormat:@"[an error occured while attempting to contact pushshift api (%@)]", [error localizedDescription]];
  61. }
  62. NSString *bodyHTML = [%c(MMMarkdown) HTMLStringWithMarkdown:body extensions:MMMarkdownExtensionsGitHubFlavored error:nil];
  63. [comment setAuthor:author];
  64. [comment setBody:body];
  65. [comment setBodyHTML:bodyHTML];
  66. [[self delegate] performSelectorOnMainThread:@selector(respondToStyleChange) withObject:nil waitUntilDone:NO];
  67. [sender setEnabled:YES];
  68. }];
  69. }
  70. %new
  71. -(void) didTapPostUndeleteButton:(id) sender {
  72. [sender setEnabled:NO];
  73. id post = [[self node] post];
  74. id postComment = [[self node] comment]; //Don't know why he used a comment to store info about a post, but it exists
  75. NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
  76. NSOperationQueue *queue = [[NSOperationQueue alloc] init];
  77. [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://api.pushshift.io/reddit/search/submission/?ids=%@&fields=author,selftext",[post ident]]]];
  78. [request setHTTPMethod:@"GET"];
  79. [request setTimeoutInterval:pushshiftRequestTimeoutValue];
  80. [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
  81. NSString *author = @"[author]";
  82. NSString *body = @"[body]";
  83. if (data != nil && error == nil){
  84. id jsonData = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
  85. if ([[jsonData objectForKey:@"data"] count] != 0){
  86. author = [[jsonData objectForKey:@"data"][0] objectForKey:@"author"];
  87. body = [[jsonData objectForKey:@"data"][0] objectForKey:@"selftext"];
  88. if ([body isEqualToString:@"[deleted]"] || [body isEqualToString:@"[removed]"]){
  89. body = @"[pushshift was unable to archive this]";
  90. }
  91. } else {
  92. body = @"[pushshift has not archived this yet]";
  93. }
  94. } else if (error != nil || data == nil){
  95. body = [NSString stringWithFormat:@"[an error occured while attempting to contact pushshift api (%@)]", [error localizedDescription]];
  96. }
  97. NSString *bodyHTML = [%c(MMMarkdown) HTMLStringWithMarkdown:body extensions:MMMarkdownExtensionsGitHubFlavored error:nil];
  98. [post setAuthor:author];
  99. [post setSelftext:body];
  100. [postComment setBodyHTML:bodyHTML];
  101. [[self delegate] performSelectorOnMainThread:@selector(respondToStyleChange) withObject:nil waitUntilDone:NO];
  102. [sender setEnabled:YES];
  103. }];
  104. }
  105. %end
  106. %end
  107. static void loadPrefs(){
  108. NSMutableDictionary *prefs = [[NSMutableDictionary alloc] initWithContentsOfFile:@"/User/Library/Preferences/com.lint.undelete.prefs.plist"];
  109. if (prefs){
  110. if ([prefs objectForKey:@"isAlienBlueEnabled"] != nil){
  111. isAlienBlueEnabled = [[prefs objectForKey:@"isAlienBlueEnabled"] boolValue];
  112. } else {
  113. isAlienBlueEnabled = YES;
  114. }
  115. if ([prefs objectForKey:@"isAlienBlueDeletedOnly"] != nil){
  116. isAlienBlueDeletedOnly = [[prefs objectForKey:@"isAlienBlueDeletedOnly"] boolValue];
  117. } else {
  118. isAlienBlueDeletedOnly = YES;
  119. }
  120. if ([prefs objectForKey:@"requestTimeoutValue"] != nil){
  121. pushshiftRequestTimeoutValue = [[prefs objectForKey:@"requestTimeoutValue"] doubleValue];
  122. } else {
  123. pushshiftRequestTimeoutValue = 10;
  124. }
  125. } else {
  126. isAlienBlueEnabled = YES;
  127. isAlienBlueDeletedOnly = YES;
  128. pushshiftRequestTimeoutValue = 10;
  129. }
  130. }
  131. static void prefsChanged(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) {
  132. loadPrefs();
  133. }
  134. %ctor {
  135. loadPrefs();
  136. NSString* processName = [[NSProcessInfo processInfo] processName];
  137. if ([processName isEqualToString:@"AlienBlue"]){
  138. if (isAlienBlueEnabled){
  139. CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, prefsChanged, CFSTR("com.lint.undelete.prefs.changed"), NULL, CFNotificationSuspensionBehaviorDeliverImmediately);
  140. %init(AlienBlue);
  141. }
  142. }
  143. }