Notification Service Extension: move its logic into a tested module
I maintainer di solito rispondono entro 1 giorno
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Idoneità per principianti
- 35/100
Direzione di ricerca
Start with the extension sources in WordPress/WordPressNotificationServiceExtension/Sources/ and the NotificationServiceExtensionCore target in Modules/Package.swift; inspect the existing notification tests and fixtures under Tests/KeystoneTests/Resources/Mocks/Notifications/. Then add the core builder and its tests, update the test plan, and compare extension binary size. Done means the listed cases pass and device checks confirm comment replies and 2FA actions still work.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
The notification service extension changes every WordPress.com push before iOS shows it, and none of its logic has tests. This plan moves the logic into the existing NotificationServiceExtensionCore package module so it can be unit-tested, and removes code left over from the content extension that was dropped in #24177.
Why
- No coverage. Nothing in
Tests/orModules/Tests/referencesNotificationService,RichNotificationContentFormatter,RemoteNotificationActionParser,RemoteNotificationStylesor the payload helpers. The code only exists in the extension targets, and no test target links them. Nearby code is tested (NotificationSyncServiceRemoteTestscoversloadNotes, and theFormattable*ContentTestsin KeystoneTests cover how the main app parses notes), but the extension's own flow isn't. - It can't be tested by hand on a simulator either. On an iOS 27 simulator,
xcrun simctl pushwithmutable-content: 1doesn't launch the extension: the banner shows the raw payload, and no extension process starts. Only a real push on a real device exercises it. That's why #6644 is stuck waiting for a device check. - It's data in, data out. The input is the push payload, plus the token saved in the keychain at sign-in, plus the note from
loadNotesand an optional image. The output is oneUNMutableNotificationContent. Once the network calls are injected, everything else is deterministic.
What the extension does today
WordPress/WordPressNotificationServiceExtension/Sources/ (about 935 lines; the Jetpack target reuses these sources with its own Info.plist):
- It reads the token, username and user ID from the keychain (
NotificationServiceExtensionConfiguration). If the token,aps.alert,typeor a knownNotificationKindis missing, it shows the push unchanged. - View milestone: it sets the title to "You hit a milestone 🚀".
- It only enriches comment, comment-like, like, matcher (
automattcher) and login (push_auth) pushes (NotificationKind.isSupportedByRichNotifications). Everything else is shown unchanged. - It sets
categoryIdentifierfromNotificationKind.contentExtensionCategoryIdentifier. - Login: it makes the title and body bold, with no network call.
- Everything else: it calls
loadNotes(noteIds: [note_id]).- The title becomes the note's subject.
- For kinds with body text, the body becomes the comment text, with whitespace condensed and cut to 256 characters.
- It downloads
mediaURL, or the sender's avatar for kinds that support an icon. The token is sent only to WordPress.com hosts (#25863). The image is attached as aUNNotificationAttachment. serviceExtensionTimeWillExpirecancels the request and delivers the partly updated content.- It sends Tracks events: launched, malformed, discarded, retrieval failed, assembled and timed out.
Plan
1. Move the formatting code into NotificationServiceExtensionCore
- Move
RichNotificationContentFormatter.swift,RemoteNotificationActionParser.swift,RemoteNotificationStyles.swiftandUNNotificationContent+RemoteNotification.swiftintoModules/Sources/NotificationServiceExtensionCore/. Make public only what the extension calls. - In
Modules/Package.swift, addFormattableContentKit,WordPressKitandWordPressSharedto that target's dependencies. The extension already uses all three. - Check the extension's binary size before and after. It should stay about the same. If it grows a lot, a dependency is being linked twice: the stats widget once grew by ~25 MB because of one extra dependency.
RemoteNotificationStylesforce-unwrapsUIFont(name: "Noticons", …)!. The font is registered through the extension'sInfo.plist(UIAppFonts), so a test bundle won't have it and would crash. Make the lookup fall back to the system font, or register the font in the tests.
2. Pull the flow out into a builder with injected I/O
In the same module, add something like:
public struct RichNotificationBuilder {
public var loadNote: (_ noteID: Int) async throws -> RemoteNotification?
public var loadImage: (_ url: URL) async -> (data: Data, fileExtension: String)?
public var makeAttachmentURL: (_ fileName: String) -> URL? // where the downloaded image is written
/// Updates `content` in place, as the extension does today, so the timeout
/// handler can still deliver the partly updated content. Returns what happened, so the extension can send Tracks events.
public func enrich(_ content: UNMutableNotificationContent, hasToken: Bool) async -> RichNotificationOutcome
}
public enum RichNotificationOutcome: Equatable {
case malformed, viewMilestone, discarded(type: String), login,
retrievalFailed(noteID: String, error: String), assembled
}
NotificationService keeps only what depends on the extension itself:
- reading the keychain;
- creating
WordPressComRestApiandNotificationSyncServiceRemote, which backloadNote; - downloading the image, which backs
loadImage; - Tracks events, sent from the returned outcome;
serviceExtensionTimeWillExpire.
3. Remove the leftover content-extension code
RichNotificationViewModeland theuserInfo[richNotificationViewModel]data are only written by the extension. Nothing reads them since #24177 removed the content extension. Delete both.- In
contentExtensionCategoryIdentifier, keep.login→push_auth. That category is registered byInteractiveNotificationsManager.NoteCategoryDefinition.loginand is what attaches the Approve/Deny actions to 2FA pushes. like,comment_likeandautomattcherdon't match any registered category. Either drop them, so the server's category is kept, or leave them alone and mention why in the PR. Check what category the server sends for those pushes before deciding.
4. Tests (Swift Testing)
Add a NotificationServiceExtensionCoreTests test target in Modules/Package.swift and add it to Tests/KeystoneTests/WordPressUnitTests.xctestplan, the same way JetpackSocialTests is set up.
Use the existing note fixtures in Tests/KeystoneTests/Resources/Mocks/Notifications/ (for example notifications-comment-content.json and notifications-like.json), copied into the new target's resources, plus small hand-written push payloads:
- A comment push with a note: the title is the note's subject, and the body is the comment text, not
aps.alert. This answers the main question in #6644. - A body over 256 characters is cut, and repeated blank lines are condensed.
- Like and comment-like pushes get a title but keep their body.
- Unsupported kinds (
new_post,follow) are unchanged and return.discarded. - A view milestone sets only the title.
- A login push gets bold content,
categoryIdentifier == "push_auth", andloadNoteis never called. - Missing token, missing
note_idor unknowntypeleave the push unchanged and return.malformed. loadNotethrows: the push is unchanged except for the title/body swap that happens before the fetch, and the result is.retrievalFailed.- An image is attached when
mediaURLis present, the avatar is used when it isn't, and nothing is attached when the download fails.
5. Check on a device
Unit tests can't show that iOS still runs the extension, so after the refactor:
- Get a real comment reply on a phone and confirm the notification shows the reply's text and avatar.
- Get a 2FA login push and confirm Approve/Deny still appear.
Out of scope
- Behaviour changes, beyond removing the dead data. The refactor should deliver exactly the same notifications.
- Enriching more notification kinds.
- Lingua principale
- Swift
- Stelle
- 3.9k
- Fork
- 1.2k
- Merge medio
- 1g 23h
- PR unite (30g)
- 136
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di wordpress-mobile/WordPress-iOS
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
wordpress-mobile/WordPress-iOS#26423 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
[Pri] Low [Status] Ready for Dev [Type] Bug /Me
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
wordpress-mobile/WordPress-iOS#26279 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
[Pri] Low [Status] Ready for Dev [Type] Bug Share Extension
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100
wordpress-mobile/WordPress-iOS#26276 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
[Pri] Low [Status] Ready for Dev [Type] Bug Blogging Prompts Notifications
Difficoltà 2/5 1-3 ore Idoneità per principianti 79/100
wordpress-mobile/WordPress-iOS#26256 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
[Status] Ready for Dev [Type] Bug Reader
Difficoltà 2/5 1-3 ore Idoneità per principianti 83/100
wordpress-mobile/WordPress-iOS#26254 · 2 commenti ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di wordpress-mobile/WordPress-iOS
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 62/100
-
🎉 Add RemoteCrabAperta
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 70/100
jaywcjlove/awesome-mac#4999 ·
I maintainer di solito rispondono entro 1 giorno
-
[Type] Bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 74/100
wordpress-mobile/GutenbergKit#940 ·
I maintainer di solito rispondono entro 1 giorno
-
pending-maintainer-response pending-triage
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
aws-amplify/amplify-swift#4423 ·
I maintainer di solito rispondono entro 2 giorni
-
tests
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
erkamyaman/unslop-anything#80 ·
I maintainer di solito rispondono entro 1 giorno