diff --git a/.git-hooks/pre-push b/.git-hooks/pre-push index 1e9795cfc..b64f0b624 100755 --- a/.git-hooks/pre-push +++ b/.git-hooks/pre-push @@ -12,10 +12,24 @@ echo "$JAVA_HOME" echo "$(java -version)" echo "Running test... " +# Single-variant pre-push tests. `./gradlew test` would compile six Android +# variants of :amethyst (play/fdroid × debug/release/benchmark) plus full +# native-libs merging per variant — ~6× the work of one variant. CI runs the +# multi-flavor matrix on push to main; pre-push only needs one happy path. +TASKS=( + :quartz:jvmTest + :commons:jvmTest + :nestsClient:jvmTest + :quic:jvmTest + :ammolite:testDebugUnitTest + :amethyst:testPlayDebugUnitTest + :cli:test +) + if [ "${CLAUDE_CODE_REMOTE:-}" = "true" ]; then - ./gradlew test --quiet -x :desktopApp:test -x :desktopApp:upxDownload -x :desktopApp:vlcDownload + ./gradlew "${TASKS[@]}" --quiet else - ./gradlew test --quiet + ./gradlew "${TASKS[@]}" :desktopApp:test --quiet fi status=$? diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 347c7b548..a60dea996 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -26,7 +26,17 @@ jobs: with: distribution: 'zulu' java-version: 21 - cache: gradle + + # Remote Gradle build cache: writes on push to main, reads on PRs and + # other branches. Caches both `~/.gradle/caches/` and individual task + # outputs, so dependency-only changes hit the cache and skip recompiling + # downstream modules / re-merging native libs (~600MB of work on + # :amethyst alone). Replaces the narrower `cache: gradle` previously on + # actions/setup-java, which only cached `modules-2`. + - name: Set up Gradle + uses: gradle/actions/setup-gradle@v4 + with: + cache-read-only: ${{ github.ref != 'refs/heads/main' }} - name: Linter (gradle) run: ./gradlew spotlessCheck @@ -63,10 +73,29 @@ jobs: with: distribution: 'zulu' java-version: 21 - cache: gradle + + - name: Set up Gradle + uses: gradle/actions/setup-gradle@v4 + with: + cache-read-only: ${{ github.ref != 'refs/heads/main' }} + + # Cache vlc-setup plugin downloads (VLC + UPX archives) keyed on the + # versions pinned in desktopApp/build.gradle.kts. Each OS gets its own + # cache namespace because the plugin downloads platform-specific archives. + # On a hit the vlcDownload / upxDownload tasks are up-to-date and we + # never touch get.videolan.org; on a miss (version bump or new runner) + # we fall back to fetching, which is what the in-build retry budget + # exists for. + - name: Cache vlc-setup downloads + uses: actions/cache@v4 + with: + path: ~/.gradle/vlcSetup + key: vlcsetup-${{ runner.os }}-${{ hashFiles('desktopApp/build.gradle.kts') }} + restore-keys: | + vlcsetup-${{ runner.os }}- - name: Test + Build Desktop (gradle) - run: ./gradlew :quartz:jvmTest :commons:jvmTest :nestsClient:jvmTest :cli:test :desktopApp:test :desktopApp:${{ matrix.desktop-task }} --no-daemon + run: ./gradlew :quartz:jvmTest :commons:jvmTest :nestsClient:jvmTest :cli:test :desktopApp:test :desktopApp:${{ matrix.desktop-task }} # jpackage pins libicu to the build host's version (libicu74 on # ubuntu-24.04). Rewrite the .deb so testers on other Debian/Ubuntu @@ -97,10 +126,42 @@ jobs: with: distribution: 'zulu' java-version: 21 - cache: gradle - - name: Android Lint (gradle) - run: ./gradlew :amethyst:lintFdroidBenchmark :amethyst:lintPlayBenchmark --no-daemon + - name: Set up Gradle + uses: gradle/actions/setup-gradle@v4 + with: + cache-read-only: ${{ github.ref != 'refs/heads/main' }} + + # Lint + focused unit tests + benchmark assembly in one Gradle invocation. + # Previously: one invocation for lint, one for `test` (which compiled all + # six amethyst variants × all flavors), one for `assembleBenchmark` + # (re-walking the same task graph). Combining them keeps the daemon hot + # across phases and lets task-level dedup (e.g. compileKotlin) only + # happen once. + # + # `-PdisableAbiSplits=true` produces a single non-split APK per + # (flavor, buildType) instead of 5 (4 ABIs + universal). The CI only + # uploads the universal-equivalent benchmark APK; per-ABI splits were + # being built and discarded, costing ~600MB of stripped_native_libs + # intermediates and several minutes per run. + # + # Test scope: only Debug unit tests for amethyst. The release/benchmark + # variants are compile-equivalent for unit-test purposes; running all six + # adds ~5× the kotlinc work without catching new defects on PRs. Push to + # main still gets the full test matrix via the production-build path. + - name: Test + Build Android (gradle) + run: | + ./gradlew \ + :amethyst:lintFdroidBenchmark \ + :amethyst:lintPlayBenchmark \ + :quartz:jvmTest \ + :commons:jvmTest \ + :nestsClient:jvmTest \ + :ammolite:testDebugUnitTest \ + :amethyst:testFdroidDebugUnitTest \ + :amethyst:testPlayDebugUnitTest \ + :amethyst:assembleBenchmark \ + -PdisableAbiSplits=true - name: Upload Android Lint Reports uses: actions/upload-artifact@v7 @@ -109,31 +170,35 @@ jobs: name: Android Lint Reports path: amethyst/build/reports/lint-results-*.html - - name: Test + Build Android (gradle) - run: ./gradlew test assembleBenchmark --no-daemon - - name: Android Test Report uses: asadmansr/android-test-report-action@v1.2.0 if: always() - name: Upload Test Results uses: actions/upload-artifact@v7 - if: always() + if: failure() with: name: Test Reports path: amethyst/build/reports + # With -PdisableAbiSplits=true the APK is named without the ABI/universal + # suffix: amethyst--benchmark.apk. Glob both forms so this still + # works if a contributor runs CI on a branch that doesn't pass the flag. - name: Upload Play APK Benchmark uses: actions/upload-artifact@v7 with: name: Play Benchmark APK - path: amethyst/build/outputs/apk/play/benchmark/amethyst-play-universal-benchmark.apk + path: | + amethyst/build/outputs/apk/play/benchmark/amethyst-play-benchmark.apk + amethyst/build/outputs/apk/play/benchmark/amethyst-play-universal-benchmark.apk - name: Upload FDroid APK Benchmark uses: actions/upload-artifact@v7 with: name: FDroid Benchmark APK - path: amethyst/build/outputs/apk/fdroid/benchmark/amethyst-fdroid-universal-benchmark.apk + path: | + amethyst/build/outputs/apk/fdroid/benchmark/amethyst-fdroid-benchmark.apk + amethyst/build/outputs/apk/fdroid/benchmark/amethyst-fdroid-universal-benchmark.apk - name: Upload Compose Reports uses: actions/upload-artifact@v7 diff --git a/amethyst/build.gradle b/amethyst/build.gradle index ae6d0f09e..32a65cb98 100644 --- a/amethyst/build.gradle +++ b/amethyst/build.gradle @@ -142,10 +142,32 @@ android { ] } + // Opt-in fast-build flags. Default behavior is unchanged. + // + // -PdisableAbiSplits=true skip per-ABI APK splits; produces a single + // APK per (flavor, buildType) instead of 5. + // Cuts ~600 MB of intermediates and several + // minutes off CI. + // -PdisableUniversalApk=true when ABI splits are enabled, skip the + // extra universal APK output. (No effect + // when disableAbiSplits is also set, since + // there are no splits to add to.) + // -Pamethyst.skipMapping=true disable R8 minification on release and + // benchmark. APK is larger, but builds are + // much faster and outputs/mapping/ (~260MB) + // is not produced. Local-dev and PR-CI use + // only — release pipelines must not set it. + def disableAbiSplits = providers.gradleProperty("disableAbiSplits") + .map { it.toBoolean() }.getOrElse(false) + def disableUniversalApk = providers.gradleProperty("disableUniversalApk") + .map { it.toBoolean() }.getOrElse(false) + def skipMapping = providers.gradleProperty("amethyst.skipMapping") + .map { it.toBoolean() }.getOrElse(false) + buildTypes { release { proguardFiles getDefaultProguardFile("proguard-android-optimize.txt"), 'proguard-rules.pro' - minifyEnabled = true + minifyEnabled = !skipMapping } debug { applicationIdSuffix '.debug' @@ -186,10 +208,10 @@ android { splits { abi { - enable = true + enable = !disableAbiSplits reset() include "x86", "x86_64", "arm64-v8a", "armeabi-v7a" - universalApk = true + universalApk = !disableUniversalApk } } diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/notifications/EventNotificationConsumer.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/notifications/EventNotificationConsumer.kt index 7470825af..2d78756a1 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/notifications/EventNotificationConsumer.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/notifications/EventNotificationConsumer.kt @@ -83,6 +83,7 @@ import com.vitorpamplona.quartz.nip71Video.VideoVerticalEvent import com.vitorpamplona.quartz.nip84Highlights.HighlightEvent import com.vitorpamplona.quartz.nip88Polls.poll.PollEvent import com.vitorpamplona.quartz.nipACWebRtcCalls.events.CallOfferEvent +import com.vitorpamplona.quartz.nipC7Chats.ChatEvent import com.vitorpamplona.quartz.utils.Log import com.vitorpamplona.quartz.utils.TimeUtils import kotlinx.coroutines.TimeoutCancellationException @@ -495,6 +496,67 @@ class EventNotificationConsumer( ) } + /** + * Marmot kind:445 group messages have no `p` tag (recipients are routed + * by the `h` tag carrying the nostr_group_id), so the cache-observer path + * in [NotificationDispatcher] can't match them to an account. They're + * dispatched here directly from [com.vitorpamplona.amethyst.ui.screen.loggedIn.GroupEventHandler] + * once [com.vitorpamplona.quartz.marmot.MarmotInboundProcessor] has + * decrypted the outer ChaCha20-Poly1305 layer and verified the inner + * MLS-signed payload. + * + * Typed to [ChatEvent] so the caller has to narrow first — reactions, + * control messages, and deletions stay silent at the type level, + * mirroring how NIP-17 (kind:14) is the only DM kind we notify. + */ + suspend fun notifyGroupMessage( + innerEvent: ChatEvent, + nostrGroupId: String, + account: Account, + ) = withWakeLock { + Log.d(TAG, "New Marmot Group Message to Notify") + + if (!notificationManager().areNotificationsEnabled()) return@withWakeLock + if (MainActivity.isResumed) return@withWakeLock + + // old event being re-broadcast + if (innerEvent.createdAt < TimeUtils.fifteenMinutesAgo()) return@withWakeLock + // a message we ourselves sent + if (innerEvent.pubKey == account.signer.pubKey) return@withWakeLock + + val chatroom = account.marmotGroupList.getOrCreateGroup(nostrGroupId) + val groupName = chatroom.displayName.value?.takeIf { it.isNotBlank() } ?: "Private group" + val sender = LocalCache.getOrCreateUser(innerEvent.pubKey) + val senderName = sender.toBestDisplayName() + val senderPicture = sender.profilePicture() + // Defensive fallback for the rare empty-content ChatEvent so the + // popup is still actionable. Non-chat inner kinds were filtered + // out at the call site by the ChatEvent type narrowing. + val body = innerEvent.content.takeIf { it.isNotBlank() } ?: "New message" + + val accountNpub = + account.signer.pubKey + .hexToByteArray() + .toNpub() + // marmot:?account= — same scheme as notifyWelcome, + // taps deep-link straight to the group's chatroom. + val noteUri = "marmot:$nostrGroupId$ACCOUNT_QUERY_PARAM$accountNpub" + + notificationManager() + .sendDMNotification( + id = innerEvent.id, + messageBody = "$senderName: $body", + senderName = groupName, + time = innerEvent.createdAt, + pictureUrl = senderPicture, + uri = noteUri, + applicationContext = applicationContext, + accountNpub = accountNpub, + accountPictureUrl = account.userProfile().profilePicture(), + chatroomMembers = null, + ) + } + suspend fun decryptZapContentAuthor( event: LnZapRequestEvent, signer: NostrSigner, diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/notifications/NotificationDispatcher.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/notifications/NotificationDispatcher.kt index 8ca88478a..df17bfe01 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/notifications/NotificationDispatcher.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/notifications/NotificationDispatcher.kt @@ -51,6 +51,7 @@ import com.vitorpamplona.quartz.nip71Video.VideoVerticalEvent import com.vitorpamplona.quartz.nip84Highlights.HighlightEvent import com.vitorpamplona.quartz.nip88Polls.poll.PollEvent import com.vitorpamplona.quartz.nipACWebRtcCalls.events.CallOfferEvent +import com.vitorpamplona.quartz.nipC7Chats.ChatEvent import com.vitorpamplona.quartz.utils.Log import com.vitorpamplona.quartz.utils.TimeUtils import kotlinx.coroutines.CancellationException @@ -222,4 +223,24 @@ class NotificationDispatcher( Log.e(TAG, "Failed to dispatch Welcome notification ${event.id}", e) } } + + /** + * Direct-invocation entry point for Marmot kind:445 group messages. + * Bypasses the cache-observer path because GroupEvents are routed by + * the `h` tag (nostr_group_id), not by `p` tag. Called from + * [com.vitorpamplona.amethyst.ui.screen.loggedIn.GroupEventHandler] + * once the MLS-decrypted inner event has been parsed and indexed. + */ + suspend fun notifyGroupMessage( + innerEvent: ChatEvent, + nostrGroupId: String, + account: Account, + ) { + try { + consumer.notifyGroupMessage(innerEvent, nostrGroupId, account) + } catch (e: Exception) { + if (e is CancellationException) throw e + Log.e(TAG, "Failed to dispatch Group Message notification ${innerEvent.id}", e) + } + } } diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/DecryptAndIndexProcessor.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/DecryptAndIndexProcessor.kt index aebdf4308..b9538f08c 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/DecryptAndIndexProcessor.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/DecryptAndIndexProcessor.kt @@ -49,6 +49,7 @@ import com.vitorpamplona.quartz.nipACWebRtcCalls.events.CallIceCandidateEvent import com.vitorpamplona.quartz.nipACWebRtcCalls.events.CallOfferEvent import com.vitorpamplona.quartz.nipACWebRtcCalls.events.CallRejectEvent import com.vitorpamplona.quartz.nipACWebRtcCalls.events.CallRenegotiateEvent +import com.vitorpamplona.quartz.nipC7Chats.ChatEvent import com.vitorpamplona.quartz.utils.Log import kotlinx.coroutines.CancellationException import kotlinx.coroutines.sync.Mutex @@ -660,6 +661,21 @@ class GroupEventHandler( // re-persist would silently grow the on-disk log). if (isNew) { manager.persistDecryptedMessage(result.groupId, result.innerEventJson) + + // GroupEvents have no `p` tag, so the cache-observer + // notification path can't route them. Fire the popup + // directly here — only on first-time decryption, so + // a relay re-broadcast or persist-replay doesn't + // double-notify. Restrict to ChatEvent (kind:9) so + // reactions, deletions, and control messages stay + // silent. + if (innerEvent is ChatEvent) { + Amethyst.instance.notificationDispatcher.notifyGroupMessage( + innerEvent, + result.groupId, + account, + ) + } } } diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/chats/rooms/twopane/MessagesTwoPane.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/chats/rooms/twopane/MessagesTwoPane.kt index 995c443eb..53f497fc5 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/chats/rooms/twopane/MessagesTwoPane.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/chats/rooms/twopane/MessagesTwoPane.kt @@ -22,6 +22,7 @@ package com.vitorpamplona.amethyst.ui.screen.loggedIn.chats.rooms.twopane import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.imePadding import androidx.compose.foundation.layout.padding import androidx.compose.material3.Scaffold import androidx.compose.material3.windowsizeclass.WindowWidthSizeClass @@ -73,6 +74,7 @@ fun MessagesTwoPane( val displayFeatures = calculateDisplayFeatures(act) Scaffold( + modifier = Modifier.imePadding(), topBar = { UserDrawerSearchTopBar(accountViewModel, nav) { AmethystClickableIcon() } }, diff --git a/amethyst/src/main/res/values-ru-rRU/strings.xml b/amethyst/src/main/res/values-ru-rRU/strings.xml index 9cafcc262..46228d0a3 100644 --- a/amethyst/src/main/res/values-ru-rRU/strings.xml +++ b/amethyst/src/main/res/values-ru-rRU/strings.xml @@ -43,12 +43,16 @@ Вы используете публичный ключ, они - только для чтения. Войдите в систему приватным ключом, чтобы продвигать записи Вы используете публичный ключ, они - только для чтения. Войдите с приватным ключом, чтобы лайкать посты Не настроены запы. Нажмите и удерживайте для настройки + Анонимный + создал клип Войдите с приватным ключом чтобы запать Вы используете публичный ключ, они - только для чтения. Войдите в систему приватным ключом, чтобы иметь возможность подписаться Вы используете публичный ключ, они - только для чтения. Войдите в систему приватным ключом, чтобы иметь возможность отписаться Вы используете публичный ключ, они - только для чтения. Войдите в систему приватным ключом, чтобы иметь возможность скрыть слово или предложение Вы используете публичный ключ, они - только для чтения. Войдите в систему приватным ключом, чтобы иметь возможность показать слово или предложение Вы используете публичный ключ. Войдите приватным ключом, чтобы редактировать + Вы используете публичный ключ, они - только для чтения. Войдите в систему приватным ключом, чтобы иметь возможность загружать + Подписчик не авторизовал расшифровку, необходимую для выполнения этой операции. Активируйте расшифровки NIP-44 в приложении для подписчика и попробуйте еще раз Подпись не найдена Запы Просмотры @@ -146,10 +150,14 @@ Загрузить файл Сделать фото Снять видео + Записать сообщение + Записать сообщение Нажмите и удерживайте, чтобы записать сообщение Перезаписать Запись Загрузка… + Ошибка загрузки + Неудалось загрузить голосовое сообщение Отсутствует Глубокий Высокий @@ -230,6 +238,7 @@ Отписаться Канал создан "Информация о канале изменена на" + Исчезающий чат Публичный чат записей получено Удалить @@ -336,6 +345,14 @@ Новый список закладок Удалить список закладок Приватные статьи + Удалить закладку из списка + Добавить закладку в список + Добавить в Публичные закладки + Добавить в Личные закладки + Удалить из списка закладок + Метаданные списков закладок могут быть видны любым пользователем Nostr. Только ваши частные участники зашифрованы. + Переместить в публчный + Переместить в личные Служба Wallet Connect Позволяет оплачивать запы с помощью секрета, не выходя из приложения. Храните секрет в безопасности и по возможности используйте приватный релей Публичный ключ Wallet Connect @@ -343,6 +360,8 @@ Секрет Wallet Connect Показать секрет nsec / приватный ключ в hex + Подключено + Дополнительно: ввести детали соединения вручную Определяет, как отображается ваша личность, когда вы отправляете зап. Подключить кошелек Сумма взноса в sat @@ -381,6 +400,8 @@ Добавить видео Добавить документ Добавить к сообщению + Мой дорогой друг + Использовать прямой URL-адрес Описание содержимого Компания весёлых молодых людей Тип запа @@ -393,7 +414,10 @@ Никто не видит отправителя платежа Не-зап Без следа в Nostr, обычный Lightning платеж + Опубликовать в качестве одноразовой идентификации. Ваша учетная запись не будет связана с этим ответом. + Этот ответ будет опубликован от новой анонимной идентификации Сервер для загрузки + Выберите сервер для загрузки этого файла LnAddress или @User Список Amethyst. Вы можете добавить его индивидуально или добавить список. Загрузка diff --git a/commons/src/commonMain/composeResources/font/material_symbols_outlined.ttf b/commons/src/commonMain/composeResources/font/material_symbols_outlined.ttf index a8166fc2d..c9276464a 100644 Binary files a/commons/src/commonMain/composeResources/font/material_symbols_outlined.ttf and b/commons/src/commonMain/composeResources/font/material_symbols_outlined.ttf differ diff --git a/nestsClient/src/commonTest/kotlin/com/vitorpamplona/nestsclient/ReconnectingNestsListenerTest.kt b/nestsClient/src/commonTest/kotlin/com/vitorpamplona/nestsclient/ReconnectingNestsListenerTest.kt index dfccc847f..3d4249c16 100644 --- a/nestsClient/src/commonTest/kotlin/com/vitorpamplona/nestsclient/ReconnectingNestsListenerTest.kt +++ b/nestsClient/src/commonTest/kotlin/com/vitorpamplona/nestsclient/ReconnectingNestsListenerTest.kt @@ -39,6 +39,7 @@ import kotlinx.coroutines.flow.SharedFlow import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.first +import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.flow.onSubscription import kotlinx.coroutines.flow.take import kotlinx.coroutines.flow.toList @@ -189,6 +190,18 @@ class ReconnectingNestsListenerTest { // before the test thread races into the first emit. val consumerSubscribed = CompletableDeferred() + // Counter for frames the consumer has actually observed. + // Needed to break the FRAME1-delivery race: emit() into + // first.frames is non-suspending and just enqueues into + // the pump's slot. If we trigger a session swap before + // the pump's collect lambda runs (`frames.emit(it)` to + // the wrapper), collectLatest cancels iteration 1 mid- + // resume and FRAME1 is lost — consumer ends with 1/2 + // frames and the async's withTimeout fires. The + // collector is single-coroutine so a plain StateFlow + // update is safe; the test thread reads it via .first. + val consumerProgress = MutableStateFlow(0) + // The wrapper backs handle.objects with a SharedFlow // (frames.asSharedFlow); the cast lets us use // SharedFlow.onSubscription, which fires AFTER the @@ -201,6 +214,7 @@ class ReconnectingNestsListenerTest { objectsAsShared .onSubscription { consumerSubscribed.complete(Unit) } .take(2) + .onEach { consumerProgress.value += 1 } .toList() } } @@ -220,6 +234,19 @@ class ReconnectingNestsListenerTest { first.frames.emit(frame(byteArrayOf(0x01))) + // Wait for FRAME1 to traverse pump → wrapper.frames → + // consumer collector. Without this sync the next + // `first.fail(...)` can race ahead and trigger + // collectLatest cancellation of pump-iteration-1 while + // FRAME1 is still queued in first.frames; the cancel + // interrupts the pump's resume before its lambda runs + // and FRAME1 never reaches the wrapper. Consumer then + // observes only FRAME2 (1 frame ≠ take(2)) and the + // async's withTimeout fires after 5 s. + withTimeout(5_000L) { + consumerProgress.first { it >= 1 } + } + // Force a reconnect: fail the first listener, the // orchestrator opens the next one. first.fail("scripted-disconnect") diff --git a/quartz/src/commonTest/kotlin/com/vitorpamplona/quartz/nip01Core/cache/projection/EventStoreProjectionTest.kt b/quartz/src/commonTest/kotlin/com/vitorpamplona/quartz/nip01Core/cache/projection/EventStoreProjectionTest.kt index 126cac4d8..f7d5b7b0f 100644 --- a/quartz/src/commonTest/kotlin/com/vitorpamplona/quartz/nip01Core/cache/projection/EventStoreProjectionTest.kt +++ b/quartz/src/commonTest/kotlin/com/vitorpamplona/quartz/nip01Core/cache/projection/EventStoreProjectionTest.kt @@ -26,6 +26,7 @@ import com.vitorpamplona.quartz.nip01Core.relay.filters.Filter import com.vitorpamplona.quartz.nip01Core.relay.normalizer.normalizeRelayUrl import com.vitorpamplona.quartz.nip01Core.signers.NostrSignerSync import com.vitorpamplona.quartz.nip01Core.store.ObservableEventStore +import com.vitorpamplona.quartz.nip01Core.store.ObservableEventStore.StoreChange import com.vitorpamplona.quartz.nip01Core.store.sqlite.EventStore import com.vitorpamplona.quartz.nip01Core.tags.hashtags.hashtag import com.vitorpamplona.quartz.nip09Deletions.DeletionEvent @@ -393,29 +394,41 @@ class EventStoreProjectionTest { /** * NIP-40 expiration drops slots only when the application calls * `deleteExpiredEvents()` on the observable store — projections - * no longer run their own ticker. + * no longer run their own ticker. Driven through the projection + * directly with a controllable clock so the SQL trigger's wall- + * clock check (`<= unixepoch()`) doesn't race the inserts and we + * don't need a real-time delay to simulate expiration. The full + * SQL sweep path is covered by `ExpirationTest.testDeletingExpiredEvents`. */ @Test fun nip40ExpirationDroppedOnStoreSweep() = runBlocking { val time = TimeUtils.now() + // Both expirations safely in the future so the SQL + // trigger accepts both inserts unconditionally. val safe = signer.sign(TextNoteEvent.build("safe", createdAt = time) { expiration(time + 100) }) + val short = signer.sign(TextNoteEvent.build("short", createdAt = time) { expiration(time + 50) }) observable.insert(safe) - val short = signer.sign(TextNoteEvent.build("short", createdAt = time) { expiration(time + 1) }) observable.insert(short) + var fakeNow = time val projection = - projectionOf(Filter(kinds = listOf(TextNoteEvent.KIND))) - projection.awaitLoaded() - assertEquals(2, projection.items.size) + EventStoreProjection( + observable, + listOf(Filter(kinds = listOf(TextNoteEvent.KIND))), + nowProvider = { fakeNow }, + ) + projection.seed() + assertEquals(2, projection.snapshot().items.size) - // Let the short expiration lapse, then ask the store to - // sweep — the projection drops the expired slot in - // response to the resulting StoreChange.Delete(Expired). - delay(2000) - observable.deleteExpiredEvents() + // Jump the clock past `short`'s expiration and replay the + // StoreChange.DeleteExpired the observable would emit + // after a sweep at fakeNow. + fakeNow = time + 75 + projection.apply(StoreChange.DeleteExpired(asOf = fakeNow)) - val after = projection.awaitItems { it.size == 1 } + val after = projection.snapshot().items + assertEquals(1, after.size) assertEquals(safe.id, after[0].value.id) } diff --git a/tools/material-symbols-subset/README.md b/tools/material-symbols-subset/README.md new file mode 100644 index 000000000..c6f689020 --- /dev/null +++ b/tools/material-symbols-subset/README.md @@ -0,0 +1,56 @@ +# Material Symbols Subset + +The font shipped at +`commons/src/commonMain/composeResources/font/material_symbols_outlined.ttf` +is a **subset** of Google's [Material Symbols +Outlined](https://github.com/google/material-design-icons) variable font, +trimmed to only the codepoints referenced from +`commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/icons/symbols/MaterialSymbols.kt`. + +| | Full upstream | Subset | +|---|---|---| +| Size | ~11 MB | ~410 KB | +| Glyphs | ~3500 | ~210 | + +The subset cuts >10 MB out of every commons build artifact, every Android APK, +and every Desktop app bundle. + +## When to regenerate + +Run `subset.sh` whenever you: + +- add or remove a `MaterialSymbol("\uXXXX")` entry in `MaterialSymbols.kt` +- pin a newer upstream font version + +## Prerequisites + +```bash +pip install fonttools brotli +``` + +## Regenerating + +From the repo root: + +```bash +./tools/material-symbols-subset/subset.sh +``` + +That downloads the upstream variable font, intersects its codepoints with the +ones referenced from `MaterialSymbols.kt`, and overwrites the checked-in +`material_symbols_outlined.ttf`. + +If you already have an upstream `.ttf` on disk: + +```bash +./tools/material-symbols-subset/subset.sh /path/to/MaterialSymbolsOutlined-Regular.ttf +``` + +Commit the regenerated `.ttf` alongside the `MaterialSymbols.kt` change. + +## How it works + +`pyftsubset` (from `fonttools`) reads the codepoints we care about, drops every +glyph and OpenType feature not reachable from those codepoints, and rewrites +`name`, `cmap`, and `GSUB` tables accordingly. The Compose resource pipeline +treats the result as a normal TTF — no code changes needed. diff --git a/tools/material-symbols-subset/subset.sh b/tools/material-symbols-subset/subset.sh new file mode 100755 index 000000000..0689a78ae --- /dev/null +++ b/tools/material-symbols-subset/subset.sh @@ -0,0 +1,62 @@ +#!/usr/bin/env bash +# +# Subset commons/.../material_symbols_outlined.ttf to only the codepoints +# referenced from MaterialSymbols.kt. The full Google variable font is ~11 MB +# (≈3500 glyphs); the subset is ~410 KB. +# +# Run this whenever MaterialSymbols.kt gains or loses a codepoint, or when +# pulling a newer upstream font release. +# +# Inputs: +# $1 (optional) — path to a full upstream MaterialSymbolsOutlined-Regular.ttf +# to subset. If omitted, the script downloads the variable +# font from Google Fonts. +# +# Output: +# Overwrites commons/src/commonMain/composeResources/font/material_symbols_outlined.ttf + +set -euo pipefail + +REPO_ROOT="$(cd "$(dirname "$0")/../.." && pwd)" +SYMBOLS_KT="$REPO_ROOT/commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/icons/symbols/MaterialSymbols.kt" +TARGET="$REPO_ROOT/commons/src/commonMain/composeResources/font/material_symbols_outlined.ttf" + +if ! command -v pyftsubset >/dev/null 2>&1; then + echo "pyftsubset not found. Install with: pip install fonttools brotli" >&2 + exit 1 +fi + +if [ "${1:-}" ]; then + SOURCE_TTF="$1" +else + SOURCE_TTF="$(mktemp -t material_symbols.XXXXXX.ttf)" + trap 'rm -f "$SOURCE_TTF"' EXIT + echo "Downloading upstream Material Symbols Outlined variable font..." + curl -fsSL -o "$SOURCE_TTF" \ + "https://github.com/google/material-design-icons/raw/master/variablefont/MaterialSymbolsOutlined%5BFILL%2CGRAD%2Copsz%2Cwght%5D.ttf" +fi + +CODEPOINTS_FILE="$(mktemp -t material_symbols_cp.XXXXXX.txt)" +trap 'rm -f "$CODEPOINTS_FILE" ${SOURCE_TTF:+}' EXIT + +# Pull every \uXXXX literal out of MaterialSymbols.kt as U+XXXX, deduplicated. +grep -oE '\\u[A-Fa-f0-9]{4}' "$SYMBOLS_KT" | sort -u | sed 's/\\u/U+/' > "$CODEPOINTS_FILE" + +count=$(wc -l < "$CODEPOINTS_FILE") +echo "Subsetting to $count codepoints from MaterialSymbols.kt" + +pyftsubset "$SOURCE_TTF" \ + --unicodes-file="$CODEPOINTS_FILE" \ + --output-file="$TARGET" \ + --layout-features='*' \ + --no-hinting \ + --desubroutinize \ + --notdef-outline \ + --recommended-glyphs \ + --name-IDs='*' \ + --name-legacy \ + --name-languages='*' \ + --glyph-names \ + --symbol-cmap + +echo "Wrote $TARGET ($(du -h "$TARGET" | cut -f1))"