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.

420 lines
12KB

  1. #import "Apollo.h"
  2. #import "assets/TFHelper.h"
  3. static BOOL isTFDeletedOnly;
  4. static BOOL isApolloEnabled;
  5. static CGFloat pushshiftRequestTimeoutValue;
  6. static BOOL shouldApolloHaveButton;
  7. %group Apollo
  8. NSDictionary* apolloBodyAttributes = nil;
  9. BOOL shouldAddUndeleteCell = NO;
  10. id apolloCommentCell;
  11. id apolloCommentController;
  12. %hook ApolloButtonNode
  13. %end
  14. %hook IconActionTableViewCell
  15. %end
  16. %hook RKComment
  17. -(BOOL) isDeleted{
  18. return NO;
  19. }
  20. -(BOOL) isModeratorRemoved{
  21. return NO;
  22. }
  23. %end
  24. %hook RKLink
  25. %property(strong, nonatomic) NSString *undeleteAuthor;
  26. -(id) author{
  27. if ([self undeleteAuthor]){
  28. return [self undeleteAuthor];
  29. } else {
  30. return %orig;
  31. }
  32. }
  33. %end
  34. %hook MarkdownRenderer
  35. +(id) attributedStringFromHTML:(id)arg1 attributes:(id) arg2 compact:(BOOL) arg3{
  36. apolloBodyAttributes = [arg2 copy];
  37. return %orig;
  38. }
  39. %end
  40. %hook ActionController
  41. -(id) tableView:(id) arg1 cellForRowAtIndexPath:(NSIndexPath *) arg2{
  42. if (shouldAddUndeleteCell){
  43. if ([arg2 row] == [self tableView:arg1 numberOfRowsInSection:0] - 1){
  44. id undeleteCell = [arg1 dequeueReusableCellWithIdentifier:@"IconActionCell" forIndexPath:arg2];
  45. id prevCell = [arg1 dequeueReusableCellWithIdentifier:@"IconActionCell"];
  46. UIImageView *prevCellImageView = MSHookIvar<UIImageView *>(prevCell, "iconImageView");
  47. CGSize prevImageSize = [[prevCellImageView image] size];
  48. UIImage *undeleteImage;
  49. if (@available(iOS 13.0, *)){
  50. undeleteImage = [UIImage systemImageNamed:@"eye"];
  51. if (!undeleteImage){
  52. undeleteImage = [UIImage imageWithContentsOfFile:@"/var/mobile/Library/Application Support/TFDidThatSay/eye160dark.png"];
  53. }
  54. } else {
  55. undeleteImage = [UIImage imageWithContentsOfFile:@"/var/mobile/Library/Application Support/TFDidThatSay/eye160dark.png"];
  56. }
  57. CGFloat undeleteImageSizeValue = prevImageSize.width > prevImageSize.height ? prevImageSize.width : prevImageSize.height;
  58. if (undeleteImageSizeValue == 0){
  59. undeleteImageSizeValue = 25;
  60. }
  61. UIGraphicsBeginImageContextWithOptions(CGSizeMake(undeleteImageSizeValue, undeleteImageSizeValue), NO, 0);
  62. [undeleteImage drawInRect:CGRectMake(0, 0, undeleteImageSizeValue, undeleteImageSizeValue)];
  63. undeleteImage = [UIGraphicsGetImageFromCurrentImageContext() imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
  64. UIGraphicsEndImageContext();
  65. UILabel *undeleteLabel = MSHookIvar<UILabel *>(undeleteCell, "actionTitleLabel");
  66. UIImageView *undeleteImageView = MSHookIvar<UIImageView *>(undeleteCell, "iconImageView");
  67. undeleteLabel.text = @"TF Did That Say?";
  68. undeleteImageView.image = undeleteImage;
  69. return undeleteCell;
  70. }
  71. }
  72. return %orig;
  73. }
  74. -(void) tableView:(id) arg1 didSelectRowAtIndexPath:(NSIndexPath *)arg2{
  75. if (shouldAddUndeleteCell){
  76. if ([arg2 row] == [self tableView:arg1 numberOfRowsInSection:0] - 1){
  77. if (apolloCommentCell){
  78. [apolloCommentCell undeleteCellWasSelected];
  79. } else {
  80. [apolloCommentController undeleteCellWasSelected];
  81. }
  82. }
  83. }
  84. %orig;
  85. }
  86. -(NSInteger) tableView:(id) arg1 numberOfRowsInSection:(NSInteger) arg2{
  87. if (shouldAddUndeleteCell){
  88. return %orig + 1;
  89. } else {
  90. return %orig;
  91. }
  92. }
  93. -(id) animationControllerForDismissedController:(id) arg1{
  94. shouldAddUndeleteCell = NO;
  95. return %orig;
  96. }
  97. %end
  98. %hook CommentCellNode
  99. %property(strong,nonatomic) UIButton *undeleteButton;
  100. -(void) moreOptionsTappedWithSender:(id) arg1{
  101. if (!shouldApolloHaveButton){
  102. NSString *commentBody = [MSHookIvar<RKComment *>(self, "comment") body];
  103. if ([%c(TFHelper) shouldShowUndeleteButtonWithInfo:commentBody isDeletedOnly:isTFDeletedOnly]){
  104. shouldAddUndeleteCell = YES;
  105. apolloCommentCell = self;
  106. apolloCommentController = nil;
  107. }
  108. }
  109. %orig;
  110. }
  111. -(void) longPressedWithGestureRecognizer:(id) arg1{
  112. if (!shouldApolloHaveButton){
  113. NSString *commentBody = [MSHookIvar<RKComment *>(self, "comment") body];
  114. if ([%c(TFHelper) shouldShowUndeleteButtonWithInfo:commentBody isDeletedOnly:isTFDeletedOnly]){
  115. shouldAddUndeleteCell = YES;
  116. apolloCommentCell = self;
  117. apolloCommentController = nil;
  118. }
  119. }
  120. %orig;
  121. }
  122. -(void) didLoad {
  123. %orig;
  124. if (shouldApolloHaveButton){
  125. NSString *commentBody = [MSHookIvar<RKComment *>(self, "comment") body];
  126. if ([%c(TFHelper) shouldShowUndeleteButtonWithInfo:commentBody isDeletedOnly:isTFDeletedOnly]){
  127. CGFloat imageSize = 20.0f;
  128. UIButton *undeleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
  129. [undeleteButton addTarget:self action:@selector(didTapUndeleteButton:) forControlEvents:UIControlEventTouchUpInside];
  130. undeleteButton.frame = CGRectMake(0, 0, imageSize, imageSize);
  131. UIImage* undeleteImage = [UIImage imageWithContentsOfFile:@"/var/mobile/Library/Application Support/TFDidThatSay/eye160dark.png"];
  132. [undeleteButton setImage:undeleteImage forState:UIControlStateNormal];
  133. [[self view] addSubview:undeleteButton];
  134. [self setUndeleteButton:undeleteButton];
  135. }
  136. }
  137. }
  138. -(void) _layoutSublayouts{
  139. %orig;
  140. if (shouldApolloHaveButton){
  141. if ([self undeleteButton]){
  142. CGFloat imageSize = 20.0f;
  143. id moreNode = MSHookIvar<id>(self, "moreOptionsNode");
  144. id ageNode = MSHookIvar<id>(self, "ageNode");
  145. CGRect nodeFrame = [moreNode frame];
  146. CGFloat centerHeight = (nodeFrame.size.height + nodeFrame.origin.y * 2) / 2.0f;
  147. CGFloat nodeSpacing = [ageNode frame].origin.x - nodeFrame.origin.x - nodeFrame.size.width;
  148. [[self undeleteButton] setFrame:CGRectMake(nodeFrame.origin.x - imageSize - nodeSpacing, centerHeight - (imageSize / 2), imageSize, imageSize)];
  149. }
  150. }
  151. }
  152. %new
  153. -(void) didTapUndeleteButton:(id) sender{
  154. [sender setEnabled:NO];
  155. id comment = MSHookIvar<id>(self, "comment");
  156. [%c(TFHelper) getUndeleteDataWithID:[[comment fullName] componentsSeparatedByString:@"_"][1] isComment:YES timeout:pushshiftRequestTimeoutValue extraData:@{@"sender" : sender} completionTarget:self completionSelector:@selector(completeUndeleteCommentAction:)];
  157. }
  158. %new
  159. -(void) undeleteCellWasSelected{
  160. RKComment *comment = MSHookIvar<RKComment *>(self, "comment");
  161. [%c(TFHelper) getUndeleteDataWithID:[[comment fullName] componentsSeparatedByString:@"_"][1] isComment:YES timeout:pushshiftRequestTimeoutValue extraData:nil completionTarget:self completionSelector:@selector(completeUndeleteCommentAction:)];
  162. }
  163. %new
  164. -(void) completeUndeleteCommentAction:(NSDictionary *) data{
  165. RKComment *comment = MSHookIvar<RKComment *>(self, "comment");
  166. id bodyNode = MSHookIvar<id>(self, "bodyNode");
  167. id authorNode = MSHookIvar<id>(self, "authorNode");
  168. NSString *author = data[@"author"];
  169. NSString *body = data[@"body"];
  170. [comment setAuthor:author];
  171. [comment setBody:body];
  172. NSAttributedString *prevAuthorAttributedString = [authorNode attributedTitleForState:UIControlStateNormal];
  173. NSDictionary *authorStringAttributes = [prevAuthorAttributedString attributesAtIndex:0 longestEffectiveRange:nil inRange:NSMakeRange(0, [prevAuthorAttributedString length])];
  174. NSAttributedString *newAuthorAttributedString = [[NSAttributedString alloc] initWithString:author attributes:authorStringAttributes];
  175. [authorNode setAttributedTitle:newAuthorAttributedString forState:UIControlStateNormal];
  176. [bodyNode setAttributedString:[%c(MarkdownRenderer) attributedStringFromMarkdown:body withAttributes:apolloBodyAttributes]];
  177. if ([data objectForKey:@"sender"]) {
  178. [data[@"sender"] setEnabled:YES];
  179. }
  180. }
  181. %end
  182. %hook CommentsViewController
  183. %property(strong, nonatomic) id headerCellNode;
  184. -(void) moreOptionsBarButtonItemTappedWithSender:(id) arg1{
  185. RKLink *post = MSHookIvar<RKLink *>(self, "link");
  186. NSString *postBody = [post selfText];
  187. if ([post isSelfPost]){
  188. if ([%c(TFHelper) shouldShowUndeleteButtonWithInfo:postBody isDeletedOnly:isTFDeletedOnly]){
  189. shouldAddUndeleteCell = YES;
  190. apolloCommentCell = nil;
  191. apolloCommentController = self;
  192. }
  193. }
  194. %orig;
  195. }
  196. %new
  197. -(void) undeleteCellWasSelected{
  198. RKLink *post = MSHookIvar<RKLink *>(self, "link");
  199. [%c(TFHelper) getUndeleteDataWithID:[[post fullName] componentsSeparatedByString:@"_"][1] isComment:NO timeout:pushshiftRequestTimeoutValue extraData:nil completionTarget:self completionSelector:@selector(completeUndeletePostAction:)];
  200. }
  201. %new
  202. -(void) completeUndeletePostAction:(NSDictionary *) data{
  203. RKLink *post = MSHookIvar<RKLink *>(self, "link");
  204. id headerCellNode = [self headerCellNode];
  205. id bodyNode = MSHookIvar<id>(headerCellNode, "bodyNode");
  206. id postInfoNode = MSHookIvar<id>(headerCellNode, "postInfoNode");
  207. id authorNode = MSHookIvar<id>(postInfoNode, "authorButtonNode");
  208. NSString *author = data[@"author"];
  209. NSString *authorTextString = [NSString stringWithFormat:@"by %@", author];
  210. NSString *body = data[@"body"];
  211. [post setUndeleteAuthor:author];
  212. [post setSelfText:body];
  213. NSAttributedString *prevAuthorAttributedString = [authorNode attributedTitleForState:UIControlStateNormal];
  214. NSDictionary *authorStringAttributes = [prevAuthorAttributedString attributesAtIndex:0 longestEffectiveRange:nil inRange:NSMakeRange(0, [prevAuthorAttributedString length])];
  215. NSAttributedString* newAuthorAttributedString = [[NSAttributedString alloc] initWithString:authorTextString attributes:authorStringAttributes];
  216. [authorNode setAttributedTitle:newAuthorAttributedString forState:UIControlStateNormal];
  217. [bodyNode setAttributedString:[%c(MarkdownRenderer) attributedStringFromMarkdown:body withAttributes:apolloBodyAttributes]];
  218. }
  219. %end
  220. %hook CommentsHeaderCellNode
  221. -(void) didLoad{
  222. %orig;
  223. [[self closestViewController] setHeaderCellNode:self];
  224. }
  225. -(void) _layoutSublayouts{
  226. %orig;
  227. [[self closestViewController] setHeaderCellNode:self];
  228. }
  229. -(void) longPressedWithGestureRecognizer:(id) arg1{
  230. RKLink *post = MSHookIvar<RKLink *>(self, "link");
  231. NSString *postBody = [post selfText];
  232. if ([post isSelfPost]){
  233. if ([%c(TFHelper) shouldShowUndeleteButtonWithInfo:postBody isDeletedOnly:isTFDeletedOnly]){
  234. shouldAddUndeleteCell = YES;
  235. apolloCommentCell = nil;
  236. apolloCommentController = self;
  237. }
  238. }
  239. %orig;
  240. }
  241. %end
  242. %end
  243. static void loadPrefs(){
  244. NSMutableDictionary *prefs = [[NSMutableDictionary alloc] initWithContentsOfFile:@"/User/Library/Preferences/com.lint.undelete.prefs.plist"];
  245. if (prefs){
  246. if ([prefs objectForKey:@"isApolloEnabled"] != nil) {
  247. isApolloEnabled = [[prefs objectForKey:@"isApolloEnabled"] boolValue];
  248. } else {
  249. isApolloEnabled = YES;
  250. }
  251. if ([prefs objectForKey:@"isTFDeletedOnly"] != nil) {
  252. isTFDeletedOnly = [[prefs objectForKey:@"isTFDeletedOnly"] boolValue];
  253. } else {
  254. isTFDeletedOnly = YES;
  255. }
  256. if ([prefs objectForKey:@"requestTimeoutValue"] != nil){
  257. pushshiftRequestTimeoutValue = [[prefs objectForKey:@"requestTimeoutValue"] doubleValue];
  258. } else {
  259. pushshiftRequestTimeoutValue = 10;
  260. }
  261. if ([prefs objectForKey:@"shouldApolloHaveButton"] != nil){
  262. shouldApolloHaveButton = [[prefs objectForKey:@"shouldApolloHaveButton"] boolValue];
  263. } else {
  264. shouldApolloHaveButton = NO;
  265. }
  266. } else {
  267. isApolloEnabled = YES;
  268. isTFDeletedOnly = YES;
  269. pushshiftRequestTimeoutValue = 10;
  270. shouldApolloHaveButton = NO;
  271. }
  272. }
  273. static void prefsChanged(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) {
  274. loadPrefs();
  275. }
  276. %ctor {
  277. loadPrefs();
  278. NSString* processName = [[NSProcessInfo processInfo] processName];
  279. if ([processName isEqualToString:@"Apollo"]){
  280. if (isApolloEnabled){
  281. CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, prefsChanged, CFSTR("com.lint.undelete.prefs.changed"), NULL, CFNotificationSuspensionBehaviorDeliverImmediately);
  282. %init(Apollo, CommentsHeaderCellNode = objc_getClass("Apollo.CommentsHeaderCellNode"), CommentCellNode = objc_getClass("Apollo.CommentCellNode"), ApolloButtonNode = objc_getClass("Apollo.ApolloButtonNode"), ActionController = objc_getClass("Apollo.ActionController"), IconActionTableViewCell = objc_getClass("Apollo.IconActionTableViewCell"), CommentsViewController = objc_getClass("Apollo.CommentsViewController"));
  283. }
  284. }
  285. }