Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

792 lines
28KB

  1. #import "Reddit.h"
  2. static BOOL isRedditEnabled;
  3. static BOOL isTFDeletedOnly;
  4. static CGFloat pushshiftRequestTimeoutValue;
  5. static NSArray *redditVersion;
  6. %group Reddit_v4_current
  7. %hook CommentTreeNode
  8. %property(assign,nonatomic)id commentTreeHeaderNode;
  9. %property(assign,nonatomic)id commentTreeCommandBarNode;
  10. %end
  11. %hook CommentTreeHeaderView
  12. -(void) layoutSubviews{
  13. %orig;
  14. [[self commentTreeNode] setCommentTreeHeaderNode:self];
  15. }
  16. %end
  17. %hook CommentTreeHeaderNode
  18. -(void) didLoad{
  19. %orig;
  20. [[self commentTreeNode] setCommentTreeHeaderNode:self];
  21. }
  22. %end
  23. %hook CommentTreeCommandBarNode
  24. -(void) didLoad{
  25. %orig;
  26. [[self commentTreeNode] setCommentTreeCommandBarNode:self];
  27. }
  28. %end
  29. %hook CommentActionSheetViewController
  30. -(void) setItems:(id) arg1{
  31. NSString *commentBody = [[self comment] bodyText];
  32. if ((isTFDeletedOnly && ([commentBody isEqualToString:@"[deleted]"] || [commentBody isEqualToString:@"[removed]"])) || !isTFDeletedOnly){
  33. UIImage* origImage = [UIImage imageWithContentsOfFile:@"/var/mobile/Library/Application Support/TFDidThatSay/eye160dark.png"];
  34. CGSize existingImageSize = [[arg1[0] leftIconImage] size];
  35. CGFloat scale = origImage.size.width / existingImageSize.width;
  36. UIImage *newImage = [UIImage imageWithCGImage:[origImage CGImage] scale:scale orientation:origImage.imageOrientation];
  37. id undeleteItem = [[%c(RUIActionSheetItem) alloc] initWithLeftIconImage:newImage text:@"TF did that say?" identifier:@"undeleteItemIdentifier" context:[self comment]];
  38. arg1 = [arg1 arrayByAddingObject:undeleteItem];
  39. [undeleteItem release];
  40. }
  41. %orig;
  42. }
  43. -(void) handleDidSelectActionSheetItem:(id) arg1{
  44. %orig;
  45. if ([[arg1 identifier] isEqualToString:@"undeleteItemIdentifier"]){
  46. [self dismissViewControllerAnimated:YES completion:nil];
  47. id commentTreeNode = [self commentTreeNode];
  48. Comment *comment = [commentTreeNode comment];
  49. NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
  50. NSOperationQueue *queue = [[NSOperationQueue alloc] init];
  51. [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://api.pushshift.io/reddit/search/comment/?ids=%@&fields=author,body",[[comment pk] componentsSeparatedByString:@"_"][1]]]];
  52. [request setHTTPMethod:@"GET"];
  53. [request setTimeoutInterval:pushshiftRequestTimeoutValue];
  54. [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
  55. NSString *author = @"[author]";
  56. NSString *body = @"[body]";
  57. if (data != nil && error == nil){
  58. id jsonData = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
  59. if ([[jsonData objectForKey:@"data"] count] != 0){
  60. author = [[jsonData objectForKey:@"data"][0] objectForKey:@"author"];
  61. body = [[jsonData objectForKey:@"data"][0] objectForKey:@"body"];
  62. if ([body isEqualToString:@"[deleted]"] || [body isEqualToString:@"[removed]"]){
  63. body = @"[pushshift was unable to archive this]";
  64. }
  65. } else {
  66. body = @"[pushshift has not archived this yet]";
  67. }
  68. } else if (error != nil || data == nil){
  69. body = [NSString stringWithFormat:@"[an error occured while attempting to contact pushshift api (%@)]", [error localizedDescription]];
  70. }
  71. NSMutableAttributedString *bodyMutableAttributedText;
  72. id themeManager;
  73. id isNightMode;
  74. id textColor;
  75. if ([redditVersion[1] integerValue] >= 45){
  76. themeManager = [[%c(ThemeManager) alloc] initWithAppSettings:[%c(AppSettings) sharedSettings]];
  77. isNightMode = [[[%c(AccountManager) sharedManager] defaults] objectForKey:@"kUseNightKey"];
  78. if (isNightMode) {
  79. textColor = [[themeManager darkTheme] bodyTextColor];
  80. } else{
  81. textColor = [[themeManager lightTheme] bodyTextColor];
  82. }
  83. [themeManager release];
  84. } else if ([redditVersion[1] integerValue] >= 37){
  85. themeManager = [[%c(ThemeManager) alloc] initWithTraitCollection:nil appSettings:[%c(AppSettings) sharedSettings]];
  86. isNightMode = [[[%c(AccountManager) sharedManager] defaults] objectForKey:@"kUseNightKey"];
  87. if (isNightMode) {
  88. textColor = [[themeManager nightTheme] bodyTextColor];
  89. } else{
  90. textColor = [[themeManager dayTheme] bodyTextColor];
  91. }
  92. [themeManager release];
  93. } else {
  94. themeManager = [%c(ThemeManager) sharedManager];
  95. isNightMode = [[[%c(AccountManager) sharedManager] defaults] objectForKey:@"kUseNightKey"];
  96. if (isNightMode) {
  97. textColor = [[themeManager nightTheme] bodyTextColor];
  98. } else{
  99. textColor = [[themeManager dayTheme] bodyTextColor];
  100. }
  101. }
  102. bodyMutableAttributedText = [[NSMutableAttributedString alloc] initWithAttributedString:[%c(NSAttributedStringMarkdownParser) attributedStringUsingCurrentConfig:body]];
  103. [bodyMutableAttributedText beginEditing];
  104. [bodyMutableAttributedText enumerateAttribute:NSForegroundColorAttributeName inRange:NSMakeRange(0, bodyMutableAttributedText.length) options:0 usingBlock:^(id _Nullable value, NSRange range, BOOL * _Nonnull stop) {
  105. [bodyMutableAttributedText removeAttribute:NSForegroundColorAttributeName range:range];
  106. [bodyMutableAttributedText addAttribute:NSForegroundColorAttributeName value:textColor range:range];
  107. }];
  108. [bodyMutableAttributedText endEditing];
  109. [comment setAuthor:author];
  110. [comment setBodyText:body];
  111. [comment setBodyRichTextAttributed:bodyMutableAttributedText];
  112. [comment setBodyAttributedText:bodyMutableAttributedText];
  113. [[commentTreeNode commentTreeHeaderNode] performSelectorOnMainThread:@selector(updateContentViewsForData:) withObject:comment waitUntilDone:NO];
  114. [request release];
  115. [queue release];
  116. [bodyMutableAttributedText release];
  117. }];
  118. }
  119. }
  120. %end
  121. %hook PostDetailViewController
  122. %property(strong,nonatomic) id feedPostTextWithThumbnailNode;
  123. %property(strong,nonatomic) id feedPostDetailCellNode;
  124. %end
  125. %hook FeedPostDetailCellNode
  126. -(void) didLoad{
  127. %orig;
  128. [[[self delegate] viewController] setFeedPostDetailCellNode:self];
  129. }
  130. %end
  131. %hook PostActionSheetViewController
  132. -(void) setItems:(id) arg1{
  133. Post *post = [self post];
  134. NSString *postBody = [post selfText];
  135. if ([post isSelfPost]){
  136. if ((isTFDeletedOnly && ([postBody isEqualToString:@"[deleted]"] || [postBody isEqualToString:@"[removed]"])) || !isTFDeletedOnly){
  137. UIImage* origImage = [UIImage imageWithContentsOfFile:@"/var/mobile/Library/Application Support/TFDidThatSay/eye160dark.png"];
  138. CGSize existingImageSize = [[arg1[0] leftIconImage] size];
  139. CGFloat scale = origImage.size.width / existingImageSize.width;
  140. UIImage *newImage = [UIImage imageWithCGImage:[origImage CGImage] scale:scale orientation:origImage.imageOrientation];
  141. id undeleteItem = [[%c(RUIActionSheetItem) alloc] initWithLeftIconImage:newImage text:@"TF did that say?" identifier:@"undeleteItemIdentifier" context:[self post]];
  142. arg1 = [arg1 arrayByAddingObject:undeleteItem];
  143. [undeleteItem release];
  144. }
  145. }
  146. %orig;
  147. }
  148. -(void) handleDidSelectActionSheetItem:(id) arg1{
  149. %orig;
  150. if ([[arg1 identifier] isEqualToString:@"undeleteItemIdentifier"]){
  151. [self dismissViewControllerAnimated:YES completion:nil];
  152. Post *post = [self post];
  153. if ([post isSelfPost]){
  154. NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
  155. NSOperationQueue *queue = [[NSOperationQueue alloc] init];
  156. [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://api.pushshift.io/reddit/search/submission/?ids=%@&fields=author,selftext",[[post pk] componentsSeparatedByString:@"_"][1]]]];
  157. [request setHTTPMethod:@"GET"];
  158. [request setTimeoutInterval:pushshiftRequestTimeoutValue];
  159. [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
  160. NSString *author = @"[author]";
  161. NSString *body = @"[body]";
  162. if (data != nil && error == nil){
  163. id jsonData = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
  164. if ([[jsonData objectForKey:@"data"] count] != 0){
  165. author = [[jsonData objectForKey:@"data"][0] objectForKey:@"author"];
  166. body = [[jsonData objectForKey:@"data"][0] objectForKey:@"selftext"];
  167. if ([body isEqualToString:@"[deleted]"] || [body isEqualToString:@"[removed]"]){
  168. body = @"[pushshift was unable to archive this]";
  169. }
  170. } else {
  171. body = @"[pushshift has not archived this yet]";
  172. }
  173. } else if (error != nil || data == nil){
  174. body = [NSString stringWithFormat:@"[an error occured while attempting to contact pushshift api (%@)]", [error localizedDescription]];
  175. }
  176. id themeManager;
  177. id isNightMode;
  178. id textColor;
  179. if ([redditVersion[1] integerValue] >= 45){
  180. themeManager = [[%c(ThemeManager) alloc] initWithAppSettings:[%c(AppSettings) sharedSettings]];
  181. isNightMode = [[[%c(AccountManager) sharedManager] defaults] objectForKey:@"kUseNightKey"];
  182. if (isNightMode) {
  183. textColor = [[themeManager darkTheme] bodyTextColor];
  184. } else{
  185. textColor = [[themeManager lightTheme] bodyTextColor];
  186. }
  187. [themeManager release];
  188. } else if ([redditVersion[1] integerValue] >= 37){
  189. themeManager = [[%c(ThemeManager) alloc] initWithTraitCollection:nil appSettings:[%c(AppSettings) sharedSettings]];
  190. isNightMode = [[[%c(AccountManager) sharedManager] defaults] objectForKey:@"kUseNightKey"];
  191. if (isNightMode) {
  192. textColor = [[themeManager nightTheme] bodyTextColor];
  193. } else{
  194. textColor = [[themeManager dayTheme] bodyTextColor];
  195. }
  196. [themeManager release];
  197. } else {
  198. themeManager = [%c(ThemeManager) sharedManager];
  199. isNightMode = [[[%c(AccountManager) sharedManager] defaults] objectForKey:@"kUseNightKey"];
  200. if (isNightMode) {
  201. textColor = [[themeManager nightTheme] bodyTextColor];
  202. } else{
  203. textColor = [[themeManager dayTheme] bodyTextColor];
  204. }
  205. }
  206. NSMutableAttributedString *bodyMutableAttributedText = [[NSMutableAttributedString alloc] initWithAttributedString:[%c(NSAttributedStringMarkdownParser) attributedStringUsingCurrentConfig:body]];
  207. [bodyMutableAttributedText beginEditing];
  208. [bodyMutableAttributedText enumerateAttribute:NSForegroundColorAttributeName inRange:NSMakeRange(0, bodyMutableAttributedText.length) options:0 usingBlock:^(id _Nullable value, NSRange range, BOOL * _Nonnull stop) {
  209. [bodyMutableAttributedText removeAttribute:NSForegroundColorAttributeName range:range];
  210. [bodyMutableAttributedText addAttribute:NSForegroundColorAttributeName value:textColor range:range];
  211. }];
  212. [bodyMutableAttributedText endEditing];
  213. [post setSelfText:body];
  214. [post setAuthor:author];
  215. [post setSelfPostRichTextAttributed:bodyMutableAttributedText];
  216. [post setPreviewFeedPostTextString:bodyMutableAttributedText];
  217. if ([redditVersion[1] integerValue] >= 44){
  218. [[[[[self postActionSheetDelegate] controller] feedPostDetailCellNode] contentNode] configureSelfTextNode];
  219. } else if ([redditVersion[1] integerValue] >= 38) {
  220. [[[[self postActionSheetDelegate] controller] feedPostDetailCellNode] configureSelfTextNode];
  221. } else {
  222. [[[[self postActionSheetDelegate] controller] feedPostDetailCellNode] configureSelfTextNode];
  223. [[[[[self postActionSheetDelegate] controller] feedPostDetailCellNode] titleNode] configureNodes];
  224. }
  225. [request release];
  226. [queue release];
  227. [bodyMutableAttributedText release];
  228. }];
  229. }
  230. }
  231. }
  232. %end
  233. %end
  234. %group Reddit_v4_ios10
  235. %hook CommentsViewController
  236. %new
  237. -(void) updateComments{
  238. [self reloadCommentsWithNewCommentsHighlight:NO autoScroll:NO animated:NO];
  239. }
  240. %new
  241. -(void) updatePostText{
  242. if ([redditVersion[1] integerValue] >= 2){
  243. [self reloadPostSection:YES];
  244. } else {
  245. [self feedPostViewDidUpdatePost:[self postData] shouldReloadFeed:NO];
  246. }
  247. }
  248. %end
  249. %hook CommentActionSheetViewController
  250. -(void) setItems:(id) arg1{
  251. NSString *commentBody = [[self comment] bodyText];
  252. if ((isTFDeletedOnly && ([commentBody isEqualToString:@"[deleted]"] || [commentBody isEqualToString:@"[removed]"])) || !isTFDeletedOnly){
  253. UIImage* origImage = [UIImage imageWithContentsOfFile:@"/var/mobile/Library/Application Support/TFDidThatSay/eye160dark.png"];
  254. CGSize existingImageSize = [[arg1[0] leftIconImage] size];
  255. CGFloat scale = origImage.size.width / existingImageSize.width;
  256. UIImage *newImage = [UIImage imageWithCGImage:[origImage CGImage] scale:scale orientation:origImage.imageOrientation];
  257. id undeleteItem;
  258. if ([redditVersion[1] integerValue] >= 18) {
  259. undeleteItem = [[%c(RUIActionSheetItem) alloc] initWithLeftIconImage:newImage text:@"TF did that say?" identifier:@"undeleteItemIdentifier" context:[self comment]];
  260. } else {
  261. undeleteItem = [[%c(ActionSheetItem) alloc] initWithLeftIconImage:newImage text:@"TF did that say?" identifier:@"undeleteItemIdentifier" context:[self comment]];
  262. }
  263. arg1 = [arg1 arrayByAddingObject:undeleteItem];
  264. [undeleteItem release];
  265. }
  266. %orig;
  267. }
  268. // >= 4.21
  269. -(void) handleDidSelectActionSheetItem:(id) arg1{
  270. %orig;
  271. if ([[arg1 identifier] isEqualToString:@"undeleteItemIdentifier"]){
  272. [self dismissViewControllerAnimated:YES completion:nil];
  273. Comment *comment = [self comment];
  274. NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
  275. NSOperationQueue *queue = [[NSOperationQueue alloc] init];
  276. [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://api.pushshift.io/reddit/search/comment/?ids=%@&fields=author,body",[[comment pk] componentsSeparatedByString:@"_"][1]]]];
  277. [request setHTTPMethod:@"GET"];
  278. [request setTimeoutInterval:pushshiftRequestTimeoutValue];
  279. [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
  280. NSString *author = @"[author]";
  281. NSString *body = @"[body]";
  282. if (data != nil && error == nil){
  283. id jsonData = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
  284. if ([[jsonData objectForKey:@"data"] count] != 0){
  285. author = [[jsonData objectForKey:@"data"][0] objectForKey:@"author"];
  286. body = [[jsonData objectForKey:@"data"][0] objectForKey:@"body"];
  287. if ([body isEqualToString:@"[deleted]"] || [body isEqualToString:@"[removed]"]){
  288. body = @"[pushshift was unable to archive this]";
  289. }
  290. } else {
  291. body = @"[pushshift has not archived this yet]";
  292. }
  293. } else if (error != nil || data == nil){
  294. body = [NSString stringWithFormat:@"[an error occured while attempting to contact pushshift api (%@)]", [error localizedDescription]];
  295. }
  296. NSMutableAttributedString *bodyMutableAttributedText = [[NSMutableAttributedString alloc] initWithAttributedString:[%c(NSAttributedStringMarkdownParser) attributedStringUsingCurrentConfig:body]];
  297. [comment setAuthor:author];
  298. [comment setBodyText:body];
  299. [comment setBodyRichTextAttributed:bodyMutableAttributedText];
  300. [comment setBodyAttributedText:bodyMutableAttributedText];
  301. [[self commentActionSheetDelegate] performSelectorOnMainThread:@selector(updateComments) withObject:nil waitUntilDone:NO];
  302. [request release];
  303. [queue release];
  304. [bodyMutableAttributedText release];
  305. }];
  306. }
  307. }
  308. // <= 4.20
  309. -(void) actionSheetViewController:(id) arg1 didSelectItem:(id) arg2{
  310. %orig;
  311. if ([[arg2 identifier] isEqualToString:@"undeleteItemIdentifier"]){
  312. [self dismissViewControllerAnimated:YES completion:nil];
  313. Comment *comment = [self comment];
  314. NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
  315. NSOperationQueue *queue = [[NSOperationQueue alloc] init];
  316. [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://api.pushshift.io/reddit/search/comment/?ids=%@&fields=author,body",[[comment pk] componentsSeparatedByString:@"_"][1]]]];
  317. [request setHTTPMethod:@"GET"];
  318. [request setTimeoutInterval:pushshiftRequestTimeoutValue];
  319. [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
  320. NSString *author = @"[author]";
  321. NSString *body = @"[body]";
  322. if (data != nil && error == nil){
  323. id jsonData = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
  324. if ([[jsonData objectForKey:@"data"] count] != 0){
  325. author = [[jsonData objectForKey:@"data"][0] objectForKey:@"author"];
  326. body = [[jsonData objectForKey:@"data"][0] objectForKey:@"body"];
  327. if ([body isEqualToString:@"[deleted]"] || [body isEqualToString:@"[removed]"]){
  328. body = @"[pushshift was unable to archive this]";
  329. }
  330. } else {
  331. body = @"[pushshift has not archived this yet]";
  332. }
  333. } else if (error != nil || data == nil){
  334. body = [NSString stringWithFormat:@"[an error occured while attempting to contact pushshift api (%@)]", [error localizedDescription]];
  335. }
  336. NSMutableAttributedString *bodyMutableAttributedText = [[NSMutableAttributedString alloc] initWithAttributedString:[%c(NSAttributedStringMarkdownParser) attributedStringUsingCurrentConfig:body]];
  337. [comment setAuthor:author];
  338. [comment setBodyText:body];
  339. [comment setBodyAttributedText:bodyMutableAttributedText];
  340. if ([redditVersion[1] integerValue] >= 12) {
  341. [comment setBodyRichTextAttributed:bodyMutableAttributedText];
  342. }
  343. [[self commentActionSheetDelegate] performSelectorOnMainThread:@selector(updateComments) withObject:nil waitUntilDone:NO];
  344. [request release];
  345. [queue release];
  346. [bodyMutableAttributedText release];
  347. }];
  348. }
  349. }
  350. %end
  351. %hook PostActionSheetViewController
  352. -(void) setItems:(id) arg1{
  353. Post *post = [self post];
  354. NSString *postBody = [post selfText];
  355. if ([post isSelfPost]){
  356. if ((isTFDeletedOnly && ([postBody isEqualToString:@"[deleted]"] || [postBody isEqualToString:@"[removed]"])) || !isTFDeletedOnly){
  357. UIImage* origImage = [UIImage imageWithContentsOfFile:@"/var/mobile/Library/Application Support/TFDidThatSay/eye160dark.png"];
  358. CGSize existingImageSize = [[arg1[0] leftIconImage] size];
  359. CGFloat scale = origImage.size.width / existingImageSize.width;
  360. UIImage *newImage = [UIImage imageWithCGImage:[origImage CGImage] scale:scale orientation:origImage.imageOrientation];
  361. id undeleteItem;
  362. if ([redditVersion[1] integerValue] >= 18) {
  363. undeleteItem = [[%c(RUIActionSheetItem) alloc] initWithLeftIconImage:newImage text:@"TF did that say?" identifier:@"undeleteItemIdentifier" context:[self post]];
  364. } else {
  365. undeleteItem = [[%c(ActionSheetItem) alloc] initWithLeftIconImage:newImage text:@"TF did that say?" identifier:@"undeleteItemIdentifier" context:[self post]];
  366. }
  367. arg1 = [arg1 arrayByAddingObject:undeleteItem];
  368. [undeleteItem release];
  369. }
  370. }
  371. %orig;
  372. }
  373. // >= 4.21
  374. -(void) handleDidSelectActionSheetItem:(id) arg1{
  375. %orig;
  376. if ([[arg1 identifier] isEqualToString:@"undeleteItemIdentifier"]){
  377. [self dismissViewControllerAnimated:YES completion:nil];
  378. Post *post = [self post];
  379. if ([post isSelfPost]){
  380. NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
  381. NSOperationQueue *queue = [[NSOperationQueue alloc] init];
  382. [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://api.pushshift.io/reddit/search/submission/?ids=%@&fields=author,selftext",[[post pk] componentsSeparatedByString:@"_"][1]]]];
  383. [request setHTTPMethod:@"GET"];
  384. [request setTimeoutInterval:pushshiftRequestTimeoutValue];
  385. [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
  386. NSString *author = @"[author]";
  387. NSString *body = @"[body]";
  388. if (data != nil && error == nil){
  389. id jsonData = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
  390. if ([[jsonData objectForKey:@"data"] count] != 0){
  391. author = [[jsonData objectForKey:@"data"][0] objectForKey:@"author"];
  392. body = [[jsonData objectForKey:@"data"][0] objectForKey:@"selftext"];
  393. if ([body isEqualToString:@"[deleted]"] || [body isEqualToString:@"[removed]"]){
  394. body = @"[pushshift was unable to archive this]";
  395. }
  396. } else {
  397. body = @"[pushshift has not archived this yet]";
  398. }
  399. } else if (error != nil || data == nil){
  400. body = [NSString stringWithFormat:@"[an error occured while attempting to contact pushshift api (%@)]", [error localizedDescription]];
  401. }
  402. NSMutableAttributedString *bodyMutableAttributedText = [[NSMutableAttributedString alloc] initWithAttributedString:[%c(NSAttributedStringMarkdownParser) attributedStringUsingCurrentConfig:body]];
  403. [post setSelfText:body];
  404. [post setAuthor:author];
  405. [post setSelfPostRichTextAttributed:bodyMutableAttributedText];
  406. [post setPreviewFeedPostTextString:bodyMutableAttributedText];
  407. [[self postActionSheetDelegate] performSelectorOnMainThread:@selector(updatePostText) withObject:nil waitUntilDone:NO];
  408. [request release];
  409. [queue release];
  410. [bodyMutableAttributedText release];
  411. }];
  412. }
  413. }
  414. }
  415. // <= 4.20
  416. -(void) actionSheetViewController:(id) arg1 didSelectItem:(id) arg2{
  417. %orig;
  418. if ([[arg2 identifier] isEqualToString:@"undeleteItemIdentifier"]){
  419. [self dismissViewControllerAnimated:YES completion:nil];
  420. Post *post = [self post];
  421. if ([post isSelfPost]){
  422. NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
  423. NSOperationQueue *queue = [[NSOperationQueue alloc] init];
  424. [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://api.pushshift.io/reddit/search/submission/?ids=%@&fields=author,selftext",[[post pk] componentsSeparatedByString:@"_"][1]]]];
  425. [request setHTTPMethod:@"GET"];
  426. [request setTimeoutInterval:pushshiftRequestTimeoutValue];
  427. [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
  428. NSString *author = @"[author]";
  429. NSString *body = @"[body]";
  430. if (data != nil && error == nil){
  431. id jsonData = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
  432. if ([[jsonData objectForKey:@"data"] count] != 0){
  433. author = [[jsonData objectForKey:@"data"][0] objectForKey:@"author"];
  434. body = [[jsonData objectForKey:@"data"][0] objectForKey:@"selftext"];
  435. if ([body isEqualToString:@"[deleted]"] || [body isEqualToString:@"[removed]"]){
  436. body = @"[pushshift was unable to archive this]";
  437. }
  438. } else {
  439. body = @"[pushshift has not archived this yet]";
  440. }
  441. } else if (error != nil || data == nil){
  442. body = [NSString stringWithFormat:@"[an error occured while attempting to contact pushshift api (%@)]", [error localizedDescription]];
  443. }
  444. NSMutableAttributedString *bodyMutableAttributedText = [[NSMutableAttributedString alloc] initWithAttributedString:[%c(NSAttributedStringMarkdownParser) attributedStringUsingCurrentConfig:body]];
  445. [post setAuthor:author];
  446. [post setSelfText:body];
  447. [post setSelfTextAttributed:bodyMutableAttributedText];
  448. if ([redditVersion[1] integerValue] >= 8) {
  449. [post setSelfPostRichTextAttributed:bodyMutableAttributedText];
  450. }
  451. if ([redditVersion[1] integerValue] >= 15) {
  452. [post setPreviewFeedPostTextString:bodyMutableAttributedText];
  453. }
  454. [[self postActionSheetDelegate] performSelectorOnMainThread:@selector(updatePostText) withObject:nil waitUntilDone:NO];
  455. [request release];
  456. [queue release];
  457. [bodyMutableAttributedText release];
  458. }];
  459. }
  460. }
  461. }
  462. %end
  463. %end
  464. //outdated and unchanged from first version of this tweak...
  465. //TODO: move button to menu, add post support, make async requests once I feel like doing it
  466. %group Reddit_v3
  467. %hook CommentView
  468. %new
  469. -(void) buttonAction {
  470. id commentsViewController = [self delegate];
  471. id comment = [self comment];
  472. NSError* error;
  473. NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
  474. [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://api.pushshift.io/reddit/search/comment/?ids=%@&fields=author,body",[comment pkWithoutPrefix]]]];
  475. [request setHTTPMethod:@"GET"];
  476. NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
  477. NSString *author = @"[author]";
  478. NSString *body = @"[body]";
  479. if (data != nil && error == nil){
  480. id jsonData = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
  481. author = [[jsonData objectForKey:@"data"][0] objectForKey:@"author"];
  482. body = [[jsonData objectForKey:@"data"][0] objectForKey:@"body"];
  483. if ([body isEqualToString:@"[deleted]"] || [body isEqualToString:@"[removed]"]){
  484. body = @"[comment was unable to be archived]";
  485. }
  486. } else if (error != nil || data == nil){
  487. body = @"[an error occured]";
  488. }
  489. [comment setValue:author forKey:@"author"];
  490. [comment setValue:[%c(MarkDownParser) attributedStringFromMarkdownString: body] forKey:@"bodyAttributedText"];
  491. [comment setValue:body forKey:@"bodyText"];
  492. [commentsViewController reloadCommentsWithNewCommentsHighlight:NO autoScroll:NO animated:NO];
  493. }
  494. -(id) initWithFrame:(id)arg1{
  495. id orig = %orig;
  496. id commandView = [self commandView];
  497. UIButton *undeleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
  498. [undeleteButton addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
  499. UIImage* undeleteImage = [UIImage imageWithContentsOfFile:@"/var/mobile/Library/Application Support/TFDidThatSay/eye160dark.png"];
  500. [undeleteButton setImage:undeleteImage forState:UIControlStateNormal];
  501. [commandView setUndeleteButton:undeleteButton];
  502. [commandView addSubview:undeleteButton];
  503. return orig;
  504. }
  505. %end
  506. %hook CommentCommandView
  507. %property (assign, nonatomic) id undeleteButton;
  508. -(void) layoutSubviews{
  509. %orig;
  510. UIButton *button = [self undeleteButton];
  511. button.frame = CGRectMake([[self overflowButton ] frame].origin.x - 32, 0, 32, 32);
  512. }
  513. %end
  514. %end
  515. static void loadPrefs(){
  516. NSMutableDictionary *prefs = [[NSMutableDictionary alloc] initWithContentsOfFile:@"/User/Library/Preferences/com.lint.undelete.prefs.plist"];
  517. if (prefs){
  518. if ([prefs objectForKey:@"isRedditEnabled"] != nil){
  519. isRedditEnabled = [[prefs objectForKey:@"isRedditEnabled"] boolValue];
  520. } else {
  521. isRedditEnabled = YES;
  522. }
  523. if ([prefs objectForKey:@"isTFDeletedOnly"] != nil) {
  524. isTFDeletedOnly = [[prefs objectForKey:@"isTFDeletedOnly"] boolValue];
  525. } else {
  526. isTFDeletedOnly = YES;
  527. }
  528. if ([prefs objectForKey:@"requestTimeoutValue"] != nil){
  529. pushshiftRequestTimeoutValue = [[prefs objectForKey:@"requestTimeoutValue"] doubleValue];
  530. } else {
  531. pushshiftRequestTimeoutValue = 10;
  532. }
  533. } else {
  534. isRedditEnabled = YES;
  535. isTFDeletedOnly = YES;
  536. pushshiftRequestTimeoutValue = 10;
  537. }
  538. }
  539. static void prefsChanged(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) {
  540. loadPrefs();
  541. }
  542. %ctor{
  543. loadPrefs();
  544. NSString* processName = [[NSProcessInfo processInfo] processName];
  545. redditVersion = [[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"] componentsSeparatedByString:@"."];
  546. if ([processName isEqualToString:@"Reddit"]){
  547. if (isRedditEnabled) {
  548. CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, prefsChanged, CFSTR("com.lint.undelete.prefs.changed"), NULL, CFNotificationSuspensionBehaviorDeliverImmediately);
  549. if ([redditVersion[0] isEqualToString:@"4"]){
  550. if ([redditVersion[1] integerValue] <= 32){
  551. %init(Reddit_v4_ios10);
  552. } else{
  553. %init(Reddit_v4_current);
  554. }
  555. } else if ([redditVersion[0] isEqualToString:@"3"]) {
  556. %init(Reddit_v3);
  557. }
  558. }
  559. }
  560. }