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.

217 lines
7.0KB

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