forked from level3tjg/RedditFilter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tweak.xm
executable file
·370 lines (338 loc) · 15 KB
/
Tweak.xm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#import <Carousel.h>
#import <Comment.h>
#import <Post.h>
#import <ToggleImageTableViewCell.h>
#import <UIKit/UIKit.h>
#import <dlfcn.h>
#import <mach-o/dyld.h>
#import <objc/runtime.h>
#import "Preferences.h"
static NSMutableArray *assetBundles;
extern "C" UIImage *iconWithName(NSString *iconName) {
NSArray *commonIconSizes = @[
@"24",
@"20",
@"16",
];
UIImage *iconImage;
for (NSBundle *bundle in assetBundles) {
for (NSString *iconSize in commonIconSizes) {
if (iconImage) break;
iconImage = [UIImage imageNamed:[NSString stringWithFormat:@"%@_%@", iconName, iconSize]
inBundle:bundle
compatibleWithTraitCollection:nil];
}
if (!iconImage)
iconImage = [UIImage imageNamed:iconName inBundle:bundle compatibleWithTraitCollection:nil];
}
return iconImage;
}
extern "C" NSString *localizedString(NSString *key, NSString *table) {
for (NSBundle *bundle in assetBundles) {
NSString *localizedString = [bundle localizedStringForKey:key value:nil table:table];
if (![localizedString isEqualToString:key]) return localizedString;
}
return nil;
}
extern "C" Class CoreClass(NSString *name) {
Class cls = NSClassFromString(name);
NSArray *prefixes = @[
@"Reddit.",
@"RedditCore.",
@"RedditCoreModels.",
@"RedditCore_RedditCoreModels.",
@"RedditUI.",
];
for (NSString *prefix in prefixes) {
if (cls) break;
cls = NSClassFromString([prefix stringByAppendingString:name]);
}
return cls;
}
static BOOL shouldFilterObject(id object) {
NSString *className = NSStringFromClass(object_getClass(object));
BOOL isAdPost = [className hasSuffix:@"AdPost"] ||
([object respondsToSelector:@selector(isAdPost)] && ((Post *)object).isAdPost) ||
([object respondsToSelector:@selector(isPromotedUserPostAd)] &&
[(Post *)object isPromotedUserPostAd]) ||
([object respondsToSelector:@selector(isPromotedCommunityPostAd)] &&
[(Post *)object isPromotedCommunityPostAd]);
BOOL isRecommendation = [className containsString:@"Recommend"];
BOOL isLivestream = [className containsString:@"Stream"];
BOOL isNSFW = [object respondsToSelector:@selector(isNSFW)] && ((Post *)object).isNSFW;
if ([NSUserDefaults.standardUserDefaults boolForKey:kRedditFilterPromoted] && isAdPost)
return YES;
if ([NSUserDefaults.standardUserDefaults boolForKey:kRedditFilterRecommended] && isRecommendation)
return YES;
if ([NSUserDefaults.standardUserDefaults boolForKey:kRedditFilterLivestreams] && isLivestream)
return YES;
if ([NSUserDefaults.standardUserDefaults boolForKey:kRedditFilterNSFW] && isNSFW) return YES;
return NO;
}
static NSArray *filteredObjects(NSArray *objects) {
return [objects filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(
id object, NSDictionary *bindings) {
return !shouldFilterObject(object);
}]];
}
static void filterNode(NSMutableDictionary *node) {
if (![node isKindOfClass:NSMutableDictionary.class]) return;
// Regular post
if ([node[@"__typename"] isEqualToString:@"SubredditPost"]) {
if ([NSUserDefaults.standardUserDefaults boolForKey:kRedditFilterAwards]) {
node[@"awardings"] = @[];
node[@"isGildable"] = @NO;
}
if ([NSUserDefaults.standardUserDefaults boolForKey:kRedditFilterScores])
node[@"isScoreHidden"] = @YES;
if ([NSUserDefaults.standardUserDefaults boolForKey:kRedditFilterNSFW] &&
[node[@"isNsfw"] boolValue])
node[@"isHidden"] = @YES;
}
if ([node[@"__typename"] isEqualToString:@"CellGroup"]) {
for (NSMutableDictionary *cell in node[@"cells"]) {
if ([cell[@"__typename"] isEqualToString:@"ActionCell"]) {
if ([NSUserDefaults.standardUserDefaults boolForKey:kRedditFilterAwards]) {
cell[@"isAwardHidden"] = @YES;
cell[@"goldenUpvoteInfo"][@"isGildable"] = @NO;
}
if ([NSUserDefaults.standardUserDefaults boolForKey:kRedditFilterScores])
cell[@"isScoreHidden"] = @YES;
}
}
}
// Ad post
if ([NSUserDefaults.standardUserDefaults boolForKey:kRedditFilterPromoted]) {
if ([node[@"__typename"] isEqualToString:@"AdPost"]) node[@"isHidden"] = @YES;
if ([node[@"__typename"] isEqualToString:@"CellGroup"] &&
[node[@"adPayload"] isKindOfClass:NSDictionary.class])
node[@"cells"] = @[];
}
// Comment
if ([node[@"__typename"] isEqualToString:@"Comment"]) {
if ([NSUserDefaults.standardUserDefaults boolForKey:kRedditFilterAwards]) {
node[@"awardings"] = @[];
node[@"isGildable"] = @NO;
}
if ([NSUserDefaults.standardUserDefaults boolForKey:kRedditFilterScores])
node[@"isScoreHidden"] = @YES;
if ([node[@"authorInfo"] isKindOfClass:NSDictionary.class] &&
[node[@"authorInfo"][@"id"] isEqualToString:@"t2_6l4z3"] &&
[NSUserDefaults.standardUserDefaults boolForKey:kRedditFilterAutoCollapseAutoMod])
node[@"isInitiallyCollapsed"] = @YES;
}
}
%hook NSURLSession
- (NSURLSessionDataTask *)dataTaskWithRequest:(NSURLRequest *)request
completionHandler:(void (^)(NSData *data, NSURLResponse *response,
NSError *error))completionHandler {
if (![request.URL.host hasPrefix:@"gql"] && ![request.URL.host hasPrefix:@"oauth"])
return %orig;
void (^newCompletionHandler)(NSData *, NSURLResponse *, NSError *) =
^(NSData *data, NSURLResponse *response, NSError *error) {
if (!data || error) return completionHandler(data, response, error);
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data
options:NSJSONReadingMutableContainers
error:&error];
if (!json || error) return completionHandler(data, response, error);
if ([json isKindOfClass:NSDictionary.class]) {
if (json[@"data"] && [json[@"data"] isKindOfClass:NSDictionary.class]) {
NSDictionary *data = json[@"data"];
NSMutableDictionary *root = data.allValues.firstObject;
if ([root isKindOfClass:NSDictionary.class]) {
if ([root.allValues.firstObject isKindOfClass:NSDictionary.class] &&
root.allValues.firstObject[@"edges"])
for (NSMutableDictionary *edge in root.allValues.firstObject[@"edges"])
filterNode(edge[@"node"]);
if (root[@"commentForest"])
for (NSMutableDictionary *tree in root[@"commentForest"][@"trees"])
filterNode(tree[@"node"]);
if (root[@"commentsPageAds"] &&
[NSUserDefaults.standardUserDefaults boolForKey:kRedditFilterPromoted])
root[@"commentsPageAds"] = @[];
if (root[@"recommendations"] &&
[NSUserDefaults.standardUserDefaults boolForKey:kRedditFilterRecommended])
root[@"recommendations"] = @[];
} else if ([root isKindOfClass:NSArray.class]) {
for (NSMutableDictionary *node in (NSArray *)root) filterNode(node);
}
}
}
data = [NSJSONSerialization dataWithJSONObject:json options:0 error:nil];
completionHandler(data, response, error);
};
return %orig(request, newCompletionHandler);
}
%end
// Only necessary for older app versions
%group Legacy
%hook Listing
- (void)fetchNextPage:(id (^)(NSArray *, id))completionHandler {
id (^newCompletionHandler)(NSArray *, id) = ^(NSArray *objects, id _) {
return completionHandler(filteredObjects(objects), _);
};
return %orig(newCompletionHandler);
}
%end
%hook FeedNetworkSource
- (NSArray *)postsAndCommentsFromData:(id)data {
return filteredObjects(%orig);
}
%end
%hook PostDetailPresenter
- (BOOL)shouldFetchCommentAdPost {
return [NSUserDefaults.standardUserDefaults boolForKey:kRedditFilterPromoted] ? NO
: %orig;
}
- (BOOL)shouldFetchAdditionalCommentAdPosts {
return [NSUserDefaults.standardUserDefaults boolForKey:kRedditFilterPromoted] ? NO
: %orig;
}
%end
%hook StreamManager
- (instancetype)initWithAccountContext:(id)accountContext
source:(NSInteger)source
deeplinkSubredditName:(id)deeplinkSubredditName
streamingConfig:(id)streamingConfig {
if ([NSUserDefaults.standardUserDefaults boolForKey:kRedditFilterLivestreams]) return nil;
return %orig;
}
- (instancetype)initWithService:(id)service
source:(NSInteger)source
deeplinkSubredditName:(id)deeplinkSubredditName
streamingConfig:(id)streamingConfig {
if ([NSUserDefaults.standardUserDefaults boolForKey:kRedditFilterLivestreams]) return nil;
return %orig;
}
%end
%hook Carousel
- (BOOL)isHiddenByUserWithAccountSettings:(id)accountSettings {
return ([NSUserDefaults.standardUserDefaults boolForKey:kRedditFilterRecommended] &&
([self.analyticType containsString:@"recommended"] ||
[self.analyticType containsString:@"similar"] ||
[self.analyticType containsString:@"popular"])) ||
%orig;
}
%end
%hook QuickActionViewModel
- (void)fetchActions {
if ([NSUserDefaults.standardUserDefaults boolForKey:kRedditFilterRecommended]) return;
%orig;
}
%end
%hook Post
- (NSArray *)awardingTotals {
return [NSUserDefaults.standardUserDefaults boolForKey:kRedditFilterAwards] ? nil
: %orig;
}
- (NSUInteger)totalAwardsReceived {
return [NSUserDefaults.standardUserDefaults boolForKey:kRedditFilterAwards] ? 0
: %orig;
}
- (BOOL)canAward {
return [NSUserDefaults.standardUserDefaults boolForKey:kRedditFilterAwards] ? NO
: %orig;
}
- (BOOL)isScoreHidden {
return [NSUserDefaults.standardUserDefaults boolForKey:kRedditFilterScores] ? YES
: %orig;
}
%end
%hook Comment
- (NSArray *)awardingTotals {
return [NSUserDefaults.standardUserDefaults boolForKey:kRedditFilterAwards] ? nil
: %orig;
}
- (NSUInteger)totalAwardsReceived {
return [NSUserDefaults.standardUserDefaults boolForKey:kRedditFilterAwards] ? 0
: %orig;
}
- (BOOL)canAward {
return [NSUserDefaults.standardUserDefaults boolForKey:kRedditFilterAwards] ? NO
: %orig;
}
- (BOOL)shouldHighlightForHighAward {
return [NSUserDefaults.standardUserDefaults boolForKey:kRedditFilterAwards] ? NO
: %orig;
}
- (BOOL)isScoreHidden {
return [NSUserDefaults.standardUserDefaults boolForKey:kRedditFilterScores] ? YES
: %orig;
}
- (BOOL)shouldAutoCollapse {
return [NSUserDefaults.standardUserDefaults boolForKey:kRedditFilterAutoCollapseAutoMod] &&
[((Comment *)self).authorPk isEqualToString:@"t2_6l4z3"]
? YES
: %orig;
}
%end
%hook ToggleImageTableViewCell
- (void)updateConstraints {
%orig;
UIStackView *horizontalStackView =
[self respondsToSelector:@selector(imageLabelView)]
? [self imageLabelView].horizontalStackView
: object_getIvar(self,
class_getInstanceVariable(object_getClass(self), "horizontalStackView"));
UILabel *detailLabel = [self respondsToSelector:@selector(imageLabelView)]
? [self imageLabelView].detailLabel
: [self detailLabel];
if (!horizontalStackView || !detailLabel) return;
if (detailLabel.text) {
UIView *contentView = [self contentView];
[contentView addConstraints:@[
[NSLayoutConstraint constraintWithItem:detailLabel
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:horizontalStackView
attribute:NSLayoutAttributeHeight
multiplier:.33
constant:0],
[NSLayoutConstraint constraintWithItem:horizontalStackView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:contentView
attribute:NSLayoutAttributeHeight
multiplier:1
constant:0],
[NSLayoutConstraint constraintWithItem:horizontalStackView
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:contentView
attribute:NSLayoutAttributeCenterY
multiplier:1
constant:0]
]];
}
}
%end
%end
%ctor {
assetBundles = [NSMutableArray new];
[assetBundles addObject:NSBundle.mainBundle];
for (NSString *file in
[NSFileManager.defaultManager contentsOfDirectoryAtPath:NSBundle.mainBundle.bundlePath
error:nil]) {
if (![file hasSuffix:@"bundle"]) continue;
NSBundle *bundle = [NSBundle
bundleWithPath:[NSBundle.mainBundle pathForResource:[file stringByDeletingPathExtension]
ofType:@"bundle"]];
if (bundle) [assetBundles addObject:bundle];
}
for (NSString *file in [NSFileManager.defaultManager
contentsOfDirectoryAtPath:[NSBundle.mainBundle.bundlePath
stringByAppendingPathComponent:@"Frameworks"]
error:nil]) {
if (![file hasSuffix:@"framework"]) continue;
NSBundle *bundle = [NSBundle
bundleWithPath:[NSBundle.mainBundle pathForResource:[file stringByDeletingPathExtension]
ofType:@"framework"
inDirectory:@"Frameworks"]];
if (bundle) [assetBundles addObject:bundle];
}
%init;
%init(Legacy, Comment = CoreClass(@"Comment"), Post = CoreClass(@"Post"),
QuickActionViewModel = CoreClass(@"QuickActionViewModel"),
StreamManager = CoreClass(@"StreamManager"),
ToggleImageTableViewCell = CoreClass(@"ToggleImageTableViewCell"));
}