Skip to content

Commit a902d2e

Browse files
committed
Universal Roster Item API
A Universal API for both Memory and Core Data Roster Storage for getting information about a Roster Item: - Subscription - Ask - Nickname - Groups
1 parent a88e580 commit a902d2e

File tree

5 files changed

+151
-0
lines changed

5 files changed

+151
-0
lines changed

Extensions/Roster/CoreDataStorage/XMPPRosterCoreDataStorage.m

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,5 +522,52 @@ - (NSArray *)jidsForXMPPStream:(XMPPStream *)stream{
522522
return results;
523523
}
524524

525+
- (void)getSubscription:(NSString **)subscription
526+
ask:(NSString **)ask
527+
nickname:(NSString **)nickname
528+
groups:(NSArray **)groups
529+
forJID:(XMPPJID *)jid
530+
xmppStream:(XMPPStream *)stream
531+
{
532+
XMPPLogTrace();
533+
534+
[self executeBlock:^{
535+
536+
NSManagedObjectContext *moc = [self managedObjectContext];
537+
XMPPUserCoreDataStorageObject *user = [self userForJID:jid xmppStream:stream managedObjectContext:moc];
538+
539+
if(user)
540+
{
541+
if(subscription)
542+
{
543+
*subscription = user.subscription;
544+
}
545+
546+
if(ask)
547+
{
548+
*ask = user.ask;
549+
}
550+
551+
if(nickname)
552+
{
553+
*nickname = user.nickname;
554+
}
555+
556+
if(groups)
557+
{
558+
if([user.groups count])
559+
{
560+
NSMutableArray *groupNames = [NSMutableArray array];
561+
562+
for(XMPPGroupCoreDataStorageObject *group in user.groups){
563+
[groupNames addObject:group.name];
564+
}
565+
566+
*groups = groupNames;
567+
}
568+
}
569+
}
570+
}];
571+
}
525572

526573
@end

Extensions/Roster/MemoryStorage/XMPPRosterMemoryStorage.m

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -819,4 +819,42 @@ - (NSArray *)jidsForXMPPStream:(XMPPStream *)stream
819819
return results;
820820
}
821821

822+
- (void)getSubscription:(NSString **)subscription
823+
ask:(NSString **)ask
824+
nickname:(NSString **)nickname
825+
groups:(NSArray **)groups
826+
forJID:(XMPPJID *)jid
827+
xmppStream:(XMPPStream *)stream
828+
{
829+
830+
XMPPLogTrace();
831+
AssertParentQueue();
832+
833+
XMPPJID *jidKey = [jid bareJID];
834+
XMPPUserMemoryStorageObject *rosterUser = [roster objectForKey:jidKey];
835+
836+
if(rosterUser)
837+
{
838+
if(subscription)
839+
{
840+
*subscription = rosterUser.subscription;
841+
}
842+
843+
if(ask)
844+
{
845+
*ask = rosterUser.ask;
846+
}
847+
848+
if(nickname)
849+
{
850+
*nickname = rosterUser.nickname;
851+
}
852+
853+
if(groups)
854+
{
855+
*groups = rosterUser.groups;
856+
}
857+
}
858+
}
859+
822860
@end

Extensions/Roster/MemoryStorage/XMPPUserMemoryStorageObject.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
{
1414
XMPPJID *jid;
1515
NSMutableDictionary *itemAttributes;
16+
NSMutableArray *groups;
1617

1718
NSMutableDictionary *resources;
1819
XMPPResourceMemoryStorageObject *primaryResource;
@@ -39,13 +40,22 @@
3940
4041
*/
4142

43+
- (NSString *)subscription;
44+
45+
- (NSString *)ask;
46+
4247
/**
4348
* Simple convenience method.
4449
* If a nickname exists for the user, the nickname is returned.
4550
* Otherwise the jid is returned (as a string).
4651
**/
4752
- (NSString *)displayName;
4853

54+
/**
55+
* An array of Group Names.
56+
**/
57+
- (NSArray *)groups;
58+
4959
/**
5060
* If XMPPvCardAvatarModule is included in the framework, the XMPPRoster will automatically integrate with it,
5161
* and we'll save the the user photos after they've been downloaded.

Extensions/Roster/MemoryStorage/XMPPUserMemoryStorageObject.m

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ - (id)initWithJID:(XMPPJID *)aJid
3333

3434
itemAttributes = [[NSMutableDictionary alloc] initWithCapacity:0];
3535

36+
groups = [[NSMutableArray alloc] initWithCapacity:0];
37+
3638
[self commonInit];
3739
}
3840
return self;
@@ -47,6 +49,19 @@ - (id)initWithItem:(NSXMLElement *)item
4749

4850
itemAttributes = [item attributesAsDictionary];
4951

52+
groups = [[NSMutableArray alloc] initWithCapacity:0];
53+
54+
NSArray *groupElements = [item elementsForName:@"group"];
55+
56+
for (NSXMLElement *groupElement in groupElements) {
57+
NSString *groupName = [groupElement stringValue];
58+
59+
if ([groupName length])
60+
{
61+
[groups addObject:groupName];
62+
}
63+
}
64+
5065
[self commonInit];
5166
}
5267
return self;
@@ -65,6 +80,7 @@ - (id)copyWithZone:(NSZone *)zone
6580

6681
deepCopy->jid = [jid copy];
6782
deepCopy->itemAttributes = [itemAttributes mutableCopy];
83+
deepCopy->groups = [groups mutableCopy];
6884

6985
deepCopy->resources = [[NSMutableDictionary alloc] initWithCapacity:[resources count]];
7086

@@ -106,6 +122,7 @@ - (id)initWithCoder:(NSCoder *)coder
106122
{
107123
jid = [coder decodeObjectOfClass:[XMPPJID class] forKey:@"jid"];
108124
itemAttributes = [[coder decodeObjectOfClass:[NSDictionary class] forKey:@"itemAttributes"] mutableCopy];
125+
groups = [[coder decodeObjectOfClass:[NSArray class] forKey:@"groups"] mutableCopy];
109126
#if TARGET_OS_IPHONE
110127
photo = [[UIImage alloc] initWithData:[coder decodeObjectOfClass:[NSData class] forKey:@"photo"]];
111128
#else
@@ -118,6 +135,7 @@ - (id)initWithCoder:(NSCoder *)coder
118135
{
119136
jid = [coder decodeObjectForKey:@"jid"];
120137
itemAttributes = [[coder decodeObjectForKey:@"itemAttributes"] mutableCopy];
138+
groups = [[coder decodeObjectForKey:@"groups"] mutableCopy];
121139
#if TARGET_OS_IPHONE
122140
photo = [[UIImage alloc] initWithData:[coder decodeObjectForKey:@"photo"]];
123141
#else
@@ -131,6 +149,7 @@ - (id)initWithCoder:(NSCoder *)coder
131149
{
132150
jid = [coder decodeObject];
133151
itemAttributes = [[coder decodeObject] mutableCopy];
152+
groups = [[coder decodeObject] mutableCopy];
134153
#if TARGET_OS_IPHONE
135154
photo = [[UIImage alloc] initWithData:[coder decodeObject]];
136155
#else
@@ -149,6 +168,7 @@ - (void)encodeWithCoder:(NSCoder *)coder
149168
{
150169
[coder encodeObject:jid forKey:@"jid"];
151170
[coder encodeObject:itemAttributes forKey:@"itemAttributes"];
171+
[coder encodeObject:groups forKey:@"groups"];
152172
#if TARGET_OS_IPHONE
153173
[coder encodeObject:UIImagePNGRepresentation(photo) forKey:@"photo"];
154174
#else
@@ -161,6 +181,7 @@ - (void)encodeWithCoder:(NSCoder *)coder
161181
{
162182
[coder encodeObject:jid];
163183
[coder encodeObject:itemAttributes];
184+
[coder encodeObject:groups];
164185
#if TARGET_OS_IPHONE
165186
[coder encodeObject:UIImagePNGRepresentation(photo)];
166187
#else
@@ -192,6 +213,16 @@ - (NSString *)nickname
192213
return (NSString *)[itemAttributes objectForKey:@"name"];
193214
}
194215

216+
- (NSString *)subscription
217+
{
218+
return (NSString *)[itemAttributes objectForKey:@"subscription"];
219+
}
220+
221+
- (NSString *)ask
222+
{
223+
return (NSString *)[itemAttributes objectForKey:@"ask"];
224+
}
225+
195226
- (NSString *)displayName
196227
{
197228
NSString *nickname = [self nickname];
@@ -201,6 +232,11 @@ - (NSString *)displayName
201232
return [jid bare];
202233
}
203234

235+
- (NSArray *)groups
236+
{
237+
return [groups copy];
238+
}
239+
204240
- (BOOL)isOnline
205241
{
206242
return (primaryResource != nil);
@@ -309,6 +345,19 @@ - (void)updateWithItem:(NSXMLElement *)item
309345

310346
[itemAttributes setObject:value forKey:key];
311347
}
348+
349+
[groups removeAllObjects];
350+
351+
NSArray *groupElements = [item elementsForName:@"group"];
352+
353+
for (NSXMLElement *groupElement in groupElements) {
354+
NSString *groupName = [groupElement stringValue];
355+
356+
if ([groupName length])
357+
{
358+
[groups addObject:groupName];
359+
}
360+
}
312361
}
313362

314363
- (int)updateWithPresence:(XMPPPresence *)presence

Extensions/Roster/XMPPRoster.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,13 @@
333333

334334
- (NSArray *)jidsForXMPPStream:(XMPPStream *)stream;
335335

336+
- (void)getSubscription:(NSString **)subscription
337+
ask:(NSString **)ask
338+
nickname:(NSString **)nickname
339+
groups:(NSArray **)groups
340+
forJID:(XMPPJID *)jid
341+
xmppStream:(XMPPStream *)stream;
342+
336343
@optional
337344

338345
/**

0 commit comments

Comments
 (0)