Skip to content

Commit

Permalink
UserActivityService: make sure roomID exists
Browse files Browse the repository at this point in the history
Signed-off-by: Finn Behrens <[email protected]>
  • Loading branch information
kloenk committed Oct 31, 2021
1 parent 448ed1a commit a971424
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 23 deletions.
27 changes: 5 additions & 22 deletions Riot/Managers/Activities/UserActivityService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@
//

import Foundation
import CoreSpotlight
import MatrixSDK

@objcMembers
class UserActivityService: NSObject {

// MARK: - Properties

#warning("This is initialised lazily so currently only observes left rooms if RoomViewController has been presented.")
static let shared = UserActivityService()

// MARK: - Setup
Expand All @@ -38,50 +36,35 @@ 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
userActivity.isEligibleForPrediction = true

var contentAttributes: CSSearchableItemAttributeSet
if #available(iOS 14.0, *) {
contentAttributes = CSSearchableItemAttributeSet(contentType: UTType.item)
} else {
contentAttributes = CSSearchableItemAttributeSet(itemContentType: "public.item")
}

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
// TODO: contentAttributes.weakRelatedUniqueIdentifier (is this needed? does it break anything else?)
contentAttributes.instantMessageAddresses = [ room.roomId ]

userActivity.contentAttributeSet = contentAttributes
}

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)
}

func didLogOut(_ notification: Notification) {
NSUserActivity.deleteAllSavedUserActivities(completionHandler: { })
CSSearchableIndex.default().deleteAllSearchableItems(completionHandler: nil)
}
}
3 changes: 3 additions & 0 deletions Riot/Modules/Application/AppCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ final class AppCoordinator: NSObject, AppCoordinatorType {
func start() {
self.setupLogger()
self.setupTheme()

// Make sure the UserActivityService is loaded
let _ = UserActivityService.shared

// Setup navigation router store
_ = NavigationRouterStore.shared
Expand Down
7 changes: 6 additions & 1 deletion Riot/Modules/Application/LegacyAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -2790,6 +2790,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 @@ -2823,7 +2828,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 a971424

Please sign in to comment.