From ab5985156f2d39d98c45eb48a9979e364f8d2c3d Mon Sep 17 00:00:00 2001 From: Hivert Quentin Date: Fri, 13 Nov 2026 16:56:29 +0100 Subject: [PATCH] fix(gab): improve cardav on gab and ldap --- SoObjects/Contacts/SOGoContactSourceFolder.m | 79 +++++++++- SoObjects/SOGo/LDAPSource.h | 7 + SoObjects/SOGo/LDAPSource.m | 152 +++++++++++++++++-- 3 files changed, 221 insertions(+), 17 deletions(-) diff --git a/SoObjects/Contacts/SOGoContactSourceFolder.m b/SoObjects/Contacts/SOGoContactSourceFolder.m index c11bb1991..3399c58cb 100644 --- a/SoObjects/Contacts/SOGoContactSourceFolder.m +++ b/SoObjects/Contacts/SOGoContactSourceFolder.m @@ -210,10 +210,35 @@ - (NSArray *) toOneRelationshipKeys { - NSString *userDomain; + NSDictionary *record; + NSEnumerator *records; + NSMutableArray *keys; + NSArray *allRecords; + NSString *userDomain, *key; userDomain = [[context activeUser] domain]; - return [source allEntryIDsVisibleFromDomain: userDomain]; + + // Fetch all the entries at once and keep them in childRecords, so that the + // following -lookupName: of each child (e.g. in a PROPFIND) doesn't trigger + // one LDAP search per entry. + allRecords = nil; + if ([source respondsToSelector: @selector (allContactEntriesVisibleFromDomain:)]) + allRecords = [(LDAPSource *) source allContactEntriesVisibleFromDomain: userDomain]; + + if (!allRecords) + return [source allEntryIDsVisibleFromDomain: userDomain]; + + keys = [NSMutableArray arrayWithCapacity: [allRecords count]]; + records = [allRecords objectEnumerator]; + while ((record = [records nextObject])) + { + key = [record objectForKey: @"c_name"]; + [keys addObject: key]; + if (![childRecords objectForKey: key]) + [childRecords setObject: record forKey: key]; + } + + return keys; } - (NSException *) saveLDIFEntry: (SOGoContactLDIFEntry *) ldifEntry @@ -481,6 +506,11 @@ records = [source lookupContactsWithQualifier: qualifier andSortOrdering: ordering inDomain: domain]; + // Keep the entries so that a subsequent -lookupName: (e.g. in an + // addressbook-query REPORT) doesn't search them again one by one + [childRecords setObjects: records + forKeys: [records objectsForKey: @"c_name" + notFoundMarker: nil]]; records = [self _flattenedRecords: records]; } @@ -700,7 +730,11 @@ NSString *url, *baseURL, *cname, *domain; NSString **propertiesArray; NSMutableString *buffer; + NSMutableArray *urls, *cnames; + NSMutableDictionary *prefetched; NSDictionary *object; + NSEnumerator *records; + NSArray *allRecords; id connection; unsigned int count, max, propertiesCount; @@ -717,15 +751,48 @@ buffer = [[NSMutableString alloc] initWithCapacity: max*512]; domain = [[context activeUser] domain]; connection = [source connection]; - pool = [[NSAutoreleasePool alloc] init]; + + urls = [NSMutableArray arrayWithCapacity: max]; + cnames = [NSMutableArray arrayWithCapacity: max]; for (count = 0; count < max; count++) { element = [refs objectAtIndex: count]; url = [[[element firstChild] nodeValue] stringByUnescapingURL]; cname = [self _deduceObjectNameFromURL: url fromBaseURL: baseURL]; - object = [source lookupContactEntry: cname - inDomain: domain - usingConnection: connection]; + [urls addObject: (url ? url : @"")]; + [cnames addObject: (cname ? cname : @"")]; + } + + // Fetch the requested entries with a few searches instead of one per href. + // If several entries share an ID, keep the first one, as a single lookup + // would. + prefetched = [NSMutableDictionary dictionaryWithCapacity: max]; + if ([source respondsToSelector: @selector (lookupContactEntries:inDomain:usingConnection:)]) + { + allRecords = [(LDAPSource *) source lookupContactEntries: cnames + inDomain: domain + usingConnection: connection]; + records = [allRecords objectEnumerator]; + while ((object = [records nextObject])) + { + cname = [object objectForKey: @"c_name"]; + if ([cname length] && ![prefetched objectForKey: cname]) + [prefetched setObject: object forKey: cname]; + } + } + + pool = [[NSAutoreleasePool alloc] init]; + for (count = 0; count < max; count++) + { + url = [urls objectAtIndex: count]; + cname = [cnames objectAtIndex: count]; + object = [prefetched objectForKey: cname]; + // Not prefetched (e.g. missing entry or ID returned with a different + // case): fall back to a single lookup + if (!object) + object = [source lookupContactEntry: cname + inDomain: domain + usingConnection: connection]; if (object) [self appendObject: object properties: propertiesArray diff --git a/SoObjects/SOGo/LDAPSource.h b/SoObjects/SOGo/LDAPSource.h index e07c15ba2..a6dd666bf 100644 --- a/SoObjects/SOGo/LDAPSource.h +++ b/SoObjects/SOGo/LDAPSource.h @@ -133,6 +133,13 @@ andMultipleBookingsField: (NSString *) newMultipleBookingsField; - (BOOL) groupExpansionEnabled; +/* Bulk lookups used by the CardDAV address book (SOGoContactSourceFolder) to + avoid one LDAP search per contact */ +- (NSArray *) allContactEntriesVisibleFromDomain: (NSString *) theDomain; +- (NSArray *) lookupContactEntries: (NSArray *) theIDs + inDomain: (NSString *) theDomain + usingConnection: (id) connection; + @end #endif /* LDAPSOURCE_H */ diff --git a/SoObjects/SOGo/LDAPSource.m b/SoObjects/SOGo/LDAPSource.m index cf6692017..5ad5d1a66 100644 --- a/SoObjects/SOGo/LDAPSource.m +++ b/SoObjects/SOGo/LDAPSource.m @@ -1081,21 +1081,16 @@ groupObjectClasses: (NSArray *) newGroupObjectClasses return [self allEntryIDs]; } -- (NSArray *) allEntryIDs +/* Search all the entries listed by the address book, returning the requested + attributes */ +- (NSEnumerator *) _allEntriesWithAttributes: (NSArray *) attributes { NSEnumerator *entries; - NGLdapEntry *currentEntry; NGLdapConnection *ldapConnection; EOQualifier *qualifier; NSMutableString *qs; - NSString *value; - NSArray *attributes; - NSMutableArray *ids; - - ids = [NSMutableArray array]; ldapConnection = [self _ldapConnection]; - attributes = [NSArray arrayWithObject: _IDField]; qs = [NSMutableString stringWithFormat: @"(%@='*')", _CNField]; if ([_filter length]) @@ -1115,6 +1110,19 @@ groupObjectClasses: (NSArray *) newGroupObjectClasses qualifier: qualifier attributes: attributes]; + return entries; +} + +- (NSArray *) allEntryIDs +{ + NSEnumerator *entries; + NGLdapEntry *currentEntry; + NSString *value; + NSMutableArray *ids; + + ids = [NSMutableArray array]; + + entries = [self _allEntriesWithAttributes: [NSArray arrayWithObject: _IDField]]; while ((currentEntry = [entries nextObject])) { value = [[currentEntry attributeWithName: _IDField] @@ -1126,6 +1134,22 @@ groupObjectClasses: (NSArray *) newGroupObjectClasses return ids; } +/* The bulk lookups below match the returned entries with their IDs (c_name), + which requires the ID attribute to be part of the lookup fields. */ +- (BOOL) _lookupFieldsIncludeIDField +{ + NSEnumerator *fields; + NSString *field; + + fields = [_lookupFields objectEnumerator]; + while ((field = [fields nextObject])) + if ([field isEqualToString: @"*"] + || [field caseInsensitiveCompare: _IDField] == NSOrderedSame) + return YES; + + return NO; +} + - (void) _fillEmailsOfEntry: (NGLdapEntry *) ldapEntry intoLDIFRecord: (NSMutableDictionary *) ldifRecord { @@ -1429,6 +1453,43 @@ groupObjectClasses: (NSArray *) newGroupObjectClasses return ldifRecord; } +/* Same entries as -allEntryIDs, but with all the lookup fields, converted as + with -lookupContactEntry:inDomain:. Returns nil when the ID of the entries + can't be retrieved this way. */ +- (NSArray *) allContactEntriesVisibleFromDomain: (NSString *) theDomain +{ + NSAutoreleasePool *pool; + NSEnumerator *entries; + NGLdapEntry *currentEntry; + NSDictionary *ldifRecord; + NSMutableArray *contacts; + unsigned int i; + + if (![self _lookupFieldsIncludeIDField]) + return nil; + + contacts = [NSMutableArray array]; + + entries = [self _allEntriesWithAttributes: _lookupFields]; + i = 0; + pool = [NSAutoreleasePool new]; + while ((currentEntry = [entries nextObject])) + { + ldifRecord = [self _convertLDAPEntryToContact: currentEntry]; + if ([[ldifRecord objectForKey: @"c_name"] length] > 0) + [contacts addObject: ldifRecord]; + i++; + if (i % 10 == 0) + { + [pool release]; + pool = [NSAutoreleasePool new]; + } + } + [pool release]; + + return contacts; +} + - (NSArray *) fetchContactsMatching: (NSString *) match withCriteria: (NSArray *) criteria inDomain: (NSString *) theDomain @@ -1537,8 +1598,8 @@ groupObjectClasses: (NSArray *) newGroupObjectClasses return contacts; } -- (NGLdapEntry *) _lookupLDAPEntry: (EOQualifier *) theQualifier - usingConnection: (id) connection +- (NSEnumerator *) _lookupLDAPEntries: (EOQualifier *) theQualifier + usingConnection: (id) connection { NGLdapConnection *ldapConnection; NSEnumerator *entries; @@ -1558,7 +1619,14 @@ groupObjectClasses: (NSArray *) newGroupObjectClasses qualifier: theQualifier attributes: _lookupFields]; - return [entries nextObject]; + return entries; +} + +- (NGLdapEntry *) _lookupLDAPEntry: (EOQualifier *) theQualifier + usingConnection: (id) connection +{ + return [[self _lookupLDAPEntries: theQualifier + usingConnection: connection] nextObject]; } - (NGLdapEntry *) _lookupLDAPEntry: (EOQualifier *) theQualifier @@ -1600,6 +1668,68 @@ groupObjectClasses: (NSArray *) newGroupObjectClasses usingConnection: [self _ldapConnection]]; } +/* Fetch several entries by ID with one search per chunk of IDs, instead of + one search per ID as with -lookupContactEntry:inDomain:usingConnection:. + Entries are returned in no particular order; the caller matches them by + their "c_name". IDs with no matching entry are simply absent. Returns nil + when the ID of the entries can't be retrieved this way. */ +- (NSArray *) lookupContactEntries: (NSArray *) theIDs + inDomain: (NSString *) theDomain + usingConnection: (id) connection +{ + NSAutoreleasePool *pool; + NSEnumerator *entries; + NGLdapEntry *ldapEntry; + EOQualifier *qualifier; + NSMutableArray *contacts, *chunk; + NSMutableString *qs; + NSString *theID; + NSUInteger count, max, chunkSize; + + if (![self _lookupFieldsIncludeIDField]) + return nil; + + contacts = [NSMutableArray array]; + + // Stay below the size limit of the connection + chunkSize = 100; + if (_queryLimit > 0 && (NSUInteger) _queryLimit < chunkSize) + chunkSize = _queryLimit; + + chunk = [NSMutableArray arrayWithCapacity: chunkSize]; + max = [theIDs count]; + for (count = 0; count < max; count++) + { + theID = [theIDs objectAtIndex: count]; + if ([theID length] > 0 && ![chunk containsObject: theID]) + [chunk addObject: theID]; + + if ([chunk count] > 0 + && ([chunk count] == chunkSize || count == max - 1)) + { + pool = [NSAutoreleasePool new]; + qs = [NSMutableString string]; + entries = [chunk objectEnumerator]; + while ((theID = [entries nextObject])) + { + if ([qs length]) + [qs appendString: @" OR "]; + [qs appendFormat: @"(%@='%@')", _IDField, + [self LDAPEscapeFilterChars: theID keepWildcard: NO]]; + } + qualifier = [EOQualifier qualifierWithQualifierFormat: qs]; + entries = [self _lookupLDAPEntries: qualifier + usingConnection: connection]; + while ((ldapEntry = [entries nextObject])) + [contacts addObject: [self _convertLDAPEntryToContact: ldapEntry]]; + [pool release]; + [chunk removeAllObjects]; + } + } + + return contacts; +} + - (NSDictionary *) lookupContactEntryWithUIDorEmail: (NSString *) uid inDomain: (NSString *) theDomain {