diff --git a/MatrixKit/Models/Room/MXKRoomDataSource.h b/MatrixKit/Models/Room/MXKRoomDataSource.h index a89ea41d7..91d7f9368 100644 --- a/MatrixKit/Models/Room/MXKRoomDataSource.h +++ b/MatrixKit/Models/Room/MXKRoomDataSource.h @@ -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 diff --git a/MatrixKit/Models/Room/MXKRoomDataSource.m b/MatrixKit/Models/Room/MXKRoomDataSource.m index d70397ddd..26b099c0e 100644 --- a/MatrixKit/Models/Room/MXKRoomDataSource.m +++ b/MatrixKit/Models/Room/MXKRoomDataSource.m @@ -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 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 diff --git a/MatrixKit/Models/Search/MXKSearchDataSource.m b/MatrixKit/Models/Search/MXKSearchDataSource.m index 7a2cffe20..416693061 100644 --- a/MatrixKit/Models/Search/MXKSearchDataSource.m +++ b/MatrixKit/Models/Search/MXKSearchDataSource.m @@ -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) {