Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add NSUserActivity to rooms to appear in spotlight search #4865

Open
wants to merge 5 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
31 changes: 31 additions & 0 deletions Riot/Managers/Activities/UserActivityService.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// Copyright 2021 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#ifndef UserActivityService_h
#define UserActivityService_h

/// MXUserActivityTypes identifes user activities
typedef NSString *const UserActivityType NS_TYPED_EXTENSIBLE_ENUM;

static UserActivityType const MXUserActivityTypeRoom = @"org.matrix.room";

/// MXUserActivityFields identifies fields in the userInfo of a UserActivity
typedef NSString *const UserActivityField NS_TYPED_EXTENSIBLE_ENUM;

static UserActivityField const UserActivityFieldRoom = @"roomID";
static UserActivityField const UserActivityFieldUser = @"userID";

#endif /* UserActivityService_h */
70 changes: 70 additions & 0 deletions Riot/Managers/Activities/UserActivityService.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//
// Copyright 2021 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

import Foundation
import MatrixSDK

@objcMembers
class UserActivityService: NSObject {

// MARK: - Properties

static let shared = UserActivityService()

// MARK: - Setup

private override init() {
super.init()

NotificationCenter.default.addObserver(self, selector: #selector(didLeaveRoom(_:)), name: .mxSessionDidLeaveRoom, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(didLogOut(_:)), name: .mxkAccountManagerDidRemoveAccount, object: nil)
}

// 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] = 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 = roomId

userActivity.isEligibleForHandoff = true
userActivity.isEligibleForSearch = true
userActivity.isEligibleForPrediction = true


}

func didLeaveRoom(_ notification: Notification) {
guard let roomId = notification.userInfo?[kMXSessionNotificationRoomIdKey] as? String else { return }
NSUserActivity.deleteSavedUserActivities(withPersistentIdentifiers: [roomId], completionHandler: { })
}

func didLogOut(_ notification: Notification) {
NSUserActivity.deleteAllSavedUserActivities(completionHandler: { })
}
}
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
20 changes: 18 additions & 2 deletions Riot/Modules/Application/LegacyAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
#import "MXSession+Riot.h"
#import "MXRoom+Riot.h"

#import "UserActivityService.h"

#import "Riot-Swift.h"
#import "PushNotificationService.h"

Expand Down Expand Up @@ -745,13 +747,22 @@ - (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserAct
{
continueUserActivity = [self handleUniversalLink:userActivity];
}
else if ([userActivity.activityType isEqualToString:MXUserActivityTypeRoom])
{
NSString *roomID = userActivity.userInfo[UserActivityFieldRoom];
if (!roomID)
return continueUserActivity;

[self navigateToRoomById:roomID];
continueUserActivity = YES;
}
else if ([userActivity.activityType isEqualToString:INStartAudioCallIntentIdentifier] ||
[userActivity.activityType isEqualToString:INStartVideoCallIntentIdentifier])
{
INInteraction *interaction = userActivity.interaction;

// roomID provided by Siri intent
NSString *roomID = userActivity.userInfo[@"roomID"];
NSString *roomID = userActivity.userInfo[UserActivityFieldRoom];

// We've launched from calls history list
if (!roomID)
Expand Down Expand Up @@ -2779,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 @@ -2812,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
17 changes: 17 additions & 0 deletions Riot/Modules/Room/RoomViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/

@import MobileCoreServices;
@import CoreSpotlight;

#import "RoomViewController.h"

Expand Down Expand Up @@ -128,6 +129,7 @@
#import "SecurityViewController.h"

#import "TypingUserInfo.h"
#import "UserActivityService.h"

#import "MXSDKOptions.h"

Expand Down Expand Up @@ -610,6 +612,8 @@ - (void)viewWillAppear:(BOOL)animated
notificationTaskProfile = [MXSDKOptions.sharedInstance.profiler startMeasuringTaskWithName:AnalyticsNoficationsTimeToDisplayContent
category:AnalyticsNoficationsCategory];
}

[self updateUserActivity];
}

- (void)viewWillDisappear:(BOOL)animated
Expand Down Expand Up @@ -2030,6 +2034,19 @@ - (void)setupActions {
roomInputView.actionsBar.actionItems = actionItems;
}

- (void)updateUserActivity
{
if (!self.userActivity) {
self.userActivity = [[NSUserActivity alloc] initWithActivityType:MXUserActivityTypeRoom];
}

[UserActivityService.shared update:self.userActivity from:self.roomDataSource.room];

// TODO: add a NSUserActivityDelegate to save the current text in the userinfo of the activity
// self.userActivity.delegate = self;
// self.userActivity.needsSave = true;
}

- (void)roomInputToolbarViewPresentStickerPicker
{
// Search for the sticker picker widget in the user account
Expand Down
4 changes: 4 additions & 0 deletions Riot/SupportingFiles/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@
<string>The photo library is used to send photos and videos.</string>
<key>NSSiriUsageDescription</key>
<string>Siri is used to perform calls even from the lock screen.</string>
<key>NSUserActivityTypes</key>
<array>
<string>org.matrix.room</string>
</array>
<key>UIBackgroundModes</key>
<array>
<string>audio</string>
Expand Down
1 change: 1 addition & 0 deletions Riot/SupportingFiles/Riot-Bridging-Header.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,4 @@
#import "GroupDetailsViewController.h"
#import "RoomInputToolbarView.h"
#import "NSArray+Element.h"
#import "UserActivityService.h"
1 change: 1 addition & 0 deletions changelog.d/4865.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add NSUserActivity to rooms to appear in spotlight search