Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

438 lines
13KB

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