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

chore: add logs for ExpiringActivity - WPB-9221 #1635

Merged
merged 13 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from 11 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
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import Foundation

// sourcery: AutoMockable
@objc
public protocol LastEventIDRepositoryInterface {

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 10 additions & 7 deletions wire-ios-notification-engine/Sources/NotificationSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,8 @@ public final class NotificationSession {
cryptoboxMigrationManager: cryptoboxMigrationManager,
earService: earService,
proteusService: ProteusService(coreCryptoProvider: coreCryptoProvider),
mlsDecryptionService: MLSDecryptionService(context: coreDataStack.syncContext, mlsActionExecutor: mlsActionExecutor)
mlsDecryptionService: MLSDecryptionService(context: coreDataStack.syncContext, mlsActionExecutor: mlsActionExecutor),
lastEventIDRepository: lastEventIDRepository
)
}

Expand All @@ -283,7 +284,8 @@ public final class NotificationSession {
cryptoboxMigrationManager: CryptoboxMigrationManagerInterface,
earService: EARServiceInterface,
proteusService: ProteusServiceInterface,
mlsDecryptionService: MLSDecryptionServiceInterface
mlsDecryptionService: MLSDecryptionServiceInterface,
lastEventIDRepository: LastEventIDRepositoryInterface

) throws {
self.coreDataStack = coreDataStack
Expand All @@ -296,7 +298,8 @@ public final class NotificationSession {

eventDecoder = EventDecoder(
eventMOC: coreDataStack.eventContext,
syncMOC: coreDataStack.syncContext
syncMOC: coreDataStack.syncContext,
lastEventIDRepository: lastEventIDRepository
)

pushNotificationStrategy.delegate = self
Expand Down Expand Up @@ -395,13 +398,13 @@ extension NotificationSession: PushNotificationStrategyDelegate {
func pushNotificationStrategy(
_ strategy: PushNotificationStrategy,
didFetchEvents events: [ZMUpdateEvent]
) async {
let decodedEvents = await eventDecoder.decryptAndStoreEvents(
) async throws {
let decodedEvents = try await self.eventDecoder.decryptAndStoreEvents(
events,
publicKeys: try? earService.fetchPublicKeys()
publicKeys: try? self.earService.fetchPublicKeys()
)

await context.perform { [self] in
await self.context.perform { [self] in
processDecodedEvents(decodedEvents)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,18 @@ import WireRequestStrategy

protocol PushNotificationStrategyDelegate: AnyObject {

func pushNotificationStrategy(_ strategy: PushNotificationStrategy, didFetchEvents events: [ZMUpdateEvent]) async
func pushNotificationStrategy(_ strategy: PushNotificationStrategy, didFetchEvents events: [ZMUpdateEvent]) async throws
func pushNotificationStrategyDidFinishFetchingEvents(_ strategy: PushNotificationStrategy)

}

final class PushNotificationStrategy: AbstractRequestStrategy, ZMRequestGeneratorSource {
final class PushNotificationStrategy: AbstractRequestStrategy {

// MARK: - Properties

var sync: NotificationStreamSync!
private var pushNotificationStatus: PushNotificationStatus!
private var isProcessingNotifications = false

weak var delegate: PushNotificationStrategyDelegate?

Expand Down Expand Up @@ -66,8 +67,8 @@ final class PushNotificationStrategy: AbstractRequestStrategy, ZMRequestGenerato
}

public override func nextRequest(for apiVersion: APIVersion) -> ZMTransportRequest? {
guard isFetchingStreamForAPNS else { return nil }
let request = requestGenerators.nextRequest(for: apiVersion)
guard isFetchingStreamForAPNS && !isProcessingNotifications else { return nil }
let request = sync.nextRequest(for: apiVersion)

if request != nil {
pushNotificationStatus.didStartFetching()
Expand All @@ -76,10 +77,6 @@ final class PushNotificationStrategy: AbstractRequestStrategy, ZMRequestGenerato
return request
}

public var requestGenerators: [ZMRequestGenerator] {
return [sync]
}

public var isFetchingStreamForAPNS: Bool {
return self.pushNotificationStatus.hasEventsToFetch
}
Expand All @@ -93,6 +90,8 @@ extension PushNotificationStrategy: NotificationStreamSyncDelegate {
public func fetchedEvents(_ events: [ZMUpdateEvent], hasMoreToFetch: Bool) {
WireLogger.notifications.info("fetched \(events.count) events, \(hasMoreToFetch ? "" : "no ")more to fetch")

isProcessingNotifications = true

let eventIds = events.compactMap(\.uuid)
let latestEventId = events.last(where: { !$0.isTransient })?.uuid

Expand All @@ -101,14 +100,26 @@ extension PushNotificationStrategy: NotificationStreamSyncDelegate {
}

Task {
await delegate?.pushNotificationStrategy(self, didFetchEvents: events)
await managedObjectContext.perform {
self.pushNotificationStatus.didFetch(eventIds: eventIds, lastEventId: latestEventId, finished: !hasMoreToFetch)
}

if !hasMoreToFetch {
do {
try await delegate?.pushNotificationStrategy(self, didFetchEvents: events)
await managedObjectContext.perform {
self.isProcessingNotifications = false
self.pushNotificationStatus.didFetch(eventIds: eventIds, lastEventId: latestEventId, finished: !hasMoreToFetch)
RequestAvailableNotification.notifyNewRequestsAvailable(nil)
}

if !hasMoreToFetch {
delegate?.pushNotificationStrategyDidFinishFetchingEvents(self)
}
} catch {
WireLogger.notifications.warn("Failed to process fetched events: \(error)")
await managedObjectContext.perform {
self.isProcessingNotifications = false
}
sync.reset()
delegate?.pushNotificationStrategyDidFinishFetchingEvents(self)
}

}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,8 @@ class BaseTest: ZMTBaseTest {
cryptoboxMigrationManager: mockCryptoboxMigrationManager,
earService: mockEARService,
proteusService: mockProteusService,
mlsDecryptionService: mockMLSDecryptionService
mlsDecryptionService: mockMLSDecryptionService,
lastEventIDRepository: lastEventIDRepository
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ import WireDataModel
import WireProtos
import XCTest

@testable import WireDataModelSupport
@testable import WireRequestStrategy

final class EventDecoderDecryptionTests: MessagingTestBase {

func testThatItCanDecryptOTRMessageAddEvent() async throws {
// GIVEN
let sut = EventDecoder(eventMOC: self.eventMOC, syncMOC: self.syncMOC)
let lastEventIDRepository = MockLastEventIDRepositoryInterface()
let sut = EventDecoder(eventMOC: self.eventMOC, syncMOC: self.syncMOC, lastEventIDRepository: lastEventIDRepository)
let text = "Trentatre trentini andarono a Trento tutti e trentatre trotterellando"
let generic = GenericMessage(content: Text(content: text))

Expand All @@ -53,7 +55,8 @@ final class EventDecoderDecryptionTests: MessagingTestBase {

func testThatItCanDecryptOTRAssetAddEvent() async throws {
// GIVEN
let sut = EventDecoder(eventMOC: self.eventMOC, syncMOC: self.syncMOC)
let lastEventIDRepository = MockLastEventIDRepositoryInterface()
let sut = EventDecoder(eventMOC: self.eventMOC, syncMOC: self.syncMOC, lastEventIDRepository: lastEventIDRepository)
let image = self.verySmallJPEGData()
let imageSize = ZMImagePreprocessor.sizeOfPrerotatedImage(with: image)
let properties = ZMIImageProperties(size: imageSize, length: UInt(image.count), mimeType: "image/jpg")
Expand All @@ -78,7 +81,8 @@ final class EventDecoderDecryptionTests: MessagingTestBase {

func testThatItInsertsAUnableToDecryptMessageIfItCanNotEstablishASession() async throws {
// GIVEN
let sut = EventDecoder(eventMOC: self.eventMOC, syncMOC: self.syncMOC)
let lastEventIDRepository = MockLastEventIDRepositoryInterface()
let sut = EventDecoder(eventMOC: self.eventMOC, syncMOC: self.syncMOC, lastEventIDRepository: lastEventIDRepository)
var event: ZMUpdateEvent!

await self.syncMOC.perform {
Expand Down Expand Up @@ -125,7 +129,8 @@ final class EventDecoderDecryptionTests: MessagingTestBase {

func testThatItInsertsAnUnableToDecryptMessageIfTheEncryptedPayloadIsLongerThan_18_000() async throws {
// Given
let sut = EventDecoder(eventMOC: self.eventMOC, syncMOC: self.syncMOC)
let lastEventIDRepository = MockLastEventIDRepositoryInterface()
let sut = EventDecoder(eventMOC: self.eventMOC, syncMOC: self.syncMOC, lastEventIDRepository: lastEventIDRepository)
let crlf = "\u{0000}\u{0001}\u{0000}\u{000D}\u{0000A}"
let text = "https://wir\("".padding(toLength: crlf.count * 20_000, withPad: crlf, startingAt: 0))e.com/"
XCTAssertGreaterThan(text.count, 18_000)
Expand Down Expand Up @@ -174,7 +179,8 @@ final class EventDecoderDecryptionTests: MessagingTestBase {

func testThatItInsertsAnUnableToDecryptMessageIfTheEncryptedPayloadIsLongerThan_18_000_External_Message() async throws {
// Given
let sut = EventDecoder(eventMOC: self.eventMOC, syncMOC: self.syncMOC)
let lastEventIDRepository = MockLastEventIDRepositoryInterface()
let sut = EventDecoder(eventMOC: self.eventMOC, syncMOC: self.syncMOC, lastEventIDRepository: lastEventIDRepository)
let crlf = "\u{0000}\u{0001}\u{0000}\u{000D}\u{0000A}"
let text = "https://wir\("".padding(toLength: crlf.count * 20_000, withPad: crlf, startingAt: 0))e.com/"
XCTAssertGreaterThan(text.count, 18_000)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ - (ZMTransportRequest *)requestForSingleRequestSync:(ZMSingleRequestSync * __unu
UserClient *selfClient = [ZMUser selfUserInContext:self.moc].selfClient;
if (selfClient.remoteIdentifier != nil) {
[queryItems addObject:[NSURLQueryItem queryItemWithName:@"client" value:selfClient.remoteIdentifier]];
} else {
return nil;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ public class NotificationStreamSync: NSObject, ZMRequestGenerator, ZMSimpleListR
notificationStreamSyncDelegate = delegate
}

public func reset() {
listPaginator.resetFetching()
}

public func nextRequest(for apiVersion: APIVersion) -> ZMTransportRequest? {

// We only reset the paginator if it is neither in progress nor has more pages to fetch.
Expand All @@ -71,13 +75,7 @@ public class NotificationStreamSync: NSObject, ZMRequestGenerator, ZMSimpleListR
}

private var lastUpdateEventID: UUID? {
get {
lastEventIDRepository.fetchLastEventID()
}

set {
lastEventIDRepository.storeLastEventID(newValue)
}
lastEventIDRepository.fetchLastEventID()
}

@objc(nextUUIDFromResponse:forListPaginator:)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,6 @@ open class PushNotificationStatus: NSObject {

WireLogger.updateEvent.info("finished fetching all available events, last event id: " + String(describing: lastEventId?.uuidString), attributes: .safePublic)

if let lastEventId {
lastEventIDRepository.storeLastEventID(lastEventId)
}

guard finished else { return }

// We take all events that are older than or equal to lastEventId and add highest ranking event ID
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import XCTest

@testable import WireDataModelSupport
@testable import WireRequestStrategy
@testable import WireRequestStrategySupport

Expand Down Expand Up @@ -209,7 +210,8 @@ extension ClientMessageRequestStrategyTests {

func testThatANewOtrMessageIsCreatedFromADecryptedAPNSEvent() async throws {
// GIVEN
let eventDecoder = EventDecoder(eventMOC: self.eventMOC, syncMOC: self.syncMOC)
let lastEventIDRepository = MockLastEventIDRepositoryInterface()
let eventDecoder = EventDecoder(eventMOC: self.eventMOC, syncMOC: self.syncMOC, lastEventIDRepository: lastEventIDRepository)
let text = "Everything"
let event = try await self.decryptedUpdateEventFromOtherClient(text: text, eventDecoder: eventDecoder)

Expand Down
Loading
Loading