/
ANEntity.m
162 lines (118 loc) · 4 KB
/
ANEntity.m
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
//
// ANEntity.m
// AppNetKit
//
// Created by Brent Royal-Gordon on 8/22/12.
// Copyright (c) 2012 Architechies. See README.md for licensing information.
//
#import "ANEntity.h"
#import <objc/runtime.h>
@interface ANEntity ()
+ (NSArray*)entitiesWithRepresentations:(NSArray*)reps session:(ANSession*)session;
@property (readonly) ANResourceID ID;
@end
@interface ANMentionEntity : ANEntity @end
@interface ANTagEntity : ANEntity @end
@interface ANLinkEntity : ANEntity @end
@implementation ANEntitySet
@dynamic mentionRepresentations;
@dynamic tagRepresentations;
@dynamic linkRepresentations;
- (NSArray *)mentions {
return [ANMentionEntity entitiesWithRepresentations:self.mentionRepresentations session:self.session];
}
- (NSArray *)tags {
return [ANTagEntity entitiesWithRepresentations:self.tagRepresentations session:self.session];
}
- (NSArray *)links {
return [ANLinkEntity entitiesWithRepresentations:self.linkRepresentations session:self.session];
}
- (NSArray *)all {
NSArray * ret = objc_getAssociatedObject(self, _cmd);
if(!ret) {
NSMutableArray * allEntities = [NSMutableArray new];
NSMutableArray * groupedEntities = [NSMutableArray arrayWithObjects:
self.mentions.mutableCopy,
self.tags.mutableCopy,
self.links.mutableCopy,
nil];
for(NSMutableArray * someEntities in groupedEntities.copy) {
if(someEntities.count == 0) {
[groupedEntities removeObject:someEntities];
}
}
while(groupedEntities.count) {
NSMutableArray * nextEntityArray;
for(NSMutableArray * someEntities in groupedEntities) {
if(!nextEntityArray) {
nextEntityArray = someEntities;
continue;
}
if([[nextEntityArray objectAtIndex:0] range].location > [[someEntities objectAtIndex:0] range].location) {
nextEntityArray = someEntities;
continue;
}
}
[allEntities addObject:[nextEntityArray objectAtIndex:0]];
[nextEntityArray removeObjectAtIndex:0];
if(nextEntityArray.count == 0) {
[groupedEntities removeObject:nextEntityArray];
}
}
ret = allEntities.copy;
objc_setAssociatedObject(self, _cmd, ret, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
return ret;
}
@end
@implementation ANEntity
- (NSUInteger)_location {
return self.range.location;
}
+ (NSArray *)entitiesWithRepresentations:(NSArray *)reps session:(ANSession *)session {
NSMutableArray * entities = [NSMutableArray new];
for(NSDictionary * rep in reps) {
[entities addObject:[[self alloc] initWithRepresentation:rep session:session]];
}
return entities.copy;
}
@dynamic entityType;
@dynamic URL;
@dynamic name;
@dynamic text;
@dynamic ID;
- (NSRange)range {
return NSMakeRange([[self.representation objectForKey:@"pos"] unsignedIntegerValue],
[[self.representation objectForKey:@"len"] unsignedIntegerValue]);
}
- (ANResourceID)userID {
return self.ID;
}
@end
@implementation ANMentionEntity
- (ANEntityType)entityType {
return ANEntityTypeMention;
}
- (NSString *)text {
return self.name.appNetUsernameString;
}
- (NSURL *)URL {
return [NSURL URLWithString:[NSString stringWithFormat:@"https://alpha.app.net/%@", self.name]];
}
@end
@implementation ANTagEntity
- (ANEntityType)entityType {
return ANEntityTypeTag;
}
- (NSString *)text {
return self.name.appNetTagString;
}
- (NSURL *)URL {
return [NSURL URLWithString:[NSString stringWithFormat:@"https://alpha.app.net/hashtags/%@", self.name]];
}
@end
@implementation ANLinkEntity
- (ANEntityType)entityType {
return ANEntityTypeLink;
}
@end