Skip to content

Commit

Permalink
User directory: Use the new hs api (element-hq/riot-meta#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
manuroe committed Jul 28, 2017
1 parent 9ae59f4 commit 736d9bb
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 5 deletions.
3 changes: 2 additions & 1 deletion Riot/Assets/en.lproj/Vector.strings
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@
"contacts_address_book_no_contact" = "No local contacts";
"contacts_address_book_permission_required" = "Permission required to access local contacts";
"contacts_address_book_permission_denied" = "You didn't allow Riot to access your local contacts";
"contacts_matrix_users_section" = "KNOWN CONTACTS";
"contacts_user_directory_section" = "USER DIRECTORY";
"contacts_user_directory_offline_section" = "USER DIRECTORY (offline)";

// Chat participants
"room_participants_title" = "Participants";
Expand Down
19 changes: 19 additions & 0 deletions Riot/Model/Contact/ContactsDataSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@

#import <MatrixKit/MatrixKit.h>

/**
The state of the users search from the homeserver user directory.
*/
typedef enum : NSUInteger
{
ContactsDataSourceUserDirectoryStateLoading,
ContactsDataSourceUserDirectoryStateLoadedButLimited,
ContactsDataSourceUserDirectoryStateLoaded,
// The search is based on local known matrix contacts
ContactsDataSourceUserDirectoryStateOfflineLoading,
ContactsDataSourceUserDirectoryStateOfflineLoaded
} ContactsDataSourceUserDirectoryState;


/**
'ContactsDataSource' is a base class to handle contacts in Riot.
*/
Expand Down Expand Up @@ -145,4 +159,9 @@
*/
@property (nonatomic, readonly) MXKContact *searchInputContact;

/**
The state of the users search from the homeserver user directory.
*/
@property (nonatomic, readonly) ContactsDataSourceUserDirectoryState userDirectoryState;

@end
84 changes: 80 additions & 4 deletions Riot/Model/Contact/ContactsDataSource.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ @interface ContactsDataSource ()
NSString *searchProcessingText;
NSMutableArray<MXKContact*> *searchProcessingLocalContacts;
NSMutableArray<MXKContact*> *searchProcessingMatrixContacts;

// The current request to the homeserver user directory
MXHTTPOperation *hsUserDirectoryOperation;

BOOL forceSearchResultRefresh;

Expand Down Expand Up @@ -115,6 +118,9 @@ - (void)destroy

localContactsCheckboxContainer = nil;
localContactsCheckbox = nil;

[hsUserDirectoryOperation cancel];
hsUserDirectoryOperation = nil;

[super destroy];
}
Expand Down Expand Up @@ -145,6 +151,13 @@ - (void)setForceMatrixIdInDisplayName:(BOOL)forceMatrixIdInDisplayName
}

- (void)searchWithPattern:(NSString *)searchText forceReset:(BOOL)forceRefresh
{
// If possible, always start a new search by asking the homeserver user directory
BOOL hsUserDirectory = (self.mxSession.state != MXSessionStateHomeserverNotReachable);
[self searchWithPattern:searchText forceReset:forceRefresh hsUserDirectory:hsUserDirectory];
}

- (void)searchWithPattern:(NSString *)searchText forceReset:(BOOL)forceRefresh hsUserDirectory:(BOOL)hsUserDirectory
{
// Update search results.
searchText = [searchText stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
Expand All @@ -161,11 +174,59 @@ - (void)searchWithPattern:(NSString *)searchText forceReset:(BOOL)forceRefresh
shrinkedSectionsBitMask = 0;
}
}
else if (forceRefresh || !searchProcessingText.length || [searchText hasPrefix:searchProcessingText] == NO)
else if (forceRefresh || ![searchText isEqualToString:searchProcessingText])
{
// Prepare on the main thread the arrays used to initialize the search on the processing queue.
unfilteredLocalContacts = [self unfilteredLocalContactsArray];
unfilteredMatrixContacts = [self unfilteredMatrixContactsArray];
if (!hsUserDirectory)
{
_userDirectoryState = ContactsDataSourceUserDirectoryStateOfflineLoading;
unfilteredMatrixContacts = [self unfilteredMatrixContactsArray];
}
else if (![searchText isEqualToString:searchProcessingText])
{
_userDirectoryState = ContactsDataSourceUserDirectoryStateLoading;

// Make a search on the homeserver user directory
[filteredMatrixContacts removeAllObjects];
filteredMatrixContacts = nil;

// Cancel previous operation
if (hsUserDirectoryOperation)
{
[hsUserDirectoryOperation cancel];
hsUserDirectoryOperation = nil;
}

hsUserDirectoryOperation = [self.mxSession.matrixRestClient searchUsers:searchText limit:100 success:^(MXUserSearchResponse *userSearchResponse) {

filteredMatrixContacts = [NSMutableArray arrayWithCapacity:userSearchResponse.results.count];

// Keep the response order as the hs ordered users by relevance
for (MXUser *mxUser in userSearchResponse.results)
{
MXKContact *contact = [[MXKContact alloc] initMatrixContactWithDisplayName:mxUser.displayname andMatrixID:mxUser.userId];
[filteredMatrixContacts addObject:contact];
}

hsUserDirectoryOperation = nil;

_userDirectoryState = userSearchResponse.limited ? ContactsDataSourceUserDirectoryStateLoadedButLimited : ContactsDataSourceUserDirectoryStateLoaded;

// And inform the delegate about the update
[self.delegate dataSource:self didCellChange:nil];

} failure:^(NSError *error) {

// Ignore connection cancellation error
if ((![error.domain isEqualToString:NSURLErrorDomain] || error.code != NSURLErrorCancelled))
{
// But for other errors, launch a local search
NSLog(@"[ContactsDataSource] [MXRestClient searchUsers] returns an error. Do a search on local known contacts");
[self searchWithPattern:searchText forceReset:forceRefresh hsUserDirectory:NO];
}
}];
}

// Disclose the sections
shrinkedSectionsBitMask = 0;
Expand Down Expand Up @@ -239,7 +300,12 @@ - (void)searchWithPattern:(NSString *)searchText forceReset:(BOOL)forceRefresh
// Update the filtered contacts.
currentSearchText = searchProcessingText;
filteredLocalContacts = searchProcessingLocalContacts;
filteredMatrixContacts = searchProcessingMatrixContacts;

if (!hsUserDirectory)
{
filteredMatrixContacts = searchProcessingMatrixContacts;
_userDirectoryState = ContactsDataSourceUserDirectoryStateOfflineLoaded;
}

if (!self.forceMatrixIdInDisplayName)
{
Expand Down Expand Up @@ -643,7 +709,17 @@ - (NSAttributedString *)attributedStringForHeaderTitleInSection:(NSInteger)secti
}
else //if (section == filteredMatrixContactsSection)
{
title = NSLocalizedStringFromTable(@"contacts_matrix_users_section", @"Vector", nil);
switch (_userDirectoryState)
{
case ContactsDataSourceUserDirectoryStateOfflineLoading:
case ContactsDataSourceUserDirectoryStateOfflineLoaded:
title = NSLocalizedStringFromTable(@"contacts_user_directory_offline_section", @"Vector", nil);
break;

default:
title = NSLocalizedStringFromTable(@"contacts_user_directory_section", @"Vector", nil);
break;
}

if (currentSearchText.length)
{
Expand Down

0 comments on commit 736d9bb

Please sign in to comment.