Skip to content

Commit b4cde8e

Browse files
committed
Fixed a few issues from the rebase and minor SessionNetworkAPI tweaks
• Updated SessionNetworkAPI so preparing requests doesn't use the database • Fixed build errors
1 parent 63f8c06 commit b4cde8e

File tree

11 files changed

+57
-77
lines changed

11 files changed

+57
-77
lines changed

Session/Notifications/NotificationActionHandler.swift

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -222,14 +222,11 @@ public class NotificationActionHandler {
222222
switch result {
223223
case .finished: break
224224
case .failure:
225-
dependencies[singleton: .storage].read { db in
226-
dependencies[singleton: .notificationsManager].notifyForFailedSend(
227-
db,
228-
threadId: threadId,
229-
threadVariant: threadVariant,
230-
applicationState: applicationState
231-
)
232-
}
225+
dependencies[singleton: .notificationsManager].notifyForFailedSend(
226+
threadId: threadId,
227+
threadVariant: threadVariant,
228+
applicationState: applicationState
229+
)
233230
}
234231
}
235232
)

Session/Notifications/NotificationPresenter.swift

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ public class NotificationPresenter: NSObject, UNUserNotificationCenterDelegate,
7979
// MARK: - Presentation
8080

8181
public func notifyForFailedSend(
82-
_ db: Database,
8382
threadId: String,
8483
threadVariant: SessionThread.Variant,
8584
applicationState: UIApplication.State
@@ -107,25 +106,34 @@ public class NotificationPresenter: NSObject, UNUserNotificationCenterDelegate,
107106
switch notificationSettings.previewType {
108107
case .noNameNoPreview: content = content.with(title: Constants.app_name)
109108
case .nameNoPreview, .nameAndPreview:
109+
typealias ThreadInfo = (profile: Profile?, openGroupName: String?, openGroupUrlInfo: LibSession.OpenGroupUrlInfo?)
110+
let threadInfo: ThreadInfo? = dependencies[singleton: .storage].read { db in
111+
return (
112+
(threadVariant != .contact ? nil :
113+
try? Profile.fetchOne(db, id: threadId)
114+
),
115+
(threadVariant != .community ? nil :
116+
try? OpenGroup
117+
.select(.name)
118+
.filter(id: threadId)
119+
.asRequest(of: String.self)
120+
.fetchOne(db)
121+
),
122+
(threadVariant != .community ? nil :
123+
try? LibSession.OpenGroupUrlInfo.fetchOne(db, id: threadId)
124+
)
125+
)
126+
}
127+
110128
content = content.with(
111129
title: dependencies.mutate(cache: .libSession) { cache in
112130
cache.conversationDisplayName(
113131
threadId: threadId,
114132
threadVariant: threadVariant,
115-
contactProfile: (threadVariant != .contact ? nil :
116-
try? Profile.fetchOne(db, id: threadId)
117-
),
133+
contactProfile: threadInfo?.profile,
118134
visibleMessage: nil, /// This notification is unrelated to the received message
119-
openGroupName: (threadVariant != .community ? nil :
120-
try? OpenGroup
121-
.select(.name)
122-
.filter(id: threadId)
123-
.asRequest(of: String.self)
124-
.fetchOne(db)
125-
),
126-
openGroupUrlInfo: (threadVariant != .community ? nil :
127-
try? LibSession.OpenGroupUrlInfo.fetchOne(db, id: threadId)
128-
)
135+
openGroupName: threadInfo?.openGroupName,
136+
openGroupUrlInfo: threadInfo?.openGroupUrlInfo
129137
)
130138
}
131139
)
@@ -156,7 +164,7 @@ public class NotificationPresenter: NSObject, UNUserNotificationCenterDelegate,
156164
let identifier: String = "sessionNetworkPageLocalNotifcation_\(UUID().uuidString)" // stringlint:disable
157165

158166
// Schedule the notification after 1 hour
159-
var content: NotificationContent = NotificationContent(
167+
let content: NotificationContent = NotificationContent(
160168
threadId: nil,
161169
threadVariant: nil,
162170
identifier: identifier,

Session/Notifications/Types/AppNotificationAction.swift

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,3 @@ extension NotificationCategory {
4141
}
4242
}
4343
}
44-
45-
extension NotificationCategory {
46-
var actions: [AppNotificationAction] {
47-
switch self {
48-
case .incomingMessage: return [.markAsRead, .reply]
49-
case .errorMessage: return []
50-
case .threadlessErrorMessage: return []
51-
}
52-
}
53-
}

SessionMessagingKit/Jobs/GroupLeavingJob.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public enum GroupLeavingJob: JobExecutor {
3737
let destination: Message.Destination = .closedGroup(groupPublicKey: threadId)
3838

3939
dependencies[singleton: .storage]
40-
.writePublisher { db -> LeaveType in
40+
.readPublisher(value: { db -> RequestType in
4141
guard (try? ClosedGroup.exists(db, id: threadId)) == true else {
4242
Log.error(.cat, "Failed due to non-existent group")
4343
throw MessageSenderError.invalidClosedGroupUpdate

SessionMessagingKit/Sending & Receiving/Notifications/NotificationsManagerType.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ public protocol NotificationsManagerType {
2828
func notificationUserInfo(threadId: String, threadVariant: SessionThread.Variant) -> [String: Any]
2929
func notificationShouldPlaySound(applicationState: UIApplication.State) -> Bool
3030

31-
func notifyForFailedSend(_ db: Database, in thread: SessionThread, applicationState: UIApplication.State)
31+
func notifyForFailedSend(
32+
threadId: String,
33+
threadVariant: SessionThread.Variant,
34+
applicationState: UIApplication.State
35+
)
3236
func scheduleSessionNetworkPageLocalNotifcation(force: Bool)
3337
func addNotificationRequest(
3438
content: NotificationContent,
@@ -370,7 +374,11 @@ public struct NoopNotificationsManager: NotificationsManagerType {
370374
return false
371375
}
372376

373-
public func notifyForFailedSend(_ db: Database, in thread: SessionThread, applicationState: UIApplication.State) {}
377+
public func notifyForFailedSend(
378+
threadId: String,
379+
threadVariant: SessionThread.Variant,
380+
applicationState: UIApplication.State
381+
) {}
374382
public func scheduleSessionNetworkPageLocalNotifcation(force: Bool) {}
375383

376384
public func addNotificationRequest(

SessionMessagingKitTests/Jobs/DisplayPictureDownloadJobSpec.swift

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -118,15 +118,6 @@ class DisplayPictureDownloadJobSpec: QuickSpec {
118118
.thenReturn(Array(Array(Data(hex: TestConstants.edSecretKey)).prefix(upTo: 32)))
119119
}
120120
)
121-
@TestState(cache: .general, in: dependencies) var mockGeneralCache: MockGeneralCache! = MockGeneralCache(
122-
initialSetup: { cache in
123-
cache.when { $0.sessionId }.thenReturn(SessionId(.standard, hex: TestConstants.publicKey))
124-
cache.when { $0.ed25519SecretKey }.thenReturn(Array(Data(hex: TestConstants.edSecretKey)))
125-
cache
126-
.when { $0.ed25519Seed }
127-
.thenReturn(Array(Array(Data(hex: TestConstants.edSecretKey)).prefix(upTo: 32)))
128-
}
129-
)
130121

131122
// MARK: - a DisplayPictureDownloadJob
132123
describe("a DisplayPictureDownloadJob") {

SessionMessagingKitTests/_TestUtilities/MockNotificationsManager.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,11 @@ public class MockNotificationsManager: Mock<NotificationsManagerType>, Notificat
3737
}
3838

3939
public func notifyForFailedSend(
40-
_ db: Database,
4140
threadId: String,
4241
threadVariant: SessionThread.Variant,
4342
applicationState: UIApplication.State
4443
) {
45-
mockNoReturn(args: [threadId, threadVariant, applicationState], untrackedArgs: [db])
44+
mockNoReturn(args: [threadId, threadVariant, applicationState])
4645
}
4746

4847
public func scheduleSessionNetworkPageLocalNotifcation(force: Bool) {

SessionNotificationServiceExtension/NSENotificationPresenter.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public class NSENotificationPresenter: NotificationsManagerType {
4343

4444
// MARK: - Presentation
4545

46-
public func notifyForFailedSend(_ db: Database, threadId: String, threadVariant: SessionThread.Variant, applicationState: UIApplication.State) {
46+
public func notifyForFailedSend(threadId: String, threadVariant: SessionThread.Variant, applicationState: UIApplication.State) {
4747
// Not possible in the NotificationServiceExtension
4848
}
4949

SessionSnodeKit/SessionNetworkAPI/SessionNetworkAPI+Network.swift

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,12 @@ extension SessionNetworkAPI {
4141
.eraseToAnyPublisher()
4242
}
4343

44-
return dependencies[singleton: .storage]
45-
.readPublisher { db -> Network.PreparedRequest<Info> in
46-
try SessionNetworkAPI
47-
.prepareInfo(
48-
db,
49-
using: dependencies
50-
)
44+
return Result {
45+
try SessionNetworkAPI
46+
.prepareInfo(using: dependencies)
5147
}
52-
.flatMap { $0.send(using: dependencies) }
48+
.publisher
49+
.flatMap { [dependencies] in $0.send(using: dependencies) }
5350
.map { _, info in info }
5451
.flatMapStorageWritePublisher(using: dependencies) { [dependencies] db, info -> Bool in
5552
// Token info

SessionSnodeKit/SessionNetworkAPI/SessionNetworkAPI.swift

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import Foundation
66
import Combine
7-
import GRDB
87
import SessionUtilitiesKit
98

109
public enum SessionNetworkAPI {
@@ -18,7 +17,6 @@ public enum SessionNetworkAPI {
1817
/// `GET/info`
1918

2019
public static func prepareInfo(
21-
_ db: Database,
2220
using dependencies: Dependencies
2321
) throws -> Network.PreparedRequest<Info> {
2422
return try Network.PreparedRequest(
@@ -35,13 +33,12 @@ public enum SessionNetworkAPI {
3533
requestAndPathBuildTimeout: Network.defaultTimeout,
3634
using: dependencies
3735
)
38-
.signed(db, with: SessionNetworkAPI.signRequest, using: dependencies)
36+
.signed(with: SessionNetworkAPI.signRequest, using: dependencies)
3937
}
4038

4139
// MARK: - Authentication
4240

4341
fileprivate static func signatureHeaders(
44-
_ db: Database,
4542
url: URL,
4643
method: HTTPMethod,
4744
body: Data?,
@@ -52,7 +49,6 @@ public enum SessionNetworkAPI {
5249
.appending(url.query.map { value in "?\(value)" })
5350

5451
let signResult: (publicKey: String, signature: [UInt8]) = try sign(
55-
db,
5652
timestamp: timestamp,
5753
method: method.rawValue,
5854
path: path,
@@ -68,7 +64,6 @@ public enum SessionNetworkAPI {
6864
}
6965

7066
private static func sign(
71-
_ db: Database,
7267
timestamp: UInt64,
7368
method: String,
7469
path: String,
@@ -81,20 +76,22 @@ public enum SessionNetworkAPI {
8176
}()
8277

8378
guard
84-
let userEdKeyPair: KeyPair = Identity.fetchUserEd25519KeyPair(db),
79+
!dependencies[cache: .general].ed25519SecretKey.isEmpty,
8580
let blinded07KeyPair: KeyPair = dependencies[singleton: .crypto].generate(
86-
.versionBlinded07KeyPair(ed25519SecretKey: userEdKeyPair.secretKey)
81+
.versionBlinded07KeyPair(
82+
ed25519SecretKey: dependencies[cache: .general].ed25519SecretKey
83+
)
8784
),
8885
let signatureResult: [UInt8] = dependencies[singleton: .crypto].generate(
8986
.signatureVersionBlind07(
9087
timestamp: timestamp,
9188
method: method,
9289
path: path,
9390
body: bodyString,
94-
ed25519SecretKey: userEdKeyPair.secretKey
91+
ed25519SecretKey: dependencies[cache: .general].ed25519SecretKey
9592
)
9693
)
97-
else { throw NetworkError.signingFailed }
94+
else { throw CryptoError.signatureGenerationFailed }
9895

9996
return (
10097
publicKey: SessionId(.versionBlinded07, publicKey: blinded07KeyPair.publicKey).hexString,
@@ -103,22 +100,17 @@ public enum SessionNetworkAPI {
103100
}
104101

105102
private static func signRequest<R>(
106-
_ db: Database,
107103
preparedRequest: Network.PreparedRequest<R>,
108104
using dependencies: Dependencies
109105
) throws -> Network.Destination {
110-
guard let url: URL = preparedRequest.destination.url else {
111-
throw NetworkError.signingFailed
112-
}
113-
114-
guard case let .server(info) = preparedRequest.destination else {
115-
throw NetworkError.signingFailed
116-
}
106+
guard
107+
let url: URL = preparedRequest.destination.url,
108+
case let .server(info) = preparedRequest.destination
109+
else { throw NetworkError.invalidPreparedRequest }
117110

118111
return .server(
119112
info: info.updated(
120113
with: try signatureHeaders(
121-
db,
122114
url: url,
123115
method: preparedRequest.method,
124116
body: preparedRequest.body,

SessionSnodeKit/Types/NetworkError.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ public enum NetworkError: Error, Equatable, CustomStringConvertible {
88
case invalidState
99
case invalidURL
1010
case invalidPreparedRequest
11-
case signingFailed
1211
case forbidden
1312
case notFound
1413
case parsingFailed
@@ -30,7 +29,6 @@ public enum NetworkError: Error, Equatable, CustomStringConvertible {
3029
case .invalidState: return "The network is in an invalid state (NetworkError.invalidState)."
3130
case .invalidURL: return "Invalid URL (NetworkError.invalidURL)."
3231
case .invalidPreparedRequest: return "Invalid PreparedRequest provided (NetworkError.invalidPreparedRequest)."
33-
case .signingFailed: return "Couldn't sign request (NetworkError.signingFailed)."
3432
case .forbidden: return "Forbidden (NetworkError.forbidden)."
3533
case .notFound: return "Not Found (NetworkError.notFound)."
3634
case .parsingFailed: return "Invalid response (NetworkError.parsingFailed)."

0 commit comments

Comments
 (0)