Skip to content

Commit

Permalink
Create real CSSearchableItem so leaving a room delets the index item
Browse files Browse the repository at this point in the history
Signed-off-by: Finn Behrens <[email protected]>
  • Loading branch information
kloenk committed Sep 29, 2021
1 parent 8bf64aa commit 5628de5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
29 changes: 22 additions & 7 deletions Riot/Managers/Activities/UserActivityService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,21 @@ class UserActivityService: NSObject {
// MARK: - Public

func update(_ userActivity: NSUserActivity, from room: MXRoom) {
guard let roomId = room.roomId else {
return
}
userActivity.title = room.summary.displayname

userActivity.requiredUserInfoKeys = [ UserActivityField.room.rawValue ]
var userInfo = [String: Any]()
userInfo[UserActivityField.room.rawValue] = room.roomId
userInfo[UserActivityField.room.rawValue] = roomId
if room.isDirect {
userInfo[UserActivityField.user.rawValue] = room.directUserId
}
userActivity.userInfo = userInfo

// TODO: if we add more userActivities, a `org.matrix.room` prefix should probably be added
userActivity.persistentIdentifier = room.roomId
userActivity.persistentIdentifier = roomId

userActivity.isEligibleForHandoff = true
userActivity.isEligibleForSearch = true
Expand All @@ -65,19 +68,31 @@ class UserActivityService: NSObject {
contentAttributes.title = room.summary.displayname
contentAttributes.displayName = room.summary.displayname
contentAttributes.contentDescription = room.summary.lastMessage.text
// TODO: contentAttributes.thumbnailURL
contentAttributes.domainIdentifier = room.roomId
contentAttributes.relatedUniqueIdentifier = room.summary.lastMessage.eventId
contentAttributes.domainIdentifier = roomId
contentAttributes.relatedUniqueIdentifier = roomId
// TODO: contentAttributes.weakRelatedUniqueIdentifier (is this needed? does it break anything else?)
contentAttributes.instantMessageAddresses = [ room.roomId ]
contentAttributes.instantMessageAddresses = [ roomId ]

// TODO: contentAttributes.thumbnailURL

userActivity.contentAttributeSet = contentAttributes

let item = CSSearchableItem(uniqueIdentifier: "\(roomId)/\(room.summary.lastMessage.eventId)", domainIdentifier: room.roomId, attributeSet: contentAttributes)
CSSearchableIndex.default().indexSearchableItems([item], completionHandler: {e in
if let e = e {
MXLog.warning("[UserActivityService] Failed to index item", context: e)
}
})
}

func didLeaveRoom(_ notification: Notification) {
guard let roomId = notification.userInfo?[kMXSessionNotificationRoomIdKey] as? String else { return }
NSUserActivity.deleteSavedUserActivities(withPersistentIdentifiers: [roomId], completionHandler: { })
CSSearchableIndex.default().deleteSearchableItems(withDomainIdentifiers: [roomId], completionHandler: nil)
CSSearchableIndex.default().deleteSearchableItems(withDomainIdentifiers: [roomId], completionHandler: { e in
if let e = e {
MXLog.warning("[UserActivityService] Failed to delete index domain", context: e)
}
})
}

func didLogOut(_ notification: Notification) {
Expand Down
18 changes: 17 additions & 1 deletion Riot/Modules/Application/LegacyAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#import <Intents/Intents.h>
#import <Contacts/Contacts.h>
@import CoreSpotlight;

#import "RecentsDataSource.h"
#import "RoomDataSource.h"
Expand Down Expand Up @@ -760,6 +761,16 @@ - (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserAct
[self navigateToRoomById:roomID];
continueUserActivity = YES;
}
else if ([userActivity.activityType isEqualToString:CSSearchableItemActionType])
{
NSString *identifier = userActivity.userInfo[CSSearchableItemActivityIdentifier];
NSArray *parts = [identifier componentsSeparatedByString:@"/"];
NSString *roomID = parts[0];
NSString *eventID = parts[1];

[self navigateToRoomById:roomID andEventId:eventID];
continueUserActivity = YES;
}
else if ([userActivity.activityType isEqualToString:INStartAudioCallIntentIdentifier] ||
[userActivity.activityType isEqualToString:INStartVideoCallIntentIdentifier])
{
Expand Down Expand Up @@ -2781,6 +2792,11 @@ - (void)selectMatrixAccount:(void (^)(MXKAccount *selectedAccount))onSelection
#pragma mark - Matrix Rooms handling

- (void)navigateToRoomById:(NSString *)roomId
{
[self navigateToRoomById:roomId andEventId:nil];
}

- (void)navigateToRoomById:(NSString *)roomId andEventId:(NSString *)eventId
{
if (roomId.length)
{
Expand Down Expand Up @@ -2814,7 +2830,7 @@ - (void)navigateToRoomById:(NSString *)roomId
{
MXLogDebug(@"[AppDelegate][Push] navigateToRoomById: open the roomViewController %@", roomId);

[self showRoom:roomId andEventId:nil withMatrixSession:dedicatedAccount.mxSession];
[self showRoom:roomId andEventId:eventId withMatrixSession:dedicatedAccount.mxSession];
}
else
{
Expand Down

0 comments on commit 5628de5

Please sign in to comment.