Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

737 Zeilen
26KB

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