Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Support for draft entities

  • Loading branch information...
commit ac67318e0f5f805231cc0588240c170c3c72ed34 1 parent 060e16a
Brent Royal-Gordon authored
Showing with 246 additions and 3 deletions.
  1. +4 −1 ANDraft.h
  2. +10 −2 ANDraft.m
  3. +38 −0 ANEntity.h
  4. +184 −0 ANEntity.m
  5. +10 −0 ANPost.m
View
5 ANDraft.h
@@ -9,13 +9,16 @@
#import <Foundation/Foundation.h>
#import "AppNetKit.h"
+@class ANUser;
+@class ANDraftEntitySet;
+
@interface ANDraft : NSObject
@property (strong) NSString * text;
@property (assign) ANResourceID replyTo;
@property (assign) BOOL machineOnly;
@property (readonly) NSMutableArray * annotations;
-//@property (strong) NSArray * links;
+@property (readonly) ANDraftEntitySet * entities;
@property (copy) NSDictionary * representation;
View
12 ANDraft.m
@@ -14,6 +14,7 @@ @implementation ANDraft
- (id)init {
if((self = [super init])) {
_annotations = [NSMutableArray new];
+ _entities = [ANDraftEntitySet new];
}
return self;
}
@@ -35,6 +36,11 @@ - (NSDictionary *)representation {
[dict setObject:[self.annotations valueForKey:@"representation"] forKey:@"annotations"];
}
+ id entitiesRep = self.entities.representation;
+ if(entitiesRep) {
+ [dict setObject:entitiesRep forKey:@"entities"];
+ }
+
return dict.copy;
}
@@ -46,14 +52,16 @@ - (void)setRepresentation:(NSDictionary *)dict {
self.replyTo = [ANResource.IDFormatter numberFromString:[dict objectForKey:@"reply_to"]].unsignedLongLongValue;
}
- [_annotations removeAllObjects];
+ [self.annotations removeAllObjects];
if([dict objectForKey:@"annotations"]) {
for(NSDictionary * annotationRep in [dict objectForKey:@"annotations"]) {
ANDraftAnnotation * annotation = [ANDraftAnnotation new];
annotation.representation = annotationRep;
- [_annotations addObject: annotation];
+ [self.annotations addObject:annotation];
}
}
+
+ self.entities.representation = [dict objectForKey:@"entities"];
}
- (void)createPostViaSession:(ANSession*)session completion:(ANPostRequestCompletion)completion {
View
38 ANEntity.h
@@ -9,12 +9,17 @@
#import "ANResource.h"
#import "AppNetKit.h"
+extern const NSRange ANEntityNoRange;
+
typedef enum {
ANEntityTypeMention,
ANEntityTypeTag,
ANEntityTypeLink
} ANEntityType;
+@class ANDraftEntitySet;
+@class ANDraftEntity;
+
@interface ANEntitySet : ANResource
@property (readonly) NSArray * all;
@@ -28,6 +33,8 @@ typedef enum {
@property (readonly) NSArray * links;
@property (readonly) NSArray * linkRepresentations;
+- (ANDraftEntitySet*)draftEntitySet;
+
@end
@interface ANEntity : ANResource
@@ -41,5 +48,36 @@ typedef enum {
@property (readonly) NSURL * URL;
@property (readonly) NSString * text;
+- (ANDraftEntity*)draftEntity;
+
+@end
+
+@interface ANDraftEntitySet : NSObject
+
+@property (readonly) NSMutableArray * links;
+@property (readonly) NSMutableArray * mentions;
+
+@property (copy) NSDictionary * representation;
+
+- (NSUInteger)addLinkEntityWithURL:(NSURL*)url range:(NSRange)range;
+
+- (NSUInteger)addMentionEntityForUsername:(NSString*)username;
+- (NSUInteger)addMentionEntityForUserID:(ANResourceID)userID;
+- (NSUInteger)addMentionEntityForUser:(ANUser*)user;
+
+- (void)removeAllEntities;
+
+@end
+
+@interface ANDraftEntity : NSObject
+
+@property (assign) NSRange range;
+
+@property (strong) NSURL * URL;
+@property (strong) NSString * name;
+@property (assign) ANResourceID userID;
+
+@property (copy) NSDictionary * representation;
+
@end
View
184 ANEntity.m
@@ -10,6 +10,8 @@
#import <objc/runtime.h>
+const NSRange ANEntityNoRange = (NSRange){ NSNotFound, 0 };
+
@interface ANEntity ()
+ (NSArray*)entitiesWithRepresentations:(NSArray*)reps session:(ANSession*)session;
@@ -88,6 +90,15 @@ - (NSArray *)all {
return ret;
}
+- (ANDraftEntitySet *)draftEntitySet {
+ ANDraftEntitySet * draftSet = [ANDraftEntitySet new];
+
+ [draftSet.links addObjectsFromArray:[self.links valueForKey:@"draftEntity"]];
+ [draftSet.mentions addObjectsFromArray:[self.mentions valueForKey:@"draftEntity"]];
+
+ return draftSet;
+}
+
@end
@implementation ANEntity
@@ -111,6 +122,10 @@ + (NSArray *)entitiesWithRepresentations:(NSArray *)reps session:(ANSession *)se
@dynamic ID;
- (NSRange)range {
+ if(![self.representation objectForKey:@"pos"]) {
+ return ANEntityNoRange;
+ }
+
return NSMakeRange([[self.representation objectForKey:@"pos"] unsignedIntegerValue],
[[self.representation objectForKey:@"len"] unsignedIntegerValue]);
}
@@ -119,6 +134,16 @@ - (ANResourceID)userID {
return self.ID;
}
+- (ANDraftEntity *)draftEntity {
+ ANDraftEntity * draft = [ANDraftEntity new];
+
+ if(!NSEqualRanges(self.range, ANEntityNoRange)) {
+ draft.range = self.range;
+ }
+
+ return draft;
+}
+
@end
@implementation ANMentionEntity
@@ -135,6 +160,13 @@ - (NSURL *)URL {
return [NSURL URLWithString:[NSString stringWithFormat:@"https://alpha.app.net/%@", self.name]];
}
+- (ANDraftEntity *)draftEntity {
+ ANDraftEntity * draft = [super draftEntity];
+ draft.userID = self.userID;
+ draft.name = self.name;
+ return draft;
+}
+
@end
@implementation ANTagEntity
@@ -151,6 +183,10 @@ - (NSURL *)URL {
return [NSURL URLWithString:[NSString stringWithFormat:@"https://alpha.app.net/hashtags/%@", self.name]];
}
+- (ANDraftEntity *)draftEntity {
+ return nil;
+}
+
@end
@implementation ANLinkEntity
@@ -159,4 +195,152 @@ - (ANEntityType)entityType {
return ANEntityTypeLink;
}
+- (ANDraftEntity *)draftEntity {
+ ANDraftEntity * draft = [super draftEntity];
+ draft.URL = self.URL;
+ return draft;
+}
+
+@end
+
+@implementation ANDraftEntitySet
+
+- (id)init {
+ if((self = [super init])) {
+ _links = [NSMutableArray new];
+ _mentions = [NSMutableArray new];
+ }
+ return self;
+}
+
+- (NSUInteger)addLinkEntityWithURL:(NSURL*)url range:(NSRange)range {
+ NSUInteger i = self.links.count;
+
+ ANDraftEntity * entity = [ANDraftEntity new];
+ entity.URL = url;
+ entity.range = range;
+
+ [self.links insertObject:entity atIndex:i];
+
+ return i;
+}
+
+- (NSUInteger)addMentionEntityForUsername:(NSString*)username {
+ NSUInteger i = self.mentions.count;
+
+ ANDraftEntity * entity = [ANDraftEntity new];
+ entity.name = username;
+
+ [self.mentions insertObject:entity atIndex:i];
+
+ return i;
+}
+
+- (NSUInteger)addMentionEntityForUserID:(ANResourceID)userID {
+ NSUInteger i = self.mentions.count;
+
+ ANDraftEntity * entity = [ANDraftEntity new];
+ entity.userID = userID;
+
+ [self.mentions insertObject:entity atIndex:i];
+
+ return i;
+}
+
+- (NSUInteger)addMentionEntityForUser:(ANUser*)user {
+ NSUInteger i = self.mentions.count;
+
+ ANDraftEntity * entity = [ANDraftEntity new];
+ entity.name = user.username;
+ entity.userID = user.ID;
+
+ [self.mentions insertObject:entity atIndex:i];
+
+ return i;
+}
+
+- (void)removeAllEntities {
+ [self.links removeAllObjects];
+ [self.mentions removeAllObjects];
+}
+
+- (NSDictionary *)representation {
+ NSMutableDictionary * rep = [NSMutableDictionary new];
+
+ if(self.links.count) {
+ [rep setObject:[self.links valueForKey:@"representation"] forKey:@"links"];
+ }
+ if(self.mentions.count) {
+ [rep setObject:[self.mentions valueForKey:@"representation"] forKey:@"mentions"];
+ }
+
+ if(!rep.count) {
+ return nil;
+ }
+
+ return rep;
+}
+
+- (void)setRepresentation:(NSDictionary *)rep {
+ [self removeAllEntities];
+
+ for(NSDictionary * entityRep in [rep objectForKey:@"links"]) {
+ ANDraftEntity * draftEntity = [ANDraftEntity new];
+ draftEntity.representation = entityRep;
+
+ [self.links addObject:draftEntity];
+ }
+
+ for(NSDictionary * entityRep in [rep objectForKey:@"mentions"]) {
+ ANDraftEntity * draftEntity = [ANDraftEntity new];
+ draftEntity.representation = entityRep;
+
+ [self.mentions addObject:draftEntity];
+ }
+}
+
+@end
+
+@implementation ANDraftEntity
+
+- (id)init {
+ if((self = [super init])) {
+ _range = ANEntityNoRange;
+ }
+ return self;
+}
+
+- (NSDictionary *)representation {
+ NSMutableDictionary * rep = [NSMutableDictionary new];
+
+ if(self.URL) {
+ [rep setObject:self.URL.absoluteString forKey:@"url"];
+ }
+ if(self.name) {
+ [rep setObject:self.name forKey:@"name"];
+ }
+ if(self.userID != 0) {
+ [rep setObject:[ANResource.IDFormatter stringFromNumber:[NSNumber numberWithUnsignedLongLong:self.userID]] forKey:@"id"];
+ }
+ if(!NSEqualRanges(self.range, ANEntityNoRange)) {
+ [rep setObject:[NSString stringWithFormat:@"%d", self.range.location] forKey:@"pos"];
+ [rep setObject:[NSString stringWithFormat:@"%d", self.range.length] forKey:@"len"];
+ }
+
+ return rep;
+}
+
+- (void)setRepresentation:(NSDictionary *)rep {
+ self.URL = [NSURL URLWithString:[rep objectForKey:@"url"]];
+ self.name = [rep objectForKey:@"name"];
+ self.userID = [[ANResource.IDFormatter numberFromString:[rep objectForKey:@"id"]] unsignedLongLongValue];
+
+ if([rep objectForKey:@"pos"]) {
+ self.range = NSMakeRange([[rep objectForKey:@"pos"] integerValue], [[rep objectForKey:@"len"] integerValue]);
+ }
+ else {
+ self.range = ANEntityNoRange;
+ }
+}
+
@end
View
10 ANPost.m
@@ -64,6 +64,7 @@ - (ANDraft*)draftForward {
ANDraft * draft = [ANDraft new];
draft.text = [NSString stringWithFormat:@"» %@ %@", self.user.username.appNetUsernameString, self.text];
+ [draft.entities.links addObjectsFromArray:[self.entities.links valueForKey:@"draftEntity"]];
return draft;
}
@@ -75,6 +76,15 @@ - (ANDraft*)draftCopy {
draft.replyTo = self.replyTo;
[draft.annotations setArray:[self.annotations.all valueForKey:@"draftAnnotation"]];
+ draft.machineOnly = self.machineOnly;
+
+ if(self.machineOnly) {
+ [draft.entities.mentions addObjectsFromArray:[self.entities.mentions valueForKey:@"draftEntity"]];
+ }
+ else {
+ [draft.entities.links addObjectsFromArray:[self.entities.links valueForKey:@"draftEntity"]];
+ }
+
return draft;
}
Please sign in to comment.
Something went wrong with that request. Please try again.