Skip to content
This repository has been archived by the owner on Dec 12, 2022. It is now read-only.

Allow users to search in encrypted room locally [#783]. #787

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions MatrixKit/Models/Room/MXKRoomDataSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -708,4 +708,6 @@ extern NSString *const kMXKRoomDataSourceTimelineErrorErrorKey;
*/
- (NSString*)editableTextMessageForEvent:(MXEvent*)event;

- (void)search:(NSString* )text filter:(MXRoomEventFilter *)filter success:(void (^)(MXSearchRoomEventResults *result))success;

@end
54 changes: 54 additions & 0 deletions MatrixKit/Models/Room/MXKRoomDataSource.m
Original file line number Diff line number Diff line change
Expand Up @@ -3582,4 +3582,58 @@ - (void)replaceTextMessageForEventWithId:(NSString*)eventId
}
}

#pragma mark - Search

- (void)paginateTillBeginWithComplete:(void (^)(void))onComplete{
MXWeakify(self);

if (NO == [self canPaginate:MXTimelineDirectionBackwards]) {
onComplete();
return;
}
[self paginate:100 direction:MXTimelineDirectionBackwards onlyFromStore:NO success:^(NSUInteger addedCellNumber) {
MXStrongifyAndReturnIfNil(self);

if(addedCellNumber == 0) {
onComplete();
} else {
[self paginateTillBeginWithComplete:onComplete];
}
} failure:^(NSError *error) {
onComplete();
}];
}

- (void)search:(NSString* )text filter:(MXRoomEventFilter *)filter success:(void (^)(MXSearchRoomEventResults *result))success {
MXWeakify(self);
[self paginateTillBeginWithComplete:^{
MXStrongifyAndReturnIfNil(self);

id<MXEventsEnumerator> storeMessagesEnumerator = [self.room.mxSession.store messagesEnumeratorForRoom:self.roomId];
NSMutableArray *roomEvents = [[NSMutableArray alloc] init];
while (0 < storeMessagesEnumerator.remaining) {
MXEvent *event = [storeMessagesEnumerator nextEvent];
NSString *body = [event.content valueForKey:@"body"];
if (body && [body rangeOfString:text options:NSCaseInsensitiveSearch].location != NSNotFound) {
[roomEvents addObject:event];
}
}

NSMutableArray *searchResult = [[NSMutableArray alloc] init];
for (MXEvent* event in roomEvents) {
if ( (filter.containsURL && event.isMediaAttachment) || !filter.containsURL) {
MXSearchResult *result = [[MXSearchResult alloc] init];
result.result = event;
[searchResult addObject:result];
}
}

MXSearchRoomEventResults *result = [[MXSearchRoomEventResults alloc] init];
result.count = searchResult.count;
result.nextBatch = nil;
result.groups = nil;
result.results = [NSArray arrayWithArray:searchResult];
success(result);
}];
}
@end
28 changes: 28 additions & 0 deletions MatrixKit/Models/Search/MXKSearchDataSource.m
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,36 @@ - (void)setState:(MXKDataSourceState)newState
}
}

-(void)doLocalSearch {
MXWeakify(self);

[_roomDataSource search:_searchText filter:_roomEventFilter success:^(MXSearchRoomEventResults *result) {
// Process HS response to cells data
[self convertHomeserverResultsIntoCells:result onComplete:^{
MXStrongifyAndReturnIfNil(self);

self.state = MXKDataSourceStateReady;

// Provide changes information to the delegate
NSIndexSet *insertedIndexes;
if (result.results.count)
{
insertedIndexes = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, result.results.count)];
}

[self.delegate dataSource:self didCellChange:insertedIndexes];
}];
}];
}

- (void)doSearch
{

// Handle local search
if (_roomDataSource.roomState.isEncrypted) {
[self doLocalSearch];
return;
}
// Handle one request at a time
if (searchRequest)
{
Expand Down