-
Notifications
You must be signed in to change notification settings - Fork 504
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
Remove PushKit #3268
Merged
Merged
Remove PushKit #3268
Changes from 2 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
53bb87d
Create notification service extension
ismailgulek a3890c6
update fastlane commands for new target
ismailgulek 7335682
Configure log files correctly
ismailgulek 9475cd7
Fix Manu's comments
ismailgulek f85aee0
Implement a workaround to delete some notifications afterwards
ismailgulek 87e702a
Swiftify some ifs
ismailgulek 23203af
Swiftify some ifs
ismailgulek db01577
Swiftify some ifs
ismailgulek 4e25323
Swiftify code, ignore session state
ismailgulek 09353f4
Swiftify content function, use switch for ifs when possible
ismailgulek f25e3b6
Swiftify code, style changes
ismailgulek 7f39581
Swiftify extension methods
ismailgulek 63d4a16
Update Riot/Utils/Constants.swift
ismailgulek db8b5a4
Update RiotNSE/NotificationService.swift
ismailgulek ba50f47
Remove some commented code
ismailgulek b1a654d
Change logs for fallback cases
ismailgulek 69d9b5b
Add fallback method calls on some points
ismailgulek c4bdf33
Remove some redundant semicolons
ismailgulek fa44dae
Update RiotNSE/NotificationService.swift
ismailgulek a0e9540
Update RiotNSE/NotificationService.swift
ismailgulek f5324ae
Update RiotNSE/NotificationService.swift
ismailgulek 3bd9e6b
Change some guarding to if check
ismailgulek bcd9683
Implement a memory store specific to NSE
ismailgulek d859ce8
Drop MXKAccount and MXFileStore usages
ismailgulek 845363b
Fix Manu's comments
ismailgulek 4ef5e17
Add some comments
ismailgulek 0b639e8
Adapt to new method, change timeout to seconds
ismailgulek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
Copyright 2020 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 | ||
|
||
class NSEMemoryStore: MXMemoryStore { | ||
|
||
private var credentials: MXCredentials | ||
private var fileStore: MXFileStore | ||
|
||
init(withCredentials credentials: MXCredentials) { | ||
self.credentials = credentials | ||
fileStore = MXFileStore(credentials: credentials) | ||
fileStore.loadMetaData() | ||
} | ||
|
||
// Return real eventStreamToken, to be able to launch a meaningful background sync | ||
override var eventStreamToken: String? { | ||
get { | ||
return fileStore.eventStreamToken | ||
} set { | ||
// no-op | ||
} | ||
} | ||
|
||
// Return real userAccountData, to be able to use push rules | ||
override var userAccountData: [AnyHashable : Any]? { | ||
get { | ||
return fileStore.userAccountData | ||
} set { | ||
// no-op | ||
} | ||
} | ||
|
||
// This store should act like as a permanent one | ||
override var isPermanent: Bool { | ||
return true | ||
} | ||
|
||
// Some mandatory methods to implement to be permanent | ||
override func storeState(forRoom roomId: String, stateEvents: [MXEvent]) { | ||
|
||
} | ||
|
||
override func state(ofRoom roomId: String, success: @escaping ([MXEvent]) -> Void, failure: ((Error) -> Void)? = nil) { | ||
|
||
} | ||
|
||
override func summary(ofRoom roomId: String) -> MXRoomSummary? { | ||
return fileStore.summary(ofRoom: roomId) | ||
} | ||
|
||
override func accountData(ofRoom roomId: String) -> MXRoomAccountData? { | ||
return fileStore.accountData(ofRoom: roomId) | ||
} | ||
|
||
// Override and return a user to be stored on session.myUser | ||
override func user(withUserId userId: String) -> MXUser? { | ||
if userId == credentials.userId { | ||
return MXMyUser(userId: userId) | ||
} | ||
return MXUser(userId: userId) | ||
} | ||
|
||
override func close() { | ||
fileStore.close() | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,8 +22,8 @@ class NotificationService: UNNotificationServiceExtension { | |
var contentHandler: ((UNNotificationContent) -> Void)? | ||
var originalContent: UNMutableNotificationContent? | ||
|
||
var userAccount: MXKAccount? | ||
var store: MXFileStore? | ||
var mxSession: MXSession? | ||
var store: NSEMemoryStore! | ||
var showDecryptedContentInNotifications: Bool { | ||
return RiotSettings.shared.showDecryptedContentInNotifications | ||
} | ||
|
@@ -42,21 +42,19 @@ class NotificationService: UNNotificationServiceExtension { | |
|
||
let userInfo = content.userInfo | ||
NSLog("[NotificationService] Payload came: \(userInfo)") | ||
let roomId = userInfo["room_id"] as? String | ||
let eventId = userInfo["event_id"] as? String | ||
|
||
guard roomId != nil, eventId != nil else { | ||
guard let roomId = userInfo["room_id"] as? String, let _ = userInfo["event_id"] as? String else { | ||
// it's not a Matrix notification, do not change the content | ||
NSLog("[NotificationService] didReceiveRequest: This is not a Matrix notification.") | ||
contentHandler(content) | ||
return | ||
} | ||
|
||
// setup user account | ||
setup() | ||
|
||
// fetch the event first | ||
fetchEvent() | ||
setup(withRoomId: roomId) { | ||
// fetch the event first | ||
self.fetchEvent() | ||
} | ||
} | ||
|
||
override func serviceExtensionTimeWillExpire() { | ||
|
@@ -66,7 +64,7 @@ class NotificationService: UNNotificationServiceExtension { | |
fallbackToOriginalContent() | ||
} | ||
|
||
func setup() { | ||
func setup(withRoomId roomId: String, completion: @escaping () -> Void) { | ||
let sdkOptions = MXSDKOptions.sharedInstance() | ||
sdkOptions.applicationGroupIdentifier = "group.im.vector" | ||
sdkOptions.disableIdenticonUseForUserAvatar = true | ||
|
@@ -79,21 +77,30 @@ class NotificationService: UNNotificationServiceExtension { | |
MXLogger.redirectNSLog(toFiles: true) | ||
} | ||
|
||
userAccount = MXKAccountManager.shared()?.activeAccounts.first | ||
|
||
if let userAccount = userAccount { | ||
store = MXFileStore(credentials: userAccount.mxCredentials) | ||
if let userAccount = MXKAccountManager.shared()?.activeAccounts.first { | ||
store = NSEMemoryStore(withCredentials: userAccount.mxCredentials) | ||
store.open(with: userAccount.mxCredentials, onComplete: nil, failure: nil) | ||
store.getOrCreateRoomStore(roomId) | ||
manuroe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if userAccount.mxSession == nil { | ||
userAccount.openSession(with: store!) | ||
} | ||
mxSession = MXSession(matrixRestClient: MXRestClient(credentials: userAccount.mxCredentials, unrecognizedCertificateHandler: nil)) | ||
mxSession?.setStore(store, completion: { (response) in | ||
switch response { | ||
case .success: | ||
completion() | ||
break | ||
case .failure(let error): | ||
NSLog("[NotificationService] setup: MXSession.setStore method returned error: \(String(describing: error))") | ||
self.fallbackToOriginalContent() | ||
break | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func fetchEvent() { | ||
guard let content = originalContent, let userAccount = self.userAccount else { | ||
guard let content = originalContent, let mxSession = mxSession else { | ||
// there is something wrong, do not change the content | ||
NSLog("[NotificationService] fetchEvent: Either originalContent or userAccount is missing.") | ||
NSLog("[NotificationService] fetchEvent: Either originalContent or mxSession is missing.") | ||
fallbackToOriginalContent() | ||
return | ||
} | ||
|
@@ -106,7 +113,7 @@ class NotificationService: UNNotificationServiceExtension { | |
return | ||
} | ||
|
||
userAccount.mxSession.event(withEventId: eventId, inRoom: roomId, success: { [weak self] (event) in | ||
mxSession.event(withEventId: eventId, inRoom: roomId, success: { [weak self] (event) in | ||
guard let self = self else { | ||
NSLog("[NotificationService] fetchEvent: MXSession.event method returned too late successfully.") | ||
return | ||
|
@@ -138,7 +145,7 @@ class NotificationService: UNNotificationServiceExtension { | |
} | ||
|
||
// should decrypt it first | ||
if userAccount.mxSession.decryptEvent(event, inTimeline: nil) { | ||
if mxSession.decryptEvent(event, inTimeline: nil) { | ||
// decryption succeeded | ||
self.processEvent(event) | ||
} else { | ||
|
@@ -157,44 +164,43 @@ class NotificationService: UNNotificationServiceExtension { | |
} | ||
|
||
func launchBackgroundSync() { | ||
guard let userAccount = userAccount else { | ||
self.fallbackToOriginalContent() | ||
return | ||
} | ||
guard let store = store else { | ||
guard let mxSession = mxSession else { | ||
NSLog("[NotificationService] launchBackgroundSync: mxSession is missing.") | ||
self.fallbackToOriginalContent() | ||
return | ||
} | ||
if userAccount.mxSession == nil { | ||
userAccount.openSession(with: store) | ||
} | ||
|
||
// launch an initial background sync | ||
userAccount.initialBackgroundSync(20000, success: { [weak self] in | ||
guard let self = self else { | ||
NSLog("[NotificationService] launchBackgroundSync: MXKAccount.initialBackgroundSync returned too late successfully") | ||
return | ||
} | ||
self.fetchEvent() | ||
}) { [weak self] (error) in | ||
guard let self = self else { | ||
NSLog("[NotificationService] launchBackgroundSync: MXKAccount.initialBackgroundSync returned too late with error: \(String(describing: error))") | ||
return | ||
mxSession.initialBackgroundSync(withTimeout: 20000) { [weak self] (response) in | ||
switch response { | ||
case .success: | ||
guard let self = self else { | ||
NSLog("[NotificationService] launchBackgroundSync: MXSession.initialBackgroundSync returned too late successfully") | ||
return | ||
} | ||
self.fetchEvent() | ||
break | ||
case .failure(let error): | ||
guard let self = self else { | ||
NSLog("[NotificationService] launchBackgroundSync: MXSession.initialBackgroundSync returned too late with error: \(String(describing: error))") | ||
return | ||
} | ||
NSLog("[NotificationService] launchBackgroundSync: MXSession.initialBackgroundSync returned with error: \(String(describing: error))") | ||
self.fallbackToOriginalContent() | ||
break | ||
} | ||
NSLog("[NotificationService] launchBackgroundSync: MXKAccount.initialBackgroundSync returned with error: \(String(describing: error))") | ||
self.fallbackToOriginalContent() | ||
} | ||
} | ||
|
||
func processEvent(_ event: MXEvent) { | ||
guard let content = originalContent, let userAccount = userAccount else { | ||
guard let content = originalContent, let mxSession = mxSession else { | ||
self.fallbackToOriginalContent() | ||
return | ||
manuroe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
self.notificationContent(forEvent: event, inAccount: userAccount) { (notificationContent) in | ||
self.notificationContent(forEvent: event, inSession: mxSession) { (notificationContent) in | ||
// close store | ||
self.store?.close() | ||
self.store.close() | ||
|
||
// Modify the notification content here... | ||
if let newContent = notificationContent { | ||
|
@@ -215,7 +221,7 @@ class NotificationService: UNNotificationServiceExtension { | |
} | ||
|
||
func fallbackToOriginalContent() { | ||
store?.close() | ||
store.close() | ||
guard let content = originalContent else { | ||
NSLog("[NotificationService] fallbackToOriginalContent: Original content is missing.") | ||
return | ||
|
@@ -225,28 +231,37 @@ class NotificationService: UNNotificationServiceExtension { | |
contentHandler?(content) | ||
} | ||
|
||
func notificationContent(forEvent event: MXEvent, inAccount account: MXKAccount, onComplete: @escaping (UNNotificationContent?) -> Void) { | ||
func notificationContent(forEvent event: MXEvent, inSession session: MXSession, onComplete: @escaping (UNNotificationContent?) -> Void) { | ||
guard let content = event.content, content.count > 0 else { | ||
NSLog("[NotificationService][Push] notificationContentForEvent: empty event content") | ||
onComplete(nil) | ||
return | ||
} | ||
|
||
guard let room = account.mxSession.room(withRoomId: event.roomId) else { | ||
guard let room = MXRoom(roomId: event.roomId, matrixSession: session, andStore: store) else { | ||
NSLog("[NotificationService][Push] notificationBodyForEvent: Unknown room") | ||
onComplete(nil) | ||
return | ||
} | ||
|
||
let pushRule = room.getRoomPushRule() | ||
|
||
room.state({ (roomState: MXRoomState!) in | ||
|
||
// initialize a temporary file store, just to load the room state | ||
let fileStore = MXFileStore(credentials: session.credentials) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not using the existing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, totally makes sense. Updating. |
||
|
||
MXRoomState.load(from: fileStore, withRoomId: event.roomId, matrixSession: session) { (roomState) in | ||
guard let roomState = roomState else { | ||
NSLog("[NotificationService] notificationContentForEvent: Could not load the room state") | ||
onComplete(nil) | ||
return | ||
} | ||
|
||
var notificationTitle: String? | ||
var notificationBody: String? | ||
|
||
var threadIdentifier = room.roomId | ||
let eventSenderName = roomState.members.memberName(event.sender) | ||
let currentUserId = account.mxCredentials.userId | ||
let currentUserId = session.credentials.userId | ||
|
||
switch event.eventType { | ||
case .roomMessage, .roomEncrypted: | ||
|
@@ -284,7 +299,7 @@ class NotificationService: UNNotificationServiceExtension { | |
} | ||
|
||
let roomDisplayName = room.summary.displayname | ||
let myUserId = account.mxSession.myUser.userId | ||
let myUserId = session.myUser.userId | ||
let isIncomingEvent = event.sender != myUserId | ||
|
||
// Display the room name only if it is different than the sender name | ||
|
@@ -298,8 +313,8 @@ class NotificationService: UNNotificationServiceExtension { | |
} else if msgType == kMXMessageTypeImage { | ||
notificationBody = NSString.localizedUserNotificationString(forKey: "IMAGE_FROM_USER", arguments: [eventSenderName as Any, messageContent as Any]) | ||
} else if room.isDirect && isIncomingEvent && msgType == kMXMessageTypeKeyVerificationRequest { | ||
account.mxSession.crypto.keyVerificationManager.keyVerification(fromKeyVerificationEvent: event, | ||
success:{ (keyVerification) in | ||
session.crypto.keyVerificationManager.keyVerification(fromKeyVerificationEvent: event, | ||
success:{ (keyVerification) in | ||
guard let request = keyVerification.request, request.state == MXKeyVerificationRequestStatePending else { | ||
onComplete(nil) | ||
return | ||
|
@@ -391,7 +406,7 @@ class NotificationService: UNNotificationServiceExtension { | |
pushRule: pushRule) | ||
|
||
onComplete(notificationContent) | ||
}) | ||
} | ||
} | ||
|
||
func notificationContent(withTitle title: String?, | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It can be removed. It is called by MXSession.setStore()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's right. Removing.