選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

151 行
4.6KB

  1. #import "AlienBlue.h"
  2. #import "assets/TFHelper.h"
  3. #import "assets/MMMarkdown/MMMarkdown.h"
  4. static BOOL isEnabled;
  5. static BOOL isAlienBlueEnabled;
  6. static BOOL isTFDeletedOnly;
  7. static CGFloat pushshiftRequestTimeoutValue;
  8. %group AlienBlue
  9. %hook CommentOptionsDrawerView
  10. - (id)initWithNode:(id)arg1 {
  11. id orig = %orig;
  12. NSString *body;
  13. if ([self isPostHeader]) {
  14. body = [[arg1 post] selftext];
  15. } else {
  16. body = [[(CommentNode *)arg1 comment] body];
  17. }
  18. if ([%c(TFHelper) shouldShowUndeleteButtonWithInfo:body isDeletedOnly:isTFDeletedOnly]){
  19. CGSize refSize = [[self buttons][0] frame].size;
  20. UIButton *undeleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
  21. [undeleteButton setFrame:CGRectMake(0, 0, refSize.width, refSize.height)];
  22. if ([self isPostHeader]){
  23. [undeleteButton addTarget:self action:@selector(didTapPostUndeleteButton:) forControlEvents:UIControlEventTouchUpInside];
  24. } else {
  25. [undeleteButton addTarget:self action:@selector(didTapCommentUndeleteButton:) forControlEvents:UIControlEventTouchUpInside];
  26. }
  27. if ([%c(Resources) isNight]) {
  28. [undeleteButton setImage:[UIImage imageWithContentsOfFile:@"/var/mobile/Library/Application Support/TFDidThatSay/eye160dark.png"] forState:UIControlStateNormal];
  29. } else {
  30. [undeleteButton setImage:[UIImage imageWithContentsOfFile:@"/var/mobile/Library/Application Support/TFDidThatSay/eye160light.png"] forState:UIControlStateNormal];
  31. }
  32. undeleteButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
  33. undeleteButton.imageEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10);
  34. [self addButton:undeleteButton];
  35. }
  36. return orig;
  37. }
  38. %new
  39. - (void)didTapCommentUndeleteButton:(id)sender {
  40. [sender setEnabled:NO];
  41. id comment = [[self node] comment];
  42. [%c(TFHelper) getUndeleteDataWithID:[comment ident] isComment:YES timeout:pushshiftRequestTimeoutValue extraData:@{@"sender" : sender} completionTarget:self completionSelector:@selector(completeUndeleteCommentAction:)];
  43. }
  44. %new
  45. - (void)completeUndeleteCommentAction:(NSDictionary *)data {
  46. id comment = [[self node] comment];
  47. NSString *body = data[@"body"];
  48. NSString *bodyHTML = [%c(MMMarkdown) HTMLStringWithMarkdown:body extensions:MMMarkdownExtensionsGitHubFlavored error:nil];
  49. [comment setAuthor:data[@"author"]];
  50. [comment setBody:body];
  51. [comment setBodyHTML:bodyHTML];
  52. [[self delegate] respondToStyleChange];
  53. [data[@"sender"] setEnabled:YES];
  54. }
  55. %new
  56. - (void)didTapPostUndeleteButton:(id)sender {
  57. [sender setEnabled:NO];
  58. id post = [[self node] post];
  59. [%c(TFHelper) getUndeleteDataWithID:[post ident] isComment:NO timeout:pushshiftRequestTimeoutValue extraData:@{@"sender" : sender} completionTarget:self completionSelector:@selector(completeUndeletePostAction:)];
  60. }
  61. %new
  62. - (void)completeUndeletePostAction:(NSDictionary *)data {
  63. id post = [[self node] post];
  64. id postComment = [[self node] comment]; //Don't know why he used a comment to store info about a post, but it exists
  65. NSString *body = data[@"body"];
  66. NSString *bodyHTML = [%c(MMMarkdown) HTMLStringWithMarkdown:body extensions:MMMarkdownExtensionsGitHubFlavored error:nil];
  67. [post setAuthor:data[@"author"]];
  68. [post setSelftext:body];
  69. [postComment setBodyHTML:bodyHTML];
  70. [[self delegate] respondToStyleChange];
  71. [data[@"sender"] setEnabled:YES];
  72. }
  73. %end
  74. %end
  75. static void loadPrefs(){
  76. NSMutableDictionary *prefs = [[NSMutableDictionary alloc] initWithContentsOfFile:@"/User/Library/Preferences/com.lint.undelete.prefs.plist"];
  77. if (prefs){
  78. isEnabled = [prefs objectForKey:@"isEnabled"] ? [[prefs objectForKey:@"isEnabled"] boolValue] : YES;
  79. isAlienBlueEnabled = [prefs objectForKey:@"isAlienBlueEnabled"] ? [[prefs objectForKey:@"isAlienBlueEnabled"] boolValue] : YES;
  80. isTFDeletedOnly = [prefs objectForKey:@"isTFDeletedOnly"] ? [[prefs objectForKey:@"isTFDeletedOnly"] boolValue] : YES;
  81. pushshiftRequestTimeoutValue = [prefs objectForKey:@"requestTimeoutValue"] ? [[prefs objectForKey:@"requestTimeoutValue"] doubleValue] : 10;
  82. } else {
  83. isEnabled = YES;
  84. isAlienBlueEnabled = YES;
  85. isTFDeletedOnly = YES;
  86. pushshiftRequestTimeoutValue = 10;
  87. }
  88. }
  89. static void prefsChanged(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) {
  90. loadPrefs();
  91. }
  92. %ctor {
  93. loadPrefs();
  94. NSString* processName = [[NSProcessInfo processInfo] processName];
  95. if ([processName isEqualToString:@"AlienBlue"]){
  96. if (isAlienBlueEnabled && isEnabled){
  97. CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, (CFNotificationCallback)prefsChanged, CFSTR("com.lint.undelete.prefs.changed"), NULL, CFNotificationSuspensionBehaviorCoalesce);
  98. %init(AlienBlue);
  99. }
  100. }
  101. }