Skip to content

Commit

Permalink
Allow users to search in encrypted room locally [matrix-org#783].
Browse files Browse the repository at this point in the history
  • Loading branch information
toshanmugaraj committed Mar 10, 2021
1 parent 1631bb8 commit b5d269e
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 1 deletion.
4 changes: 4 additions & 0 deletions MatrixKit/Models/Room/MXKRoomBubbleCellData.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@
*/
@property (nonatomic, readonly) BOOL containsBubbleComponentWithEncryptionBadge;

@property (nonatomic, weak) MXEvent *event;

/**
Check and refresh the position of each component.
*/
Expand Down Expand Up @@ -148,4 +150,6 @@
*/
- (MXKRoomBubbleComponent*)getFirstBubbleComponentWithDisplay;

- (BOOL)searchText:(NSString *)text;

@end
6 changes: 5 additions & 1 deletion MatrixKit/Models/Room/MXKRoomBubbleCellData.m
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ - (instancetype)initWithEvent:(MXEvent *)event andRoomState:(MXRoomState *)roomS
{
bubbleComponents = [NSMutableArray array];
[bubbleComponents addObject:firstComponent];

_event = event;
senderId = event.sender;
roomId = roomDataSource.roomId;
senderDisplayName = [roomDataSource.eventFormatter senderDisplayNameForEvent:event withRoomState:roomState];
Expand Down Expand Up @@ -90,6 +90,10 @@ - (instancetype)initWithEvent:(MXEvent *)event andRoomState:(MXRoomState *)roomS
return self;
}

- (BOOL)searchText:(NSString *)text {
return [self.textMessage rangeOfString:text options:NSCaseInsensitiveSearch].location != NSNotFound;
}

- (void)dealloc
{
// Reset any observer on publicised groups by user.
Expand Down
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;

- (MXSearchRoomEventResults *)search:(NSString* )text filter:(MXRoomEventFilter *)filter;

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

#pragma mark - Search from locally in encrypted room.

- (MXSearchRoomEventResults *)search:(NSString* )text filter:(MXRoomEventFilter *)filter {
NSArray *bubbles = self->bubbles;
NSMutableArray *searchResult = [[NSMutableArray alloc] init];

for (MXKRoomBubbleCellData* item in bubbles) {
if([item searchText:text]) {
if ( (filter.containsURL && item.event.isMediaAttachment) || !filter.containsURL) {
MXSearchResult *result = [[MXSearchResult alloc] init];
result.result = item.event;
[searchResult addObject:result];
}
}
}

MXSearchRoomEventResults *result = [[MXSearchRoomEventResults alloc] init];
result.count = searchResult.count;
result.nextBatch = nil;
result.groups = nil;
result.results = [NSArray arrayWithArray:searchResult];

return result;
}
@end
3 changes: 3 additions & 0 deletions MatrixKit/Models/Search/MXKSearchDataSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#import "MXKDataSource.h"
#import "MXKSearchCellDataStoring.h"
#import "MXKRoomDataSource.h"

#import "MXKEventFormatter.h"

Expand Down Expand Up @@ -71,6 +72,8 @@ extern NSString *const kMXKSearchCellDataIdentifier;
*/
@property (nonatomic) BOOL shouldShowRoomDisplayName;

@property (nonatomic) MXKRoomDataSource *roomDataSource;


/**
Launch a message search homeserver side.
Expand Down
26 changes: 26 additions & 0 deletions MatrixKit/Models/Search/MXKSearchDataSource.m
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,34 @@ - (void)setState:(MXKDataSourceState)newState
}
}

-(void)doLocalSearch {
MXSearchRoomEventResults *roomEventResults = [_roomDataSource search:_searchText filter:_roomEventFilter];
// Process HS response to cells data
MXWeakify(self);
[self convertHomeserverResultsIntoCells:roomEventResults onComplete:^{
MXStrongifyAndReturnIfNil(self);

self.state = MXKDataSourceStateReady;

// Provide changes information to the delegate
NSIndexSet *insertedIndexes;
if (roomEventResults.results.count)
{
insertedIndexes = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, roomEventResults.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

0 comments on commit b5d269e

Please sign in to comment.