diff --git a/.github/workflows/create-release.yml b/.github/workflows/create-release.yml index 8442a8942b..4236fa6955 100644 --- a/.github/workflows/create-release.yml +++ b/.github/workflows/create-release.yml @@ -44,7 +44,7 @@ jobs: - { os: ubuntu-latest, arch: x64, family: linux, tasks: "packageReleaseDeb packageReleaseRpm" } - { os: ubuntu-latest, arch: x64, family: linux-portable, tasks: "createReleaseAppImage createReleaseDistributable" } runs-on: ${{ matrix.os }} - timeout-minutes: 45 + timeout-minutes: 60 # linux-portable leg also downloads the freedesktop runtime + builds the Flatpak bundle defaults: run: shell: bash @@ -101,6 +101,25 @@ jobs: fi chmod +x desktopApp/packaging/appimage/appimagetool-x86_64.AppImage + # Flatpak tooling + the freedesktop runtime/sdk the manifest pins + # (runtime-version is greped from the manifest so this never drifts). + # Retried: the runtime download from Flathub is ~1 GB and flatpak + # install resumes cleanly on re-run. + - name: Install Flatpak tooling + runtimes (linux-portable only) + if: matrix.family == 'linux-portable' + uses: nick-fields/retry@ad984534de44a9489a53aefd81eb77f87c70dc60 # v4.0.0 + with: + max_attempts: 3 + timeout_minutes: 15 + command: | + set -euo pipefail + sudo apt-get update && sudo apt-get install -y flatpak flatpak-builder + flatpak remote-add --user --if-not-exists flathub https://dl.flathub.org/repo/flathub.flatpakrepo + FDO_VER=$(grep -E "^runtime-version:" desktopApp/packaging/flatpak/com.vitorpamplona.amethyst.Desktop.yml | cut -d"'" -f2) + flatpak install --user --noninteractive flathub \ + "org.freedesktop.Platform//${FDO_VER}" \ + "org.freedesktop.Sdk//${FDO_VER}" + # macOS only: import the Developer ID Application cert into a throwaway # keychain so jpackage's codesign pass can find it. Soft — if the # MAC_CERTIFICATE_P12 secret isn't set (forks, or before Apple creds are @@ -161,6 +180,36 @@ jobs: ( cd "$APP" && tar czf "../../../../portable/amethyst-desktop-${VER}-linux-x64.tar.gz" Amethyst/ ) fi + # Flatpak bundle: wraps the same createReleaseDistributable tree the + # AppImage uses. The manifest (desktopApp/packaging/flatpak/) copies the + # prebuilt jpackage tree into /app — no Gradle runs inside the sandbox. + # build-bundle emits a single-file .flatpak whose baked-in runtime-repo + # lets the user's flatpak fetch the freedesktop runtime from Flathub on + # install. --disable-rofiles-fuse: GH runners lack a usable rofiles-fuse. + - name: Build Flatpak bundle (linux-portable only) + if: matrix.family == 'linux-portable' + run: | + set -euo pipefail + VER="${{ steps.ver.outputs.version }}" + PKG="desktopApp/packaging/flatpak" + APP_ID="com.vitorpamplona.amethyst.Desktop" + OUT="desktopApp/build/flatpak" + # Inject the AppStream entry for this build (the checked-in + # metainfo deliberately carries none — CI is the source of truth). + sed -i "s||\n |" \ + "${PKG}/${APP_ID}.metainfo.xml" + mkdir -p "$OUT" + flatpak-builder --user --force-clean --disable-rofiles-fuse \ + --state-dir="${OUT}/.flatpak-builder" \ + --repo="${OUT}/repo" \ + "${OUT}/build-dir" \ + "${PKG}/${APP_ID}.yml" + flatpak build-bundle "${OUT}/repo" \ + "${OUT}/Amethyst-${VER}-x86_64.flatpak" \ + "$APP_ID" \ + --runtime-repo=https://dl.flathub.org/repo/flathub.flatpakrepo + ls -la "$OUT" + - name: Collect + rename assets run: | set -euo pipefail diff --git a/.gitignore b/.gitignore index 45f5f1d139..23d04ed5ff 100644 --- a/.gitignore +++ b/.gitignore @@ -180,6 +180,11 @@ desktopApp/src/jvmMain/appResources/*/ffmpeg/* desktopApp/packaging/appimage/appimagetool-x86_64.AppImage desktopApp/packaging/appimage/squashfs-root/ +# flatpak-builder state/cache from local builds (CI uses --state-dir under desktopApp/build/) +.flatpak-builder/ +desktopApp/packaging/flatpak/**/build-dir/ +desktopApp/packaging/flatpak/**/repo/ + # Git worktrees .worktrees/ .claude/worktrees/ diff --git a/BUILDING.md b/BUILDING.md index 54b6021e97..e719e3aad2 100644 --- a/BUILDING.md +++ b/BUILDING.md @@ -37,7 +37,9 @@ Platform-specific: - **macOS**: Xcode Command Line Tools (`xcode-select --install`) - **Windows**: WiX Toolset 3.x on PATH (for MSI). `winget install WiXToolset.WiXToolset` - **Linux (all)**: nothing extra for `.deb`; `rpm` + `fakeroot` for `.rpm`; - `appimagetool` + `desktop-file-utils` for AppImage + `appimagetool` + `desktop-file-utils` for AppImage; `flatpak` + + `flatpak-builder` for the Flatpak bundle (see + [`desktopApp/packaging/flatpak/README.md`](desktopApp/packaging/flatpak/README.md)) Install Linux RPM tooling: @@ -109,6 +111,7 @@ are **not** required to build Amethyst from the committed sources. | Linux `.deb` | `./gradlew :desktopApp:packageReleaseDeb` | `desktopApp/build/compose/binaries/main-release/deb/amethyst_*.deb` | | Linux `.rpm` | `./gradlew :desktopApp:packageReleaseRpm` | `desktopApp/build/compose/binaries/main-release/rpm/amethyst-*.rpm` | | Linux AppImage | `./gradlew :desktopApp:createReleaseAppImage` | `desktopApp/build/appimage/Amethyst-*-x86_64.AppImage` | +| Linux Flatpak | `flatpak-builder` over `createReleaseDistributable` output — see [`desktopApp/packaging/flatpak/README.md`](desktopApp/packaging/flatpak/README.md) | `desktopApp/build/flatpak/Amethyst-*-x86_64.flatpak` (CI) | | Windows `.zip` portable | See below (inline `7z`) | — | | Linux `.tar.gz` portable | See below (inline `tar`) | — | @@ -148,7 +151,7 @@ Where: | `` | Tag stripped of leading `vX.YY.ZZ` | | `` | `macos`, `windows`, `linux` | | `` | `x64`, `arm64` | -| `` | `dmg`, `msi`, `zip`, `deb`, `rpm`, `AppImage`, `tar.gz` | +| `` | `dmg`, `msi`, `zip`, `deb`, `rpm`, `AppImage`, `flatpak`, `tar.gz` | Single source of truth: [`scripts/asset-name.sh`](scripts/asset-name.sh). Package manager manifests (Homebrew cask, Winget) depend on this exact scheme — @@ -160,6 +163,7 @@ Examples: - `amethyst-desktop-1.12.1-macos-arm64.dmg` - `amethyst-desktop-1.12.1-windows-x64.msi` - `amethyst-desktop-1.12.1-linux-x64.AppImage` +- `amethyst-desktop-1.12.1-linux-x64.flatpak` --- @@ -625,12 +629,18 @@ State is shared across install channels (DMG, Homebrew, MSI, Winget, .deb, expose downgrade migration risks — **prefer a single install channel per machine**. +**Exception: Flatpak.** The sandbox redirects XDG dirs into +`~/.var/app/com.vitorpamplona.amethyst.Desktop/`, so a Flatpak install keeps +its own separate state and does not see (or risk downgrading) state written +by any other channel. + | OS | App location | State directories | |---|---|---| | macOS | `/Applications/Amethyst.app` | `~/Library/Application Support/Amethyst`
`~/Library/Preferences/com.vitorpamplona.amethyst.desktop.plist`
`~/Library/Caches/Amethyst` | | Windows | `%LOCALAPPDATA%\Amethyst` or `C:\Program Files\Amethyst` | `%APPDATA%\Amethyst`
`%LOCALAPPDATA%\Amethyst` | | Linux (deb/rpm) | `/opt/amethyst` | `~/.config/amethyst`
`~/.local/share/amethyst`
`~/.cache/amethyst` | | Linux (AppImage/tar.gz) | user-chosen | Same as above | +| Linux (Flatpak) | `/var/lib/flatpak` or `~/.local/share/flatpak` | `~/.var/app/com.vitorpamplona.amethyst.Desktop/` | Uninstall: @@ -639,6 +649,8 @@ Uninstall: - .deb: `sudo apt remove amethyst` - .rpm: `sudo dnf remove amethyst` - AppImage / tar.gz: delete the file / extracted directory +- Flatpak: `flatpak uninstall com.vitorpamplona.amethyst.Desktop` (add + `--delete-data` to also remove `~/.var/app/…`) - macOS `.dmg`: drag from `/Applications` to Trash, then delete state dirs manually --- diff --git a/PULL_NOTIFICATION.md b/PULL_NOTIFICATION.md index ef71d19e2a..bd407f484b 100644 --- a/PULL_NOTIFICATION.md +++ b/PULL_NOTIFICATION.md @@ -158,13 +158,17 @@ Google Play Services infrastructure. ### Layer 4: AlarmManager Watchdog (5 minutes) -**What:** `ServiceWatchdogManager` fires an `ELAPSED_REALTIME_WAKEUP` alarm every 5 +**What:** `ServiceWatchdogManager` fires an `ELAPSED_REALTIME` alarm every 5 minutes. The receiver checks if the service should be running and restarts it. **Why needed:** This is the "belt and suspenders" layer. If all of the above layers fail (sticky restart blocked, alarm from `onTaskRemoved` didn't fire, broadcast wasn't -delivered), the watchdog will catch it within 5 minutes. Uses `ELAPSED_REALTIME_WAKEUP` to -wake the device from sleep, ensuring the check happens even in Doze. +delivered), the watchdog will catch it within 5 minutes of the device being awake. +The alarm deliberately does NOT use the `_WAKEUP` variant: pulling the CPU out of +sleep every 5 minutes is a battery cost with no payoff, because a service restarted +on a sleeping device can't do useful network work until the device wakes anyway. +While the device sleeps, Layer 5 (WorkManager) and Layer 8 (FCM/UnifiedPush) cover +delivery; the moment the device wakes, the pending watchdog alarm fires. ### Layer 5: WorkManager Periodic Catch-Up (15 minutes) diff --git a/amethyst/build.gradle.kts b/amethyst/build.gradle.kts index e4df9eb8bf..63cf319a46 100644 --- a/amethyst/build.gradle.kts +++ b/amethyst/build.gradle.kts @@ -1,4 +1,7 @@ +import org.gradle.api.services.BuildService +import org.gradle.api.services.BuildServiceParameters import org.jetbrains.kotlin.gradle.dsl.JvmTarget +import org.jetbrains.kotlin.gradle.tasks.KotlinCompile plugins { alias(libs.plugins.androidApplication) @@ -342,6 +345,30 @@ kotlin { } } +// Gradle schedules Kotlin compilations of different variants of this module +// concurrently (e.g. playDebug + playBenchmark when CI runs unit tests, lint, +// and assembleBenchmark in one invocation), but they all share a single Kotlin +// daemon whose heap (kotlin.daemon.jvmargs) cannot fit two full :amethyst +// codegen passes — CI runs died with "GC overhead limit exceeded" inside the +// daemon. This no-op shared build service with maxParallelUsages = 1 tells the +// scheduler to run this module's Kotlin compile tasks one at a time; other +// projects' tasks (JVM tests, lint analysis, packaging) still run in parallel. +// +// CI-only: the OOM needs a cache-cold compile of several variants at once, +// which local builds (incremental, usually one variant) don't produce. +abstract class AmethystKotlinCompileLimiter : BuildService + +if (System.getenv("CI") != null) { + val kotlinCompileLimiter = + gradle.sharedServices.registerIfAbsent("amethystKotlinCompileLimiter", AmethystKotlinCompileLimiter::class) { + maxParallelUsages.set(1) + } + + tasks.withType().configureEach { + usesService(kotlinCompileLimiter) + } +} + composeCompiler { reportsDestination = layout.buildDirectory.dir("compose_compiler") metricsDestination = layout.buildDirectory.dir("compose_compiler") diff --git a/amethyst/plans/2026-07-12-relay-ping-interval-study.md b/amethyst/plans/2026-07-12-relay-ping-interval-study.md new file mode 100644 index 0000000000..66e75b20b9 --- /dev/null +++ b/amethyst/plans/2026-07-12-relay-ping-interval-study.md @@ -0,0 +1,245 @@ +# WebSocket Ping Interval Study — 122 Production Relays + +**Date:** 2026-07-12 +**Question:** Would relays drop Amethyst's connections if the client WebSocket +ping interval were raised (e.g. 120s → 240s on mobile data to save battery)? +Was the long-standing 120s value ever load-bearing, and what is the best +middle ground? + +**Answer (TL;DR):** Keep a single **120s** ping interval on every network. +Raising it to 240s saves almost no battery — 90% of surveyed relays send +their *own* pings every 30–70s, which OkHttp must answer, so the radio's +wake cadence is set by the relays, not by our interval — and it starts +dropping real relay tiers: 240s pings lose `relay.ditto.pub` (~240s idle +timeout) and every `nostr1.com`-hosted relay (~300s tier); 300s pings even +lose `relay.snort.social` (~600s tier). Lowering below 120s would only +rescue a ~120s tier of 6/122 relays that already cycle today, at 2× the +ping traffic on every other connection. 120s is, by measurement, the sweet +spot it was presumably never designed to be. + +--- + +## 1. Motivation + +`OkHttpClientFactoryForRelays` sets `pingInterval(120s)` on every relay +WebSocket. During battery work the interval was tentatively doubled on +mobile data on the theory that each client ping on an otherwise-idle +cellular connection wakes the radio and pays the multi-second tail-energy +cost. The maintainer asked the right question: *do we actually know how +production relays react to different ping intervals?* Nobody had tested +the 120s value. This study answers it empirically. + +Two distinct drop mechanisms are in play: + +1. **Relay/reverse-proxy idle timeouts** — testable from any vantage. +2. **Carrier NAT idle timeouts** — only testable from a real cellular + network (not from this environment; see §7). + +## 2. Relay population + +Production relays were harvested by fetching **600 kind:10002 (NIP-65) +relay-list events** from indexer relays (`indexer.coracle.social`, +`user.kindpag.es`) and counting `r`-tag references: **1,468 distinct +relays**, ranked by how many users actually list them. The **top 140** +(plus all Amethyst default relays) formed the test population. + +- **122 relays accepted a WebSocket** from the test vantage. +- 18 were unreachable *from a datacenter IP* (Cloudflare 403 challenges: + `nostr.wine`, `relay.0xchat.com`; TCP resets: `relay.nostr.band`, + `nostr.bitcoiner.social`, `relayable.org`, `nostr.fmt.wiz.biz`; plus + ordinary 5xx/410s). These blocks are IP-reputation-based, not + ping-related, and don't affect the conclusions — but they mean the + study cannot speak for those relays. + +## 3. Method + +Three experiments, all through the same stack (Python `websocket-client`, +TLS, one REQ per connection whose filter matches nothing, so the relay +answers EOSE and the connection then carries zero application traffic). +Server pings were always answered with pongs automatically (as OkHttp +does) and logged. + +- **Phase A — idle survival.** 140 relays, **zero client pings**, hold + for **780s (13 min)**. Records: drop time, close code, server-ping + timestamps. A relay surviving 780s of total client-ping silence proves + *any* client interval ≤ 780s is safe for it. +- **Phase B — ping efficacy.** Every Phase A dropper re-tested with + client pings at **55 / 110 / 120 / 180 / 240 / 300s** (one connection + per interval, window = observed idle timeout + 2 ping cycles + margin, + capped at 780s). This distinguishes "pings reset the relay's idle + timer" from "only data frames count". +- **Case study —** `relay.ditto.pub` with 60s pings for 420s (it had + dropped an idle connection at 257s while *its own* ping got our pong at + 123s — proving pongs don't reset its timer but client pings do). + +## 4. Phase A results — idle survival with zero client pings + +**99 of 122 relays (81%) survived 13 minutes of complete client-ping +silence.** For four out of five relays, the client ping interval is +irrelevant to connection survival at any plausible value. + +The 23 droppers cluster into clean idle-timeout tiers: + +| Tier | Count | Relays | +|---|---|---| +| < 30s (probe rejected / non-idle close) | 2 | `nostr.petrkr.net/strfry`, `next.nsite.run` | +| **~60s** | 8 | `nostr.pareto.space` (47s), `nostr.vps.satsnode.xyz` (×2), `relay.mostro.network`, `nostr.bond/alpha`, `nostr.sgiath.dev`, `nostr.bitcoinplebs.de`, `nostr.schneimi.de` | +| **~120s** | 8 | `cfrelay.snowcait.workers.dev` (118s), `nostr-verified.wellorder.net` (120.7s), `nostr-pub.wellorder.net` (120.8s), `git.shakespeare.diy` (125.7s), `nostr-relay.irgenius.org` (126.0s), `nostr-verif.slothy.win` (126.1s), `nostr.bit4use.com` (126.6s), `sendit.nosflare.com` (131.4s) | +| **~240s** | 1 | `relay.ditto.pub` (240.9s; 257.1s in an earlier run) | +| **~300s** | 2 | `david.nostr1.com` (300.5s), `dkkc.nostr1.com` (300.7s) — i.e. the **nostr1.com / relay.tools hosting tier** | +| **~600s** | 2 | `nos.lol/` (600.8s), `relay.snort.social` (601.0s) | + +### Server-ping cadence (the finding that reframes the question) + +Among the 99 relays that held an idle connection for the full window: + +| Server→client ping cadence | Relays | +|---|---| +| ≤ 35s | 45 | +| 36–70s | 37 | +| ~300s | 8 | +| no server pings at all | 9 | + +**90 of 99 relays ping the client; 82 of them every ≤ 70s.** OkHttp +answers every server ping with a pong regardless of the client-side +`pingInterval`. So on a connected cellular device the radio is being +woken every 30–70s *per connection* by the relays themselves. Changing +the client interval from 120s to 240s does not change that cadence at +all — the client ping is a rounding error in the connection's keepalive +traffic. **The claimed battery saving of a longer client ping interval +does not exist in practice.** (Corollary: the real mobile-battery lever +is connected time and connection count in the background — which the +app already minimizes by disconnecting 30s after backgrounding — not +the ping schedule.) + +## 5. Phase B results — which client intervals keep the droppers alive + +For every idle-dropper, one connection per candidate interval +(`x@T` = dropped at T seconds despite pinging at that interval; +`skip` = interval ≥ observed idle timeout, unsafe by construction): + +| relay | idle-drop | 55s | 110s | 120s | 180s | 240s | 300s | max safe | +|---|---|---|---|---|---|---|---|---| +| nostr.pareto.space | 46.9s | skip | skip | skip | skip | skip | skip | none | +| nostr.vps.satsnode.xyz | 51.1s | skip | skip | skip | skip | skip | skip | none | +| nostr.vps.satsnode.xyz/… | 51.2s | skip | skip | skip | skip | skip | skip | none | +| relay.mostro.network | 60.5s | x@60.8 | skip | skip | skip | skip | skip | none | +| nostr.bond/alpha | 60.8s | x@60.9 | skip | skip | skip | skip | skip | none | +| nostr.sgiath.dev | 60.8s | x@60.9 | skip | skip | skip | skip | skip | none | +| nostr.bitcoinplebs.de | 60.9s | x@61.1 | skip | skip | skip | skip | skip | none | +| nostr.schneimi.de | 62.2s | x@61.1 | skip | skip | skip | skip | skip | none | +| cfrelay.snowcait.workers.dev | 118.2s | x@85.0 | x@74.5 | skip | skip | skip | skip | none | +| nostr-verified.wellorder.net | 120.7s | **OK** | x@120.7 | x@120.8 | skip | skip | skip | 55s | +| nostr-pub.wellorder.net | 120.8s | **OK** | x@120.8 | x@120.8 | skip | skip | skip | 55s | +| git.shakespeare.diy/… | 125.7s | **OK** | x@125.9 | x@126.1 | skip | skip | skip | 55s | +| nostr-relay.irgenius.org | 126.0s | **OK** | x@125.8 | x@125.9 | skip | skip | skip | 55s | +| nostr-verif.slothy.win | 126.1s | **OK** | x@126.1 | x@126.3 | skip | skip | skip | 55s | +| nostr.bit4use.com | 126.6s | **OK** | x@126.4 | x@126.5 | skip | skip | skip | 55s | +| sendit.nosflare.com | 131.4s | x@81.7 | x@41.9 | x@7.0 | skip | skip | skip | none | +| **relay.ditto.pub** | 240.9s | OK | OK | **OK** | x@673.5 | **x@609.8** | skip | **120s** | +| **david.nostr1.com** | 300.5s | OK | OK | **OK** | x@300.6 | **x@300.7** | x@300.7 | **120s** | +| **dkkc.nostr1.com/…** | 300.7s | OK | OK | **OK** | x@300.7 | **x@301.1** | x@300.6 | **120s** | +| nos.lol/ | 600.8s | OK | OK | OK | OK | OK | x@602.1 | 240s | +| **relay.snort.social** | 601.0s | OK | OK | OK | OK | OK | **x@607.7** | 240s | + +Key observations: + +1. **A client ping interval numerically below the idle timeout is NOT + sufficient.** 180s and 240s pings failed against the ~300s + `nostr1.com` tier, and 300s pings failed against the ~600s + `snort.social` tier, even though each ping "should" have arrived in + time. The empirical rule across every tier: **pings only reliably + reset a relay's idle timer when the interval is at most roughly half + the timeout.** (Likely cause: these stacks check activity in coarse + windows rather than resetting a precise per-frame deadline, so an + interval near the window size loses boundary races.) +2. The **~60s tier is unsalvageable** — even 55s pings didn't help + (their timers count only data frames). These 8 relays drop idle + Amethyst connections *today* under the 120s setting and would under + any setting; the existing reconnect-on-demand path is the correct + handling for them. +3. The **~120s tier is only rescued by ≤55s pings** — meaning + **today's 120s interval never kept them alive either** (110s and + 120s pings both failed). They cycle today; they'd cycle at 240s. + No candidate change affects them. +4. The tiers that DO depend on our ping interval are exactly + **ditto (~240s), nostr1.com (~300s), and snort/nos.lol-haven + (~600s)** — and 120s holds all of them, while 240s loses the first + two and 300s loses all three. + +Connections kept alive (of 122 reachable), by candidate interval: +**55s → 110 · 120s → 104 · 240s → 101 · 300s → 99.** + +The `relay.ditto.pub` case study confirms the mechanism: with an idle +connection its *own* ping at t=123s received our pong and it still +closed at 257s (pongs don't count as activity), but with 60s client +pings it stayed up indefinitely (client pings do count). + +## 6. Why not go lower than 120s? + +55s pings would rescue the ~120s tier (6 relays). But: + +- those relays already cycle today, so the status quo loses nothing; +- 55s pings double the client-ping traffic on all ~100+ connections to + rescue 5% of relays whose operators chose aggressive timeouts; +- the radio is already woken every ≤70s on 82/122 connections by server + pings, so the *incremental* battery cost is modest — but so is the + benefit, and drop/reconnect for those 6 relays is already handled + gracefully by `BasicRelayClient`'s backoff + the keep-alive sweep. + +A per-relay adaptive interval (shorten pings only for relays observed to +drop idle connections) is possible future work, but OkHttp's +`pingInterval` is per-client, not per-socket, so it would require +per-relay client instances — not worth the complexity for 6 relays. + +## 7. Carrier NAT — the part this study cannot measure + +The other purpose of client pings is keeping carrier NAT/firewall +mappings alive on cellular. That is untestable from a datacenter vantage. +Published measurements and platform folklore put aggressive carrier TCP +idle timeouts around 4–5 minutes (most are 15–30 min; FCM survives on +~28 min heartbeats *with OS cooperation Amethyst doesn't get*). 120s +sits comfortably inside even the aggressive bound, so relay-side and +NAT-side constraints agree on the same answer. Anyone wanting to raise +the interval later must first re-run Phase B *and* validate on real +cellular networks — the relay data alone already rules out 240s. + +## 8. Decision + +- **`WEBSOCKET_PING_INTERVAL_SECS = 120`, one value for wifi and mobile.** + The tentative 240s mobile value was reverted in this same branch after + these measurements: it saved ~nothing (server pings dominate radio + wakes) and dropped the ditto and nostr1.com tiers. +- OkHttp's `pingInterval` doubles as the dead-connection detector (a + missed pong fails the socket within one interval), so 120s also keeps + failure detection twice as fast as 240s would — relevant after silent + network path changes. + +## 9. Reproduction + +Vantage caveats: datacenter egress IP (18 relays refused it), all +traffic via an HTTP CONNECT proxy. A control connection with 100s pings +survived every window, ruling out proxy-imposed idle limits ≤ 780s. + +Sketch (Python `websocket-client`): open `wss://` to each relay, send +one REQ whose filter matches nothing (`{"kinds":[1],"authors":["00…01"], +"limit":1}`), auto-pong server pings, and either never ping (Phase A, +780s window) or ping at the candidate interval (Phase B). Log connect / +EOSE / server-ping / close timestamps. Population: top-N relays by +`r`-tag frequency across kind:10002 events fetched from indexer relays. + +## Appendix — Phase A survivor cadences (99 relays) + +Server-ping cadence measured over the 13-minute window. `none` means the +relay sent no pings at all and still held the idle connection. + +| cadence | relays | +|---|---| +| ~25–35s | `articles.layer3.news`, `aegis.relayted.de`, `assistantrelay.rodbishop.nz`, `bots.utxo.one`, `custom.fiatjaf.com`, `dev.calendar-relay.edufeed.org`, `greensoul.space` (×2), `groups.0xchat.com`, `groups.satsdisco.com`, `h.codingarena.top/inbox`, `haven.calva.dev/inbox`, `haven.nostrfreedom.net`, `haven.relayted.de`, `hist.nostr.land`, `lang.relays.land` (×3), `nexus.libernet.app`, `nip17.com`, `nostr-01.uid.ovh`, `nostr-relay.derekross.me` (×2), `nostr.damupi.com/inbox`, `nostr.easydns.ca`, `nostr.kfx.fr` (×2), `nostr.land`, `nostr.nothing.is-lost.org/haven`, and 17 more at ~30s; `nostrelites.org`, `purplepag.es`, `relay.noswhere.com` at ~30s | +| ~55–70s | `nostr.thalheim.io`, `nostr.xmr.rocks`, `offchain.pub`, `relay.mostr.pub`, `relay.nostr.net`, `relay.primal.net`, `relay.damus.io`, `indexer.coracle.social`, `directory.yabu.me`, `user.kindpag.es`, `profiles.nostr1.com`, `nostr.oxtr.dev`, and ~25 more | +| ~300s | `nostr-relay.corb.net`, `nostr.001.j5s9.dev`, `nostr.8777.ch`, `nostr.einundzwanzig.space`, `nostr.mikoshi.de`, `nostr.pbfs.io`, `nostr.sectiontwo.org`, `nostr.wild-vibes.ts.net` | +| none | `nos.lol`, `nostr.mom`, `relay.divine.video`, `relay.fountain.fm`, `koru.bitcointxoko.org`, `nostr-pr02.redscrypt.org`, 2 × Cloudflare-Workers relays, 1 other | + +Raw JSON for both phases (per-relay timestamps, close codes, server-ping +series) was captured during the study session; the tables above are the +complete decision-relevant summary. diff --git a/amethyst/plans/2026-07-12-resource-usage-ledger.md b/amethyst/plans/2026-07-12-resource-usage-ledger.md new file mode 100644 index 0000000000..eecf3161a1 --- /dev/null +++ b/amethyst/plans/2026-07-12-resource-usage-ledger.md @@ -0,0 +1,178 @@ +# Resource Usage Ledger — battery/data accounting, user-visible + NIP-17 reportable + +**Date:** 2026-07-12 +**Goal:** Let users (and developers) see how much network, connection time, and +background activity the app consumes, per subsystem — and let a user send that +data to the developers over NIP-17, reusing the crash-report consent pattern. +When consumption crosses "something is wrong" thresholds, proactively ask the +user (rate-limited, opt-out-able) whether they'd like to send a report. + +Background: the 2026-07-12 ping-interval study (see +`2026-07-12-relay-ping-interval-study.md`) showed the dominant energy proxy is +connection-time (relays server-ping every 30–70s while connected) and that +battery bugs are production-only phenomena — so the ledger ships in release, +collects passively, and never transmits anything without an explicit user +action. + +## Survey (existing components reused) + +- **Send path** — the crash-report pipeline: `DisplayCrashMessages` prefills + the NIP-17 DM composer via `routeToMessage(user = , draftMessage, + expiresDays = 30)`; the user taps Send; `Account.sendNip17PrivateMessage` + gift-wraps to the recipient's kind-10050 DM relays. Reused as-is — the + ledger only builds a different draft string. +- **Persistence idiom** — `ScheduledPostStore` (Jackson + Mutex + tmp-rename + + version envelope + StateFlow). Cloned as `ResourceUsageStore`. +- **Relay traffic** — counted by a new `RelayConnectionListener` + (same hook `RelayStats` uses), NOT by modifying quartz. +- **Connection time** — integrated from `INostrClient.connectedRelaysFlow()` + (exact between emissions; no timers). +- **Network class** — `ConnectivityManager.isMobileOrFalse` StateFlow. +- **Foreground** — new tiny `ForegroundTracker` (ActivityLifecycleCallbacks → + StateFlow), registered next to `AppForegroundRecycleHook`; + `MainActivity.isResumed` is not observable and slightly stricter than + process-foreground. +- **HTTP subsystems** — `RoleBasedHttpClientBuilder` already funnels every + role (image/video/uploads/money/nip05/preview/push) through two shared + clients; a cached per-role `newBuilder().addInterceptor(counting)` wrapper + gives per-subsystem byte attribution without touching the shared clients. +- **UI idioms** — `NotificationSettingsScreen` structure (`Scaffold` + + `TopBarWithBackButton` + `SettingsSection` cards), route in `Routes.kt`, + `composableFromEnd` registration, catalog entry via + `SettingsCatalogBuilder.symEntry` (icon: existing `MaterialSymbols.Bolt` — + no font regen). +- **App-open dialog** — `DisplayCrashMessages` pattern, mounted in the same + `AppNavigation` block. + +## Design + +### Counters +Flat `Map` per UTC epoch-day, retained ~30 days. Key grammar: +`....[.]`, e.g.: + +- `net.image.mobile.bg.rx` — bytes downloaded by the image subsystem on + cellular while backgrounded (same for video/uploads/money/nip05/preview/push) +- `relay.msg.wifi.fg.rx|tx` — approx relay websocket payload bytes +- `relay.connms.mobile.bg` — relay-connection-milliseconds (Σ relays × time) +- `wakelock.notif.ms` / `wakelock.notif.count` +- `worker.scheduledPost.runs` / `worker.calendarReminder.runs` / + `worker.notificationCatchUp.runs` +- `app.starts` — process starts (detects WorkManager cold-start churn) +- `relay.connects..` / `relay.connfails..` — completed + (re)connections and failed dials; each connect paid a TCP+TLS handshake, + so high daily counts are the reconnect-churn signature +- `cpu.ms` — whole-process CPU time deltas ([android.os.Process + .getElapsedCpuTime] sampled at flush): the honest aggregate of parsing, + crypto, coroutines, and UI without per-subsystem guesswork +- `app.fgms` — time with UI visible; display power is proportional to it and + it's the denominator for every per-day comparison +- `crypto.verify.count` / `crypto.verify.us` — event signature verifications + (LocalCache.justVerify hook), settling "does Schnorr verify cost matter" + with data +- `net....reqs` / `.activems` — HTTP request counts and + active-transfer time per subsystem; counting lives on the shared base + client (OkHttpClientFactory) with tag-based role attribution, so untagged + callers land in `other` instead of escaping the ledger +- `net.bursts..` — estimated radio wake-ups from HTTP burst + patterns (new activity after >10s of HTTP silence): the battery-relevant + measure that bytes alone can't capture, since scattered small requests + each pay the radio ramp+tail +- `media.playms` — actual media playback time (ExoPlayer isPlaying + segments): decoder + screen + streaming at once, the denominator for + video bytes + +- `pow.ms` / `pow.sessions` — NIP-13 mining time (any job mining in the + PoW queue): full-core CPU, the largest attributable CPU consumer +- `tor.ms` / `tor.starts` — in-app (Arti) Tor uptime and bootstraps, from the + raw TorService status (NOT TorManager.status, whose WhileSubscribed + upstream calls service.start() when collected). External Tor (Orbot) is + deliberately untracked — its battery belongs to Orbot +- `service.alwayson.ms` — NotificationRelayService uptime: the mode context + that explains a device's relay connection-time +- `call.ms`/`call.sessions`, `nests.ms`/`nests.sessions` — calls and NIP-53 + audio rooms (mic + Opus + live media connection), from the foreground + services' lifecycles +- `location.ms` — time actively listening for GPS updates (geohash tagging); + mostly a tripwire for a leaked location subscription +- `crypto.decrypt.count/us`, `crypto.encrypt.count/us` — NIP-04/44 work via a + MeteringNostrSigner decorator wrapped inside NostrSignerWithClientTag at + account load; durations metered only for local-key signers (external/ + remote waits are IPC/network, not CPU) +- `sign.local|nip46|nip55.count` — signatures by signer kind: NIP-46 is a + relay round-trip and NIP-55 an Amber IPC wake, so the kind is the + battery-relevant dimension (this supersedes "signing is negligible", which + is only true for local keys) +- `battery.drain.fg|bg` — measured battery percent while discharging, sampled + at flush from BatteryManager: NOT app-isolated, but the ground truth that + report corpora can correlate the other counters against + +- `screen..ms` — foreground time per screen, added after the original + privacy review: only the route's base NAME is recorded (screenNameOf strips + every navigation argument before the value leaves the nav layer), so the + ledger can say "Profile" but never whose profile + +Deliberately not tracked (v1): per-coroutine or per-dispatcher CPU (needs a +thread registry; `cpu.ms` answers whether CPU matters at all first). +(Two earlier v1 exclusions were later revisited: per-screen time ships with +names-only privacy as above, and signing is now counted per signer kind +because NIP-46/NIP-55 signatures are network/IPC round-trips, not local CPU.) + +Flat keys keep the store schema-free: new counters need no migration. + +### Components (`amethyst/.../service/resourceusage/`) +- `UsageKeys` — key constants/builders + dimension helpers. +- `ResourceUsageStore` — daily buckets on disk (`resource_usage.json`), + `mergeInto(day, deltas)`, `allDays()`, prune, plus alert state + (lastAlertAtSec, optOut). +- `ResourceUsageAccountant` — in-memory `ConcurrentHashMap` + hot path (`add()` is called per relay frame), debounced flush (30s) into the + store, day-rollover handling, merged read API for UI/report. +- `ForegroundTracker` — startedActivities>0 as StateFlow. +- `RelayUsageListener` — `RelayConnectionListener` counting sent/received + frame sizes with current network/visibility dims. +- `RelayConnectionTimeIntegrator` — combines connectedRelays × isMobile × + isForeground; closes an accounting segment on every change and on + `closeOpenSegment()` (called from accountant flush and reads, so multi-hour + stable background sessions still account without any timer). +- `UsageCountingInterceptor` + counting response body — per-role HTTP bytes; + wrapped clients cached per (role, base client identity). +- `ResourceUsageReportAssembler` — Markdown: device/app header (crash-report + style), human summary (today + 7 days), fenced per-day counter dump. +- `ResourceUsageAlerts` — pure threshold logic (see below) + rate limiting. +- `DisplayResourceUsageAlert` — consent dialog (view details / send / not + now / don't ask again). +- UI: `ResourceUsageScreen` under `ui/screen/loggedIn/settings/`. + +### Wiring (AppModules / Amethyst / hooks) +- store + accountant + integrator constructed in `AppModules`; listener added + via `client.addConnectionListener`. +- `ForegroundTracker` registered in `Amethyst.onCreate` (main process only). +- `RoleBasedHttpClientBuilder` gains an optional usage meter. +- `EventNotificationConsumer.withWakeLock` gains an optional held-duration + callback (threaded through `NotificationDispatcher`). +- Workers increment their run counters via `Amethyst.instance` (guarded). +- `AppModules.trim()` flushes the accountant (backgrounding = natural flush). + +### Alert thresholds (v1, deliberately conservative — tune with real reports) +Evaluated on the last *complete* day, OR today once exceeded: +- background cellular traffic > 50 MB/day +- relay connection time > 12 relay-hours/day while backgrounded on cellular +- notification wakelock held > 30 min/day +- process starts > 75/day +Rate limit: at most one prompt per 7 days; "don't ask again" persisted. +Never auto-sends: every path goes through the DM composer where the user sees +exactly what will be sent and must tap Send. + +### Privacy +Counters are sizes, durations, and counts — no URLs, no relay names, no event +content. The report includes device model fields identical to the crash +report. Everything stays on-device until the user explicitly sends the DM +(NIP-40 30-day expiration, same as crash reports). + +### Explicitly out of scope (v1) +- Layer 1 (Perfetto/ODPM macrobenchmarks) and Layer 2 (`TrafficStats` socket + tags) — add only if the ledger proves blind somewhere (e.g. WS bytes are + payload-approximate; TrafficStats would give exact on-wire bytes). +- Per-relay attribution in the ledger (RelayStats screens already exist). +- Desktop: accountant/store are Android-module for now; extraction to commons + is mechanical if desktop wants it. diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/Amethyst.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/Amethyst.kt index 31b4ea27e8..c615047be8 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/Amethyst.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/Amethyst.kt @@ -110,6 +110,10 @@ class Amethyst : Application() { // kdoc for the threshold rationale. registerActivityLifecycleCallbacks(AppForegroundRecycleHook()) + // Foreground signal for the resource-usage ledger (fg/bg attribution + // of bytes and connection-time). Main process only. + registerActivityLifecycleCallbacks(instance.foregroundTracker) + if (isDebug) { Logging.setup() // Auto-enable the Nests session-trace recorder in debug diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/AppModules.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/AppModules.kt index 8b633a1fb5..0450df1ce6 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/AppModules.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/AppModules.kt @@ -22,6 +22,7 @@ package com.vitorpamplona.amethyst import android.content.ComponentCallbacks2 import android.content.Context +import android.os.BatteryManager import androidx.security.crypto.EncryptedSharedPreferences import coil3.disk.DiskCache import coil3.memory.MemoryCache @@ -50,6 +51,8 @@ import com.vitorpamplona.amethyst.model.torState.TorRelayState import com.vitorpamplona.amethyst.napplet.DataStoreNappletPermissionStore import com.vitorpamplona.amethyst.napplet.DataStoreNostrSignerPermissionStore import com.vitorpamplona.amethyst.service.CachedRichTextParser +import com.vitorpamplona.amethyst.service.calendar.CalendarReminderPrefs +import com.vitorpamplona.amethyst.service.calendar.CalendarReminderWorker import com.vitorpamplona.amethyst.service.cast.CastRegistry import com.vitorpamplona.amethyst.service.connectivity.ConnectivityManager import com.vitorpamplona.amethyst.service.connectivity.ConnectivityStatus @@ -84,8 +87,24 @@ import com.vitorpamplona.amethyst.service.relayClient.reqCommand.RelaySubscripti import com.vitorpamplona.amethyst.service.relayClient.reqCommand.event.EventFinderQueryState import com.vitorpamplona.amethyst.service.relayClient.reqCommand.user.UserFinderQueryState import com.vitorpamplona.amethyst.service.relayClient.speedLogger.RelaySpeedLogger +import com.vitorpamplona.amethyst.service.resourceusage.BatteryDrainSampler +import com.vitorpamplona.amethyst.service.resourceusage.ForegroundTimeIntegrator +import com.vitorpamplona.amethyst.service.resourceusage.ForegroundTracker +import com.vitorpamplona.amethyst.service.resourceusage.HttpUsageMeter +import com.vitorpamplona.amethyst.service.resourceusage.MeteringNostrSigner +import com.vitorpamplona.amethyst.service.resourceusage.ProcessCpuSampler +import com.vitorpamplona.amethyst.service.resourceusage.RadioBurstEstimator +import com.vitorpamplona.amethyst.service.resourceusage.RelayConnectionTimeIntegrator +import com.vitorpamplona.amethyst.service.resourceusage.RelayUsageListener +import com.vitorpamplona.amethyst.service.resourceusage.ResourceUsageAccountant +import com.vitorpamplona.amethyst.service.resourceusage.ResourceUsageStore +import com.vitorpamplona.amethyst.service.resourceusage.ScreenTimeIntegrator +import com.vitorpamplona.amethyst.service.resourceusage.SessionTimeIntegrator +import com.vitorpamplona.amethyst.service.resourceusage.UsageCountingInterceptor +import com.vitorpamplona.amethyst.service.resourceusage.UsageKeys import com.vitorpamplona.amethyst.service.safeCacheDir import com.vitorpamplona.amethyst.service.scheduledposts.ScheduledPostStore +import com.vitorpamplona.amethyst.service.scheduledposts.ScheduledPostWorkGate import com.vitorpamplona.amethyst.service.scheduledposts.ScheduledPostWorker import com.vitorpamplona.amethyst.service.uploads.blossom.bud10.BlossomServerResolver import com.vitorpamplona.amethyst.service.uploads.blossom.bud10.LocalBlossomCacheProbe @@ -96,7 +115,9 @@ import com.vitorpamplona.amethyst.ui.screen.AccountState import com.vitorpamplona.amethyst.ui.screen.UiSettingsState import com.vitorpamplona.amethyst.ui.tor.TorManager import com.vitorpamplona.amethyst.ui.tor.TorService +import com.vitorpamplona.amethyst.ui.tor.TorServiceStatus import com.vitorpamplona.quartz.nip01Core.core.Address +import com.vitorpamplona.quartz.nip01Core.core.Event import com.vitorpamplona.quartz.nip01Core.relay.client.INostrClient import com.vitorpamplona.quartz.nip01Core.relay.client.NostrClient import com.vitorpamplona.quartz.nip01Core.relay.client.accessories.RelayLogger @@ -104,6 +125,7 @@ import com.vitorpamplona.quartz.nip01Core.relay.client.accessories.RelayOfflineT import com.vitorpamplona.quartz.nip01Core.relay.client.reqs.stats.RelayReqStats import com.vitorpamplona.quartz.nip01Core.relay.client.stats.RelayStats import com.vitorpamplona.quartz.nip01Core.relay.commands.toClient.CachingEventDecoder +import com.vitorpamplona.quartz.nip01Core.relay.filters.Filter import com.vitorpamplona.quartz.nip01Core.relay.sockets.okhttp.SurgeDns import com.vitorpamplona.quartz.nip01Core.relay.sockets.okhttp.SurgeDnsStore import com.vitorpamplona.quartz.nip03Timestamp.VerificationStateCache @@ -123,10 +145,15 @@ import com.vitorpamplona.quartz.nip05DnsIdentifiers.namecoin.NamecoinCoreRpcClie import com.vitorpamplona.quartz.nip05DnsIdentifiers.namecoin.NamecoinNameResolver import com.vitorpamplona.quartz.nip05DnsIdentifiers.namecoin.TOR_ELECTRUMX_SERVERS import com.vitorpamplona.quartz.nip19Bech32.decodePublicKeyAsHexOrNull +import com.vitorpamplona.quartz.nip52Calendar.appt.day.CalendarDateSlotEvent +import com.vitorpamplona.quartz.nip52Calendar.appt.tags.RSVPStatusTag +import com.vitorpamplona.quartz.nip52Calendar.appt.time.CalendarTimeSlotEvent +import com.vitorpamplona.quartz.nip52Calendar.rsvp.CalendarRSVPEvent import com.vitorpamplona.quartz.nipB7Blossom.BlossomServersEvent import com.vitorpamplona.quartz.nipBCOnchainZaps.chain.CachingOnchainBackend import com.vitorpamplona.quartz.nipBCOnchainZaps.chain.EsploraBackend import com.vitorpamplona.quartz.utils.Log +import com.vitorpamplona.quartz.utils.TimeUtils import kotlinx.coroutines.CoroutineExceptionHandler import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers @@ -139,6 +166,7 @@ import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.asSharedFlow import kotlinx.coroutines.flow.collectLatest +import kotlinx.coroutines.flow.conflate import kotlinx.coroutines.flow.distinctUntilChanged import kotlinx.coroutines.flow.drop import kotlinx.coroutines.flow.filterNotNull @@ -209,7 +237,7 @@ class AppModules( // App services that should be run as soon as there are subscribers to their flows val locationManager by lazy { Log.d("AppModules", "LocationManager Init") - LocationState(appContext, applicationIOScope) + LocationState(appContext, applicationIOScope, onListening = { locationSession.setActive(it) }) } val connManager = ConnectivityManager(appContext, applicationIOScope) @@ -218,7 +246,8 @@ class AppModules( UiSettingsState(uiPrefs.value, connManager.isMobileOrFalse, applicationIOScope) } - val torManager = TorManager(torPrefs, TorService(appContext), applicationIOScope) + private val torService = TorService(appContext) + val torManager = TorManager(torPrefs, torService, applicationIOScope) // Network identity change (wifi↔cellular, regained from offline, captive portal // cleared) — the old network's guards/circuits are dead, and Arti's in-memory @@ -273,6 +302,87 @@ class AppModules( // on Tor-enabled clients to transparently redirect to .onion addresses. val onionLocationCache = OnionLocationCache() + // ---- Resource-usage ledger (battery/data accounting) ---- + // Passive on-device counters (bytes per subsystem x network x visibility, + // relay connection-time, wakelock time, worker runs). Never transmitted; + // the user can review them in Settings and explicitly DM a report to the + // developers. See amethyst/plans/2026-07-12-resource-usage-ledger.md. + val foregroundTracker = ForegroundTracker() + + val resourceUsageStore = ResourceUsageStore(File(appContext.filesDir, ResourceUsageStore.FILE_NAME)) + + val resourceUsage = ResourceUsageAccountant(resourceUsageStore, applicationIOScope) + + // Estimates radio wake-ups from HTTP burst patterns — bytes alone don't + // predict battery; scattered small requests each pay the radio ramp+tail. + private val radioBurstEstimator = + RadioBurstEstimator( + accountant = resourceUsage, + isMobile = { connManager.isMobileOrFalse.value }, + isForeground = { foregroundTracker.isForeground.value }, + ) + + // Single catch-all counter on the shared non-relay HTTP client: role + // wrappers only relabel via request tags, so no HTTP traffic (including + // direct getHttpClient users like the napplet broker) escapes the ledger. + private val httpUsageInterceptor = + UsageCountingInterceptor( + accountant = resourceUsage, + isMobile = { connManager.isMobileOrFalse.value }, + isForeground = { foregroundTracker.isForeground.value }, + bursts = radioBurstEstimator, + ) + + private val httpUsageMeter = HttpUsageMeter() + + // Session-time counters for the app's long-running battery consumers. + // All timer-free segment integrators: services and status flows flip them + // on/off, so tracking costs one counter write per transition. + val alwaysOnSession = SessionTimeIntegrator(resourceUsage, UsageKeys.ALWAYS_ON_MS, UsageKeys.ALWAYS_ON_STARTS).also { it.registerFlushHook() } + val callSession = SessionTimeIntegrator(resourceUsage, UsageKeys.CALL_MS, UsageKeys.CALL_SESSIONS).also { it.registerFlushHook() } + val nestsSession = SessionTimeIntegrator(resourceUsage, UsageKeys.NESTS_MS, UsageKeys.NESTS_SESSIONS).also { it.registerFlushHook() } + private val powSession = SessionTimeIntegrator(resourceUsage, UsageKeys.POW_MS, UsageKeys.POW_SESSIONS).also { it.registerFlushHook() } + private val torSession = SessionTimeIntegrator(resourceUsage, UsageKeys.TOR_MS, UsageKeys.TOR_STARTS).also { it.registerFlushHook() } + private val locationSession = SessionTimeIntegrator(resourceUsage, UsageKeys.LOCATION_MS).also { it.registerFlushHook() } + + // Time-per-screen (route base names only — arguments never reach the + // ledger). Fed by the navigation listener in AppNavigation; foreground + // gating means backgrounding on a screen closes its segment. + val screenTime = ScreenTimeIntegrator(resourceUsage) + + init { + screenTime.start(applicationIOScope, foregroundTracker.isForeground) + } + + // In-app (Arti) Tor uptime. Watches the raw TorService status — NOT + // TorManager.status, whose upstream is WhileSubscribed and calls + // service.start() when collected, so a permanent ledger subscription + // there would keep Tor's control flow alive on its own. External Tor + // (Orbot) is deliberately untracked: its battery belongs to Orbot. + init { + applicationIOScope.launch { + torService.status + .map { it is TorServiceStatus.Active } + .distinctUntilChanged() + .collect { torSession.setActive(it) } + } + } + + // Measured battery drain (percent while discharging, fg/bg) — the ground + // truth the app counters get correlated against. One binder read per + // ledger flush, nothing while idle. + init { + val batteryManager = appContext.getSystemService(Context.BATTERY_SERVICE) as? BatteryManager + if (batteryManager != null) { + BatteryDrainSampler( + accountant = resourceUsage, + capacityPct = { batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY).takeIf { it in 1..100 } }, + isCharging = { batteryManager.isCharging }, + isForeground = { foregroundTracker.isForeground.value }, + ).register() + } + } + // manages all the other connections separately from relays. val okHttpClients: DualHttpClientManager = DualHttpClientManager( @@ -292,10 +402,11 @@ class AppModules( master && !profileOnly && localBlossomCacheProbe.available.value }, onionCache = onionLocationCache, + usageInterceptor = httpUsageInterceptor, ) // Offers easy methods to know when connections are happening through Tor or not - val roleBasedHttpClientBuilder = RoleBasedHttpClientBuilder(okHttpClients, torPrefs.value) + val roleBasedHttpClientBuilder = RoleBasedHttpClientBuilder(okHttpClients, torPrefs.value, httpUsageMeter) val electrumXClient by lazy { Log.d("AppModules", "ElectrumXClient Init") @@ -599,6 +710,33 @@ class AppModules( // Captures statistics about relays val relayStats = RelayStats(client) + // Resource-usage ledger: relay traffic/reconnect + connection-time, + // foreground-time, process-CPU, and signature-verification collectors. + init { + client.addConnectionListener( + RelayUsageListener( + accountant = resourceUsage, + isMobile = { connManager.isMobileOrFalse.value }, + isForeground = { foregroundTracker.isForeground.value }, + ), + ) + RelayConnectionTimeIntegrator( + connectedCount = client.connectedRelaysFlow().map { it.size }, + isMobile = connManager.isMobileOrNull, + isForeground = foregroundTracker.isForeground, + accountant = resourceUsage, + ).start(applicationIOScope) + ForegroundTimeIntegrator( + isForeground = foregroundTracker.isForeground, + accountant = resourceUsage, + ).start(applicationIOScope) + ProcessCpuSampler(resourceUsage).register() + cache.verifyMeter = { elapsedNanos, _ -> + resourceUsage.add(UsageKeys.VERIFY_COUNT, 1) + resourceUsage.add(UsageKeys.VERIFY_US, elapsedNanos / 1_000) + } + } + // Logs debug messages when needed val detailedLogger = if (isDebug) RelayLogger(client, debugSending = false, debugReceiving = false) else null val relayReqStats = if (isDebug) RelayReqStats(client) else null @@ -635,7 +773,18 @@ class AppModules( minerThreads = PoWPolicy.minerWorkers(Runtime.getRuntime().availableProcessors()), persistence = powJobStore, onQueueActive = { PowMiningForegroundService.start(appContext) }, - ) + ).also { queue -> + // Resource ledger: mining burns half the cores flat-out for as + // long as it runs — without this, PoW shows up in cpu.ms as an + // unattributed mystery. Wired inside the lazy so the ledger never + // forces the queue to initialize. + applicationIOScope.launch { + queue.jobs + .map { jobs -> jobs.any { it.isMining } } + .distinctUntilChanged() + .collect { powSession.setActive(it) } + } + } } val powJobRestorer by lazy { @@ -656,6 +805,7 @@ class AppModules( client = client, rootFilesDir = { appContext.filesDir }, powQueue = { powPublishQueue }, + meterSigner = { MeteringNostrSigner(it, resourceUsage) }, ) val sessionManager = @@ -734,7 +884,11 @@ class AppModules( // Observes LocalCache for notification-relevant events and routes them to // EventNotificationConsumer. Sources: FCM, UnifiedPush, Pokey, active relay // subscriptions, and NotificationRelayService. - val notificationDispatcher = NotificationDispatcher(appContext, applicationIOScope) + val notificationDispatcher = + NotificationDispatcher(appContext, applicationIOScope) { heldMs -> + resourceUsage.add(UsageKeys.WAKELOCK_NOTIF_MS, heldMs) + resourceUsage.add(UsageKeys.WAKELOCK_NOTIF_COUNT, 1) + } // Local store for posts the user has scheduled to publish later. Backed by a // single JSON file under the app's private filesDir; read by ScheduledPostWorker. @@ -807,7 +961,9 @@ class AppModules( diskCache = { diskCache }, memoryCache = { memoryCache }, blossomServerResolver = { blossomResolver }, - callFactory = { okHttpClients.getHttpClient(roleBasedHttpClientBuilder.shouldUseTorForImageDownload(it)) }, + // Through the role builder (not raw getHttpClient) so Coil's image + // traffic carries the "image" ledger tag. Same Tor decision inside. + callFactory = { roleBasedHttpClientBuilder.okHttpClientForImage(it) }, thumbnailCache = thumbnailDiskCache, backgroundScope = applicationIOScope, ) @@ -818,6 +974,10 @@ class AppModules( fun initiate(appContext: Context) { Thread.setDefaultUncaughtExceptionHandler(UnexpectedCrashSaver(crashReportCache, applicationIOScope)) + // Ledger: count process starts — high counts reveal WorkManager/restart + // churn that cold-starts the whole app graph repeatedly. + resourceUsage.add(UsageKeys.APP_STARTS, 1) + // Restore the persisted DNS cache before any networking starts. Lookups that fire // before this completes fall through to the sync resolver path (existing behavior); // once restored, every previously-seen host hits the stale-while-revalidate path @@ -886,17 +1046,59 @@ class AppModules( // starts observing LocalCache for notification-worthy events notificationDispatcher.start() - // Schedule the scheduled-posts worker (periodic + one-time catch-up). - // Runs independently of the always-on notification setting so scheduled - // posts still fire when always-on notifications are disabled. - ScheduledPostWorker.schedule(appContext) - ScheduledPostWorker.scheduleCatchUp(appContext) + // Keep the scheduled-posts worker (15-min periodic + one-time catch-up) + // enqueued exactly while the store holds a PENDING post — see + // ScheduledPostWorkGate. Runs independently of the always-on + // notification setting so scheduled posts still fire when always-on + // notifications are disabled. + ScheduledPostWorkGate( + store = scheduledPostStore, + scope = applicationIOScope, + onPendingWork = { + ScheduledPostWorker.schedule(appContext) + ScheduledPostWorker.scheduleCatchUp(appContext) + }, + onNoPendingWork = { ScheduledPostWorker.cancelPeriodic(appContext) }, + ).start() - // Periodic scan that posts "starting soon" notifications for NIP-52 appointments the - // user has RSVP'd to as ACCEPTED. 15-minute cadence matches both the WorkManager - // periodic minimum and the lead-time window. - com.vitorpamplona.amethyst.service.calendar.CalendarReminderWorker - .schedule(appContext) + // "Starting soon" reminders for NIP-52 appointments the user RSVP'd to as + // ACCEPTED. The 15-min periodic scanner is only scheduled while it can + // plausibly fire: this observer enqueues it when an accepted RSVP lands in + // LocalCache, and the worker cancels its own chain when the cache holds + // nothing that could still start. LocalCache is memory-only, so the + // unconditional schedule this replaces could never fire from a WorkManager + // cold start anyway — it only cost battery. + applicationIOScope.launch { + LocalCache + .observeNewEvents(Filter(kinds = listOf(CalendarRSVPEvent.KIND))) + .collect { rsvp -> + if (rsvp.status() == RSVPStatusTag.STATUS.ACCEPTED) { + CalendarReminderWorker.schedule(appContext) + } + } + } + + // A rescheduled appointment must also re-arm the chain: the worker + // cancels itself when every known target is in the past, and a + // kind-31922/31923 update (the organizer moving the event) arrives + // WITHOUT any new RSVP — the user's existing RSVP still points at the + // same address, and observeNewEvents never re-fires for it. conflate + + // delay bounds the cache rescan to one per 30s while event feeds + // stream slot events; the scan only runs for users with reminders on. + applicationIOScope.launch { + LocalCache + .observeNewEvents( + Filter(kinds = listOf(CalendarDateSlotEvent.KIND, CalendarTimeSlotEvent.KIND)), + ).conflate() + .collect { + if (CalendarReminderPrefs(appContext).isEnabled() && + CalendarReminderWorker.couldStillFire(CalendarReminderWorker.acceptedRsvpsInCache(), TimeUtils.now()) + ) { + CalendarReminderWorker.schedule(appContext) + } + delay(30_000) + } + } // Watch for account login and start/stop always-on notification service applicationIOScope.launch { @@ -979,6 +1181,8 @@ class AppModules( fun trim(level: Int) { _trimLevelEvents.tryEmit(level) + // Backgrounding is a natural moment to flush the usage ledger too. + resourceUsage.flushAsync() applicationIOScope.launch { // Backgrounding is a natural moment to flush the DNS cache. dnsStore.save() diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/Account.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/Account.kt index c374cc6d95..25e869c8f9 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/Account.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/Account.kt @@ -45,6 +45,8 @@ import com.vitorpamplona.amethyst.commons.model.nip51Lists.muteList.MuteListDecr import com.vitorpamplona.amethyst.commons.model.nip51Lists.peopleList.PeopleListDecryptionCache import com.vitorpamplona.amethyst.commons.model.nip56Reports.ReportAction import com.vitorpamplona.amethyst.commons.model.nip72Communities.CommunityListDecryptionCache +import com.vitorpamplona.amethyst.commons.model.nip85TrustedAssertions.ContactCardDecryptionCache +import com.vitorpamplona.amethyst.commons.model.nip85TrustedAssertions.ContactCardsState import com.vitorpamplona.amethyst.commons.model.nip85TrustedAssertions.TrustProviderListDecryptionCache import com.vitorpamplona.amethyst.commons.onchain.OnchainZapSendError import com.vitorpamplona.amethyst.commons.onchain.OnchainZapSendResult @@ -478,6 +480,10 @@ class Account( val emoji = EmojiPackState(signer, cache, scope) val ownedEmojiPacks = OwnedEmojiPacksState(signer, cache, scope) + // needs `emoji` above: nickname edits resolve :shortcodes: against the account's packs + val contactCardDecryptionCache = ContactCardDecryptionCache(signer) + val contactCards = ContactCardsState(signer, cache, contactCardDecryptionCache, emoji) + val vanish = VanishRequestsState(signer, cache, client, scope) val appSpecific = AppSpecificState(signer, cache, scope, settings) @@ -3814,6 +3820,18 @@ class Account( sendMyPublicAndPrivateOutbox(muteList.hideUser(pubkeyHex)) } + /** + * Nicknames a user by publishing the account's kind:30382 contact card about + * them, with the petname, summary and their custom emoji mappings NIP-44 + * encrypted in the content. `null` clears a field. Goes out through the + * account's extended outbox relays. + */ + suspend fun updateContactCardPetName( + pubkeyHex: HexKey, + petName: String?, + summary: String?, + ) = sendMyPublicAndPrivateOutbox(contactCards.updatePetNameAndSummary(pubkeyHex, petName, summary)) + suspend fun showUser(pubkeyHex: HexKey) { sendMyPublicAndPrivateOutbox(blockPeopleList.showUser(pubkeyHex)) sendMyPublicAndPrivateOutbox(muteList.showUser(pubkeyHex)) diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/AntiSpamFilter.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/AntiSpamFilter.kt index 791d69dff1..dbecbb436f 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/AntiSpamFilter.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/AntiSpamFilter.kt @@ -82,10 +82,12 @@ class AntiSpamFilter { (recentAddressables[hash] != null && recentAddressables[hash] != address) || (spamMessages[hash] != null && !spamMessages[hash].duplicatedEventAddresses.contains(address)) ) { + // may be null if the first duplicate was evicted from the LRU cache + // while the spammer record still matches this hash. val existingAddress = recentAddressables[hash] - val link1 = njumpLink(NAddress.create(existingAddress.kind, existingAddress.pubKeyHex, existingAddress.dTag, relay)) val link2 = njumpLink(NAddress.create(event.kind, event.pubKey, event.dTag(), relay)) + val link1 = existingAddress?.let { njumpLink(NAddress.create(it.kind, it.pubKeyHex, it.dTag, relay)) } ?: link2 Log.w("Duplicated/SPAM") { "${relay?.url} $link1 $link2" } @@ -111,8 +113,10 @@ class AntiSpamFilter { (existingEvent != null && existingEvent != event.id) || (spamMessages[hash] != null && !spamMessages[hash].duplicatedEventIds.contains(event.id)) ) { - val link1 = njumpLink(NEvent.create(existingEvent, null, null, relay)) val link2 = njumpLink(NEvent.create(event.id, null, null, relay)) + // existingEvent may be null if the first duplicate was evicted from the + // LRU cache while the spammer record still matches this hash. + val link1 = existingEvent?.let { njumpLink(NEvent.create(it, null, null, relay)) } ?: link2 Log.w("Duplicated/SPAM") { "${relay?.url} $link1 $link2" } @@ -149,12 +153,12 @@ class AntiSpamFilter { Spammer( pubkeyHex = event.pubKey, duplicatedEventIds = setOf(), - duplicatedEventAddresses = setOf(recentAddressables[hashCode], event.address()), + duplicatedEventAddresses = setOfNotNull(recentAddressables[hashCode], event.address()), ) } else { Spammer( pubkeyHex = event.pubKey, - duplicatedEventIds = setOf(recentEventIds[hashCode], event.id), + duplicatedEventIds = setOfNotNull(recentEventIds[hashCode], event.id), duplicatedEventAddresses = setOf(), ) } diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/LocalCache.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/LocalCache.kt index 58cdad1fc9..e311fb35ac 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/LocalCache.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/LocalCache.kt @@ -3216,10 +3216,28 @@ object LocalCache : ILocalCache, ICacheProvider { live.removedNote(newNote) } + /** + * Resource-usage ledger hook: called with (elapsedNanos, valid) for every + * signature verification so the app can account crypto CPU per day. + * Wired by AppModules like [onchainBackend]; null costs nothing. + */ + @Volatile + var verifyMeter: ((elapsedNanos: Long, valid: Boolean) -> Unit)? = null + fun justVerify(event: Event): Boolean { checkNotInMainThread() - return if (!event.verify()) { + val meter = verifyMeter + if (meter == null) return justVerifyInner(event) + + val start = System.nanoTime() + val valid = justVerifyInner(event) + meter(System.nanoTime() - start, valid) + return valid + } + + private fun justVerifyInner(event: Event): Boolean = + if (!event.verify()) { try { event.checkSignature() } catch (e: Exception) { @@ -3230,7 +3248,6 @@ object LocalCache : ILocalCache, ICacheProvider { } else { true } - } fun consume( event: DraftWrapEvent, @@ -3847,7 +3864,15 @@ object LocalCache : ILocalCache, ICacheProvider { } is GiftWrapEvent -> { - consumeRegularEvent(event, relay, wasVerified) + // A wrap with an empty content carries no NIP-44 ciphertext and can + // never be unwrapped — reject it before paying for a signature check + // and a cache slot. Locally stripped copies (copyNoContent) are + // assigned straight to note.event and never pass through here. + if (event.content.isEmpty()) { + false + } else { + consumeRegularEvent(event, relay, wasVerified) + } } is GroupEvent -> { diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/accountsCache/AccountCacheState.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/accountsCache/AccountCacheState.kt index 7a07f95469..165a7f3ed9 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/accountsCache/AccountCacheState.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/accountsCache/AccountCacheState.kt @@ -64,6 +64,8 @@ class AccountCacheState( val client: INostrClient, val rootFilesDir: () -> File = { File("") }, val powQueue: () -> PoWPublishQueue? = { null }, + /** Optional resource-ledger wrapper applied to every account signer (see MeteringNostrSigner). */ + val meterSigner: (NostrSigner) -> NostrSigner = { it }, ) { val accounts = MutableStateFlow>(emptyMap()) @@ -176,7 +178,7 @@ class AccountCacheState( val signerWithClientTag = NostrSignerWithClientTag( - inner = signer, + inner = meterSigner(signer), clientName = CLIENT_TAG_NAME, disabled = { !accountSettings.syncedSettings.security.addClientTag.value }, ) diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/privacyOptions/RoleBasedHttpClientBuilder.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/privacyOptions/RoleBasedHttpClientBuilder.kt index a332044be6..b2ca4dcafc 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/privacyOptions/RoleBasedHttpClientBuilder.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/privacyOptions/RoleBasedHttpClientBuilder.kt @@ -22,6 +22,8 @@ package com.vitorpamplona.amethyst.model.privacyOptions import com.vitorpamplona.amethyst.commons.tor.TorType import com.vitorpamplona.amethyst.service.okhttp.DualHttpClientManager +import com.vitorpamplona.amethyst.service.resourceusage.HttpUsageMeter +import com.vitorpamplona.amethyst.service.resourceusage.UsageKeys import com.vitorpamplona.amethyst.ui.tor.TorSettingsFlow import com.vitorpamplona.quartz.nip01Core.relay.normalizer.RelayUrlNormalizer import okhttp3.OkHttpClient @@ -32,7 +34,18 @@ import javax.net.SocketFactory class RoleBasedHttpClientBuilder( val okHttpClient: DualHttpClientManager, val torSettings: TorSettingsFlow, + /** + * When present, every role's client is wrapped with a byte-counting + * interceptor so the resource-usage ledger can attribute HTTP traffic + * per subsystem. Null keeps the raw shared clients (tests). + */ + val usageMeter: HttpUsageMeter? = null, ) : IRoleBasedHttpClientBuilder { + private fun metered( + role: String, + base: OkHttpClient, + ): OkHttpClient = usageMeter?.counted(role, base) ?: base + fun shouldUseTorForImageDownload(url: String) = shouldUseTorFor( url, @@ -131,19 +144,19 @@ class RoleBasedHttpClientBuilder( override fun proxyPortForVideo(url: String): Int? = okHttpClient.getCurrentProxyPort(shouldUseTorForVideoDownload(url)) - override fun okHttpClientForNip05(url: String): OkHttpClient = okHttpClient.getHttpClient(shouldUseTorForNIP05(url)) + override fun okHttpClientForNip05(url: String): OkHttpClient = metered(UsageKeys.ROLE_NIP05, okHttpClient.getHttpClient(shouldUseTorForNIP05(url))) - override fun okHttpClientForUploads(url: String): OkHttpClient = okHttpClient.getHttpClient(shouldUseTorForUploads(url)) + override fun okHttpClientForUploads(url: String): OkHttpClient = metered(UsageKeys.ROLE_UPLOADS, okHttpClient.getHttpClient(shouldUseTorForUploads(url))) - override fun okHttpClientForImage(url: String): OkHttpClient = okHttpClient.getHttpClient(shouldUseTorForImageDownload(url)) + override fun okHttpClientForImage(url: String): OkHttpClient = metered(UsageKeys.ROLE_IMAGE, okHttpClient.getHttpClient(shouldUseTorForImageDownload(url))) - override fun okHttpClientForVideo(url: String): OkHttpClient = okHttpClient.getHttpClient(shouldUseTorForVideoDownload(url)) + override fun okHttpClientForVideo(url: String): OkHttpClient = metered(UsageKeys.ROLE_VIDEO, okHttpClient.getHttpClient(shouldUseTorForVideoDownload(url))) - override fun okHttpClientForMoney(url: String): OkHttpClient = okHttpClient.getHttpClient(shouldUseTorForMoneyOperations(url)) + override fun okHttpClientForMoney(url: String): OkHttpClient = metered(UsageKeys.ROLE_MONEY, okHttpClient.getHttpClient(shouldUseTorForMoneyOperations(url))) - override fun okHttpClientForPreview(url: String): OkHttpClient = okHttpClient.getHttpClient(shouldUseTorForPreviewUrl(url)) + override fun okHttpClientForPreview(url: String): OkHttpClient = metered(UsageKeys.ROLE_PREVIEW, okHttpClient.getHttpClient(shouldUseTorForPreviewUrl(url))) - override fun okHttpClientForPushRegistration(url: String): OkHttpClient = okHttpClient.getHttpClient(shouldUseTorForTrustedRelays()) + override fun okHttpClientForPushRegistration(url: String): OkHttpClient = metered(UsageKeys.ROLE_PUSH, okHttpClient.getHttpClient(shouldUseTorForTrustedRelays())) /** * Returns a [SocketFactory] that routes through the user's Tor proxy diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/calendar/CalendarReminderWorker.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/calendar/CalendarReminderWorker.kt index 0cb0b6f217..fb65cb09fa 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/calendar/CalendarReminderWorker.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/calendar/CalendarReminderWorker.kt @@ -26,9 +26,11 @@ import androidx.work.ExistingPeriodicWorkPolicy import androidx.work.PeriodicWorkRequestBuilder import androidx.work.WorkManager import androidx.work.WorkerParameters +import com.vitorpamplona.amethyst.Amethyst import com.vitorpamplona.amethyst.R import com.vitorpamplona.amethyst.commons.model.nip52Calendar.appointmentView import com.vitorpamplona.amethyst.model.LocalCache +import com.vitorpamplona.amethyst.service.resourceusage.UsageKeys import com.vitorpamplona.amethyst.ui.pluralStringRes import com.vitorpamplona.amethyst.ui.stringRes import com.vitorpamplona.quartz.nip52Calendar.appt.day.CalendarDateSlotEvent @@ -47,15 +49,26 @@ import java.util.concurrent.TimeUnit * consults [CalendarReminderStore] to skip events that have already been notified for. Run as * a 15-minute periodic worker: that's the WorkManager minimum and matches the resolution of * the reminder UI ("starts in ~15 min" is the smallest interval users perceive as "soon"). + * + * The periodic chain is only kept alive while it can plausibly fire: the ACCEPTED-RSVP + * observer in AppModules calls [schedule] when an accepted RSVP lands in LocalCache, and + * [doWork] cancels the chain when the cache holds no accepted RSVP that could still start. + * LocalCache is memory-only, so a WorkManager wake of a dead process always sees an empty + * cache and can never fire a reminder — an unconditional periodic schedule would cold-start + * the whole app graph every 15 minutes forever for zero benefit. */ class CalendarReminderWorker( appContext: Context, params: WorkerParameters, ) : CoroutineWorker(appContext, params) { override suspend fun doWork(): Result { + runCatching { Amethyst.instance.resourceUsage.add(UsageKeys.workerRuns("calendarReminder"), 1) } val prefs = CalendarReminderPrefs(applicationContext) if (!prefs.isEnabled()) { - Log.d(TAG) { "Reminders disabled; skipping scan." } + Log.d(TAG) { "Reminders disabled; ending periodic chain." } + // The settings toggle re-schedules on enable; no reason to keep + // waking the process while the feature is off. + cancel(applicationContext) return Result.success() } val now = TimeUtils.now() @@ -66,12 +79,7 @@ class CalendarReminderWorker( // multi-account "all logged-in pubkeys" view here, so we accept any RSVP that's // present in cache — the alternative (looking only at the foreground account) would // silently break notifications for account switching during the lead window. - val acceptedRsvps = - LocalCache.addressables - .filterIntoSet { _, note -> - val e = note.event - e is CalendarRSVPEvent && e.status() == RSVPStatusTag.STATUS.ACCEPTED - }.mapNotNull { it.event as? CalendarRSVPEvent } + val acceptedRsvps = acceptedRsvpsInCache() Log.d(TAG) { "Worker scanning ${acceptedRsvps.size} accepted RSVPs (now=$now, lead=${prefs.leadMinutes()}m)" } @@ -110,6 +118,22 @@ class CalendarReminderWorker( // Prune entries for events that ended more than a day ago — they can't fire again. store.forgetBefore(now - PRUNE_AGE_SECONDS) + + // Nothing left that could ever fire → end the periodic chain instead of + // waking the process every 15 minutes forever. The observers in + // AppModules re-schedule the worker the next time a live session sees + // an accepted RSVP or a calendar-event update. + // + // Decide on a FRESH cache snapshot, not the one from the start of the + // run: an RSVP accepted while this run was scanning already fired the + // observer, whose schedule() uses KEEP and no-ops while this chain + // still exists — cancelling on the stale snapshot would kill the chain + // with that RSVP's reminder permanently lost (observeNewEvents never + // re-fires for an event that is already in cache). + if (!couldStillFire(acceptedRsvpsInCache(), TimeUtils.now())) { + Log.d(TAG) { "No accepted RSVP can still fire; ending periodic chain." } + cancel(applicationContext) + } return Result.success() } @@ -121,6 +145,35 @@ class CalendarReminderWorker( // more than a day ago; they can't fire again so the entry is pure overhead. private const val PRUNE_AGE_SECONDS = 24L * 60L * 60L + /** Every ACCEPTED kind-31925 RSVP currently present in LocalCache. */ + fun acceptedRsvpsInCache(): List = + LocalCache.addressables + .filterIntoSet { _, note -> + val e = note.event + e is CalendarRSVPEvent && e.status() == RSVPStatusTag.STATUS.ACCEPTED + }.mapNotNull { it.event as? CalendarRSVPEvent } + + /** + * True while at least one accepted RSVP could still produce a reminder: + * its target event either starts in the future, or hasn't been fetched + * yet (start unknown — the chain must survive until the target + * resolves). False means the periodic worker has nothing it could ever + * notify about and may cancel its own chain. + */ + fun couldStillFire( + rsvps: Collection, + now: Long, + ): Boolean = + rsvps.any { rsvp -> + val targetAddress = rsvp.calendarEventAddress() ?: return@any false + val start = + LocalCache.addressables + .get(targetAddress) + ?.appointmentView() + ?.startSeconds + start == null || start > now + } + fun schedule(context: Context) { val request = PeriodicWorkRequestBuilder(15, TimeUnit.MINUTES) diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/call/CallForegroundService.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/call/CallForegroundService.kt index 17d22eea59..37107a5ed0 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/call/CallForegroundService.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/call/CallForegroundService.kt @@ -34,6 +34,7 @@ import android.os.IBinder import androidx.core.app.NotificationCompat import androidx.core.app.ServiceCompat import androidx.core.content.ContextCompat +import com.vitorpamplona.amethyst.Amethyst import com.vitorpamplona.amethyst.R import com.vitorpamplona.amethyst.commons.nipACWebRtcCalls.CallState import com.vitorpamplona.amethyst.ui.call.CallActivity @@ -61,6 +62,7 @@ class CallForegroundService : Service() { override fun onCreate() { super.onCreate() + Amethyst.instance.callSession.setActive(true) createNotificationChannel() } @@ -151,6 +153,7 @@ class CallForegroundService : Service() { // because CallManager.hangup() transitions to Ended and a second // hangup() from Ended state returns immediately. publishHangupBlocking() + Amethyst.instance.callSession.setActive(false) super.onDestroy() } diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/crashreports/DevReportContact.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/crashreports/DevReportContact.kt new file mode 100644 index 0000000000..8cf5d02441 --- /dev/null +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/crashreports/DevReportContact.kt @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2025 Vitor Pamplona + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.vitorpamplona.amethyst.service.crashreports + +/** + * Developer recipient for every user-initiated diagnostic NIP-17 DM — crash + * reports and resource-usage reports both route here. Single definition so a + * key rotation can never leave one path DMing the old key. + */ +const val DEV_REPORT_PUBKEY = "aa9047325603dacd4f8142093567973566de3b1e20a89557b728c3be4c6a844b" diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/crashreports/DisplayCrashMessages.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/crashreports/DisplayCrashMessages.kt index 073b4a49f0..b5fcfc4b83 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/crashreports/DisplayCrashMessages.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/crashreports/DisplayCrashMessages.kt @@ -80,7 +80,7 @@ fun DisplayCrashMessages( onClick = { nav.nav { routeToMessage( - user = LocalCache.getOrCreateUser("aa9047325603dacd4f8142093567973566de3b1e20a89557b728c3be4c6a844b"), + user = LocalCache.getOrCreateUser(DEV_REPORT_PUBKEY), draftMessage = stack, accountViewModel = accountViewModel, expiresDays = 30, diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/location/LocationState.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/location/LocationState.kt index 298799e119..a5a330ae61 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/location/LocationState.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/location/LocationState.kt @@ -31,13 +31,17 @@ import kotlinx.coroutines.flow.SharingStarted import kotlinx.coroutines.flow.catch import kotlinx.coroutines.flow.emitAll import kotlinx.coroutines.flow.map +import kotlinx.coroutines.flow.onCompletion import kotlinx.coroutines.flow.onEach +import kotlinx.coroutines.flow.onStart import kotlinx.coroutines.flow.stateIn import kotlinx.coroutines.flow.transformLatest class LocationState( context: Context, scope: CoroutineScope, + /** Resource-ledger hook: true while GPS/location updates are actively requested. */ + private val onListening: ((Boolean) -> Unit)? = null, ) { companion object { const val MIN_TIME: Long = 10000L @@ -72,6 +76,8 @@ class LocationState( val result = LocationFlow(context) .get(MIN_TIME, MIN_DISTANCE) + .onStart { onListening?.invoke(true) } + .onCompletion { onListening?.invoke(false) } .map { LocationResult.Success(it.toGeoHash(GeohashPrecision.KM_5_X_5.digits)) as LocationResult }.onEach { diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/nests/NestForegroundService.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/nests/NestForegroundService.kt index cb6ac03f08..b629c41546 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/nests/NestForegroundService.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/nests/NestForegroundService.kt @@ -39,6 +39,7 @@ import android.os.IBinder import android.os.PowerManager import androidx.core.app.NotificationCompat import androidx.core.content.ContextCompat +import com.vitorpamplona.amethyst.Amethyst import com.vitorpamplona.amethyst.R import com.vitorpamplona.amethyst.commons.viewmodels.NestAudioFocusBus import com.vitorpamplona.amethyst.commons.viewmodels.NestAudioFocusState @@ -106,6 +107,7 @@ class NestForegroundService : Service() { override fun onCreate() { super.onCreate() + Amethyst.instance.nestsSession.setActive(true) createNotificationChannel() wakeLock = (getSystemService(Context.POWER_SERVICE) as PowerManager) @@ -421,6 +423,7 @@ class NestForegroundService : Service() { } override fun onDestroy() { + Amethyst.instance.nestsSession.setActive(false) wakeLock?.takeIf { it.isHeld }?.release() wakeLock = null abandonAudioFocus() 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 8b048326b9..123a03e162 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 @@ -24,6 +24,7 @@ import android.app.NotificationManager import android.content.Context import android.graphics.drawable.BitmapDrawable import android.os.PowerManager +import android.os.SystemClock import androidx.core.content.ContextCompat import coil3.ImageLoader import coil3.asDrawable @@ -108,6 +109,8 @@ private const val SCROLL_TO_QUERY_PARAM = "&scrollTo=" class EventNotificationConsumer( private val applicationContext: Context, + /** Reports how long each notification-processing wakelock was held (resource-usage ledger). */ + private val onWakeLockHeld: ((heldMs: Long) -> Unit)? = null, ) { companion object { private const val WAKELOCK_TIMEOUT_MS = 10 * 60 * 1000L // 10 minutes @@ -126,6 +129,7 @@ class EventNotificationConsumer( PowerManager.PARTIAL_WAKE_LOCK, "amethyst:notification_processing", ) + val heldSince = SystemClock.elapsedRealtime() wakeLock.acquire(WAKELOCK_TIMEOUT_MS) try { return block() @@ -133,6 +137,7 @@ class EventNotificationConsumer( if (wakeLock.isHeld) { wakeLock.release() } + onWakeLockHeld?.invoke(SystemClock.elapsedRealtime() - heldSince) } } diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/notifications/NotificationCatchUpWorker.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/notifications/NotificationCatchUpWorker.kt index 610e57a859..d11a88bd00 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/notifications/NotificationCatchUpWorker.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/notifications/NotificationCatchUpWorker.kt @@ -31,6 +31,7 @@ import androidx.work.PeriodicWorkRequestBuilder import androidx.work.WorkManager import androidx.work.WorkerParameters import com.vitorpamplona.amethyst.Amethyst +import com.vitorpamplona.amethyst.service.resourceusage.UsageKeys import com.vitorpamplona.quartz.utils.Log import kotlinx.coroutines.delay import kotlinx.coroutines.flow.first @@ -120,6 +121,7 @@ class NotificationCatchUpWorker( override suspend fun doWork(): Result { Log.d(TAG, "Starting notification catch-up") + runCatching { Amethyst.instance.resourceUsage.add(UsageKeys.workerRuns("notificationCatchUp"), 1) } return try { // If the foreground service should be running but isn't, restart it 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 37e6fb2f96..1f583ded83 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 @@ -80,6 +80,8 @@ import kotlinx.coroutines.launch class NotificationDispatcher( private val context: Context, private val scope: CoroutineScope, + /** Forwarded to [EventNotificationConsumer]: reports wakelock held-time to the resource-usage ledger. */ + onWakeLockHeld: ((heldMs: Long) -> Unit)? = null, ) { companion object { private const val TAG = "NotificationDispatcher" @@ -127,7 +129,7 @@ class NotificationDispatcher( ) } - private val consumer = EventNotificationConsumer(context) + private val consumer = EventNotificationConsumer(context, onWakeLockHeld) private var job: Job? = null fun start() { diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/notifications/NotificationRelayService.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/notifications/NotificationRelayService.kt index de2b6c2694..c255ea4b36 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/notifications/NotificationRelayService.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/notifications/NotificationRelayService.kt @@ -138,6 +138,7 @@ class NotificationRelayService : Service() { override fun onCreate() { super.onCreate() Log.d(TAG, "Service created") + Amethyst.instance.alwaysOnSession.setActive(true) createNotificationChannel() ensureForeground() } @@ -197,6 +198,7 @@ class NotificationRelayService : Service() { */ override fun onDestroy() { Log.d(TAG, "Service destroyed") + Amethyst.instance.alwaysOnSession.setActive(false) relayServiceCollectorJob?.cancel() scope.cancel() diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/notifications/ServiceWatchdogManager.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/notifications/ServiceWatchdogManager.kt index dc9fd2ccdc..1d2b317928 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/notifications/ServiceWatchdogManager.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/notifications/ServiceWatchdogManager.kt @@ -52,8 +52,13 @@ class ServiceWatchdogManager { PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT, ) + // ELAPSED_REALTIME (not _WAKEUP): a health check is not worth pulling + // the CPU out of sleep — if the device is asleep, a restarted service + // couldn't do useful network work anyway. The alarm fires as soon as + // the device is next awake, and the deeper restart layers (15-min + // WorkManager job, FCM/UnifiedPush) cover the force-stop/doze cases. alarmManager.setInexactRepeating( - AlarmManager.ELAPSED_REALTIME_WAKEUP, + AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + WATCHDOG_INTERVAL_MS, WATCHDOG_INTERVAL_MS, pendingIntent, diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/okhttp/DualHttpClientManager.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/okhttp/DualHttpClientManager.kt index e012ec2cdd..dec043dd04 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/okhttp/DualHttpClientManager.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/okhttp/DualHttpClientManager.kt @@ -28,6 +28,7 @@ import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.stateIn import okhttp3.Call +import okhttp3.Interceptor import okhttp3.OkHttpClient import okhttp3.Request import java.net.InetSocketAddress @@ -46,8 +47,11 @@ class DualHttpClientManager( // is uniform across image, upload, NIP-05, money, preview, and push roles. // See [OkHttpClientFactory] kdoc. onionCache: OnionLocationCache, + // Resource-usage ledger counter, installed on the shared base client so + // every derived client is accounted. See [OkHttpClientFactory]. + usageInterceptor: Interceptor? = null, ) : IHttpClientManager { - val factory = OkHttpClientFactory(keyCache, userAgent, dns, shouldBridgeBlossomCache, onionCache) + val factory = OkHttpClientFactory(keyCache, userAgent, dns, shouldBridgeBlossomCache, onionCache, usageInterceptor) val defaultHttpClient: StateFlow = combine(proxyPortProvider, isMobileDataProvider) { proxy, mobile -> diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/okhttp/OkHttpClientFactory.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/okhttp/OkHttpClientFactory.kt index 38efd9eb08..7a3b1f2e2e 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/okhttp/OkHttpClientFactory.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/okhttp/OkHttpClientFactory.kt @@ -27,6 +27,7 @@ import com.vitorpamplona.amethyst.service.okhttp.OkHttpClientFactoryForRelays.Co import com.vitorpamplona.quartz.nip01Core.relay.sockets.okhttp.SurgeDns import okhttp3.ConnectionPool import okhttp3.Dispatcher +import okhttp3.Interceptor import okhttp3.OkHttpClient import java.net.InetSocketAddress import java.net.Proxy @@ -61,6 +62,13 @@ class OkHttpClientFactory( */ val shouldBridgeBlossomCache: (() -> Boolean)? = null, private val onionCache: OnionLocationCache, + /** + * Resource-usage ledger counter, installed OUTERMOST on the shared base + * client so every request through any derived client is accounted — + * per-role tagging wrappers only relabel, they never bypass. Null in + * tests / pre-configuration call sites. + */ + private val usageInterceptor: Interceptor? = null, ) { // val logging = LoggingInterceptor() val keyDecryptor = EncryptedBlobInterceptor(keyCache) @@ -103,6 +111,7 @@ class OkHttpClientFactory( .pingInterval(Duration.ofSeconds(HTTP2_PING_INTERVAL_SECS)) .followRedirects(true) .followSslRedirects(true) + .apply { usageInterceptor?.let { addInterceptor(it) } } .addInterceptor(DefaultContentTypeInterceptor(userAgent)) .apply { blossomCacheRedirect?.let { addInterceptor(it) } diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/okhttp/OkHttpClientFactoryForRelays.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/okhttp/OkHttpClientFactoryForRelays.kt index fbce710e6f..1795fcd0f9 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/okhttp/OkHttpClientFactoryForRelays.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/okhttp/OkHttpClientFactoryForRelays.kt @@ -40,6 +40,19 @@ class OkHttpClientFactoryForRelays( const val DEFAULT_IS_MOBILE: Boolean = false const val DEFAULT_TIMEOUT_ON_WIFI_SECS: Int = 10 const val DEFAULT_TIMEOUT_ON_MOBILE_SECS: Int = 30 + + // 120s is a measured sweet spot, not a guess — see + // amethyst/plans/2026-07-12-relay-ping-interval-study.md (probe of 122 + // production relays). Idle-timeout tiers observed in production are + // ~60s / ~120s / ~240s / ~300s / ~600s, and a client ping only reliably + // resets a relay's idle timer when the interval sits well BELOW the + // tier (240s pings already lose the ~240s and ~300s tiers, incl. every + // nostr1.com-hosted relay; 300s pings lose relay.snort.social). Raising + // the interval also saves almost no battery: 90% of surveyed relays + // send their own server pings every 30-70s, which OkHttp must answer, + // so the radio's wake cadence is set by the relays, not by this value. + // Lowering it would only rescue the ~120s tier (6/122 relays, already + // cycling today) at 2x the ping traffic on every other connection. const val WEBSOCKET_PING_INTERVAL_SECS: Long = 120 } diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/playback/playerPool/ExoPlayerBuilder.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/playback/playerPool/ExoPlayerBuilder.kt index 3a46279de2..9575b9775a 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/playback/playerPool/ExoPlayerBuilder.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/playback/playerPool/ExoPlayerBuilder.kt @@ -37,6 +37,7 @@ import com.vitorpamplona.amethyst.service.playback.diskCache.VideoCache import com.vitorpamplona.amethyst.service.playback.playerPool.aspectRatio.AspectRatioCacher import com.vitorpamplona.amethyst.service.playback.playerPool.positions.CurrentPlayPositionCacher import com.vitorpamplona.amethyst.service.playback.playerPool.positions.VideoViewedPositionCache +import com.vitorpamplona.amethyst.service.playback.playerPool.repeat.AutoReplayLimiter import com.vitorpamplona.amethyst.service.playback.playerPool.wake.KeepVideosPlaying @OptIn(UnstableApi::class) @@ -82,8 +83,10 @@ class ExoPlayerBuilder( ) PcmTapRegistry.bind(currentMediaItem?.mediaId, sink) addListener(AspectRatioCacher(MediaAspectRatioCache)) + addListener(AutoReplayLimiter(pause = ::pause)) addListener(KeepVideosPlaying(this)) addListener(CurrentPlayPositionCacher(this, VideoViewedPositionCache)) + addListener(MediaPlayTimeTracker()) } } diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/playback/playerPool/MediaPlayTimeTracker.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/playback/playerPool/MediaPlayTimeTracker.kt new file mode 100644 index 0000000000..44acb2d203 --- /dev/null +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/playback/playerPool/MediaPlayTimeTracker.kt @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Vitor Pamplona + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.vitorpamplona.amethyst.service.playback.playerPool + +import android.os.SystemClock +import androidx.media3.common.Player +import com.vitorpamplona.amethyst.Amethyst +import com.vitorpamplona.amethyst.service.resourceusage.UsageKeys + +/** + * Accounts actual media playback time into the resource-usage ledger. + * Playback is one of the most energy-dense things the app does — decoder, + * screen, and streaming all at once — and this turns "video used 800 MB" + * into "800 MB over 2h of playback" (normal) vs "over 10 minutes" (a bug). + * + * Attached once per player in [ExoPlayerBuilder]; a player released + * mid-playback loses at most its final open segment. + */ +class MediaPlayTimeTracker( + private val onPlayed: (elapsedMs: Long) -> Unit = { ms -> + runCatching { Amethyst.instance.resourceUsage.add(UsageKeys.MEDIA_PLAY_MS, ms) } + }, + private val nowMs: () -> Long = { SystemClock.elapsedRealtime() }, +) : Player.Listener { + private var playingSinceMs = Long.MIN_VALUE + + override fun onIsPlayingChanged(isPlaying: Boolean) { + val now = nowMs() + if (isPlaying) { + playingSinceMs = now + } else if (playingSinceMs != Long.MIN_VALUE) { + onPlayed(now - playingSinceMs) + playingSinceMs = Long.MIN_VALUE + } + } +} diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/playback/playerPool/repeat/AutoReplayLimiter.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/playback/playerPool/repeat/AutoReplayLimiter.kt new file mode 100644 index 0000000000..b16b4b0714 --- /dev/null +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/playback/playerPool/repeat/AutoReplayLimiter.kt @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2025 Vitor Pamplona + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.vitorpamplona.amethyst.service.playback.playerPool.repeat + +import androidx.media3.common.MediaItem +import androidx.media3.common.Player + +/** + * Caps how many times a video loops on its own under [Player.REPEAT_MODE_ONE]. + * + * Feed videos are configured to repeat forever (keepPlaying → REPEAT_MODE_ONE in + * PlaybackService), which keeps decoders, network and the screen busy long after the + * user stopped watching. This listener lets a video play [maxAutoPlays] full times and + * then pauses it, so continuing requires an explicit press of the play button. + * + * Each loop under REPEAT_MODE_ONE surfaces as an [onMediaItemTransition] with + * [Player.MEDIA_ITEM_TRANSITION_REASON_REPEAT], marking one completed play. The + * transition has already seeked back to the start, so pausing there leaves the video + * on its first frame with the play button showing. Any resume — the user pressing + * play, or the feed mutex auto-playing when the video scrolls back to the center — + * grants a fresh allowance, and switching to a different media item resets it too. + */ +class AutoReplayLimiter( + val maxAutoPlays: Int = DEFAULT_MAX_AUTO_PLAYS, + val pause: () -> Unit, +) : Player.Listener { + private var playsCompleted = 0 + + override fun onMediaItemTransition( + mediaItem: MediaItem?, + reason: Int, + ) { + if (reason == Player.MEDIA_ITEM_TRANSITION_REASON_REPEAT) { + playsCompleted++ + if (playsCompleted >= maxAutoPlays) { + pause() + } + } else { + playsCompleted = 0 + } + } + + override fun onPlayWhenReadyChanged( + playWhenReady: Boolean, + reason: Int, + ) { + if (playWhenReady) { + playsCompleted = 0 + } + } + + companion object { + const val DEFAULT_MAX_AUTO_PLAYS = 5 + } +} diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/relayClient/reqCommand/account/metadata/FilterAccountInfoAndListsFromKey.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/relayClient/reqCommand/account/metadata/FilterAccountInfoAndListsFromKey.kt index 28b4f7b4f5..673e6cba60 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/relayClient/reqCommand/account/metadata/FilterAccountInfoAndListsFromKey.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/relayClient/reqCommand/account/metadata/FilterAccountInfoAndListsFromKey.kt @@ -20,6 +20,7 @@ */ package com.vitorpamplona.amethyst.service.relayClient.reqCommand.account.metadata +import com.vitorpamplona.amethyst.commons.relayClient.assemblers.filterContactCardsByAuthorInTheRelay import com.vitorpamplona.amethyst.model.nip78AppSpecific.AppSpecificState.Companion.APP_SPECIFIC_DATA_D_TAG import com.vitorpamplona.quartz.experimental.nipA3.PaymentTargetsEvent import com.vitorpamplona.quartz.marmot.mip00KeyPackages.KeyPackageRelayListEvent @@ -124,6 +125,13 @@ fun filterAccountInfoAndListsFromKey( since = since, ), ), + // The account's own kind:30382 contact cards (nicknames, NIP-44 encrypted). + // Addressable — one card per target user — hence its own larger-limit filter. + filterContactCardsByAuthorInTheRelay( + relay = relay, + author = pubkey, + since = since, + ), RelayBasedFilter( relay = relay, filter = diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/relayClient/reqCommand/user/UserObservers.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/relayClient/reqCommand/user/UserObservers.kt index 447b4f68d8..82a1de7a66 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/relayClient/reqCommand/user/UserObservers.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/relayClient/reqCommand/user/UserObservers.kt @@ -28,6 +28,7 @@ import androidx.lifecycle.compose.collectAsStateWithLifecycle import com.vitorpamplona.amethyst.commons.model.emphChat.EphemeralChatChannel import com.vitorpamplona.amethyst.commons.model.nip01Core.UserInfo import com.vitorpamplona.amethyst.commons.model.nip28PublicChats.PublicChatChannel +import com.vitorpamplona.amethyst.commons.model.nip85TrustedAssertions.Nickname import com.vitorpamplona.amethyst.model.Account import com.vitorpamplona.amethyst.model.AddressableNote import com.vitorpamplona.amethyst.model.NoteState @@ -43,6 +44,7 @@ import kotlinx.collections.immutable.ImmutableList import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.FlowPreview +import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.distinctUntilChanged import kotlinx.coroutines.flow.flowOn import kotlinx.coroutines.flow.map @@ -59,18 +61,29 @@ fun observeUserName( // Subscribe in the relay for changes in the metadata of this user. UserFinderFilterAssemblerSubscription(user, accountViewModel) - val flow = - remember(user) { - user - .metadata() - .flow - .map { - it?.info?.bestName() ?: user.pubkeyDisplayHex() - }.distinctUntilChanged() - } + val contactCards = accountViewModel.account.contactCards + val flow = remember(user) { contactCards.displayNameFlow(user) } // Subscribe in the LocalCache for changes that arrive in the device - return flow.collectAsStateWithLifecycle(user.toBestDisplayName()) + return flow.collectAsStateWithLifecycle(remember(user) { contactCards.cachedDisplayName(user) }) +} + +/** + * The nickname (NIP-85 petname + private summary) the logged-in account gave + * this user through its own contact card, decrypted from the card's content, + * with the card's tags so `:shortcode:` custom emojis resolve. Null when the + * account never nicknamed this user. Per the spec, the petname should be + * rendered instead of the user's display name. + */ +@Composable +fun observeUserNickname( + user: User, + accountViewModel: AccountViewModel, +): State { + val contactCards = accountViewModel.account.contactCards + val flow = remember(user) { contactCards.nicknameFlow(user) } + + return flow.collectAsStateWithLifecycle(remember(user) { contactCards.cachedNickname(user) }) } @OptIn(ExperimentalCoroutinesApi::class) @@ -296,7 +309,7 @@ fun observeUserBookmarkCount( val combined = remember(user) { - kotlinx.coroutines.flow.combine(newFlow, oldFlow) { newCount, oldCount -> + combine(newFlow, oldFlow) { newCount, oldCount -> newCount + oldCount } } diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/relayClient/reqCommand/user/watchers/UserCardsSubAssembler.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/relayClient/reqCommand/user/watchers/UserCardsSubAssembler.kt index 19474b6c48..5ab831c9c7 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/relayClient/reqCommand/user/watchers/UserCardsSubAssembler.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/relayClient/reqCommand/user/watchers/UserCardsSubAssembler.kt @@ -21,6 +21,7 @@ package com.vitorpamplona.amethyst.service.relayClient.reqCommand.user.watchers import com.vitorpamplona.amethyst.commons.model.toHexSet +import com.vitorpamplona.amethyst.commons.relayClient.assemblers.filterContactCardsToTargetKeysFromTrustedAccountsInTheRelay import com.vitorpamplona.amethyst.commons.relayClient.eoseManagers.SingleSubEoseManager import com.vitorpamplona.amethyst.model.LocalCache import com.vitorpamplona.amethyst.model.User @@ -32,6 +33,7 @@ import com.vitorpamplona.quartz.nip01Core.relay.client.INostrClient import com.vitorpamplona.quartz.nip01Core.relay.client.pool.RelayBasedFilter import com.vitorpamplona.quartz.nip01Core.relay.filters.Filter import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl +import com.vitorpamplona.quartz.nip01Core.tags.dTag.DTag import com.vitorpamplona.quartz.utils.mapOfSet class UserCardsSubAssembler( @@ -45,7 +47,10 @@ class UserCardsSubAssembler( filters: List?, ) { filters?.forEach { filter -> - filter.tags?.get("p")?.forEach { + // kind:30382 addresses the target user in the d-tag (the key the + // filter builder uses). Reading any other tag leaves the per-user + // EOSEs unset and forces full re-downloads with since = null. + filter.tags?.get(DTag.TAG_NAME)?.forEach { val targetUser = cache.getUserIfExists(it) targetUser?.cardsOrNull()?.latestEOSEs?.newEose(relay, time) } diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/resourceusage/BatteryDrainSampler.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/resourceusage/BatteryDrainSampler.kt new file mode 100644 index 0000000000..5487055071 --- /dev/null +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/resourceusage/BatteryDrainSampler.kt @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2025 Vitor Pamplona + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.vitorpamplona.amethyst.service.resourceusage + +/** + * Samples the device battery level at each ledger flush and accumulates the + * drops that happen while discharging into [UsageKeys.BATTERY_DRAIN_FG] / + * [UsageKeys.BATTERY_DRAIN_BG] (percent points, attributed by visibility at + * sample time). This is deliberately NOT app-isolated — it's the measured + * ground truth that lets the developers correlate the app's own counters + * against real drain across many reports, which is how the alert thresholds + * get tuned. + * + * Intervals touching a charging state (at either endpoint) are skipped, and + * a level that went UP just resets the baseline. Piggybacks on the pre-flush + * hook — one BatteryManager binder read per flush, nothing while idle. + */ +class BatteryDrainSampler( + private val accountant: ResourceUsageAccountant, + private val capacityPct: () -> Int?, + private val isCharging: () -> Boolean, + private val isForeground: () -> Boolean, +) { + private var lastPct: Int? = null + private var lastCharging = true + + fun register() { + accountant.addPreFlushHook(::sample) + } + + @Synchronized + fun sample() { + val pct = capacityPct() ?: return + val charging = isCharging() + val prevPct = lastPct + val prevCharging = lastCharging + lastPct = pct + lastCharging = charging + + if (prevPct == null || prevCharging || charging) return + val drop = prevPct - pct + if (drop <= 0) return + accountant.add( + if (isForeground()) UsageKeys.BATTERY_DRAIN_FG else UsageKeys.BATTERY_DRAIN_BG, + drop.toLong(), + ) + } +} diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/resourceusage/DisplayResourceUsageAlert.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/resourceusage/DisplayResourceUsageAlert.kt new file mode 100644 index 0000000000..733f06f392 --- /dev/null +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/resourceusage/DisplayResourceUsageAlert.kt @@ -0,0 +1,177 @@ +/* + * Copyright (c) 2025 Vitor Pamplona + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.vitorpamplona.amethyst.service.resourceusage + +import androidx.compose.foundation.layout.Row +import androidx.compose.material3.AlertDialog +import androidx.compose.material3.Button +import androidx.compose.material3.Text +import androidx.compose.material3.TextButton +import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.res.pluralStringResource +import com.vitorpamplona.amethyst.Amethyst +import com.vitorpamplona.amethyst.R +import com.vitorpamplona.amethyst.collectMemorySnapshot +import com.vitorpamplona.amethyst.model.LocalCache +import com.vitorpamplona.amethyst.service.crashreports.DEV_REPORT_PUBKEY +import com.vitorpamplona.amethyst.ui.navigation.navs.INav +import com.vitorpamplona.amethyst.ui.navigation.routes.routeToMessage +import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel +import com.vitorpamplona.amethyst.ui.stringRes +import com.vitorpamplona.quartz.utils.TimeUtils +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext + +/** + * On app open, checks the resource-usage ledger against the + * [ResourceUsageAlerts] thresholds and — at most once per week, unless the + * user opted out — asks whether they'd like to review + send a usage report + * to the developers. Confirming only PREFILLS the NIP-17 DM composer (crash + * report pattern): the full report text is visible there and nothing is sent + * until the user taps Send. + */ +@Composable +fun DisplayResourceUsageAlert( + accountViewModel: AccountViewModel, + nav: INav, +) { + val context = LocalContext.current + val alert = remember { mutableStateOf(null) } + + LaunchedEffect(accountViewModel) { + withContext(Dispatchers.IO) { + val store = Amethyst.instance.resourceUsageStore + val accountant = Amethyst.instance.resourceUsage + if (!ResourceUsageAlerts.shouldPrompt(store.lastAlertAtSec(), store.alertsOptOut(), TimeUtils.now())) { + return@withContext + } + val found = ResourceUsageAlerts.evaluate(accountant.allDaysIncludingLive(), accountant.today()) + if (found != null) { + alert.value = found + } + } + } + + // The 7-day rate limit is consumed when the user ACTS on the dialog (any + // button or dismissal), not when it is shown: marking before the first + // frame let a config change or process death burn the whole prompt budget + // on a dialog nobody ever saw, silencing an active battery problem for a + // week. Re-showing after an unhandled recreation is exactly what we want. + val dismiss = { + accountViewModel.runOnIO { + Amethyst.instance.resourceUsageStore.markAlertPrompted(TimeUtils.now()) + } + alert.value = null + } + + alert.value?.let { found -> + AlertDialog( + onDismissRequest = dismiss, + title = { Text(stringRes(R.string.resource_usage_alert_title)) }, + text = { + Text( + stringRes( + R.string.resource_usage_alert_message, + reasonDescription(found), + ), + ) + }, + dismissButton = { + Row { + TextButton(onClick = { + accountViewModel.runOnIO { + Amethyst.instance.resourceUsageStore.setAlertsOptOut(true) + } + dismiss() + }) { + Text(stringRes(R.string.resource_usage_alert_opt_out)) + } + TextButton(onClick = dismiss) { + Text(stringRes(R.string.resource_usage_alert_not_now)) + } + } + }, + confirmButton = { + Button(onClick = { + nav.nav { + val report = + withContext(Dispatchers.IO) { + ResourceUsageReportAssembler().buildReport( + Amethyst.instance.resourceUsage.allDaysIncludingLive(), + Amethyst.instance.resourceUsage.today(), + collectMemorySnapshot(context), + ) + } + routeToMessage( + user = LocalCache.getOrCreateUser(DEV_REPORT_PUBKEY), + draftMessage = report, + accountViewModel = accountViewModel, + expiresDays = 30, + ) + } + dismiss() + }) { + Text(stringRes(R.string.resource_usage_alert_send)) + } + }, + ) + } +} + +@Composable +private fun reasonDescription(alert: ResourceUsageAlerts.Alert): String = + when (alert.reason) { + ResourceUsageAlerts.Reason.BACKGROUND_MOBILE_DATA -> + stringRes( + R.string.resource_usage_reason_bg_data, + ResourceUsageReportAssembler.formatBytes(alert.value), + ) + + ResourceUsageAlerts.Reason.BACKGROUND_MOBILE_CONNECTION_TIME -> + stringRes( + R.string.resource_usage_reason_conn_time, + ResourceUsageReportAssembler.formatConnHours(alert.value), + ) + + ResourceUsageAlerts.Reason.WAKELOCK_TIME -> + stringRes( + R.string.resource_usage_reason_wakelock, + ResourceUsageReportAssembler.formatDurationMs(alert.value), + ) + + ResourceUsageAlerts.Reason.PROCESS_CHURN -> + pluralStringResource( + R.plurals.resource_usage_reason_churn, + alert.value.toInt(), + alert.value.toInt(), + ) + + ResourceUsageAlerts.Reason.RECONNECT_CHURN -> + pluralStringResource( + R.plurals.resource_usage_reason_reconnects, + alert.value.toInt(), + alert.value.toInt(), + ) + } diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/resourceusage/ForegroundTimeIntegrator.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/resourceusage/ForegroundTimeIntegrator.kt new file mode 100644 index 0000000000..0e1bb3200c --- /dev/null +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/resourceusage/ForegroundTimeIntegrator.kt @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2025 Vitor Pamplona + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.vitorpamplona.amethyst.service.resourceusage + +import android.os.SystemClock +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Job +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.launch + +/** + * Integrates time-with-UI-visible into the ledger ([UsageKeys.APP_FG_MS]). + * Screen-on time is what display power is proportional to, and it's the + * denominator that makes every other counter interpretable (MB per hour in + * app vs MB while backgrounded). + */ +class ForegroundTimeIntegrator( + private val isForeground: Flow, + accountant: ResourceUsageAccountant, + nowMs: () -> Long = { SystemClock.elapsedRealtime() }, +) : TimeSegmentIntegrator(accountant, nowMs) { + fun start(scope: CoroutineScope): Job { + registerFlushHook() + return scope.launch { + isForeground.collect { fg -> transitionTo(if (fg) Unit else null) } + } + } + + override fun account( + state: Unit, + elapsedMs: Long, + ) { + if (elapsedMs > 0) accountant.add(UsageKeys.APP_FG_MS, elapsedMs) + } +} diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/resourceusage/ForegroundTracker.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/resourceusage/ForegroundTracker.kt new file mode 100644 index 0000000000..0c4796a6a0 --- /dev/null +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/resourceusage/ForegroundTracker.kt @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2025 Vitor Pamplona + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.vitorpamplona.amethyst.service.resourceusage + +import android.app.Activity +import android.app.Application +import android.os.Bundle +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.flow.asStateFlow + +/** + * Process-level foreground signal as an observable StateFlow: true while at + * least one activity is STARTED. Used by the usage ledger to attribute bytes + * and connection-time to foreground vs background buckets. + * + * ([MainActivity.isResumed] is not observable and goes false during PiP / + * in-app dialogs, which would misattribute foreground traffic to background.) + */ +class ForegroundTracker : Application.ActivityLifecycleCallbacks { + private var startedActivities = 0 + + private val _isForeground = MutableStateFlow(false) + val isForeground: StateFlow = _isForeground.asStateFlow() + + override fun onActivityStarted(activity: Activity) { + startedActivities++ + _isForeground.value = startedActivities > 0 + } + + override fun onActivityStopped(activity: Activity) { + startedActivities = (startedActivities - 1).coerceAtLeast(0) + _isForeground.value = startedActivities > 0 + } + + override fun onActivityCreated( + activity: Activity, + savedInstanceState: Bundle?, + ) {} + + override fun onActivityResumed(activity: Activity) {} + + override fun onActivityPaused(activity: Activity) {} + + override fun onActivitySaveInstanceState( + activity: Activity, + outState: Bundle, + ) {} + + override fun onActivityDestroyed(activity: Activity) {} +} diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/resourceusage/MeteringNostrSigner.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/resourceusage/MeteringNostrSigner.kt new file mode 100644 index 0000000000..93bb31f4a5 --- /dev/null +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/resourceusage/MeteringNostrSigner.kt @@ -0,0 +1,138 @@ +/* + * Copyright (c) 2025 Vitor Pamplona + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.vitorpamplona.amethyst.service.resourceusage + +import com.vitorpamplona.quartz.nip01Core.core.Event +import com.vitorpamplona.quartz.nip01Core.core.HexKey +import com.vitorpamplona.quartz.nip01Core.signers.NostrSigner +import com.vitorpamplona.quartz.nip01Core.signers.NostrSignerInternal +import com.vitorpamplona.quartz.nip46RemoteSigner.signer.NostrSignerRemote +import com.vitorpamplona.quartz.nip55AndroidSigner.client.NostrSignerExternal +import com.vitorpamplona.quartz.nip57Zaps.LnZapPrivateEvent +import com.vitorpamplona.quartz.nip57Zaps.LnZapRequestEvent +import com.vitorpamplona.quartz.nip89AppHandlers.clientTag.NostrSignerWithClientTag + +/** + * A [NostrSigner] decorator (same pattern as [NostrSignerWithClientTag]) that + * accounts signing and encryption work into the resource-usage ledger: + * + * - signature counts by signer kind — a local-key signature is ~free, a + * NIP-55 one wakes the Amber process over IPC, and a NIP-46 one is a full + * relay round-trip, so the *kind* is the battery-relevant dimension; + * - NIP-04/44 decrypt/encrypt counts, with CPU time metered only when the + * wrapped signer is a local key ([NostrSignerInternal]) — for external and + * remote signers the elapsed time would be IPC/network wait, not crypto + * cost, and would poison the CPU numbers. + * + * Overhead per operation: one counter increment (plus two [System.nanoTime] + * calls for local-key crypto, nanoseconds against a millisecond-scale ECDH) — + * nothing here can slow the operations being measured. + * + * Wrapped INSIDE [NostrSignerWithClientTag] (metering the raw signer), so the + * existing `signer is NostrSignerWithClientTag` call sites keep working; + * anything that needs the raw signer should unwrap via [innermostSigner]. + */ +class MeteringNostrSigner( + val inner: NostrSigner, + private val accountant: ResourceUsageAccountant, +) : NostrSigner(inner.pubKey) { + private val signKey = UsageKeys.signs(signerKind(inner)) + private val meterCryptoTime = inner is NostrSignerInternal + + override fun isWriteable(): Boolean = inner.isWriteable() + + override suspend fun sign( + createdAt: Long, + kind: Int, + tags: Array>, + content: String, + ): T { + accountant.add(signKey, 1) + return inner.sign(createdAt, kind, tags, content) + } + + override suspend fun nip04Encrypt( + plaintext: String, + toPublicKey: HexKey, + ): String = metered(UsageKeys.ENCRYPT_COUNT, UsageKeys.ENCRYPT_US) { inner.nip04Encrypt(plaintext, toPublicKey) } + + override suspend fun nip04Decrypt( + ciphertext: String, + fromPublicKey: HexKey, + ): String = metered(UsageKeys.DECRYPT_COUNT, UsageKeys.DECRYPT_US) { inner.nip04Decrypt(ciphertext, fromPublicKey) } + + override suspend fun nip44Encrypt( + plaintext: String, + toPublicKey: HexKey, + ): String = metered(UsageKeys.ENCRYPT_COUNT, UsageKeys.ENCRYPT_US) { inner.nip44Encrypt(plaintext, toPublicKey) } + + override suspend fun nip44Decrypt( + ciphertext: String, + fromPublicKey: HexKey, + ): String = metered(UsageKeys.DECRYPT_COUNT, UsageKeys.DECRYPT_US) { inner.nip44Decrypt(ciphertext, fromPublicKey) } + + override suspend fun decryptZapEvent(event: LnZapRequestEvent): LnZapPrivateEvent = metered(UsageKeys.DECRYPT_COUNT, UsageKeys.DECRYPT_US) { inner.decryptZapEvent(event) } + + override suspend fun deriveKey(nonce: HexKey): HexKey = inner.deriveKey(nonce) + + override suspend fun signPsbt(psbtHex: String): String { + accountant.add(signKey, 1) + return inner.signPsbt(psbtHex) + } + + override fun hasForegroundSupport(): Boolean = inner.hasForegroundSupport() + + private inline fun metered( + countKey: String, + usKey: String, + op: () -> T, + ): T { + accountant.add(countKey, 1) + if (!meterCryptoTime) return op() + val start = System.nanoTime() + try { + return op() + } finally { + accountant.add(usKey, (System.nanoTime() - start) / 1_000) + } + } + + companion object { + fun signerKind(signer: NostrSigner): String = + when (signer) { + is NostrSignerExternal -> UsageKeys.SIGNER_NIP55 + is NostrSignerRemote -> UsageKeys.SIGNER_NIP46 + else -> UsageKeys.SIGNER_LOCAL + } + } +} + +/** + * Strips the app's signer decorators (client tag, metering) to reach the + * concrete signer — for call sites that need the real type, like the NIP-55 + * activity-launcher registration. + */ +fun NostrSigner.innermostSigner(): NostrSigner = + when (this) { + is NostrSignerWithClientTag -> inner.innermostSigner() + is MeteringNostrSigner -> inner.innermostSigner() + else -> this + } diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/resourceusage/ProcessCpuSampler.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/resourceusage/ProcessCpuSampler.kt new file mode 100644 index 0000000000..408101afa4 --- /dev/null +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/resourceusage/ProcessCpuSampler.kt @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2025 Vitor Pamplona + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.vitorpamplona.amethyst.service.resourceusage + +import android.os.Process + +/** + * Samples whole-process CPU time (user+system, [Process.getElapsedCpuTime]) + * into the ledger as day deltas. This is the honest aggregate cost of + * everything that runs on the CPU — event parsing, signature verification, + * coroutines, recomposition — without per-subsystem guesswork. Sampled from + * the accountant's pre-flush hook, so it costs one syscall per flush and + * nothing while idle. Per-process monotonic: a fresh process simply starts a + * fresh baseline. + */ +class ProcessCpuSampler( + private val accountant: ResourceUsageAccountant, + private val cpuMs: () -> Long = { Process.getElapsedCpuTime() }, +) { + private var lastSampleMs = cpuMs() + + fun register() { + accountant.addPreFlushHook(::sample) + } + + @Synchronized + fun sample() { + val now = cpuMs() + val delta = now - lastSampleMs + lastSampleMs = now + if (delta > 0) accountant.add(UsageKeys.CPU_MS, delta) + } +} diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/resourceusage/RelayConnectionTimeIntegrator.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/resourceusage/RelayConnectionTimeIntegrator.kt new file mode 100644 index 0000000000..b3afc9c6e5 --- /dev/null +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/resourceusage/RelayConnectionTimeIntegrator.kt @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2025 Vitor Pamplona + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.vitorpamplona.amethyst.service.resourceusage + +import android.os.SystemClock +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Job +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.combine +import kotlinx.coroutines.launch + +/** + * Integrates relay-connection-time (Σ open connections × elapsed time) into + * the ledger, split by network class and visibility. Connection-time is the + * best single battery proxy the ping study found: most relays server-ping + * every 30-70s, so the radio is active for as long as connections are open. + */ +class RelayConnectionTimeIntegrator( + private val connectedCount: Flow, + private val isMobile: Flow, + private val isForeground: Flow, + accountant: ResourceUsageAccountant, + nowMs: () -> Long = { SystemClock.elapsedRealtime() }, +) : TimeSegmentIntegrator(accountant, nowMs) { + data class SegmentState( + val count: Int, + val mobile: Boolean, + val foreground: Boolean, + ) + + fun start(scope: CoroutineScope): Job { + registerFlushHook() + return scope.launch { + combine(connectedCount, isMobile, isForeground) { count, mobile, fg -> + SegmentState(count, mobile ?: false, fg) + }.collect { next -> transitionTo(next) } + } + } + + override fun account( + state: SegmentState, + elapsedMs: Long, + ) { + if (state.count <= 0 || elapsedMs <= 0) return + accountant.add(UsageKeys.relayConnMs(state.mobile, state.foreground), state.count * elapsedMs) + } +} diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/resourceusage/RelayUsageListener.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/resourceusage/RelayUsageListener.kt new file mode 100644 index 0000000000..a8ba773138 --- /dev/null +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/resourceusage/RelayUsageListener.kt @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2025 Vitor Pamplona + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.vitorpamplona.amethyst.service.resourceusage + +import com.vitorpamplona.quartz.nip01Core.relay.client.listeners.RelayConnectionListener +import com.vitorpamplona.quartz.nip01Core.relay.client.single.IRelayClient +import com.vitorpamplona.quartz.nip01Core.relay.commands.toClient.Message +import com.vitorpamplona.quartz.nip01Core.relay.commands.toRelay.Command + +/** + * Counts relay websocket traffic into the usage ledger. Frame sizes are + * UTF-16 char counts of the JSON payload — a close proxy for on-wire bytes + * (relay JSON is ASCII-dominant), consistent with how RelayStats counts. + * Excludes WS framing/compression; good enough for "which subsystem is + * eating my data plan" comparisons. + */ +class RelayUsageListener( + private val accountant: ResourceUsageAccountant, + private val isMobile: () -> Boolean, + private val isForeground: () -> Boolean, +) : RelayConnectionListener { + override fun onSent( + relay: IRelayClient, + cmdStr: String, + cmd: Command, + success: Boolean, + ) { + if (success) { + accountant.add(UsageKeys.relayMsg(isMobile(), isForeground(), received = false), cmdStr.length.toLong()) + } + } + + override fun onIncomingMessage( + relay: IRelayClient, + msgStr: String, + msg: Message, + ) { + accountant.add(UsageKeys.relayMsg(isMobile(), isForeground(), received = true), msgStr.length.toLong()) + } + + // Every completed (re)connection paid a TCP+TLS handshake; high daily + // counts are the signature of reconnect churn (flaky network, aggressive + // relay idle timeouts, Tor bootstrap loops). + override fun onConnected( + relay: IRelayClient, + pingMillis: Int, + compressed: Boolean, + ) { + accountant.add(UsageKeys.relayConnects(isMobile(), isForeground()), 1) + } + + override fun onCannotConnect( + relay: IRelayClient, + errorMessage: String, + ) { + accountant.add(UsageKeys.relayConnectFails(isMobile(), isForeground()), 1) + } +} diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/resourceusage/ResourceUsageAccountant.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/resourceusage/ResourceUsageAccountant.kt new file mode 100644 index 0000000000..778532c29a --- /dev/null +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/resourceusage/ResourceUsageAccountant.kt @@ -0,0 +1,144 @@ +/* + * Copyright (c) 2025 Vitor Pamplona + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.vitorpamplona.amethyst.service.resourceusage + +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.delay +import kotlinx.coroutines.launch +import java.util.concurrent.ConcurrentHashMap +import java.util.concurrent.atomic.AtomicBoolean +import java.util.concurrent.atomic.AtomicLong + +/** + * In-memory hot path for usage counters. [add] is called from network threads + * per relay frame / HTTP response chunk, so it must be allocation-light and + * lock-free: a ConcurrentHashMap of AtomicLongs, drained into + * [ResourceUsageStore] by a debounced flush (at most one write per + * [flushDebounceMs] while traffic flows; nothing scheduled when idle). + * + * AtomicLong (not LongAdder) because draining must be loss-free: getAndSet(0) + * hands off the accumulated value atomically, whereas remove+sum on a + * LongAdder can strand a racing increment on an orphaned cell. Entries stay + * in the map after a drain — the key space is small and fixed (dims x areas), + * so this costs a few hundred boxed zeros at most. + * + * Counters added from inside a pre-flush hook (the CPU sampler, the segment + * integrators closing an open segment) never re-arm the debounce: they are + * drained by the very flush that invoked the hook, and letting them schedule + * would turn every flush into a perpetual 30s wake-and-write loop — the + * battery ledger becoming its own battery drain. + * + * Day attribution: deltas are drained into the bucket of the day they are + * drained on. Counts within one debounce window of midnight may land on the + * neighboring day — irrelevant at the ledger's day-level granularity. + */ +class ResourceUsageAccountant( + private val store: ResourceUsageStore, + private val scope: CoroutineScope, + private val epochDay: () -> Long = { System.currentTimeMillis() / DAY_MS }, + private val flushDebounceMs: Long = 30_000L, +) { + private val live = ConcurrentHashMap() + private val flushScheduled = AtomicBoolean(false) + + /** True only on the thread currently running the pre-flush hooks. */ + private val inHookRun = ThreadLocal.withInitial { false } + + /** Hooks run right before a flush drains the counters (e.g. the connection-time integrator closing its open segment). */ + private val preFlushHooks = ConcurrentHashMap.newKeySet<() -> Unit>() + + fun addPreFlushHook(hook: () -> Unit) { + preFlushHooks.add(hook) + } + + fun add( + key: String, + amount: Long, + ) { + if (amount <= 0) return + live.computeIfAbsent(key) { AtomicLong() }.addAndGet(amount) + if (inHookRun.get()) return + if (flushScheduled.compareAndSet(false, true)) { + scope.launch { + delay(flushDebounceMs) + flushScheduled.set(false) + flush() + } + } + } + + private fun runPreFlushHooks() { + inHookRun.set(true) + try { + preFlushHooks.forEach { runCatching { it() } } + } finally { + inHookRun.set(false) + } + } + + /** Drains the in-memory counters into today's persisted bucket. */ + suspend fun flush() { + runPreFlushHooks() + val deltas = drain() + if (deltas.isNotEmpty()) { + store.mergeInto(epochDay(), deltas) + } + } + + /** Fire-and-forget flush for non-suspending callers (onTrimMemory). */ + fun flushAsync() { + scope.launch { flush() } + } + + fun today(): Long = epochDay() + + /** + * Persisted buckets merged with the not-yet-flushed live counters + * (attributed to today). This is what the UI, the report assembler, + * and the alert evaluator read. Reading never schedules a flush. + */ + suspend fun allDaysIncludingLive(): Map> { + runPreFlushHooks() + val persisted = store.allDays() + val liveSnapshot = live.mapValues { it.value.get() }.filterValues { it > 0 } + if (liveSnapshot.isEmpty()) return persisted + val today = epochDay() + val merged = persisted.toMutableMap() + val todayBucket = merged[today].orEmpty().toMutableMap() + liveSnapshot.forEach { (key, value) -> todayBucket[key] = (todayBucket[key] ?: 0L) + value } + merged[today] = todayBucket + return merged + } + + private fun drain(): Map { + if (live.isEmpty()) return emptyMap() + val deltas = mutableMapOf() + for ((key, counter) in live) { + val value = counter.getAndSet(0L) + if (value > 0) deltas[key] = value + } + return deltas + } + + companion object { + const val DAY_MS = 24L * 60L * 60L * 1000L + } +} diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/resourceusage/ResourceUsageAlerts.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/resourceusage/ResourceUsageAlerts.kt new file mode 100644 index 0000000000..b72cbdf173 --- /dev/null +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/resourceusage/ResourceUsageAlerts.kt @@ -0,0 +1,108 @@ +/* + * Copyright (c) 2025 Vitor Pamplona + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.vitorpamplona.amethyst.service.resourceusage + +/** + * Pure threshold logic for the "this app is consuming too much" prompt. + * Thresholds are deliberately conservative: the prompt should fire for the + * pathological cases (a stuck reconnect loop, process-restart churn, runaway + * background sync) — not for a heavy day of normal use. Tune them as real + * reports come in. + * + * Never auto-sends anything: a positive evaluation only ASKS the user, at + * most once every [MIN_DAYS_BETWEEN_PROMPTS] days, and respects a permanent + * opt-out. Both are persisted in [ResourceUsageStore]. + */ +object ResourceUsageAlerts { + enum class Reason { + BACKGROUND_MOBILE_DATA, + BACKGROUND_MOBILE_CONNECTION_TIME, + WAKELOCK_TIME, + PROCESS_CHURN, + RECONNECT_CHURN, + } + + data class Alert( + val reason: Reason, + val day: Long, + val value: Long, + ) + + /** > 50 MB of background traffic on cellular in one day. */ + const val BG_MOBILE_BYTES_PER_DAY = 50L * 1024L * 1024L + + /** > 12 relay-connection-hours while backgrounded on cellular in one day. */ + const val BG_MOBILE_RELAY_CONN_MS_PER_DAY = 12L * 60L * 60L * 1000L + + /** > 30 minutes of notification wakelock held in one day. */ + const val WAKELOCK_MS_PER_DAY = 30L * 60L * 1000L + + /** > 75 process starts in one day (WorkManager/restart churn). */ + const val APP_STARTS_PER_DAY = 75L + + /** + * > 5000 completed relay (re)connections in one day. A healthy day is a + * few hundred to ~2000 even with a large relay set; sustained thousands + * means something is cycling (a stuck relay tier, a flapping network, a + * Tor bootstrap loop) and every cycle pays a TLS handshake. + */ + const val RELAY_CONNECTS_PER_DAY = 5_000L + + const val MIN_DAYS_BETWEEN_PROMPTS = 7L + + /** + * Checks yesterday (the last complete day) first, then today (so a + * runaway condition surfaces without waiting for midnight). Returns the + * first threshold crossed or null. + */ + fun evaluate( + days: Map>, + today: Long, + ): Alert? { + for (day in longArrayOf(today - 1, today)) { + val counters = days[day] ?: continue + val summary = UsageSummary.from(counters) + + if (summary.mobileBytesBg > BG_MOBILE_BYTES_PER_DAY) { + return Alert(Reason.BACKGROUND_MOBILE_DATA, day, summary.mobileBytesBg) + } + if (summary.relayConnMsMobileBg > BG_MOBILE_RELAY_CONN_MS_PER_DAY) { + return Alert(Reason.BACKGROUND_MOBILE_CONNECTION_TIME, day, summary.relayConnMsMobileBg) + } + if (summary.wakelockMs > WAKELOCK_MS_PER_DAY) { + return Alert(Reason.WAKELOCK_TIME, day, summary.wakelockMs) + } + if (summary.appStarts > APP_STARTS_PER_DAY) { + return Alert(Reason.PROCESS_CHURN, day, summary.appStarts) + } + if (summary.relayConnects > RELAY_CONNECTS_PER_DAY) { + return Alert(Reason.RECONNECT_CHURN, day, summary.relayConnects) + } + } + return null + } + + fun shouldPrompt( + lastAlertAtSec: Long, + optOut: Boolean, + nowSec: Long, + ): Boolean = !optOut && nowSec - lastAlertAtSec >= MIN_DAYS_BETWEEN_PROMPTS * 24L * 60L * 60L +} diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/resourceusage/ResourceUsageReportAssembler.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/resourceusage/ResourceUsageReportAssembler.kt new file mode 100644 index 0000000000..e26eb98678 --- /dev/null +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/resourceusage/ResourceUsageReportAssembler.kt @@ -0,0 +1,150 @@ +/* + * Copyright (c) 2025 Vitor Pamplona + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.vitorpamplona.amethyst.service.resourceusage + +import android.os.Build +import com.vitorpamplona.amethyst.BuildConfig +import com.vitorpamplona.amethyst.MemorySnapshot +import java.util.Locale + +/** + * Assembles the Markdown resource-usage report the user can DM to the + * developers via NIP-17 — same shape as the crash ReportAssembler: a device + * header table, a human-readable summary, then the full per-day counter dump + * as the technical payload. Counters are sizes/durations/counts only; no + * URLs, relay names, or content. + */ +class ResourceUsageReportAssembler { + fun buildReport( + days: Map>, + today: Long, + memory: MemorySnapshot? = null, + ): String { + val sb = StringBuilder() + sb.append("Resource Usage Report: ") + sb.append(BuildConfig.VERSION_NAME) + sb.append("-") + sb.append(BuildConfig.FLAVOR.uppercase()) + sb.append("\n\n") + + sb.append("| Prop | Value |\n") + sb.append("| --- | --- |\n") + sb.append("| Manuf | ${Build.MANUFACTURER} |\n") + sb.append("| Model | ${Build.MODEL} |\n") + sb.append("| Android | ${Build.VERSION.RELEASE} |\n") + sb.append("| SDK Int | ${Build.VERSION.SDK_INT} |\n") + sb.append("\n") + + val todayCounters = days[today].orEmpty() + val weekCounters = (today - 6..today).mapNotNull { days[it] } + + sb.append("**Today**\n\n") + sb.append(summaryTable(UsageSummary.from(todayCounters))) + sb.append("\n**Last 7 days**\n\n") + sb.append(summaryTable(UsageSummary.fromDays(weekCounters))) + + if (memory != null) { + sb.append("\n**Memory right now**\n\n") + sb.append("| Metric | Value |\n") + sb.append("| --- | --- |\n") + sb.append("| Device class | ${memory.memoryClassMb} MB |\n") + sb.append("| App heap | ${memory.heapUsedMb} / ${memory.heapMaxMb} MB |\n") + sb.append("| Native heap | ${memory.nativeHeapUsedMb} MB |\n") + sb.append("| Image cache (RAM) | ${memory.imageCacheUsedMb} / ${memory.imageCacheMaxMb} MB |\n") + sb.append("| Image cache (disk) | ${memory.imageDiskUsedMb} / ${memory.imageDiskMaxMb} MB |\n") + sb.append("| Cached notes/users/addressables/chatrooms | ${memory.noteCount}/${memory.userCount}/${memory.addressableCount}/${memory.chatroomCount} |\n") + } + + sb.append("\nTechnical details (per epoch-day):\n") + sb.append("```\n") + days.toSortedMap().forEach { (day, counters) -> + sb.append("day $day (today=$today)\n") + counters.toSortedMap().forEach { (key, value) -> + sb.append(" $key = $value\n") + } + } + sb.append("```\n") + return sb.toString() + } + + private fun summaryTable(s: UsageSummary): String = + buildString { + append("| Metric | Value |\n") + append("| --- | --- |\n") + append("| Cellular data (background) | ${formatBytes(s.mobileBytesBg)} |\n") + append("| Cellular data (foreground) | ${formatBytes(s.mobileBytesFg)} |\n") + append("| Wi-Fi data | ${formatBytes(s.wifiBytesBg + s.wifiBytesFg)} |\n") + append("| Relay connection time | ${formatConnHours(s.relayConnMs)} |\n") + append("| ... while backgrounded on cellular | ${formatConnHours(s.relayConnMsMobileBg)} |\n") + append("| Notification wakelock | ${formatDurationMs(s.wakelockMs)} (${s.wakelockCount}x) |\n") + append("| Relay reconnections | ${s.relayConnects} (${s.relayConnectFails} failed) |\n") + append("| Web requests | ${s.httpRequests} (${s.radioBursts} radio bursts, ${formatDurationMs(s.httpActiveMs)} active) |\n") + append("| Media playback | ${formatDurationMs(s.mediaPlayMs)} |\n") + append("| PoW mining | ${formatDurationMs(s.powMs)} |\n") + append("| Tor uptime (in-app) | ${formatDurationMs(s.torMs)} |\n") + append("| Always-on service | ${formatDurationMs(s.alwaysOnMs)} (${s.alwaysOnStarts} starts) |\n") + append("| Calls / audio rooms | ${formatDurationMs(s.callMs)} / ${formatDurationMs(s.nestsMs)} |\n") + append("| Location listening | ${formatDurationMs(s.locationMs)} |\n") + append("| Signatures verified | ${s.verifyCount} (${formatDurationMs(s.verifyUs / 1_000)} CPU) |\n") + append("| Decryptions | ${s.decryptCount} (${formatDurationMs(s.decryptUs / 1_000)} CPU) |\n") + append("| Remote signatures | ${s.signNip46} NIP-46, ${s.signNip55} NIP-55 |\n") + append("| Battery drain (measured, whole device) | ${s.batteryDrainFg}% in app, ${s.batteryDrainBg}% background |\n") + append("| App CPU time | ${formatDurationMs(s.cpuMs)} |\n") + append("| Time in app | ${formatDurationMs(s.foregroundMs)} |\n") + append("| Background worker runs | ${s.workerRuns} |\n") + append("| App process starts | ${s.appStarts} |\n") + val subsystems = + s.bytesPerSubsystem.entries + .sortedByDescending { it.value } + .joinToString(", ") { "${it.key} ${formatBytes(it.value)}" } + if (subsystems.isNotEmpty()) { + append("| By subsystem | $subsystems |\n") + } + val screens = + s.screenTimeMs.entries + .sortedByDescending { it.value } + .take(8) + .joinToString(", ") { "${it.key} ${formatDurationMs(it.value)}" } + if (screens.isNotEmpty()) { + append("| Screen time | $screens |\n") + } + } + + companion object { + fun formatBytes(bytes: Long): String = + when { + bytes >= 1024L * 1024L * 1024L -> String.format(Locale.US, "%.2f GB", bytes / (1024.0 * 1024.0 * 1024.0)) + bytes >= 1024L * 1024L -> String.format(Locale.US, "%.1f MB", bytes / (1024.0 * 1024.0)) + bytes >= 1024L -> String.format(Locale.US, "%.1f KB", bytes / 1024.0) + else -> "$bytes B" + } + + /** Relay-connection time: Σ connections x time, so shown as "relay-hours". */ + fun formatConnHours(ms: Long): String = String.format(Locale.US, "%.1f relay-hours", ms / (1000.0 * 60.0 * 60.0)) + + fun formatDurationMs(ms: Long): String = + when { + ms >= 60L * 60L * 1000L -> String.format(Locale.US, "%.1f h", ms / (1000.0 * 60.0 * 60.0)) + ms >= 60L * 1000L -> String.format(Locale.US, "%.1f min", ms / (1000.0 * 60.0)) + else -> String.format(Locale.US, "%.1f s", ms / 1000.0) + } + } +} diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/resourceusage/ResourceUsageStore.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/resourceusage/ResourceUsageStore.kt new file mode 100644 index 0000000000..7e7824631b --- /dev/null +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/resourceusage/ResourceUsageStore.kt @@ -0,0 +1,154 @@ +/* + * Copyright (c) 2025 Vitor Pamplona + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.vitorpamplona.amethyst.service.resourceusage + +import com.fasterxml.jackson.databind.DeserializationFeature +import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper +import com.fasterxml.jackson.module.kotlin.readValue +import com.vitorpamplona.quartz.utils.Log +import kotlinx.coroutines.sync.Mutex +import kotlinx.coroutines.sync.withLock +import java.io.File + +/** + * Durable daily buckets for the resource-usage ledger, one JSON file in the + * app's private filesDir. Same persistence idiom as ScheduledPostStore: + * Jackson + Mutex + write-to-tmp-then-rename + version envelope. + * + * Day keys are UTC epoch-days (stringified for JSON). Buckets older than + * [keepDays] are pruned on every merge, so the file stays small (a few KB). + * Also carries the high-consumption alert state (last prompt time, opt-out) + * so the whole feature has exactly one file. + */ +class ResourceUsageStore( + private val storageFile: File, + private val keepDays: Long = 30, +) { + data class UsageFile( + val version: Int = 1, + val days: Map> = emptyMap(), + val lastAlertAtSec: Long = 0L, + val alertsOptOut: Boolean = false, + ) + + private val mapper = + jacksonObjectMapper() + .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) + + private val mutex = Mutex() + private var loaded = false + private var data = UsageFile() + + suspend fun mergeInto( + day: Long, + deltas: Map, + ) { + if (deltas.isEmpty()) return + mutex.withLock { + ensureLoaded() + val dayKey = day.toString() + val bucket = data.days[dayKey].orEmpty().toMutableMap() + deltas.forEach { (key, amount) -> bucket[key] = (bucket[key] ?: 0L) + amount } + val pruned = + data.days + .filterKeys { (it.toLongOrNull() ?: Long.MAX_VALUE) >= day - keepDays } + .toMutableMap() + pruned[dayKey] = bucket + data = data.copy(days = pruned) + persist() + } + } + + /** All persisted daily buckets, keyed by epoch-day. */ + suspend fun allDays(): Map> = + mutex.withLock { + ensureLoaded() + data.days.mapNotNull { (k, v) -> k.toLongOrNull()?.let { it to v } }.toMap() + } + + suspend fun lastAlertAtSec(): Long = + mutex.withLock { + ensureLoaded() + data.lastAlertAtSec + } + + suspend fun alertsOptOut(): Boolean = + mutex.withLock { + ensureLoaded() + data.alertsOptOut + } + + suspend fun markAlertPrompted(atSec: Long) = + mutex.withLock { + ensureLoaded() + data = data.copy(lastAlertAtSec = atSec) + persist() + } + + suspend fun setAlertsOptOut(optOut: Boolean) = + mutex.withLock { + ensureLoaded() + data = data.copy(alertsOptOut = optOut) + persist() + } + + private fun ensureLoaded() { + if (loaded) return + data = + try { + if (storageFile.exists() && storageFile.length() > 0) { + mapper.readValue(storageFile) + } else { + UsageFile() + } + } catch (e: Exception) { + Log.e(TAG, "Failed to load resource usage from $storageFile", e) + UsageFile() + } + loaded = true + } + + private fun persist() { + storageFile.parentFile?.mkdirs() + val tmp = File(storageFile.parentFile, storageFile.name + ".tmp") + try { + mapper.writeValue(tmp, data) + if (!tmp.renameTo(storageFile)) { + if (!storageFile.delete() || !tmp.renameTo(storageFile)) { + Log.e(TAG) { "Failed to rename $tmp to $storageFile" } + if (!tmp.delete()) { + Log.w(TAG) { "Failed to clean up temp file $tmp" } + } + } + } + } catch (e: Exception) { + Log.e(TAG, "Failed to persist resource usage to $storageFile", e) + if (!tmp.delete()) { + Log.w(TAG) { "Failed to clean up temp file $tmp" } + } + } + } + + companion object { + private const val TAG = "ResourceUsageStore" + const val FILE_NAME = "resource_usage.json" + } +} diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/resourceusage/ScreenTimeIntegrator.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/resourceusage/ScreenTimeIntegrator.kt new file mode 100644 index 0000000000..b995054ace --- /dev/null +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/resourceusage/ScreenTimeIntegrator.kt @@ -0,0 +1,85 @@ +/* + * Copyright (c) 2025 Vitor Pamplona + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.vitorpamplona.amethyst.service.resourceusage + +import android.os.SystemClock +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Job +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.combine +import kotlinx.coroutines.launch + +/** + * Integrates time-per-screen into the ledger (`screen..ms`): which + * parts of the app the display/CPU time actually goes to, so a report can + * distinguish "8 h of video" from "8 h of feeds". + * + * PRIVACY: only the route's base name is recorded ([screenNameOf] strips + * navigation arguments before anything reaches the ledger) — "Profile" is + * tracked, whose profile never is. Time only accrues while the app is in the + * foreground: the current-route × foreground combination is the segment + * state, so backgrounding on a screen closes its segment. + */ +class ScreenTimeIntegrator( + accountant: ResourceUsageAccountant, + nowMs: () -> Long = { SystemClock.elapsedRealtime() }, +) : TimeSegmentIntegrator(accountant, nowMs) { + private val currentScreen = MutableStateFlow(null) + + /** Called from the navigation listener with an already-sanitized name (or null when unknown). */ + fun onScreen(name: String?) { + currentScreen.value = name + } + + fun start( + scope: CoroutineScope, + isForeground: Flow, + ): Job { + registerFlushHook() + return scope.launch { + combine(currentScreen, isForeground) { screen, fg -> if (fg) screen else null } + .collect { transitionTo(it) } + } + } + + override fun account( + state: String, + elapsedMs: Long, + ) { + if (elapsedMs > 0) accountant.add(UsageKeys.screenMs(state), elapsedMs) + } + + companion object { + /** + * Reduces a Navigation Compose route pattern to its bare screen name: + * `com...routes.Route.Profile/{userId}?tab={tab}` becomes `Profile`. + * Everything after `/` or `?` — where the arguments live — is dropped + * BEFORE the value leaves the navigation layer. + */ + fun screenNameOf(route: String?): String? = + route + ?.substringBefore('/') + ?.substringBefore('?') + ?.substringAfterLast('.') + ?.takeIf { it.isNotBlank() } + } +} diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/resourceusage/TimeSegmentIntegrator.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/resourceusage/TimeSegmentIntegrator.kt new file mode 100644 index 0000000000..c70ae6032c --- /dev/null +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/resourceusage/TimeSegmentIntegrator.kt @@ -0,0 +1,106 @@ +/* + * Copyright (c) 2025 Vitor Pamplona + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.vitorpamplona.amethyst.service.resourceusage + +import android.os.SystemClock + +/** + * Timer-free duration integrator: the elapsed time of the current state is + * exact between transitions, so a segment is accounted only when the state + * changes — plus on the accountant's pre-flush hook, so multi-hour stable + * segments (a call, a backgrounded night with relays connected) still land in + * the right day bucket without any periodic timer. + * + * Durations are measured with [SystemClock.elapsedRealtime] — the monotonic + * clock — never the wall clock: an NTP/timezone correction mid-segment must + * not fabricate or delete accounted time. (Wall time is only ever used for + * choosing the epoch-day bucket, which happens in the accountant.) + * + * A `null` state means "nothing to account" (idle); subclasses decide what a + * non-null state costs per elapsed millisecond. + */ +abstract class TimeSegmentIntegrator( + protected val accountant: ResourceUsageAccountant, + private val nowMs: () -> Long = { SystemClock.elapsedRealtime() }, +) { + private val lock = Any() + private var current: S? = null + private var segmentStartMs = 0L + + /** Registers [closeOpenSegment] so flushes and reads see up-to-date totals. */ + fun registerFlushHook() { + accountant.addPreFlushHook(::closeOpenSegment) + } + + /** Accounts the running segment up to now without changing state. */ + fun closeOpenSegment() { + synchronized(lock) { + val state = current ?: return + val now = nowMs() + account(state, now - segmentStartMs) + segmentStartMs = now + } + } + + /** + * Accounts the previous segment and starts a new one. Returns the previous + * state so callers can detect activations (null -> non-null). + */ + fun transitionTo(next: S?): S? = + synchronized(lock) { + val now = nowMs() + val prev = current + prev?.let { account(it, now - segmentStartMs) } + current = next + segmentStartMs = now + prev + } + + protected abstract fun account( + state: S, + elapsedMs: Long, + ) +} + +/** + * The simplest integrator: total time a boolean condition is active (Tor + * running, a call in progress, GPS listening), written to [msKey] — plus an + * optional [startsKey] counting activations, since starts are often the + * expensive part (a Tor bootstrap, a service cold start). + */ +class SessionTimeIntegrator( + accountant: ResourceUsageAccountant, + private val msKey: String, + private val startsKey: String? = null, + nowMs: () -> Long = { SystemClock.elapsedRealtime() }, +) : TimeSegmentIntegrator(accountant, nowMs) { + fun setActive(active: Boolean) { + val prev = transitionTo(if (active) Unit else null) + if (active && prev == null && startsKey != null) accountant.add(startsKey, 1) + } + + override fun account( + state: Unit, + elapsedMs: Long, + ) { + if (elapsedMs > 0) accountant.add(msKey, elapsedMs) + } +} diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/resourceusage/UsageCountingInterceptor.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/resourceusage/UsageCountingInterceptor.kt new file mode 100644 index 0000000000..83e8cafe21 --- /dev/null +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/resourceusage/UsageCountingInterceptor.kt @@ -0,0 +1,223 @@ +/* + * Copyright (c) 2025 Vitor Pamplona + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.vitorpamplona.amethyst.service.resourceusage + +import android.os.SystemClock +import okhttp3.Interceptor +import okhttp3.OkHttpClient +import okhttp3.Response +import okhttp3.ResponseBody +import okio.Buffer +import okio.ForwardingSource +import okio.Source +import okio.buffer +import java.util.concurrent.ConcurrentHashMap +import java.util.concurrent.atomic.AtomicBoolean + +/** Request tag carrying the ledger subsystem a request belongs to. */ +class UsageRoleTag( + val role: String, +) + +/** + * Estimates cellular-radio wake-ups caused by HTTP traffic: a new burst is + * counted whenever bytes flow after more than [BURST_GAP_MS] of HTTP silence, + * approximating the radio having dropped to idle in between (LTE/5G inactivity + * timers are typically ~10s). Bytes alone don't predict battery — many small + * scattered requests cost far more than one continuous download of the same + * size, because every burst pays the radio's ramp + tail energy. This counter + * is what makes that pattern visible. + * + * Caveat: bursts are counted from HTTP activity only. While relays are + * connected their traffic keeps the radio awake anyway — that cost is already + * captured by the relay connection-time counters. + */ +class RadioBurstEstimator( + private val accountant: ResourceUsageAccountant, + private val isMobile: () -> Boolean, + private val isForeground: () -> Boolean, + private val nowMs: () -> Long = { SystemClock.elapsedRealtime() }, +) { + @Volatile private var lastActivityMs = Long.MIN_VALUE + + fun onHttpActivity() { + val now = nowMs() + val last = lastActivityMs + lastActivityMs = now + if (last == Long.MIN_VALUE || now - last > BURST_GAP_MS) { + accountant.add(UsageKeys.radioBursts(isMobile(), isForeground()), 1) + } + } + + companion object { + const val BURST_GAP_MS = 10_000L + } +} + +/** + * Application interceptor that accounts every HTTP request into the ledger: + * bytes up/down, request count, and active-transfer time, attributed to the + * subsystem named by the request's [UsageRoleTag] (set by the per-role + * clients from RoleBasedHttpClientBuilder) or to [defaultRole] for anything + * that reaches the shared clients untagged — so no HTTP traffic can escape + * the ledger. + * + * Installed FIRST on the base client (outermost), so the tagging interceptor + * of role-wrapped clients must be inserted BEFORE it (see [HttpUsageMeter]). + * Download bytes are counted as the app actually consumes the streamed body, + * so cancelled loads count only what crossed to the app. Network/visibility + * dims are sampled when bytes flow, matching what the radio actually did. + */ +class UsageCountingInterceptor( + private val accountant: ResourceUsageAccountant, + private val isMobile: () -> Boolean, + private val isForeground: () -> Boolean, + private val bursts: RadioBurstEstimator? = null, + private val defaultRole: String = UsageKeys.ROLE_OTHER, + private val nowMs: () -> Long = { SystemClock.elapsedRealtime() }, +) : Interceptor { + override fun intercept(chain: Interceptor.Chain): Response { + val request = chain.request() + + // Loopback traffic (LocalBlossomCacheRedirectInterceptor rewrites cache + // hits to 127.0.0.1) never touches the radio: counting it would inflate + // the network numbers — and could trip the background-data alert — for + // exactly the users who cache aggressively to SAVE data. + if (isLoopback(request.url.host)) return chain.proceed(request) + + val role = request.tag(UsageRoleTag::class.java)?.role ?: defaultRole + + accountant.add(UsageKeys.netReqs(role, isMobile(), isForeground()), 1) + bursts?.onHttpActivity() + + val requestBytes = request.body?.contentLength()?.coerceAtLeast(0L) ?: 0L + if (requestBytes > 0) { + accountant.add(UsageKeys.net(role, isMobile(), isForeground(), received = false), requestBytes) + } + + val startedAtMs = nowMs() + val response = chain.proceed(request) + return response + .newBuilder() + .body( + CountingResponseBody( + delegate = response.body, + onBytes = { bytes -> + accountant.add(UsageKeys.net(role, isMobile(), isForeground(), received = true), bytes) + bursts?.onHttpActivity() + }, + onFinished = { + accountant.add(UsageKeys.netActiveMs(role, isMobile(), isForeground()), nowMs() - startedAtMs) + }, + ), + ).build() + } + + companion object { + /** OkHttp reports IPv6 hosts unbracketed ("::1"); keep the bracketed form defensively. */ + fun isLoopback(host: String): Boolean = host.startsWith("127.") || host == "localhost" || host == "::1" || host == "[::1]" + } + + private class CountingResponseBody( + private val delegate: ResponseBody, + private val onBytes: (Long) -> Unit, + onFinished: () -> Unit, + ) : ResponseBody() { + private val finished = AtomicBoolean(false) + private val onFinishedOnce = { + if (finished.compareAndSet(false, true)) onFinished() + } + + override fun contentType() = delegate.contentType() + + override fun contentLength() = delegate.contentLength() + + private val countedSource by lazy { + object : ForwardingSource(delegate.source() as Source) { + override fun read( + sink: Buffer, + byteCount: Long, + ): Long { + val read = super.read(sink, byteCount) + if (read > 0) { + onBytes(read) + } else if (read == -1L) { + onFinishedOnce() + } + return read + } + + override fun close() { + super.close() + onFinishedOnce() + } + }.buffer() + } + + override fun source() = countedSource + } +} + +/** + * Hands out per-subsystem OkHttp clients: the shared base client (which + * carries the single [UsageCountingInterceptor]) wrapped with a tagging + * interceptor inserted at position 0, OUTSIDE the counter, so the tag is + * visible when the counter reads it. Wrapped clients are cached per + * (role, base identity); base clients are rebuilt on proxy/network changes, + * so the cache is cleared when it grows past a small bound. + */ +class HttpUsageMeter { + private val wrapped = ConcurrentHashMap, OkHttpClient>() + + fun counted( + role: String, + base: OkHttpClient, + ): OkHttpClient { + if (wrapped.size > MAX_CACHED) wrapped.clear() + return wrapped.getOrPut(role to base) { + base + .newBuilder() + .apply { interceptors().add(0, RoleTaggingInterceptor(role)) } + .build() + } + } + + private class RoleTaggingInterceptor( + private val role: String, + ) : Interceptor { + private val tag = UsageRoleTag(role) + + override fun intercept(chain: Interceptor.Chain): Response = + chain.proceed( + chain + .request() + .newBuilder() + .tag(UsageRoleTag::class.java, tag) + .build(), + ) + } + + companion object { + // roles (8) x live base clients (proxy on/off ~2) with headroom for + // network-change rebuilds before the clear kicks in. + private const val MAX_CACHED = 32 + } +} diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/resourceusage/UsageKeys.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/resourceusage/UsageKeys.kt new file mode 100644 index 0000000000..e43cde73a9 --- /dev/null +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/resourceusage/UsageKeys.kt @@ -0,0 +1,204 @@ +/* + * Copyright (c) 2025 Vitor Pamplona + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.vitorpamplona.amethyst.service.resourceusage + +/** + * Counter-key grammar for the resource-usage ledger. Keys are flat strings so + * the on-disk store is schema-free — adding a counter never needs a migration. + * + * Dimensions: + * - network: `mobile` (cellular/metered) vs `wifi` (everything else) + * - visibility: `fg` (an activity is started) vs `bg` + * - direction: `rx` (downloaded) vs `tx` (uploaded) + * + * Counters are sizes, durations, and counts only — never URLs, relay names, or + * content. See plans/2026-07-12-resource-usage-ledger.md. + */ +object UsageKeys { + const val MOBILE = "mobile" + const val WIFI = "wifi" + const val FG = "fg" + const val BG = "bg" + const val RX = "rx" + const val TX = "tx" + + /** HTTP subsystems, matching IRoleBasedHttpClientBuilder's roles. */ + const val ROLE_IMAGE = "image" + const val ROLE_VIDEO = "video" + const val ROLE_UPLOADS = "uploads" + const val ROLE_MONEY = "money" + const val ROLE_NIP05 = "nip05" + const val ROLE_PREVIEW = "preview" + const val ROLE_PUSH = "push" + + /** Catch-all for HTTP requests that reach the shared clients without a role tag. */ + const val ROLE_OTHER = "other" + + val HTTP_ROLES = listOf(ROLE_IMAGE, ROLE_VIDEO, ROLE_UPLOADS, ROLE_MONEY, ROLE_NIP05, ROLE_PREVIEW, ROLE_PUSH, ROLE_OTHER) + + /** `net.image.mobile.bg.rx` — HTTP bytes for a subsystem. */ + fun net( + role: String, + mobile: Boolean, + foreground: Boolean, + received: Boolean, + ): String = "net.$role.${dim(mobile, foreground)}.${if (received) RX else TX}" + + /** `net.image.mobile.bg.reqs` — HTTP request count for a subsystem. */ + fun netReqs( + role: String, + mobile: Boolean, + foreground: Boolean, + ): String = "net.$role.${dim(mobile, foreground)}.reqs" + + /** `net.image.mobile.bg.activems` — wall time spent actively transferring. */ + fun netActiveMs( + role: String, + mobile: Boolean, + foreground: Boolean, + ): String = "net.$role.${dim(mobile, foreground)}.activems" + + /** `net.bursts.mobile.bg` — estimated radio wake-ups caused by HTTP traffic. */ + fun radioBursts( + mobile: Boolean, + foreground: Boolean, + ): String = "net.bursts.${dim(mobile, foreground)}" + + /** `relay.msg.mobile.bg.rx` — approximate relay websocket payload bytes. */ + fun relayMsg( + mobile: Boolean, + foreground: Boolean, + received: Boolean, + ): String = "relay.msg.${dim(mobile, foreground)}.${if (received) RX else TX}" + + /** `relay.connms.mobile.bg` — Σ(open relay connections × elapsed ms). */ + fun relayConnMs( + mobile: Boolean, + foreground: Boolean, + ): String = "relay.connms.${dim(mobile, foreground)}" + + /** `relay.connects.mobile.bg` — completed relay (re)connections: each one paid a TCP+TLS handshake. */ + fun relayConnects( + mobile: Boolean, + foreground: Boolean, + ): String = "relay.connects.${dim(mobile, foreground)}" + + /** `relay.connfails.mobile.bg` — dials that failed before the websocket opened. */ + fun relayConnectFails( + mobile: Boolean, + foreground: Boolean, + ): String = "relay.connfails.${dim(mobile, foreground)}" + + /** `worker.scheduledPost.runs` */ + fun workerRuns(worker: String): String = "worker.$worker.runs" + + const val WAKELOCK_NOTIF_MS = "wakelock.notif.ms" + const val WAKELOCK_NOTIF_COUNT = "wakelock.notif.count" + const val APP_STARTS = "app.starts" + + /** Whole-process CPU time (user+system) — the honest aggregate of parsing, crypto, coroutines, and UI. */ + const val CPU_MS = "cpu.ms" + + /** Time with at least one activity STARTED — the denominator that makes the other counters interpretable. */ + const val APP_FG_MS = "app.fgms" + + /** Event signature verifications (LocalCache.justVerify). */ + const val VERIFY_COUNT = "crypto.verify.count" + const val VERIFY_US = "crypto.verify.us" + + /** Media (video/audio) playback time — decoder + screen + streaming all at once. */ + const val MEDIA_PLAY_MS = "media.playms" + + /** NIP-13 proof-of-work mining: full-core CPU for as long as it runs. */ + const val POW_MS = "pow.ms" + const val POW_SESSIONS = "pow.sessions" + + /** In-app (Arti) Tor: circuit crypto + directory/guard keep-alives while up; each start pays a bootstrap. */ + const val TOR_MS = "tor.ms" + const val TOR_STARTS = "tor.starts" + + /** + * Always-on notification relay service: uptime (the mode context for its + * relay connections) and starts — each start beyond the first is churn + * (watchdog alarm or auto-restart re-launching a killed service). + */ + const val ALWAYS_ON_MS = "service.alwayson.ms" + const val ALWAYS_ON_STARTS = "service.alwayson.starts" + + /** Calls and NIP-53 audio rooms: mic + Opus + a live media connection. */ + const val CALL_MS = "call.ms" + const val CALL_SESSIONS = "call.sessions" + const val NESTS_MS = "nests.ms" + const val NESTS_SESSIONS = "nests.sessions" + + /** Time spent actively listening for GPS/location updates (geohash tagging). */ + const val LOCATION_MS = "location.ms" + + /** + * `screen.Home.ms` — time a screen was visible while the app was in the + * foreground. PRIVACY: only the route's base NAME is ever recorded, never + * its navigation arguments — "Profile" is tracked, whose profile is not + * (see ScreenTimeIntegrator.screenNameOf, which strips them). + */ + fun screenMs(screen: String): String = "$SCREEN_PREFIX$screen.ms" + + const val SCREEN_PREFIX = "screen." + + /** + * NIP-04/44 decryptions and encryptions through account signers. Durations + * are only metered for local-key signers (CPU cost); external/remote + * signer waits are IPC/network, tracked by the sign/decrypt counts alone. + */ + const val DECRYPT_COUNT = "crypto.decrypt.count" + const val DECRYPT_US = "crypto.decrypt.us" + const val ENCRYPT_COUNT = "crypto.encrypt.count" + const val ENCRYPT_US = "crypto.encrypt.us" + + /** `sign.nip46.count` — signatures by signer kind: local key, NIP-55 (Amber IPC), NIP-46 (relay round-trip). */ + fun signs(kind: String): String = "sign.$kind.count" + + const val SIGNER_LOCAL = "local" + const val SIGNER_NIP46 = "nip46" + const val SIGNER_NIP55 = "nip55" + + /** + * Measured battery drain (percent points while discharging), split by + * visibility. Not app-isolated — it's the ground truth the other counters + * get correlated against across reports. + */ + const val BATTERY_DRAIN_FG = "battery.drain.fg" + const val BATTERY_DRAIN_BG = "battery.drain.bg" + + fun dim( + mobile: Boolean, + foreground: Boolean, + ): String = "${if (mobile) MOBILE else WIFI}.${if (foreground) FG else BG}" + + /** Sums every counter whose key matches all the given dot-delimited parts. */ + fun Map.sumMatching(vararg parts: String): Long { + var total = 0L + for ((key, value) in this) { + val segments = key.split('.') + if (parts.all { it in segments }) total += value + } + return total + } +} diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/resourceusage/UsageSummary.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/resourceusage/UsageSummary.kt new file mode 100644 index 0000000000..1fd24f085d --- /dev/null +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/resourceusage/UsageSummary.kt @@ -0,0 +1,166 @@ +/* + * Copyright (c) 2025 Vitor Pamplona + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.vitorpamplona.amethyst.service.resourceusage + +import com.vitorpamplona.amethyst.service.resourceusage.UsageKeys.sumMatching + +/** + * Headline metrics derived from one or more daily counter buckets. Shared by + * the usage screen, the NIP-17 report, and the high-consumption alerts so + * every surface agrees on the numbers. + */ +data class UsageSummary( + val mobileBytesBg: Long, + val mobileBytesFg: Long, + val wifiBytesBg: Long, + val wifiBytesFg: Long, + val relayConnMsMobileBg: Long, + val relayConnMsMobileFg: Long, + val relayConnMsWifiBg: Long, + val relayConnMsWifiFg: Long, + val wakelockMs: Long, + val wakelockCount: Long, + val workerRuns: Long, + val appStarts: Long, + val relayConnects: Long, + val relayConnectFails: Long, + val cpuMs: Long, + val foregroundMs: Long, + val verifyCount: Long, + val verifyUs: Long, + val httpRequests: Long, + val radioBursts: Long, + val httpActiveMs: Long, + val mediaPlayMs: Long, + val powMs: Long, + val torMs: Long, + val torStarts: Long, + val alwaysOnMs: Long, + val alwaysOnStarts: Long, + val callMs: Long, + val nestsMs: Long, + val locationMs: Long, + val decryptCount: Long, + val decryptUs: Long, + val signNip46: Long, + val signNip55: Long, + val batteryDrainFg: Long, + val batteryDrainBg: Long, + /** total rx+tx bytes per subsystem (net roles + "relay"). */ + val bytesPerSubsystem: Map, + /** cellular-only rx+tx bytes per subsystem — the scarce resource. */ + val mobileBytesPerSubsystem: Map, + /** foreground time per screen NAME (arguments are never recorded). */ + val screenTimeMs: Map, + /** how many day buckets this summary was built from (>= 1). */ + val dayCount: Int, +) { + val totalBytes: Long get() = mobileBytesBg + mobileBytesFg + wifiBytesBg + wifiBytesFg + val mobileBytes: Long get() = mobileBytesBg + mobileBytesFg + val relayConnMs: Long get() = relayConnMsMobileBg + relayConnMsMobileFg + relayConnMsWifiBg + relayConnMsWifiFg + + companion object { + fun from( + counters: Map, + dayCount: Int = 1, + ): UsageSummary { + fun traffic( + net: String, + vis: String, + ) = counters.sumMatching(net, vis, UsageKeys.RX) + counters.sumMatching(net, vis, UsageKeys.TX) + + val subsystems = mutableMapOf() + val mobileSubsystems = mutableMapOf() + for (role in UsageKeys.HTTP_ROLES) { + val bytes = counters.sumMatching(role, UsageKeys.RX) + counters.sumMatching(role, UsageKeys.TX) + if (bytes > 0) subsystems[role] = bytes + val mobileBytes = + counters.sumMatching(role, UsageKeys.MOBILE, UsageKeys.RX) + + counters.sumMatching(role, UsageKeys.MOBILE, UsageKeys.TX) + if (mobileBytes > 0) mobileSubsystems[role] = mobileBytes + } + val relayBytes = counters.sumMatching("msg", UsageKeys.RX) + counters.sumMatching("msg", UsageKeys.TX) + if (relayBytes > 0) subsystems["relay"] = relayBytes + val mobileRelayBytes = + counters.sumMatching("msg", UsageKeys.MOBILE, UsageKeys.RX) + + counters.sumMatching("msg", UsageKeys.MOBILE, UsageKeys.TX) + if (mobileRelayBytes > 0) mobileSubsystems["relay"] = mobileRelayBytes + + val screens = mutableMapOf() + for ((key, value) in counters) { + if (key.startsWith(UsageKeys.SCREEN_PREFIX) && key.endsWith(".ms") && value > 0) { + val name = key.removePrefix(UsageKeys.SCREEN_PREFIX).removeSuffix(".ms") + if (name.isNotBlank()) screens[name] = (screens[name] ?: 0L) + value + } + } + + return UsageSummary( + mobileBytesBg = traffic(UsageKeys.MOBILE, UsageKeys.BG), + mobileBytesFg = traffic(UsageKeys.MOBILE, UsageKeys.FG), + wifiBytesBg = traffic(UsageKeys.WIFI, UsageKeys.BG), + wifiBytesFg = traffic(UsageKeys.WIFI, UsageKeys.FG), + relayConnMsMobileBg = counters.sumMatching("connms", UsageKeys.MOBILE, UsageKeys.BG), + relayConnMsMobileFg = counters.sumMatching("connms", UsageKeys.MOBILE, UsageKeys.FG), + relayConnMsWifiBg = counters.sumMatching("connms", UsageKeys.WIFI, UsageKeys.BG), + relayConnMsWifiFg = counters.sumMatching("connms", UsageKeys.WIFI, UsageKeys.FG), + wakelockMs = counters[UsageKeys.WAKELOCK_NOTIF_MS] ?: 0L, + wakelockCount = counters[UsageKeys.WAKELOCK_NOTIF_COUNT] ?: 0L, + workerRuns = counters.sumMatching("worker", "runs"), + appStarts = counters[UsageKeys.APP_STARTS] ?: 0L, + relayConnects = counters.sumMatching("connects"), + relayConnectFails = counters.sumMatching("connfails"), + cpuMs = counters[UsageKeys.CPU_MS] ?: 0L, + foregroundMs = counters[UsageKeys.APP_FG_MS] ?: 0L, + verifyCount = counters[UsageKeys.VERIFY_COUNT] ?: 0L, + verifyUs = counters[UsageKeys.VERIFY_US] ?: 0L, + httpRequests = counters.sumMatching("reqs"), + radioBursts = counters.sumMatching("bursts"), + httpActiveMs = counters.sumMatching("activems"), + mediaPlayMs = counters[UsageKeys.MEDIA_PLAY_MS] ?: 0L, + powMs = counters[UsageKeys.POW_MS] ?: 0L, + torMs = counters[UsageKeys.TOR_MS] ?: 0L, + torStarts = counters[UsageKeys.TOR_STARTS] ?: 0L, + alwaysOnMs = counters[UsageKeys.ALWAYS_ON_MS] ?: 0L, + alwaysOnStarts = counters[UsageKeys.ALWAYS_ON_STARTS] ?: 0L, + callMs = counters[UsageKeys.CALL_MS] ?: 0L, + nestsMs = counters[UsageKeys.NESTS_MS] ?: 0L, + locationMs = counters[UsageKeys.LOCATION_MS] ?: 0L, + decryptCount = counters[UsageKeys.DECRYPT_COUNT] ?: 0L, + decryptUs = counters[UsageKeys.DECRYPT_US] ?: 0L, + signNip46 = counters[UsageKeys.signs(UsageKeys.SIGNER_NIP46)] ?: 0L, + signNip55 = counters[UsageKeys.signs(UsageKeys.SIGNER_NIP55)] ?: 0L, + batteryDrainFg = counters[UsageKeys.BATTERY_DRAIN_FG] ?: 0L, + batteryDrainBg = counters[UsageKeys.BATTERY_DRAIN_BG] ?: 0L, + bytesPerSubsystem = subsystems, + mobileBytesPerSubsystem = mobileSubsystems, + screenTimeMs = screens, + dayCount = dayCount.coerceAtLeast(1), + ) + } + + /** Merges several day buckets and summarizes the total. */ + fun fromDays(days: Collection>): UsageSummary { + val merged = mutableMapOf() + days.forEach { day -> day.forEach { (k, v) -> merged[k] = (merged[k] ?: 0L) + v } } + return from(merged, dayCount = days.size) + } + } +} diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/scheduledposts/ScheduledPostStore.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/scheduledposts/ScheduledPostStore.kt index d5898b181b..7d86510ed3 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/scheduledposts/ScheduledPostStore.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/scheduledposts/ScheduledPostStore.kt @@ -206,9 +206,28 @@ class ScheduledPostStore( mutableListOf() } loaded = true + val recovered = releaseStaleClaims() val purged = purgeStale(nowSec()) _flow.value = posts.toList() - if (purged) persist() + if (purged || recovered) persist() + } + + /** + * A PUBLISHING row found at initial disk load is a claim from a previous + * process — the worker that held it died (crash, cancellation) before + * markSent/markFailed/releaseClaim ran. No worker in THIS process can + * hold a claim before the first load, so reset them to PENDING for the + * next cycle to retry. Returns true if any row was recovered. + */ + private fun releaseStaleClaims(): Boolean { + var recovered = false + posts.forEachIndexed { idx, post -> + if (post.status == ScheduledPostStatus.PUBLISHING) { + posts[idx] = post.copy(status = ScheduledPostStatus.PENDING) + recovered = true + } + } + return recovered } /** diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/scheduledposts/ScheduledPostWorkGate.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/scheduledposts/ScheduledPostWorkGate.kt new file mode 100644 index 0000000000..dd0542050a --- /dev/null +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/scheduledposts/ScheduledPostWorkGate.kt @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2025 Vitor Pamplona + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.vitorpamplona.amethyst.service.scheduledposts + +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Job +import kotlinx.coroutines.flow.distinctUntilChanged +import kotlinx.coroutines.flow.map +import kotlinx.coroutines.launch + +/** + * Keeps [ScheduledPostWorker]'s 15-minute periodic chain enqueued exactly while + * the store holds a PENDING post. An unconditionally-scheduled periodic worker + * wakes — and often cold-starts — the whole process every 15 minutes forever, + * even for users who never schedule a post. + * + * The store is durable (JSON on disk) and the single source of truth, so + * [onPendingWork] fires from any mutation that produces a PENDING row (add, + * publishNow, releaseClaim) and [onNoPendingWork] when the last one drains + * (markSent/markFailed/cancel/removeForAccount). + * + * PUBLISHING counts as pending work: claimDuePosts flips PENDING → PUBLISHING + * (and emits) BEFORE the worker publishes, so gating on PENDING alone would + * cancel the periodic worker mid-publish the moment it claims the last post — + * stranding the post in PUBLISHING with nothing left to finish or retry it. + * + * [start] forces the store's initial disk load BEFORE collecting: the flow's + * initial value is an empty list until the store is first touched, and acting + * on that placeholder would cancel scheduled work that a pending post still + * needs. + */ +class ScheduledPostWorkGate( + private val store: ScheduledPostStore, + private val scope: CoroutineScope, + private val onPendingWork: () -> Unit, + private val onNoPendingWork: () -> Unit, +) { + fun start(): Job = + scope.launch { + store.list() + store.flow + .map { posts -> posts.any { it.status == ScheduledPostStatus.PENDING || it.status == ScheduledPostStatus.PUBLISHING } } + .distinctUntilChanged() + .collect { hasPending -> + if (hasPending) { + onPendingWork() + } else { + onNoPendingWork() + } + } + } +} diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/scheduledposts/ScheduledPostWorker.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/scheduledposts/ScheduledPostWorker.kt index b9b339332f..40d3e4fae8 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/scheduledposts/ScheduledPostWorker.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/scheduledposts/ScheduledPostWorker.kt @@ -31,6 +31,7 @@ import androidx.work.PeriodicWorkRequestBuilder import androidx.work.WorkManager import androidx.work.WorkerParameters import com.vitorpamplona.amethyst.Amethyst +import com.vitorpamplona.amethyst.service.resourceusage.UsageKeys import com.vitorpamplona.quartz.nip01Core.core.Event import com.vitorpamplona.quartz.nip01Core.relay.client.INostrClient import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl @@ -42,10 +43,15 @@ import java.util.concurrent.TimeUnit /** * Scans the scheduled-post store and publishes posts whose publish time has arrived. * - * - schedule(context): periodic, every 15 min (WorkManager minimum). - * - scheduleCatchUp(context): one-time, on app start, to flush posts that came - * due while the device was off or while WorkManager - * was deferred by Doze. + * - schedule(context): periodic, every 15 min (WorkManager minimum). Only + * enqueued while the store holds a PENDING post — the + * store observer in AppModules schedules it when a + * pending post appears and cancels it when the last + * one drains, so the worker never wakes the process + * with nothing to publish. + * - scheduleCatchUp(context): one-time, to flush posts that came due while the + * device was off or while WorkManager was deferred + * by Doze. */ class ScheduledPostWorker( appContext: Context, @@ -107,6 +113,16 @@ class ScheduledPostWorker( Log.d(TAG) { "scheduleCatchUp(): enqueueUniqueWork($WORK_NAME_CATCH_UP, KEEP)" } } + /** + * Ends the 15-minute periodic chain (keeps any catch-up run). Called by the + * store observer in AppModules when the last PENDING post drains, so the + * worker doesn't keep waking the process with nothing to publish. + */ + fun cancelPeriodic(context: Context) { + WorkManager.getInstance(context).cancelUniqueWork(WORK_NAME) + Log.d(TAG) { "cancelPeriodic(): cancelled periodic worker" } + } + fun cancel(context: Context) { WorkManager.getInstance(context).cancelUniqueWork(WORK_NAME) WorkManager.getInstance(context).cancelUniqueWork(WORK_NAME_CATCH_UP) @@ -117,6 +133,7 @@ class ScheduledPostWorker( override suspend fun doWork(): Result { val nowSec = System.currentTimeMillis() / 1000 Log.d(TAG) { "doWork() ENTER nowSec=$nowSec runAttempt=$runAttemptCount tags=$tags" } + runCatching { Amethyst.instance.resourceUsage.add(UsageKeys.workerRuns("scheduledPost"), 1) } return try { val appModules = Amethyst.instance diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/components/ClickableRoute.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/components/ClickableRoute.kt index e87ede251a..4423e58a9c 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/components/ClickableRoute.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/components/ClickableRoute.kt @@ -60,6 +60,7 @@ import com.vitorpamplona.amethyst.model.Note import com.vitorpamplona.amethyst.model.User import com.vitorpamplona.amethyst.service.relayClient.reqCommand.event.observeNote import com.vitorpamplona.amethyst.service.relayClient.reqCommand.user.observeUserInfo +import com.vitorpamplona.amethyst.service.relayClient.reqCommand.user.observeUserNickname import com.vitorpamplona.amethyst.ui.navigation.navs.INav import com.vitorpamplona.amethyst.ui.navigation.routes.Route import com.vitorpamplona.amethyst.ui.navigation.routes.routeFor @@ -298,14 +299,16 @@ fun RenderUserAsClickableText( nav: INav, ) { val userState by observeUserInfo(baseUser, accountViewModel) + val nickname by observeUserNickname(baseUser, accountViewModel) + val petName = nickname?.petName CreateClickableTextWithEmoji( - clickablePart = "@" + (userState?.info?.bestName() ?: baseUser.pubkeyDisplayHex()), + clickablePart = "@" + (petName ?: userState?.info?.bestName() ?: baseUser.pubkeyDisplayHex()), suffix = additionalChars?.ifBlank { null }, maxLines = 1, route = remember(baseUser) { routeFor(baseUser) }, nav = nav, - tags = userState?.tags ?: EmptyTagList, + tags = (if (petName != null) nickname?.tags else userState?.tags) ?: EmptyTagList, ) } diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/components/RichTextViewer.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/components/RichTextViewer.kt index 183a5395ea..54e13bb3d9 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/components/RichTextViewer.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/components/RichTextViewer.kt @@ -105,6 +105,7 @@ import com.vitorpamplona.amethyst.model.checkForHashtagWithIcon import com.vitorpamplona.amethyst.service.CachedRichTextParser import com.vitorpamplona.amethyst.service.relayClient.reqCommand.user.UserFinderFilterAssemblerSubscription import com.vitorpamplona.amethyst.service.relayClient.reqCommand.user.observeUserInfo +import com.vitorpamplona.amethyst.service.relayClient.reqCommand.user.observeUserNickname import com.vitorpamplona.amethyst.service.uploads.blossom.bud10.openBlossomUriAsIntent import com.vitorpamplona.amethyst.ui.actions.CrossfadeIfEnabled import com.vitorpamplona.amethyst.ui.components.markdown.RenderContentAsMarkdown @@ -1010,15 +1011,17 @@ private fun DisplayUserFromTag( nav: INav, ) { val meta by observeUserInfo(baseUser, accountViewModel) + val nickname by observeUserNickname(baseUser, accountViewModel) + val petName = nickname?.petName CrossfadeIfEnabled(targetState = meta, label = "DisplayUserFromTag", accountViewModel = accountViewModel) { Row { CreateClickableTextWithEmoji( - clickablePart = remember(meta) { it?.info?.bestName() ?: baseUser.pubkeyDisplayHex() }, + clickablePart = remember(meta, petName) { petName ?: it?.info?.bestName() ?: baseUser.pubkeyDisplayHex() }, maxLines = 1, route = remember(baseUser) { routeFor(baseUser) }, nav = nav, - tags = it?.tags, + tags = if (petName != null) nickname?.tags else it?.tags, ) } } diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/navigation/AppNavigation.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/navigation/AppNavigation.kt index 0be3941fad..d4866a71b0 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/navigation/AppNavigation.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/navigation/AppNavigation.kt @@ -42,12 +42,16 @@ import androidx.compose.ui.platform.LocalContext import androidx.core.content.IntentCompat import androidx.core.util.Consumer import androidx.lifecycle.compose.collectAsStateWithLifecycle +import androidx.navigation.NavController import androidx.navigation.compose.NavHost import androidx.navigation.compose.composable +import com.vitorpamplona.amethyst.Amethyst import com.vitorpamplona.amethyst.R import com.vitorpamplona.amethyst.commons.nipACWebRtcCalls.CallState import com.vitorpamplona.amethyst.service.crashreports.DisplayCrashMessages import com.vitorpamplona.amethyst.service.relayClient.notifyCommand.compose.DisplayNotifyMessages +import com.vitorpamplona.amethyst.service.resourceusage.DisplayResourceUsageAlert +import com.vitorpamplona.amethyst.service.resourceusage.ScreenTimeIntegrator import com.vitorpamplona.amethyst.ui.actions.NewUserMetadataScreen import com.vitorpamplona.amethyst.ui.actions.mediaServers.AllMediaServersScreen import com.vitorpamplona.amethyst.ui.actions.paymentTargets.PaymentTargetsScreen @@ -228,6 +232,7 @@ import com.vitorpamplona.amethyst.ui.screen.loggedIn.settings.NotificationSettin import com.vitorpamplona.amethyst.ui.screen.loggedIn.settings.OtsSettingsScreen import com.vitorpamplona.amethyst.ui.screen.loggedIn.settings.ProfileUiSettingsScreen import com.vitorpamplona.amethyst.ui.screen.loggedIn.settings.ReactionsSettingsScreen +import com.vitorpamplona.amethyst.ui.screen.loggedIn.settings.ResourceUsageScreen import com.vitorpamplona.amethyst.ui.screen.loggedIn.settings.SecurityFiltersScreen import com.vitorpamplona.amethyst.ui.screen.loggedIn.settings.SettingsScreen import com.vitorpamplona.amethyst.ui.screen.loggedIn.settings.SpammingUsersScreen @@ -301,11 +306,13 @@ fun AppNavigation( } } + TrackScreenTime(nav) NavigateIfIntentRequested(nav, accountViewModel, accountSessionManager) DisplayErrorMessages(accountViewModel.toastManager, accountViewModel, nav) DisplayNotifyMessages(accountViewModel, nav) DisplayCrashMessages(accountViewModel, nav) + DisplayResourceUsageAlert(accountViewModel, nav) DisplayBroadcastProgress(accountViewModel) ObserveIncomingCalls(accountViewModel) @@ -324,6 +331,27 @@ private fun ObserveIncomingCalls(accountViewModel: AccountViewModel) { } } +/** + * Feeds the resource-usage ledger with time-per-screen. Only the route's + * base name crosses this boundary — [ScreenTimeIntegrator.screenNameOf] + * strips every navigation argument first, so the ledger can say "Profile" + * but never which profile. + */ +@Composable +private fun TrackScreenTime(nav: Nav) { + DisposableEffect(nav.controller) { + val listener = + NavController.OnDestinationChangedListener { _, destination, _ -> + Amethyst.instance.screenTime.onScreen(ScreenTimeIntegrator.screenNameOf(destination.route)) + } + nav.controller.addOnDestinationChangedListener(listener) + onDispose { + nav.controller.removeOnDestinationChangedListener(listener) + Amethyst.instance.screenTime.onScreen(null) + } + } +} + @Composable fun BuildNavigation( accountViewModel: AccountViewModel, @@ -481,6 +509,7 @@ fun BuildNavigation( composableFromEnd { VideoPlayerSettingsScreen(accountViewModel, nav) } composableFromEnd { CallSettingsScreen(accountViewModel, nav) } composableFromEnd { NotificationSettingsScreen(accountViewModel, nav) } + composableFromEnd { ResourceUsageScreen(accountViewModel, nav) } composableFromEnd { ImportFollowListSelectUserScreen(accountViewModel, nav) } composableFromEndArgs { ImportFollowListPickFollowsScreen(it.userHex, accountViewModel, nav) diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/navigation/drawer/DrawerContent.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/navigation/drawer/DrawerContent.kt index a4ba397054..56136b06f9 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/navigation/drawer/DrawerContent.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/navigation/drawer/DrawerContent.kt @@ -237,14 +237,14 @@ fun ProfileContentTemplate( AsyncImage( model = profileBanner, contentDescription = stringRes(id = R.string.profile_image), - contentScale = ContentScale.FillWidth, + contentScale = ContentScale.Crop, modifier = bannerModifier, ) } else { AsyncImage( model = R.drawable.profile_banner, contentDescription = stringResource(R.string.profile_banner), - contentScale = ContentScale.FillWidth, + contentScale = ContentScale.Crop, modifier = bannerModifier, ) } diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/navigation/routes/Routes.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/navigation/routes/Routes.kt index e16b15383d..15be5ed14d 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/navigation/routes/Routes.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/navigation/routes/Routes.kt @@ -422,6 +422,8 @@ sealed class Route { @Serializable object CalendarReminderSettings : Route() + @Serializable object ResourceUsage : Route() + @Serializable object Lists : Route() @Serializable data class MyPeopleListView( diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/navigation/topbars/MemoryUsageChip.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/navigation/topbars/MemoryUsageChip.kt deleted file mode 100644 index 85a205c84e..0000000000 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/navigation/topbars/MemoryUsageChip.kt +++ /dev/null @@ -1,116 +0,0 @@ -/* - * Copyright (c) 2025 Vitor Pamplona - * - * Permission is hereby granted, free of charge, to any person obtaining a copy of - * this software and associated documentation files (the "Software"), to deal in - * the Software without restriction, including without limitation the rights to use, - * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the - * Software, and to permit persons to whom the Software is furnished to do so, - * subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS - * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR - * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN - * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION - * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ -package com.vitorpamplona.amethyst.ui.navigation.topbars - -import androidx.compose.foundation.clickable -import androidx.compose.foundation.layout.Arrangement -import androidx.compose.foundation.layout.Column -import androidx.compose.foundation.layout.padding -import androidx.compose.material3.AlertDialog -import androidx.compose.material3.MaterialTheme -import androidx.compose.material3.Text -import androidx.compose.material3.TextButton -import androidx.compose.runtime.Composable -import androidx.compose.runtime.getValue -import androidx.compose.runtime.mutableStateOf -import androidx.compose.runtime.produceState -import androidx.compose.runtime.remember -import androidx.compose.runtime.setValue -import androidx.compose.ui.Modifier -import androidx.compose.ui.graphics.Color -import androidx.compose.ui.platform.LocalContext -import androidx.compose.ui.unit.dp -import com.vitorpamplona.amethyst.MemorySnapshot -import com.vitorpamplona.amethyst.collectMemorySnapshot -import com.vitorpamplona.amethyst.isDebug -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.delay -import kotlinx.coroutines.withContext - -@Composable -fun MemoryUsageChip() { - if (!isDebug) return - - val context = LocalContext.current - var showDetail by remember { mutableStateOf(false) } - - val snapshot by produceState(null) { - while (true) { - // collectMemorySnapshot reads coil3.disk.DiskLruCache.size(), which is @Synchronized and - // contends with the disk cache's own journal I/O. On cold start that lock is held by a - // background worker for seconds (initial journal read + the burst of image writes), so - // running this on the produceState default (main) dispatcher froze the UI thread — - // the "Loading account" frame couldn't repaint until size() returned. Collect off-main. - value = withContext(Dispatchers.IO) { collectMemorySnapshot(context) } - delay(2_000) - } - } - - val s = snapshot ?: return - - val chipColor = - when { - s.heapFraction > 0.80f -> Color(0xFFE53935) // red - s.heapFraction > 0.60f -> Color(0xFFFFA000) // amber - else -> Color(0xFF43A047) // green - } - - Text( - text = "${s.heapUsedMb}/${s.heapMaxMb}MB", - color = chipColor, - style = MaterialTheme.typography.labelSmall, - modifier = - Modifier - .padding(horizontal = 4.dp) - .clickable { showDetail = true }, - ) - - if (showDetail) { - MemoryDetailDialog(snapshot = s, onDismiss = { showDetail = false }) - } -} - -@Composable -private fun MemoryDetailDialog( - snapshot: MemorySnapshot, - onDismiss: () -> Unit, -) { - AlertDialog( - onDismissRequest = onDismiss, - title = { Text("Memory Usage") }, - text = { - Column(verticalArrangement = Arrangement.spacedBy(4.dp)) { - Text("Device class: ${snapshot.memoryClassMb} MB") - Text("JVM heap: ${snapshot.heapUsedMb} / ${snapshot.heapMaxMb} MB") - Text("Native heap: ${snapshot.nativeHeapUsedMb} MB") - Text("Coil memory: ${snapshot.imageCacheUsedMb} / ${snapshot.imageCacheMaxMb} MB") - Text("Coil disk: ${snapshot.imageDiskUsedMb} / ${snapshot.imageDiskMaxMb} MB") - Text("Notes: ${snapshot.noteCount}") - Text("Users: ${snapshot.userCount}") - Text("Addressables: ${snapshot.addressableCount}") - Text("Chatrooms: ${snapshot.chatroomCount}") - } - }, - confirmButton = { - TextButton(onClick = onDismiss) { Text("OK") } - }, - ) -} diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/navigation/topbars/UserDrawerSearchTopBar.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/navigation/topbars/UserDrawerSearchTopBar.kt index 5ab67c459f..efdcfff438 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/navigation/topbars/UserDrawerSearchTopBar.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/navigation/topbars/UserDrawerSearchTopBar.kt @@ -75,7 +75,6 @@ fun UserDrawerSearchTopBar( } }, actions = { - MemoryUsageChip() IconButton(onClick = { nav.nav(Route.Search) }) { SearchIcon(modifier = Size22Modifier, MaterialTheme.colorScheme.placeholderText) } diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/note/UsernameDisplay.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/note/UsernameDisplay.kt index e7f64cd167..f8fdd1f072 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/note/UsernameDisplay.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/note/UsernameDisplay.kt @@ -40,6 +40,7 @@ import com.vitorpamplona.amethyst.model.Note import com.vitorpamplona.amethyst.model.User import com.vitorpamplona.amethyst.service.relayClient.reqCommand.event.observeNote import com.vitorpamplona.amethyst.service.relayClient.reqCommand.user.observeUserInfo +import com.vitorpamplona.amethyst.service.relayClient.reqCommand.user.observeUserNickname import com.vitorpamplona.amethyst.service.tts.TextToSpeechHelper import com.vitorpamplona.amethyst.ui.actions.CrossfadeIfEnabled import com.vitorpamplona.amethyst.ui.components.CreateTextWithEmoji @@ -105,11 +106,15 @@ fun UsernameDisplay( accountViewModel: AccountViewModel, ) { val userMetadata by observeUserInfo(baseUser, accountViewModel) + val nickname by observeUserNickname(baseUser, accountViewModel) CrossfadeIfEnabled(targetState = userMetadata, modifier = weight, label = "UsernameDisplay", accountViewModel = accountViewModel) { - val name = it?.info?.bestName() + // the account's own nickname for this user wins over the user's metadata; + // its custom emojis resolve against the contact card's tags, not the profile's + val petName = nickname?.petName + val name = petName ?: it?.info?.bestName() if (name != null) { - UserDisplay(name, it.tags, weight, fontWeight, textColor, textAlign) + UserDisplay(name, if (petName != null) nickname?.tags else it?.tags, weight, fontWeight, textColor, textAlign) } else { NPubDisplay(baseUser, weight, fontWeight, textColor, textAlign) } diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/note/creators/notify/Notifying.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/note/creators/notify/Notifying.kt index 64f1d09337..b7c1ca7a31 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/note/creators/notify/Notifying.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/note/creators/notify/Notifying.kt @@ -36,6 +36,7 @@ import androidx.compose.runtime.Composable import androidx.compose.runtime.CompositionLocalProvider import androidx.compose.ui.Alignment.Companion.CenterVertically import androidx.compose.ui.Modifier +import androidx.compose.ui.draw.alpha import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.unit.Dp import androidx.compose.ui.unit.dp @@ -49,7 +50,10 @@ import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel import com.vitorpamplona.amethyst.ui.stringRes import com.vitorpamplona.amethyst.ui.theme.Size24dp import com.vitorpamplona.amethyst.ui.theme.placeholderText +import com.vitorpamplona.quartz.nip01Core.core.HexKey import kotlinx.collections.immutable.ImmutableList +import kotlinx.collections.immutable.ImmutableSet +import kotlinx.collections.immutable.persistentSetOf @OptIn(ExperimentalLayoutApi::class) @Composable @@ -58,8 +62,9 @@ fun Notifying( accountViewModel: AccountViewModel, label: String? = null, showWhenEmpty: Boolean = false, + mutedNotifies: ImmutableSet = persistentSetOf(), onAddUser: (() -> Unit)? = null, - onClick: (User) -> Unit, + onToggleNotify: (User) -> Unit, ) { val mentions = baseMentions?.toSet() @@ -83,7 +88,7 @@ fun Notifying( // spacing matches the horizontal spacing between chips. CompositionLocalProvider(LocalMinimumInteractiveComponentSize provides Dp.Unspecified) { mentions?.forEach { user -> - NotifyUserChip(user, accountViewModel) { onClick(user) } + NotifyUserChip(user, user.pubkeyHex in mutedNotifies, accountViewModel) { onToggleNotify(user) } } if (onAddUser != null) { @@ -97,12 +102,16 @@ fun Notifying( @Composable private fun NotifyUserChip( user: User, + isMuted: Boolean, accountViewModel: AccountViewModel, - onRemove: () -> Unit, + onToggleNotify: () -> Unit, ) { InputChip( selected = false, - onClick = onRemove, + onClick = onToggleNotify, + // The bell-off icon alone is easy to miss at chip size, so a muted member + // also fades as a second cue while staying in the list for easy re-adding. + modifier = if (isMuted) Modifier.alpha(0.4f) else Modifier, label = { UsernameDisplay( user, @@ -119,8 +128,8 @@ private fun NotifyUserChip( }, trailingIcon = { Icon( - symbol = MaterialSymbols.Close, - contentDescription = stringRes(R.string.notify_remove_user), + symbol = if (isMuted) MaterialSymbols.NotificationsOff else MaterialSymbols.Notifications, + contentDescription = stringRes(if (isMuted) R.string.notify_unmute_user else R.string.notify_mute_user), modifier = Modifier.size(InputChipDefaults.IconSize), ) }, diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/note/nip22Comments/CommentPostViewModel.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/note/nip22Comments/CommentPostViewModel.kt index 3c6b6d9b79..79bc61906c 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/note/nip22Comments/CommentPostViewModel.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/note/nip22Comments/CommentPostViewModel.kt @@ -34,6 +34,7 @@ import androidx.lifecycle.viewModelScope import com.vitorpamplona.amethyst.Amethyst import com.vitorpamplona.amethyst.R import com.vitorpamplona.amethyst.commons.model.nip30CustomEmojis.EmojiPackState +import com.vitorpamplona.amethyst.commons.model.nip30CustomEmojis.EmojiSuggestionState import com.vitorpamplona.amethyst.commons.service.pow.PoWReplay import com.vitorpamplona.amethyst.commons.ui.text.appendSignature import com.vitorpamplona.amethyst.commons.ui.text.currentWord @@ -56,7 +57,6 @@ import com.vitorpamplona.amethyst.ui.actions.uploads.MediaUploadTracker import com.vitorpamplona.amethyst.ui.actions.uploads.SelectedMedia import com.vitorpamplona.amethyst.ui.actions.uploads.SelectedMediaProcessing import com.vitorpamplona.amethyst.ui.note.creators.draftTags.DraftTagState -import com.vitorpamplona.amethyst.ui.note.creators.emojiSuggestions.EmojiSuggestionState import com.vitorpamplona.amethyst.ui.note.creators.expiration.IExpiration import com.vitorpamplona.amethyst.ui.note.creators.location.ILocationGrabber import com.vitorpamplona.amethyst.ui.note.creators.messagefield.IMessageField @@ -74,6 +74,7 @@ import com.vitorpamplona.quartz.experimental.nip95.data.FileStorageEvent import com.vitorpamplona.quartz.experimental.nip95.header.FileStorageHeaderEvent import com.vitorpamplona.quartz.nip01Core.core.AddressableEvent import com.vitorpamplona.quartz.nip01Core.core.Event +import com.vitorpamplona.quartz.nip01Core.core.HexKey import com.vitorpamplona.quartz.nip01Core.crypto.KeyPair import com.vitorpamplona.quartz.nip01Core.relay.filters.Filter import com.vitorpamplona.quartz.nip01Core.signers.EventTemplate @@ -96,8 +97,6 @@ import com.vitorpamplona.quartz.nip22Comments.notify import com.vitorpamplona.quartz.nip29RelayGroups.groupId import com.vitorpamplona.quartz.nip29RelayGroups.hTag import com.vitorpamplona.quartz.nip29RelayGroups.isGroupScoped -import com.vitorpamplona.quartz.nip30CustomEmoji.CustomEmoji -import com.vitorpamplona.quartz.nip30CustomEmoji.EmojiUrlTag import com.vitorpamplona.quartz.nip30CustomEmoji.emojis import com.vitorpamplona.quartz.nip36SensitiveContent.contentWarning import com.vitorpamplona.quartz.nip36SensitiveContent.contentWarningReason @@ -177,6 +176,21 @@ open class CommentPostViewModel : var notifying by mutableStateOf?>(null) + // Members of the notifying list whose bell is off: they keep their chip + // (so they are one tap away from being added back) but are dropped from + // the extra notification p tags of the outgoing comment. + var mutedNotifies by mutableStateOf>(emptySet()) + + fun toggleNotify(user: User) { + mutedNotifies = + if (user.pubkeyHex in mutedNotifies) { + mutedNotifies - user.pubkeyHex + } else { + mutedNotifies + user.pubkeyHex + } + draftTag.newVersion() + } + // NIP-9B: latest community rules document for the community we're posting into. // Null when the reply target is not a community, or no rules have been observed yet. var communityRules: CommunityRulesEvent? by mutableStateOf(null) @@ -275,7 +289,7 @@ open class CommentPostViewModel : this.userSuggestions = UserSuggestionState(accountVM.account, accountVM.nip05ClientBuilder()) this.emojiSuggestions?.reset() - this.emojiSuggestions = EmojiSuggestionState(accountVM.account) + this.emojiSuggestions = EmojiSuggestionState(accountVM.account.emoji) } fun newPostFor(externalIdentity: ExternalId) { @@ -303,6 +317,7 @@ open class CommentPostViewModel : open fun reply(post: Note) { this.replyingTo = post this.externalIdentity = (post.event as? CommentEvent)?.scope() + mutedNotifies = emptySet() (post.event as? LnZapEvent)?.let { zap -> notifying = listOfNotNull(zapSenderToNotify(zap)) } @@ -498,14 +513,16 @@ open class CommentPostViewModel : notifying = draftEvent.rootAuthorKeys().mapNotNull { LocalCache.checkGetOrCreateUser(it) } + draftEvent.replyAuthorKeys().mapNotNull { LocalCache.checkGetOrCreateUser(it) } + mutedNotifies = emptySet() // Replies to zaps notify the zap sender through a plain p tag (the receipt's - // author keys above are the lightning provider). Restore the sender chip only - // if the draft still tags them — its absence means the user removed it. + // author keys above are the lightning provider). The sender chip always comes + // back; a missing p tag in the draft means the user muted their bell. (replyingTo?.event as? LnZapEvent)?.let { zap -> zapSenderToNotify(zap)?.let { sender -> - if (draftEvent.tags.mapNotNull(PTag::parseKey).contains(sender.pubkeyHex)) { - notifying = (notifying ?: emptyList()) + sender + notifying = ((notifying ?: emptyList()) + sender).distinct() + if (!draftEvent.tags.mapNotNull(PTag::parseKey).contains(sender.pubkeyHex)) { + mutedNotifies = mutedNotifies + sender.pubkeyHex } } } @@ -626,7 +643,7 @@ open class CommentPostViewModel : val geoHash = (location?.value as? LocationState.LocationResult.Success)?.geoHash?.toString() - val emojis = findEmoji(tagger.message, account.emoji.myEmojis.value) + val emojis = account.emoji.findEmojiTags(tagger.message) val urls = findURLs(tagger.message) val usedAttachments = iMetaAttachments.filterIsIn(urls.toSet()) @@ -665,9 +682,9 @@ open class CommentPostViewModel : } } else if (replyingToEvent is LnZapEvent) { val sender = zapSenderToNotify(replyingToEvent) - // notifying starts with the sender; a missing entry means the - // user removed the chip, so respect that and don't tag them. - if (sender != null && notifying?.contains(sender) != false) { + // The sender's chip stays in the list; a muted bell means + // the user doesn't want to ping them, so don't tag them. + if (sender != null && sender.pubkeyHex !in mutedNotifies) { listOf(sender.toPTag()) } else { emptyList() @@ -717,21 +734,6 @@ open class CommentPostViewModel : return template } - fun findEmoji( - message: String, - myEmojiSet: List?, - ): List { - if (myEmojiSet == null) return emptyList() - return CustomEmoji.findAllEmojiCodes(message).mapNotNull { possibleEmoji -> - myEmojiSet.firstOrNull { it.code == possibleEmoji }?.let { - EmojiUrlTag( - it.code, - it.link, - ) - } - } - } - fun upload( alt: String?, contentWarningReason: String?, @@ -855,6 +857,7 @@ open class CommentPostViewModel : mediaUploadTracker.finishUpload() notifying = null + mutedNotifies = emptySet() wantsInvoice = false wantsZapraiser = false @@ -886,10 +889,6 @@ open class CommentPostViewModel : this.multiOrchestrator?.remove(selected) } - open fun removeFromReplyList(userToRemove: User) { - notifying = notifying?.filter { it != userToRemove } - } - override fun onMessageChanged() { urlPreviews.update(message.text.toString()) revalidateDraft() @@ -937,13 +936,9 @@ open class CommentPostViewModel : } open fun autocompleteWithEmoji(item: EmojiPackState.EmojiMedia) { - val wordToInsert = ":${item.code}:" - - message.replaceCurrentWord(wordToInsert) + emojiSuggestions?.autocompleteInto(message, item) urlPreviews.update(message.text.toString()) - emojiSuggestions?.reset() - draftTag.newVersion() } diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/note/nip22Comments/GenericCommentPostScreen.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/note/nip22Comments/GenericCommentPostScreen.kt index 8d4497e18d..e67b48fc9e 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/note/nip22Comments/GenericCommentPostScreen.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/note/nip22Comments/GenericCommentPostScreen.kt @@ -52,6 +52,7 @@ import androidx.lifecycle.viewmodel.compose.viewModel import com.vitorpamplona.amethyst.R import com.vitorpamplona.amethyst.commons.icons.symbols.Icon import com.vitorpamplona.amethyst.commons.icons.symbols.MaterialSymbols +import com.vitorpamplona.amethyst.commons.nip30CustomEmojis.ui.ShowEmojiSuggestionList import com.vitorpamplona.amethyst.model.AddressableNote import com.vitorpamplona.amethyst.ui.actions.StrippingFailureDialog import com.vitorpamplona.amethyst.ui.actions.uploads.SelectFromFiles @@ -65,7 +66,6 @@ import com.vitorpamplona.amethyst.ui.note.BaseUserPicture import com.vitorpamplona.amethyst.ui.note.NoteCompose import com.vitorpamplona.amethyst.ui.note.creators.contentWarning.ContentSensitivityExplainer import com.vitorpamplona.amethyst.ui.note.creators.contentWarning.MarkAsSensitiveButton -import com.vitorpamplona.amethyst.ui.note.creators.emojiSuggestions.ShowEmojiSuggestionList import com.vitorpamplona.amethyst.ui.note.creators.emojiSuggestions.WatchAndLoadMyEmojiList import com.vitorpamplona.amethyst.ui.note.creators.expiration.ExpirationDateButton import com.vitorpamplona.amethyst.ui.note.creators.expiration.ExpirationDatePicker @@ -99,6 +99,7 @@ import com.vitorpamplona.amethyst.ui.theme.replyModifier import com.vitorpamplona.quartz.nip01Core.core.HexKey import kotlinx.collections.immutable.persistentListOf import kotlinx.collections.immutable.toImmutableList +import kotlinx.collections.immutable.toImmutableSet import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext @@ -257,8 +258,12 @@ private fun GenericCommentPostBody( } Row { - Notifying(postViewModel.notifying?.toImmutableList(), accountViewModel) { - postViewModel.removeFromReplyList(it) + Notifying( + baseMentions = postViewModel.notifying?.toImmutableList(), + accountViewModel = accountViewModel, + mutedNotifies = postViewModel.mutedNotifies.toImmutableSet(), + ) { + postViewModel.toggleNotify(it) } } diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/note/types/Git.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/note/types/Git.kt index 99fb0abf00..db61b42ddd 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/note/types/Git.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/note/types/Git.kt @@ -21,12 +21,10 @@ package com.vitorpamplona.amethyst.ui.note.types import androidx.compose.foundation.background -import androidx.compose.foundation.border import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.FlowRow -import androidx.compose.foundation.layout.PaddingValues import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.fillMaxWidth @@ -85,7 +83,6 @@ import com.vitorpamplona.amethyst.ui.screen.loggedIn.gitRepo.repoHasFetchableClo import com.vitorpamplona.amethyst.ui.stringRes import com.vitorpamplona.amethyst.ui.theme.Font12SP import com.vitorpamplona.amethyst.ui.theme.HalfDoubleVertSpacer -import com.vitorpamplona.amethyst.ui.theme.QuoteBorder import com.vitorpamplona.amethyst.ui.theme.Size10dp import com.vitorpamplona.amethyst.ui.theme.Size16dp import com.vitorpamplona.amethyst.ui.theme.Size5dp @@ -93,7 +90,6 @@ import com.vitorpamplona.amethyst.ui.theme.Size8dp import com.vitorpamplona.amethyst.ui.theme.StdVertSpacer import com.vitorpamplona.amethyst.ui.theme.grayText import com.vitorpamplona.amethyst.ui.theme.placeholderText -import com.vitorpamplona.amethyst.ui.theme.subtleBorder import com.vitorpamplona.quartz.nip01Core.tags.hashtags.hasHashtags import com.vitorpamplona.quartz.nip34Git.issue.GitIssueEvent import com.vitorpamplona.quartz.nip34Git.patch.GitPatchEvent @@ -102,9 +98,7 @@ import com.vitorpamplona.quartz.nip34Git.pr.GitPullRequestEvent import com.vitorpamplona.quartz.nip34Git.pr.GitPullRequestUpdateEvent import com.vitorpamplona.quartz.nip34Git.repository.GitRepositoryEvent -private val CardShape = QuoteBorder private val ChipShape = RoundedCornerShape(8.dp) -private val CardPadding = PaddingValues(start = Size10dp, top = Size10dp, end = Size10dp, bottom = Size5dp) private val HeaderSpacing = Arrangement.spacedBy(Size8dp) private val LinkRowSpacing = Arrangement.spacedBy(Size8dp) @@ -113,15 +107,7 @@ private fun GitCardContainer( modifier: Modifier = Modifier, content: @Composable () -> Unit, ) { - val border = MaterialTheme.colorScheme.subtleBorder - Column( - modifier = - modifier - .fillMaxWidth() - .clip(CardShape) - .border(1.dp, border, CardShape) - .padding(CardPadding), - ) { + Column(modifier = modifier.fillMaxWidth()) { content() } } @@ -261,12 +247,31 @@ private fun GitMetaRow( /** Shortens a 40-char git object id to its 7-char prefix for display. */ private fun String.shortCommit(): String = if (length > 7) take(7) else this +/** + * Some NIP-34 clients duplicate the subject tag as a leading markdown heading of the + * content ("# Title\n\n…"). The card already renders the subject as its own title, so + * when the first line is a heading matching [subject], drop it from the body. + */ +private fun stripDuplicatedSubject( + content: String, + subject: String?, +): String { + if (subject.isNullOrBlank()) return content + val trimmed = content.trimStart() + if (!trimmed.startsWith("#")) return content + val firstLineEnd = trimmed.indexOf('\n').let { if (it < 0) trimmed.length else it } + val heading = trimmed.substring(0, firstLineEnd).trimStart('#').trim() + if (!heading.equals(subject.trim(), ignoreCase = true)) return content + return trimmed.substring(firstLineEnd).trimStart() +} + /** * The shared markdown body used by patches, issues and pull requests: honors * the collapsed [makeItShort] preview for the logged-in user's own posts and * otherwise renders the full content with sensitivity warnings and uncited * hashtags. The subject, when present, is rendered separately as a title by the - * caller, so it is not inlined here. + * caller, so it is not inlined here — pass it as [renderedSubject] so a copy + * duplicated into the content as a leading heading is stripped. */ @Composable private fun GitMarkdownBody( @@ -277,8 +282,10 @@ private fun GitMarkdownBody( backgroundColor: MutableState, accountViewModel: AccountViewModel, nav: INav, + renderedSubject: String? = null, ) { - LoadDecryptedContent(note, accountViewModel) { body -> + LoadDecryptedContent(note, accountViewModel) { rawBody -> + val body = remember(rawBody, renderedSubject) { stripDuplicatedSubject(rawBody, renderedSubject) } val isAuthorTheLoggedUser = remember(note.event) { accountViewModel.isLoggedUser(note.author) } @@ -537,7 +544,7 @@ private fun RenderGitIssueEvent( Spacer(modifier = HalfDoubleVertSpacer) - GitMarkdownBody(note, makeItShort, canPreview, quotesLeft, backgroundColor, accountViewModel, nav) + GitMarkdownBody(note, makeItShort, canPreview, quotesLeft, backgroundColor, accountViewModel, nav, renderedSubject = subject) if (!makeItShort) { GitStatusActions(note, accountViewModel) @@ -669,7 +676,7 @@ private fun RenderGitPullRequestEvent( Spacer(modifier = HalfDoubleVertSpacer) - GitMarkdownBody(note, makeItShort, canPreview, quotesLeft, backgroundColor, accountViewModel, nav) + GitMarkdownBody(note, makeItShort, canPreview, quotesLeft, backgroundColor, accountViewModel, nav, renderedSubject = subject) if (!makeItShort) { GitPullRequestChanges(cloneUrls, currentCommit, mergeBase, accountViewModel) diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/note/types/GitPullRequestChanges.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/note/types/GitPullRequestChanges.kt index 6146391da6..71148c1c5d 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/note/types/GitPullRequestChanges.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/note/types/GitPullRequestChanges.kt @@ -99,9 +99,13 @@ fun GitPullRequestChanges( when (val s = state) { ChangesState.Idle -> - FilledTonalButton(onClick = { load() }, modifier = Modifier.padding(top = 8.dp)) { - Icon(MaterialSymbols.Code, contentDescription = null, modifier = Modifier.size(18.dp)) - Text(stringRes(R.string.git_pr_view_changes), modifier = Modifier.padding(start = 6.dp)) + FilledTonalButton( + onClick = { load() }, + modifier = Modifier.padding(top = 8.dp).then(CompactButtonHeight), + contentPadding = CompactButtonPadding, + ) { + Icon(MaterialSymbols.Code, contentDescription = null, modifier = Modifier.size(16.dp)) + Text(stringRes(R.string.git_pr_view_changes), style = MaterialTheme.typography.labelMedium, modifier = Modifier.padding(start = 6.dp)) } ChangesState.Loading -> @@ -129,9 +133,13 @@ fun GitPullRequestChanges( } ChangesState.Failed -> - FilledTonalButton(onClick = { load() }, modifier = Modifier.padding(top = 8.dp)) { - Icon(MaterialSymbols.Refresh, contentDescription = null, modifier = Modifier.size(18.dp)) - Text(stringRes(R.string.git_pr_changes_retry), modifier = Modifier.padding(start = 6.dp)) + FilledTonalButton( + onClick = { load() }, + modifier = Modifier.padding(top = 8.dp).then(CompactButtonHeight), + contentPadding = CompactButtonPadding, + ) { + Icon(MaterialSymbols.Refresh, contentDescription = null, modifier = Modifier.size(16.dp)) + Text(stringRes(R.string.git_pr_changes_retry), style = MaterialTheme.typography.labelMedium, modifier = Modifier.padding(start = 6.dp)) } } } diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/note/types/GitStatusActions.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/note/types/GitStatusActions.kt index ea8cc7075a..5afc90e1e3 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/note/types/GitStatusActions.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/note/types/GitStatusActions.kt @@ -22,6 +22,8 @@ package com.vitorpamplona.amethyst.ui.note.types import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.FlowRow +import androidx.compose.foundation.layout.PaddingValues +import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.size import androidx.compose.material3.ButtonDefaults @@ -55,6 +57,10 @@ import com.vitorpamplona.quartz.nip34Git.status.GitStatusOpenEvent private enum class StatusTarget { OPEN, CLOSED, APPLIED } +/** Compact sizing shared by the small action buttons on git cards. */ +internal val CompactButtonHeight = Modifier.height(32.dp) +internal val CompactButtonPadding = PaddingValues(horizontal = 14.dp, vertical = 4.dp) + /** * NIP-34 status controls for an issue, patch or pull request. Visible only to the * people allowed to moderate the thread — the item's author and the repository @@ -85,23 +91,33 @@ fun GitStatusActions( verticalArrangement = Arrangement.spacedBy(8.dp), ) { if (closedOrApplied) { - FilledTonalButton(onClick = { sendStatus(accountViewModel, note, StatusTarget.OPEN) }) { - Icon(MaterialSymbols.RadioButtonChecked, contentDescription = null, modifier = Modifier.size(18.dp)) - Text(stringRes(R.string.git_status_reopen), modifier = Modifier.padding(start = 6.dp)) + FilledTonalButton( + onClick = { sendStatus(accountViewModel, note, StatusTarget.OPEN) }, + modifier = CompactButtonHeight, + contentPadding = CompactButtonPadding, + ) { + Icon(MaterialSymbols.RadioButtonChecked, contentDescription = null, modifier = Modifier.size(16.dp)) + Text(stringRes(R.string.git_status_reopen), style = MaterialTheme.typography.labelMedium, modifier = Modifier.padding(start = 6.dp)) } } else { if (isPatchOrPr) { - FilledTonalButton(onClick = { sendStatus(accountViewModel, note, StatusTarget.APPLIED) }) { - Icon(MaterialSymbols.Check, contentDescription = null, modifier = Modifier.size(18.dp)) - Text(stringRes(R.string.git_status_mark_merged), modifier = Modifier.padding(start = 6.dp)) + FilledTonalButton( + onClick = { sendStatus(accountViewModel, note, StatusTarget.APPLIED) }, + modifier = CompactButtonHeight, + contentPadding = CompactButtonPadding, + ) { + Icon(MaterialSymbols.Check, contentDescription = null, modifier = Modifier.size(16.dp)) + Text(stringRes(R.string.git_status_mark_merged), style = MaterialTheme.typography.labelMedium, modifier = Modifier.padding(start = 6.dp)) } } OutlinedButton( onClick = { sendStatus(accountViewModel, note, StatusTarget.CLOSED) }, colors = ButtonDefaults.outlinedButtonColors(contentColor = MaterialTheme.colorScheme.error), + modifier = CompactButtonHeight, + contentPadding = CompactButtonPadding, ) { - Icon(MaterialSymbols.Cancel, contentDescription = null, modifier = Modifier.size(18.dp)) - Text(stringRes(R.string.git_status_close), modifier = Modifier.padding(start = 6.dp)) + Icon(MaterialSymbols.Cancel, contentDescription = null, modifier = Modifier.size(16.dp)) + Text(stringRes(R.string.git_status_close), style = MaterialTheme.typography.labelMedium, modifier = Modifier.padding(start = 6.dp)) } } } diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/AccountViewModel.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/AccountViewModel.kt index 8e06823ba4..b554cf1a71 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/AccountViewModel.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/AccountViewModel.kt @@ -1704,6 +1704,12 @@ class AccountViewModel( fun hide(user: User) = launchSigner { account.hideUser(user.pubkeyHex) } + fun updateContactCardPetName( + user: User, + petName: String?, + summary: String?, + ) = launchSigner { account.updateContactCardPetName(user.pubkeyHex, petName, summary) } + fun hide(word: String) = launchSigner { account.hideWord(word) } fun showUser(pubkeyHex: String) = launchSigner { account.showUser(pubkeyHex) } diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/LoggedInPage.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/LoggedInPage.kt index 49704a9575..79bb11bde3 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/LoggedInPage.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/LoggedInPage.kt @@ -47,11 +47,11 @@ import com.vitorpamplona.amethyst.service.relayClient.authCommand.compose.RelayA import com.vitorpamplona.amethyst.service.relayClient.authCommand.compose.RelayAuthSubscription import com.vitorpamplona.amethyst.service.relayClient.reqCommand.account.AccountFilterAssemblerSubscription import com.vitorpamplona.amethyst.service.relayClient.reqCommand.account.AccountForegroundFilterAssemblerSubscription +import com.vitorpamplona.amethyst.service.resourceusage.innermostSigner import com.vitorpamplona.amethyst.ui.navigation.AppNavigation import com.vitorpamplona.amethyst.ui.navigation.routes.Route import com.vitorpamplona.amethyst.ui.screen.AccountSessionManager import com.vitorpamplona.quartz.nip55AndroidSigner.client.IActivityLauncher -import com.vitorpamplona.quartz.nip89AppHandlers.clientTag.NostrSignerWithClientTag import com.vitorpamplona.quartz.utils.Log import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch @@ -172,20 +172,7 @@ fun NotificationRegistration(accountViewModel: AccountViewModel) { @Composable private fun ListenToExternalSignerIfNeeded(accountViewModel: AccountViewModel) { - val externalSignerLauncher = - when (val signer = accountViewModel.account.signer) { - is IActivityLauncher -> { - signer - } - - is NostrSignerWithClientTag if signer.inner is IActivityLauncher -> { - signer.inner as IActivityLauncher - } - - else -> { - null - } - } + val externalSignerLauncher = accountViewModel.account.signer.innermostSigner() as? IActivityLauncher if (externalSignerLauncher != null) { val launcher = diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/calendars/CalendarReminderSettingsScreen.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/calendars/CalendarReminderSettingsScreen.kt index 8299ce35d4..6959718e23 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/calendars/CalendarReminderSettingsScreen.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/calendars/CalendarReminderSettingsScreen.kt @@ -105,11 +105,14 @@ fun CalendarReminderSettingsScreen(nav: INav) { onCheckedChange = { enabled = it prefs.setEnabled(it) - // Toggling off doesn't cancel the worker — the worker itself short- - // circuits when isEnabled() returns false. Keeping the schedule alive - // means flipping it back on takes effect immediately without needing a - // re-launch via AppModules. - if (it) CalendarReminderWorker.schedule(context) + // Cancel eagerly on disable so the periodic worker stops waking the + // process; re-enabling re-schedules immediately, and the ACCEPTED-RSVP + // observer in AppModules re-schedules on the next relevant RSVP too. + if (it) { + CalendarReminderWorker.schedule(context) + } else { + CalendarReminderWorker.cancel(context) + } }, ) } diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/chats/feed/DrawAuthorInfo.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/chats/feed/DrawAuthorInfo.kt index 2488506058..35a6fcae36 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/chats/feed/DrawAuthorInfo.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/chats/feed/DrawAuthorInfo.kt @@ -29,6 +29,7 @@ import com.vitorpamplona.amethyst.commons.model.EmptyTagList import com.vitorpamplona.amethyst.model.Note import com.vitorpamplona.amethyst.model.User import com.vitorpamplona.amethyst.service.relayClient.reqCommand.user.observeUserInfo +import com.vitorpamplona.amethyst.service.relayClient.reqCommand.user.observeUserNickname import com.vitorpamplona.amethyst.ui.components.CreateTextWithEmoji import com.vitorpamplona.amethyst.ui.navigation.navs.INav import com.vitorpamplona.amethyst.ui.note.FollowingIcon @@ -58,13 +59,15 @@ private fun WatchAndDisplayUser( nav: INav, ) { val userState by observeUserInfo(author, accountViewModel) + val nickname by observeUserNickname(author, accountViewModel) + val petName = nickname?.petName UserDisplayNameLayout( picture = { InnerUserPicture( userHex = author.pubkeyHex, userPicture = userState?.info?.picture, - userName = userState?.info?.bestName(), + userName = petName ?: userState?.info?.bestName(), size = Size20dp, modifier = Modifier, accountViewModel = accountViewModel, @@ -81,8 +84,8 @@ private fun WatchAndDisplayUser( name = { if (userState != null) { CreateTextWithEmoji( - text = userState?.info?.bestName() ?: author.pubkeyDisplayHex(), - tags = userState?.tags ?: EmptyTagList, + text = petName ?: userState?.info?.bestName() ?: author.pubkeyDisplayHex(), + tags = (if (petName != null) nickname?.tags else userState?.tags) ?: EmptyTagList, maxLines = 1, fontWeight = FontWeight.Bold, ) diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/chats/privateDM/send/ChatNewMessageViewModel.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/chats/privateDM/send/ChatNewMessageViewModel.kt index c5c94e64c6..2d92030b7b 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/chats/privateDM/send/ChatNewMessageViewModel.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/chats/privateDM/send/ChatNewMessageViewModel.kt @@ -33,6 +33,7 @@ import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.vitorpamplona.amethyst.Amethyst import com.vitorpamplona.amethyst.commons.model.nip30CustomEmojis.EmojiPackState +import com.vitorpamplona.amethyst.commons.model.nip30CustomEmojis.EmojiSuggestionState import com.vitorpamplona.amethyst.commons.ui.text.currentWord import com.vitorpamplona.amethyst.commons.ui.text.insertUrlAtCursor import com.vitorpamplona.amethyst.commons.ui.text.replaceCurrentWord @@ -46,7 +47,6 @@ import com.vitorpamplona.amethyst.service.uploads.SuspendableConfirmation import com.vitorpamplona.amethyst.ui.actions.NewMessageTagger import com.vitorpamplona.amethyst.ui.actions.uploads.SelectedMedia import com.vitorpamplona.amethyst.ui.note.creators.draftTags.DraftTagState -import com.vitorpamplona.amethyst.ui.note.creators.emojiSuggestions.EmojiSuggestionState import com.vitorpamplona.amethyst.ui.note.creators.expiration.IExpiration import com.vitorpamplona.amethyst.ui.note.creators.location.ILocationGrabber import com.vitorpamplona.amethyst.ui.note.creators.messagefield.IMessageField @@ -80,8 +80,6 @@ import com.vitorpamplona.quartz.nip17Dm.messages.ChatMessageEvent import com.vitorpamplona.quartz.nip17Dm.settings.ChatMessageRelayListEvent import com.vitorpamplona.quartz.nip18Reposts.quotes.quotes import com.vitorpamplona.quartz.nip19Bech32.toNpub -import com.vitorpamplona.quartz.nip30CustomEmoji.CustomEmoji -import com.vitorpamplona.quartz.nip30CustomEmoji.EmojiUrlTag import com.vitorpamplona.quartz.nip30CustomEmoji.emojis import com.vitorpamplona.quartz.nip36SensitiveContent.contentWarning import com.vitorpamplona.quartz.nip36SensitiveContent.contentWarningReason @@ -276,7 +274,7 @@ class ChatNewMessageViewModel : ) this.emojiSuggestions?.reset() - this.emojiSuggestions = EmojiSuggestionState(accountVM.account) + this.emojiSuggestions = EmojiSuggestionState(accountVM.account.emoji) this.uploadState = ChatFileUploadState( @@ -580,7 +578,7 @@ class ChatNewMessageViewModel : val messageText = message.text.toString() val urls = findURLs(messageText) val usedAttachments = iMetaAttachments.filterIsIn(urls.toSet()) - val emojis = findEmoji(messageText, accountViewModel.account.emoji.myEmojis.value) + val emojis = accountViewModel.account.emoji.findEmojiTags(messageText) val geoHash = if (wantsToAddGeoHash) (location?.value as? LocationState.LocationResult.Success)?.geoHash?.toString() else null val message = messageText @@ -635,16 +633,6 @@ class ChatNewMessageViewModel : } } - fun findEmoji( - message: String, - myEmojiSet: List?, - ): List { - if (myEmojiSet == null) return emptyList() - return CustomEmoji.findAllEmojiCodes(message).mapNotNull { possibleEmoji -> - myEmojiSet.firstOrNull { it.code == possibleEmoji }?.let { EmojiUrlTag(it.code, it.link) } - } - } - fun cancel() { draftTag.rotate() @@ -776,13 +764,9 @@ class ChatNewMessageViewModel : } fun autocompleteWithEmoji(item: EmojiPackState.EmojiMedia) { - val wordToInsert = ":${item.code}:" - - message.replaceCurrentWord(wordToInsert) + emojiSuggestions?.autocompleteInto(message, item) urlPreviews.update(message.text.toString()) - emojiSuggestions?.reset() - draftTag.newVersion() } diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/chats/privateDM/send/NewGroupDMScreen.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/chats/privateDM/send/NewGroupDMScreen.kt index 36987b6160..8941be093f 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/chats/privateDM/send/NewGroupDMScreen.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/chats/privateDM/send/NewGroupDMScreen.kt @@ -71,6 +71,7 @@ import com.vitorpamplona.amethyst.Amethyst import com.vitorpamplona.amethyst.R import com.vitorpamplona.amethyst.commons.icons.symbols.Icon import com.vitorpamplona.amethyst.commons.icons.symbols.MaterialSymbols +import com.vitorpamplona.amethyst.commons.nip30CustomEmojis.ui.ShowEmojiSuggestionList import com.vitorpamplona.amethyst.commons.richtext.BaseMediaContent import com.vitorpamplona.amethyst.commons.richtext.EncryptedMediaUrlImage import com.vitorpamplona.amethyst.commons.richtext.EncryptedMediaUrlVideo @@ -93,7 +94,6 @@ import com.vitorpamplona.amethyst.ui.navigation.topbars.PostingTopBar import com.vitorpamplona.amethyst.ui.note.BaseUserPicture import com.vitorpamplona.amethyst.ui.note.creators.contentWarning.ContentSensitivityExplainer import com.vitorpamplona.amethyst.ui.note.creators.contentWarning.MarkAsSensitiveButton -import com.vitorpamplona.amethyst.ui.note.creators.emojiSuggestions.ShowEmojiSuggestionList import com.vitorpamplona.amethyst.ui.note.creators.emojiSuggestions.WatchAndLoadMyEmojiList import com.vitorpamplona.amethyst.ui.note.creators.expiration.ExpirationDateButton import com.vitorpamplona.amethyst.ui.note.creators.expiration.ExpirationDatePicker diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/chats/privateDM/send/PrivateMessageEditFieldRow.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/chats/privateDM/send/PrivateMessageEditFieldRow.kt index a089688b21..fc64efe0ab 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/chats/privateDM/send/PrivateMessageEditFieldRow.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/chats/privateDM/send/PrivateMessageEditFieldRow.kt @@ -51,6 +51,7 @@ import androidx.compose.ui.unit.sp import androidx.lifecycle.compose.collectAsStateWithLifecycle import androidx.lifecycle.viewmodel.compose.viewModel import com.vitorpamplona.amethyst.R +import com.vitorpamplona.amethyst.commons.nip30CustomEmojis.ui.ShowEmojiSuggestionList import com.vitorpamplona.amethyst.model.LocalCache import com.vitorpamplona.amethyst.model.User import com.vitorpamplona.amethyst.ui.actions.MentionPreservingInputTransformation @@ -63,7 +64,6 @@ import com.vitorpamplona.amethyst.ui.navigation.navs.EmptyNav import com.vitorpamplona.amethyst.ui.navigation.navs.INav import com.vitorpamplona.amethyst.ui.navigation.routes.routeFor import com.vitorpamplona.amethyst.ui.note.ClickableUserPicture -import com.vitorpamplona.amethyst.ui.note.creators.emojiSuggestions.ShowEmojiSuggestionList import com.vitorpamplona.amethyst.ui.note.creators.userSuggestions.ShowUserSuggestionList import com.vitorpamplona.amethyst.ui.note.showCount import com.vitorpamplona.amethyst.ui.note.timeAheadNoDot diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/chats/publicChannels/send/ChannelNewMessageViewModel.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/chats/publicChannels/send/ChannelNewMessageViewModel.kt index fe79dde482..377b424bbb 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/chats/publicChannels/send/ChannelNewMessageViewModel.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/chats/publicChannels/send/ChannelNewMessageViewModel.kt @@ -38,6 +38,7 @@ import com.vitorpamplona.amethyst.commons.model.emphChat.EphemeralChatChannel import com.vitorpamplona.amethyst.commons.model.nip28PublicChats.PublicChatChannel import com.vitorpamplona.amethyst.commons.model.nip29RelayGroups.RelayGroupChannel import com.vitorpamplona.amethyst.commons.model.nip30CustomEmojis.EmojiPackState +import com.vitorpamplona.amethyst.commons.model.nip30CustomEmojis.EmojiSuggestionState import com.vitorpamplona.amethyst.commons.model.nip53LiveActivities.LiveActivitiesChannel import com.vitorpamplona.amethyst.commons.richtext.UrlParser import com.vitorpamplona.amethyst.commons.service.pow.PoWReplay @@ -56,7 +57,6 @@ import com.vitorpamplona.amethyst.service.uploads.UploadOrchestrator import com.vitorpamplona.amethyst.ui.actions.NewMessageTagger import com.vitorpamplona.amethyst.ui.actions.uploads.SelectedMedia import com.vitorpamplona.amethyst.ui.note.creators.draftTags.DraftTagState -import com.vitorpamplona.amethyst.ui.note.creators.emojiSuggestions.EmojiSuggestionState import com.vitorpamplona.amethyst.ui.note.creators.expiration.IExpiration import com.vitorpamplona.amethyst.ui.note.creators.location.ILocationGrabber import com.vitorpamplona.amethyst.ui.note.creators.userSuggestions.UserSuggestionState @@ -88,8 +88,6 @@ import com.vitorpamplona.quartz.nip18Reposts.quotes.quotes import com.vitorpamplona.quartz.nip28PublicChat.base.notify import com.vitorpamplona.quartz.nip28PublicChat.message.ChannelMessageEvent import com.vitorpamplona.quartz.nip29RelayGroups.hTag -import com.vitorpamplona.quartz.nip30CustomEmoji.CustomEmoji -import com.vitorpamplona.quartz.nip30CustomEmoji.EmojiUrlTag import com.vitorpamplona.quartz.nip30CustomEmoji.emojis import com.vitorpamplona.quartz.nip36SensitiveContent.contentWarning import com.vitorpamplona.quartz.nip36SensitiveContent.contentWarningReason @@ -212,7 +210,7 @@ open class ChannelNewMessageViewModel : ) this.emojiSuggestions?.reset() - this.emojiSuggestions = EmojiSuggestionState(accountVM.account) + this.emojiSuggestions = EmojiSuggestionState(accountVM.account.emoji) this.uploadState = ChatFileUploadState(account.settings.defaultFileServer, account.settings.stripLocationOnUpload) } @@ -433,7 +431,7 @@ open class ChannelNewMessageViewModel : val urls = findURLs(messageText) val usedAttachments = iMetaAttachments.filterIsIn(urls.toSet()) - val emojis = findEmoji(messageText, accountViewModel.account.emoji.myEmojis.value) + val emojis = accountViewModel.account.emoji.findEmojiTags(messageText) val channelRelays = channel.relays() val geoHash = if (wantsToAddGeoHash) (location?.value as? LocationState.LocationResult.Success)?.geoHash?.toString() else null @@ -597,16 +595,6 @@ open class ChannelNewMessageViewModel : } } - fun findEmoji( - message: String, - myEmojiSet: List?, - ): List { - if (myEmojiSet == null) return emptyList() - return CustomEmoji.findAllEmojiCodes(message).mapNotNull { possibleEmoji -> - myEmojiSet.firstOrNull { it.code == possibleEmoji }?.let { EmojiUrlTag(it.code, it.link) } - } - } - open fun cancel() { draftTag.rotate() @@ -690,10 +678,7 @@ open class ChannelNewMessageViewModel : } open fun autocompleteWithEmoji(item: EmojiPackState.EmojiMedia) { - val wordToInsert = ":${item.code}:" - message.replaceCurrentWord(wordToInsert) - - emojiSuggestions?.reset() + emojiSuggestions?.autocompleteInto(message, item) draftTag.newVersion() } diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/chats/publicChannels/send/EditFieldRow.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/chats/publicChannels/send/EditFieldRow.kt index e9f150b50a..f23c35fce7 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/chats/publicChannels/send/EditFieldRow.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/chats/publicChannels/send/EditFieldRow.kt @@ -37,6 +37,7 @@ import androidx.compose.ui.text.input.KeyboardCapitalization import androidx.compose.ui.text.style.TextDirection import androidx.compose.ui.unit.dp import com.vitorpamplona.amethyst.R +import com.vitorpamplona.amethyst.commons.nip30CustomEmojis.ui.ShowEmojiSuggestionList import com.vitorpamplona.amethyst.ui.actions.MentionPreservingInputTransformation import com.vitorpamplona.amethyst.ui.actions.StrippingFailureDialog import com.vitorpamplona.amethyst.ui.actions.UrlUserTagOutputTransformation @@ -44,7 +45,6 @@ import com.vitorpamplona.amethyst.ui.actions.uploads.SelectFromGallery import com.vitorpamplona.amethyst.ui.actions.uploads.SelectedMedia import com.vitorpamplona.amethyst.ui.components.ThinPaddingTextField import com.vitorpamplona.amethyst.ui.navigation.navs.INav -import com.vitorpamplona.amethyst.ui.note.creators.emojiSuggestions.ShowEmojiSuggestionList import com.vitorpamplona.amethyst.ui.note.creators.userSuggestions.ShowUserSuggestionList import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel import com.vitorpamplona.amethyst.ui.screen.loggedIn.chats.utils.DisplayReplyingToNote diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/discover/nip23LongForm/LongFormPostScreen.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/discover/nip23LongForm/LongFormPostScreen.kt index 95cdc5577c..93c8a6b0ae 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/discover/nip23LongForm/LongFormPostScreen.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/discover/nip23LongForm/LongFormPostScreen.kt @@ -83,6 +83,7 @@ import com.vitorpamplona.amethyst.R import com.vitorpamplona.amethyst.commons.icons.symbols.Icon import com.vitorpamplona.amethyst.commons.icons.symbols.MaterialSymbols import com.vitorpamplona.amethyst.commons.model.EmptyTagList +import com.vitorpamplona.amethyst.commons.nip30CustomEmojis.ui.ShowEmojiSuggestionList import com.vitorpamplona.amethyst.ui.actions.MentionPreservingInputTransformation import com.vitorpamplona.amethyst.ui.actions.StrippingFailureDialog import com.vitorpamplona.amethyst.ui.actions.UrlUserTagOutputTransformation @@ -99,7 +100,6 @@ import com.vitorpamplona.amethyst.ui.navigation.navs.Nav import com.vitorpamplona.amethyst.ui.navigation.topbars.PostingTopBar import com.vitorpamplona.amethyst.ui.note.creators.contentWarning.ContentSensitivityExplainer import com.vitorpamplona.amethyst.ui.note.creators.contentWarning.MarkAsSensitiveButton -import com.vitorpamplona.amethyst.ui.note.creators.emojiSuggestions.ShowEmojiSuggestionList import com.vitorpamplona.amethyst.ui.note.creators.emojiSuggestions.WatchAndLoadMyEmojiList import com.vitorpamplona.amethyst.ui.note.creators.expiration.ExpirationDateButton import com.vitorpamplona.amethyst.ui.note.creators.expiration.ExpirationDatePicker diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/discover/nip23LongForm/LongFormPostViewModel.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/discover/nip23LongForm/LongFormPostViewModel.kt index 7bc0a0f755..47686de456 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/discover/nip23LongForm/LongFormPostViewModel.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/discover/nip23LongForm/LongFormPostViewModel.kt @@ -35,6 +35,7 @@ import androidx.lifecycle.viewModelScope import com.vitorpamplona.amethyst.Amethyst import com.vitorpamplona.amethyst.R import com.vitorpamplona.amethyst.commons.model.nip30CustomEmojis.EmojiPackState.EmojiMedia +import com.vitorpamplona.amethyst.commons.model.nip30CustomEmojis.EmojiSuggestionState import com.vitorpamplona.amethyst.commons.service.pow.PoWReplay import com.vitorpamplona.amethyst.commons.ui.text.appendSignature import com.vitorpamplona.amethyst.commons.ui.text.currentWord @@ -60,7 +61,6 @@ import com.vitorpamplona.amethyst.ui.actions.uploads.MediaUploadTracker import com.vitorpamplona.amethyst.ui.actions.uploads.SelectedMedia import com.vitorpamplona.amethyst.ui.actions.uploads.SelectedMediaProcessing import com.vitorpamplona.amethyst.ui.note.creators.draftTags.DraftTagState -import com.vitorpamplona.amethyst.ui.note.creators.emojiSuggestions.EmojiSuggestionState import com.vitorpamplona.amethyst.ui.note.creators.expiration.IExpiration import com.vitorpamplona.amethyst.ui.note.creators.location.ILocationGrabber import com.vitorpamplona.amethyst.ui.note.creators.messagefield.IMessageField @@ -87,8 +87,6 @@ import com.vitorpamplona.quartz.nip10Notes.content.findURLs import com.vitorpamplona.quartz.nip18Reposts.quotes.quotes import com.vitorpamplona.quartz.nip22Comments.CommentEvent import com.vitorpamplona.quartz.nip23LongContent.LongTextNoteEvent -import com.vitorpamplona.quartz.nip30CustomEmoji.CustomEmoji -import com.vitorpamplona.quartz.nip30CustomEmoji.EmojiUrlTag import com.vitorpamplona.quartz.nip30CustomEmoji.emojis import com.vitorpamplona.quartz.nip36SensitiveContent.contentWarning import com.vitorpamplona.quartz.nip36SensitiveContent.contentWarningReason @@ -234,7 +232,7 @@ class LongFormPostViewModel : this.userSuggestions = UserSuggestionState(accountVM.account, accountVM.nip05ClientBuilder()) this.emojiSuggestions?.reset() - this.emojiSuggestions = EmojiSuggestionState(accountVM.account) + this.emojiSuggestions = EmojiSuggestionState(accountVM.account.emoji) } fun load( @@ -416,7 +414,7 @@ class LongFormPostViewModel : val geoHash = if (wantsToAddGeoHash) (location?.value as? LocationState.LocationResult.Success)?.geoHash?.toString() else null val localZapRaiserAmount = if (wantsZapRaiser) zapRaiserAmount.value else null - val emojis = findEmoji(tagger.message, account.emoji.myEmojis.value) + val emojis = account.emoji.findEmojiTags(tagger.message) val urls = findURLs(tagger.message) val usedAttachments = iMetaAttachments.filterIsIn(urls.toSet()) @@ -446,16 +444,6 @@ class LongFormPostViewModel : } } - private fun findEmoji( - message: String, - myEmojiSet: List?, - ): List { - if (myEmojiSet == null) return emptyList() - return CustomEmoji.findAllEmojiCodes(message).mapNotNull { possibleEmoji -> - myEmojiSet.firstOrNull { it.code == possibleEmoji }?.let { EmojiUrlTag(it.code, it.link) } - } - } - fun uploadCoverImage( uri: SelectedMedia, context: Context, @@ -704,9 +692,7 @@ class LongFormPostViewModel : } fun autocompleteWithEmoji(item: EmojiMedia) { - val wordToInsert = ":${item.code}:" - message.replaceCurrentWord(wordToInsert) - emojiSuggestions?.reset() + emojiSuggestions?.autocompleteInto(message, item) draftTag.newVersion() } diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/discover/nip99Classifieds/NewProductScreen.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/discover/nip99Classifieds/NewProductScreen.kt index 6f4fd20c97..9f32382d97 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/discover/nip99Classifieds/NewProductScreen.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/discover/nip99Classifieds/NewProductScreen.kt @@ -46,6 +46,7 @@ import androidx.compose.ui.unit.dp import androidx.core.net.toUri import androidx.lifecycle.viewmodel.compose.viewModel import com.vitorpamplona.amethyst.R +import com.vitorpamplona.amethyst.commons.nip30CustomEmojis.ui.ShowEmojiSuggestionList import com.vitorpamplona.amethyst.ui.actions.StrippingFailureDialog import com.vitorpamplona.amethyst.ui.actions.uploads.SelectFromFiles import com.vitorpamplona.amethyst.ui.actions.uploads.SelectFromGallery @@ -58,7 +59,6 @@ import com.vitorpamplona.amethyst.ui.navigation.topbars.PostingTopBar import com.vitorpamplona.amethyst.ui.note.BaseUserPicture import com.vitorpamplona.amethyst.ui.note.creators.contentWarning.ContentSensitivityExplainer import com.vitorpamplona.amethyst.ui.note.creators.contentWarning.MarkAsSensitiveButton -import com.vitorpamplona.amethyst.ui.note.creators.emojiSuggestions.ShowEmojiSuggestionList import com.vitorpamplona.amethyst.ui.note.creators.emojiSuggestions.WatchAndLoadMyEmojiList import com.vitorpamplona.amethyst.ui.note.creators.expiration.ExpirationDateButton import com.vitorpamplona.amethyst.ui.note.creators.expiration.ExpirationDatePicker diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/discover/nip99Classifieds/NewProductViewModel.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/discover/nip99Classifieds/NewProductViewModel.kt index 0cbfe7a650..6e7554d7dd 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/discover/nip99Classifieds/NewProductViewModel.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/discover/nip99Classifieds/NewProductViewModel.kt @@ -34,6 +34,7 @@ import androidx.lifecycle.viewModelScope import com.vitorpamplona.amethyst.Amethyst import com.vitorpamplona.amethyst.R import com.vitorpamplona.amethyst.commons.model.nip30CustomEmojis.EmojiPackState +import com.vitorpamplona.amethyst.commons.model.nip30CustomEmojis.EmojiSuggestionState import com.vitorpamplona.amethyst.commons.ui.text.currentWord import com.vitorpamplona.amethyst.commons.ui.text.insertUrlAtCursor import com.vitorpamplona.amethyst.commons.ui.text.replaceCurrentWord @@ -53,7 +54,6 @@ import com.vitorpamplona.amethyst.ui.actions.uploads.MediaUploadTracker import com.vitorpamplona.amethyst.ui.actions.uploads.SelectedMedia import com.vitorpamplona.amethyst.ui.actions.uploads.SelectedMediaProcessing import com.vitorpamplona.amethyst.ui.note.creators.draftTags.DraftTagState -import com.vitorpamplona.amethyst.ui.note.creators.emojiSuggestions.EmojiSuggestionState import com.vitorpamplona.amethyst.ui.note.creators.expiration.IExpiration import com.vitorpamplona.amethyst.ui.note.creators.location.ILocationGrabber import com.vitorpamplona.amethyst.ui.note.creators.messagefield.IMessageField @@ -79,8 +79,6 @@ import com.vitorpamplona.quartz.nip10Notes.content.findHashtags import com.vitorpamplona.quartz.nip10Notes.content.findNostrUris import com.vitorpamplona.quartz.nip10Notes.content.findURLs import com.vitorpamplona.quartz.nip18Reposts.quotes.quotes -import com.vitorpamplona.quartz.nip30CustomEmoji.CustomEmoji -import com.vitorpamplona.quartz.nip30CustomEmoji.EmojiUrlTag import com.vitorpamplona.quartz.nip30CustomEmoji.emojis import com.vitorpamplona.quartz.nip36SensitiveContent.contentWarning import com.vitorpamplona.quartz.nip36SensitiveContent.contentWarningReason @@ -212,7 +210,7 @@ open class NewProductViewModel : this.userSuggestions = UserSuggestionState(accountVM.account, accountVM.nip05ClientBuilder()) this.emojiSuggestions?.reset() - this.emojiSuggestions = EmojiSuggestionState(accountVM.account) + this.emojiSuggestions = EmojiSuggestionState(accountVM.account.emoji) } fun editFromDraft(draft: Note) { @@ -351,7 +349,7 @@ open class NewProductViewModel : ) tagger.run() - val emojis = findEmoji(tagger.message, account.emoji.myEmojis.value) + val emojis = account.emoji.findEmojiTags(tagger.message) val urls = findURLs(tagger.message) val usedAttachments = iMetaDescription.filterIsIn(urls.toSet()) + productImages.map { it.toIMeta() } @@ -391,16 +389,6 @@ open class NewProductViewModel : return template } - fun findEmoji( - message: String, - myEmojiSet: List?, - ): List { - if (myEmojiSet == null) return emptyList() - return CustomEmoji.findAllEmojiCodes(message).mapNotNull { possibleEmoji -> - myEmojiSet.firstOrNull { it.code == possibleEmoji }?.let { EmojiUrlTag(it.code, it.link) } - } - } - fun upload( alt: String?, contentWarningReason: String?, @@ -570,13 +558,9 @@ open class NewProductViewModel : } open fun autocompleteWithEmoji(item: EmojiPackState.EmojiMedia) { - val wordToInsert = ":${item.code}:" - - message.replaceCurrentWord(wordToInsert) + emojiSuggestions?.autocompleteInto(message, item) urlPreviews.update(message.text.toString()) - emojiSuggestions?.reset() - draftTag.newVersion() } diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/home/ShortNotePostScreen.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/home/ShortNotePostScreen.kt index 534927c0f1..342e495a1d 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/home/ShortNotePostScreen.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/home/ShortNotePostScreen.kt @@ -76,6 +76,7 @@ import androidx.lifecycle.viewmodel.compose.viewModel import com.vitorpamplona.amethyst.R import com.vitorpamplona.amethyst.commons.icons.symbols.Icon import com.vitorpamplona.amethyst.commons.icons.symbols.MaterialSymbols +import com.vitorpamplona.amethyst.commons.nip30CustomEmojis.ui.ShowEmojiSuggestionList import com.vitorpamplona.amethyst.ui.actions.StrippingFailureDialog import com.vitorpamplona.amethyst.ui.actions.mediaServers.FileServerSelectionRow import com.vitorpamplona.amethyst.ui.actions.uploads.MAX_VOICE_RECORD_SECONDS @@ -99,7 +100,6 @@ import com.vitorpamplona.amethyst.ui.note.NoteCompose import com.vitorpamplona.amethyst.ui.note.creators.aihelp.AiWritingHelpPanel import com.vitorpamplona.amethyst.ui.note.creators.contentWarning.ContentSensitivityExplainer import com.vitorpamplona.amethyst.ui.note.creators.contentWarning.MarkAsSensitiveButton -import com.vitorpamplona.amethyst.ui.note.creators.emojiSuggestions.ShowEmojiSuggestionList import com.vitorpamplona.amethyst.ui.note.creators.emojiSuggestions.WatchAndLoadMyEmojiList import com.vitorpamplona.amethyst.ui.note.creators.expiration.ExpirationDateButton import com.vitorpamplona.amethyst.ui.note.creators.expiration.ExpirationDatePicker @@ -142,6 +142,7 @@ import com.vitorpamplona.amethyst.ui.theme.replyModifier import com.vitorpamplona.quartz.nip01Core.core.HexKey import kotlinx.collections.immutable.persistentListOf import kotlinx.collections.immutable.toImmutableList +import kotlinx.collections.immutable.toImmutableSet import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.FlowPreview import kotlinx.coroutines.launch @@ -327,9 +328,10 @@ private fun NewPostScreenBody( accountViewModel = accountViewModel, label = if (postViewModel.wantsPrivateNote) stringRes(R.string.private_note_visible_to) else null, showWhenEmpty = postViewModel.wantsPrivateNote, + mutedNotifies = postViewModel.mutedNotifies.toImmutableSet(), onAddUser = { postViewModel.wantsToAddNotifyUser = !postViewModel.wantsToAddNotifyUser }, ) { - postViewModel.removeFromReplyList(it) + postViewModel.toggleNotify(it) } } @@ -350,7 +352,7 @@ private fun NewPostScreenBody( ) } - if (postViewModel.wantsPrivateNote && postViewModel.pTags.isNullOrEmpty()) { + if (postViewModel.wantsPrivateNote && postViewModel.activeNotifies().isNullOrEmpty()) { Text( text = stringRes(R.string.private_note_no_receivers), style = MaterialTheme.typography.bodySmall, diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/home/ShortNotePostViewModel.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/home/ShortNotePostViewModel.kt index 3cea485aef..f18b41f796 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/home/ShortNotePostViewModel.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/home/ShortNotePostViewModel.kt @@ -36,6 +36,7 @@ import androidx.lifecycle.viewModelScope import com.vitorpamplona.amethyst.Amethyst import com.vitorpamplona.amethyst.R import com.vitorpamplona.amethyst.commons.model.nip30CustomEmojis.EmojiPackState.EmojiMedia +import com.vitorpamplona.amethyst.commons.model.nip30CustomEmojis.EmojiSuggestionState import com.vitorpamplona.amethyst.commons.service.pow.PoWReplay import com.vitorpamplona.amethyst.commons.ui.text.appendSignature import com.vitorpamplona.amethyst.commons.ui.text.currentWord @@ -72,7 +73,6 @@ import com.vitorpamplona.amethyst.ui.actions.uploads.SelectedMediaProcessing import com.vitorpamplona.amethyst.ui.actions.uploads.VoiceAnonymizationController import com.vitorpamplona.amethyst.ui.actions.uploads.VoicePreset import com.vitorpamplona.amethyst.ui.note.creators.draftTags.DraftTagState -import com.vitorpamplona.amethyst.ui.note.creators.emojiSuggestions.EmojiSuggestionState import com.vitorpamplona.amethyst.ui.note.creators.expiration.IExpiration import com.vitorpamplona.amethyst.ui.note.creators.location.ILocationGrabber import com.vitorpamplona.amethyst.ui.note.creators.messagefield.IMessageField @@ -124,8 +124,6 @@ import com.vitorpamplona.quartz.nip18Reposts.quotes.taggedQuoteIds import com.vitorpamplona.quartz.nip22Comments.CommentEvent import com.vitorpamplona.quartz.nip22Comments.notify import com.vitorpamplona.quartz.nip29RelayGroups.hTag -import com.vitorpamplona.quartz.nip30CustomEmoji.CustomEmoji -import com.vitorpamplona.quartz.nip30CustomEmoji.EmojiUrlTag import com.vitorpamplona.quartz.nip30CustomEmoji.emojis import com.vitorpamplona.quartz.nip36SensitiveContent.contentWarning import com.vitorpamplona.quartz.nip36SensitiveContent.contentWarningReason @@ -367,10 +365,29 @@ open class ShortNotePostViewModel : } } + // Members of pTags whose bell is off: they keep their chip in the Notify + // list (so they are one tap away from being added back) but are dropped + // from the outgoing event's p tags. + var mutedNotifies by mutableStateOf>(emptySet()) + + fun toggleNotify(user: User) { + mutedNotifies = + if (user.pubkeyHex in mutedNotifies) { + mutedNotifies - user.pubkeyHex + } else { + mutedNotifies + user.pubkeyHex + } + draftTag.newVersion() + } + + // The users that will actually be p-tagged: the chip list minus the muted ones. + fun activeNotifies(): List? = pTags?.filter { it.pubkeyHex !in mutedNotifies } + fun addToReplyList(user: User) { if (pTags?.contains(user) != true) { pTags = (pTags ?: emptyList()).plus(user) } + mutedNotifies = mutedNotifies - user.pubkeyHex } // A single ephemeral signer reused for the whole compose session so that media @@ -517,7 +534,7 @@ open class ShortNotePostViewModel : this.userSuggestions = UserSuggestionState(accountVM.account, accountVM.nip05ClientBuilder()) this.emojiSuggestions?.reset() - this.emojiSuggestions = EmojiSuggestionState(accountVM.account) + this.emojiSuggestions = EmojiSuggestionState(accountVM.account.emoji) } /** @@ -569,6 +586,7 @@ open class ShortNotePostViewModel : originalNote = replyingTo privateNoteLocked = replyingTo?.isPrivateRumor() == true wantsPrivateNote = privateNoteLocked + mutedNotifies = emptySet() replyingTo?.let { replyNote -> if (replyNote.event is BaseThreadedEvent) { this.eTags = (replyNote.replyTo ?: emptyList()).plus(replyNote) @@ -577,20 +595,7 @@ open class ShortNotePostViewModel : } if (replyNote.event !is CommunityDefinitionEvent) { - replyNote.author?.let { replyUser -> - val currentMentions = - (replyNote.event as? TextNoteEvent) - ?.mentions() - ?.toSet() - ?.map { LocalCache.getOrCreateUser(it.pubKey) } - ?: emptyList() - - if (currentMentions.contains(replyUser)) { - this.pTags = currentMentions - } else { - this.pTags = currentMentions.plus(replyUser) - } - } + this.pTags = threadMembers(replyNote, this.eTags) } } ?: run { @@ -708,6 +713,22 @@ open class ShortNotePostViewModel : } } + // Everyone taking part in the thread gets a Notify chip: whoever the parent + // already p-tags, plus the author of every note in the reply chain. Members + // the user doesn't want to ping are muted via their chip's bell, not removed. + private fun threadMembers( + replyNote: Note, + threadNotes: List?, + ): List { + val mentions = + (replyNote.event as? TextNoteEvent) + ?.mentions() + ?.map { LocalCache.getOrCreateUser(it.pubKey) } + ?: emptyList() + val authors = threadNotes?.mapNotNull { it.author } ?: emptyList() + return (mentions + authors + listOfNotNull(replyNote.author)).distinct() + } + private fun loadFromDraft(draftEvent: TextNoteEvent) { canAddInvoice = accountViewModel.userProfile().lnAddress() != null canAddZapRaiser = accountViewModel.userProfile().lnAddress() != null @@ -786,6 +807,19 @@ open class ShortNotePostViewModel : privateNoteLocked = originalNote?.isPrivateRumor() == true wantsPrivateNote = privateNoteLocked + // A muted thread member is simply absent from the draft's p tags, so + // rebuild the full chip list and mark whoever the draft dropped as muted. + val draftNotifies = pTags.orEmpty().map { it.pubkeyHex }.toSet() + val members = + originalNote + ?.takeIf { it.event !is CommunityDefinitionEvent } + ?.let { threadMembers(it, eTags) } + .orEmpty() + mutedNotifies = members.mapNotNullTo(mutableSetOf()) { member -> member.pubkeyHex.takeIf { it !in draftNotifies } } + if (members.isNotEmpty()) { + pTags = (pTags.orEmpty() + members).distinct() + } + if (forwardZapTo.value.items.isNotEmpty()) { wantsForwardZapTo = true } @@ -845,6 +879,7 @@ open class ShortNotePostViewModel : draftEvent.tags.filter { it.size > 1 && it[0] == "p" }.mapNotNull { LocalCache.checkGetOrCreateUser(it[1]) } + mutedNotifies = emptySet() canUsePoll = originalNote == null canUseZapPoll = originalNote == null @@ -917,6 +952,7 @@ open class ShortNotePostViewModel : draftEvent.tags.filter { it.size > 1 && it[0] == "p" }.mapNotNull { LocalCache.checkGetOrCreateUser(it[1]) } + mutedNotifies = emptySet() canUsePoll = originalNote == null canUseZapPoll = originalNote == null @@ -1168,9 +1204,11 @@ open class ShortNotePostViewModel : val tags = prepareETagsAsReplyTo(replyingTo, null) accountViewModel.fixReplyTagHints(tags) markedETags(tags) - notify(replyingTo.toPTag()) + if (replyingTo.event.pubKey !in mutedNotifies) { + notify(replyingTo.toPTag()) + } } - pTags?.let { userList -> + activeNotifies()?.let { userList -> val tags = userList.map { val tag = it.toPTag() @@ -1190,7 +1228,7 @@ open class ShortNotePostViewModel : val tagger = NewMessageTagger( message.text.toString().trim(), - pTags, + activeNotifies(), eTags, accountViewModel, ) @@ -1201,7 +1239,7 @@ open class ShortNotePostViewModel : val geoHash = if (wantsToAddGeoHash) (location?.value as? LocationState.LocationResult.Success)?.geoHash?.toString() else null val localZapRaiserAmount = if (wantsZapRaiser) zapRaiserAmount.value else null - val emojis = findEmoji(tagger.message, account.emoji.myEmojis.value) + val emojis = account.emoji.findEmojiTags(tagger.message) val urls = findURLs(tagger.message) val usedAttachments = iMetaAttachments.filterIsIn(urls.toSet()) @@ -1381,16 +1419,6 @@ open class ShortNotePostViewModel : replyingToEvent.isClient(AccountCacheState.CLIENT_TAG_NAME) } - fun findEmoji( - message: String, - myEmojiSet: List?, - ): List { - if (myEmojiSet == null) return emptyList() - return CustomEmoji.findAllEmojiCodes(message).mapNotNull { possibleEmoji -> - myEmojiSet.firstOrNull { it.code == possibleEmoji }?.let { EmojiUrlTag(it.code, it.link) } - } - } - fun upload( alt: String?, contentWarningReason: String?, @@ -1512,6 +1540,7 @@ open class ShortNotePostViewModel : voiceSelectedServer = null voiceOrchestrator = null pTags = null + mutedNotifies = emptySet() wantsPoll = false pollOptions = newStateMapPollOptions() @@ -1562,10 +1591,6 @@ open class ShortNotePostViewModel : this.multiOrchestrator?.remove(selected) } - open fun removeFromReplyList(userToRemove: User) { - pTags = pTags?.filter { it != userToRemove } - } - open fun addToMessage(it: String) { message.setTextAndPlaceCursorAtEnd(message.text.toString() + " " + it) onMessageChanged() @@ -1622,13 +1647,9 @@ open class ShortNotePostViewModel : } open fun autocompleteWithEmoji(item: EmojiMedia) { - val wordToInsert = ":${item.code}:" - - message.replaceCurrentWord(wordToInsert) + emojiSuggestions?.autocompleteInto(message, item) urlPreviews.update(message.text.toString()) - emojiSuggestions?.reset() - draftTag.newVersion() } diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/nests/room/chat/NestEditFieldRow.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/nests/room/chat/NestEditFieldRow.kt index f6fa574b3e..88a8f1e51f 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/nests/room/chat/NestEditFieldRow.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/nests/room/chat/NestEditFieldRow.kt @@ -36,13 +36,13 @@ import androidx.compose.ui.text.input.KeyboardCapitalization import androidx.compose.ui.text.style.TextDirection import androidx.compose.ui.unit.dp import com.vitorpamplona.amethyst.R +import com.vitorpamplona.amethyst.commons.nip30CustomEmojis.ui.ShowEmojiSuggestionList import com.vitorpamplona.amethyst.ui.actions.MentionPreservingInputTransformation import com.vitorpamplona.amethyst.ui.actions.StrippingFailureDialog import com.vitorpamplona.amethyst.ui.actions.UrlUserTagOutputTransformation import com.vitorpamplona.amethyst.ui.actions.uploads.SelectFromGallery import com.vitorpamplona.amethyst.ui.components.ThinPaddingTextField import com.vitorpamplona.amethyst.ui.navigation.navs.INav -import com.vitorpamplona.amethyst.ui.note.creators.emojiSuggestions.ShowEmojiSuggestionList import com.vitorpamplona.amethyst.ui.note.creators.userSuggestions.ShowUserSuggestionList import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel import com.vitorpamplona.amethyst.ui.screen.loggedIn.chats.utils.DisplayReplyingToNote diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/nests/room/chat/NestNewMessageViewModel.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/nests/room/chat/NestNewMessageViewModel.kt index 44bfb87c27..63a55b94c4 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/nests/room/chat/NestNewMessageViewModel.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/nests/room/chat/NestNewMessageViewModel.kt @@ -35,6 +35,7 @@ import com.vitorpamplona.amethyst.Amethyst import com.vitorpamplona.amethyst.R import com.vitorpamplona.amethyst.commons.model.AddressableNote import com.vitorpamplona.amethyst.commons.model.nip30CustomEmojis.EmojiPackState +import com.vitorpamplona.amethyst.commons.model.nip30CustomEmojis.EmojiSuggestionState import com.vitorpamplona.amethyst.commons.richtext.UrlParser import com.vitorpamplona.amethyst.commons.ui.text.currentWord import com.vitorpamplona.amethyst.commons.ui.text.insertUrlAtCursor @@ -50,7 +51,6 @@ import com.vitorpamplona.amethyst.service.uploads.UploadOrchestrator import com.vitorpamplona.amethyst.ui.actions.NewMessageTagger import com.vitorpamplona.amethyst.ui.actions.uploads.SelectedMedia import com.vitorpamplona.amethyst.ui.note.creators.draftTags.DraftTagState -import com.vitorpamplona.amethyst.ui.note.creators.emojiSuggestions.EmojiSuggestionState import com.vitorpamplona.amethyst.ui.note.creators.expiration.IExpiration import com.vitorpamplona.amethyst.ui.note.creators.location.ILocationGrabber import com.vitorpamplona.amethyst.ui.note.creators.userSuggestions.UserSuggestionState @@ -75,8 +75,6 @@ import com.vitorpamplona.quartz.nip10Notes.content.findHashtags import com.vitorpamplona.quartz.nip10Notes.content.findNostrUris import com.vitorpamplona.quartz.nip10Notes.content.findURLs import com.vitorpamplona.quartz.nip18Reposts.quotes.quotes -import com.vitorpamplona.quartz.nip30CustomEmoji.CustomEmoji -import com.vitorpamplona.quartz.nip30CustomEmoji.EmojiUrlTag import com.vitorpamplona.quartz.nip30CustomEmoji.emojis import com.vitorpamplona.quartz.nip36SensitiveContent.contentWarning import com.vitorpamplona.quartz.nip36SensitiveContent.contentWarningReason @@ -212,7 +210,7 @@ open class NestNewMessageViewModel : ) this.emojiSuggestions?.reset() - this.emojiSuggestions = EmojiSuggestionState(accountVM.account) + this.emojiSuggestions = EmojiSuggestionState(accountVM.account.emoji) this.uploadState = ChatFileUploadState(account.settings.defaultFileServer, account.settings.stripLocationOnUpload) } @@ -427,7 +425,7 @@ open class NestNewMessageViewModel : val urls = findURLs(messageText) val usedAttachments = iMetaAttachments.filterIsIn(urls.toSet()) - val emojis = findEmoji(messageText, accountViewModel.account.emoji.myEmojis.value) + val emojis = accountViewModel.account.emoji.findEmojiTags(messageText) val geoHash = if (wantsToAddGeoHash) (location?.value as? LocationState.LocationResult.Success)?.geoHash?.toString() else null @@ -467,16 +465,6 @@ open class NestNewMessageViewModel : } } - fun findEmoji( - message: String, - myEmojiSet: List?, - ): List { - if (myEmojiSet == null) return emptyList() - return CustomEmoji.findAllEmojiCodes(message).mapNotNull { possibleEmoji -> - myEmojiSet.firstOrNull { it.code == possibleEmoji }?.let { EmojiUrlTag(it.code, it.link) } - } - } - open fun cancel() { draftTag.rotate() @@ -560,10 +548,7 @@ open class NestNewMessageViewModel : } open fun autocompleteWithEmoji(item: EmojiPackState.EmojiMedia) { - val wordToInsert = ":${item.code}:" - message.replaceCurrentWord(wordToInsert) - - emojiSuggestions?.reset() + emojiSuggestions?.autocompleteInto(message, item) draftTag.newVersion() } diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/notifications/dal/NotificationFeedFilter.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/notifications/dal/NotificationFeedFilter.kt index f10921bd06..124e578ad0 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/notifications/dal/NotificationFeedFilter.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/notifications/dal/NotificationFeedFilter.kt @@ -64,6 +64,7 @@ import com.vitorpamplona.quartz.nip53LiveActivities.streaming.LiveActivitiesEven import com.vitorpamplona.quartz.nip54Wiki.WikiNoteEvent import com.vitorpamplona.quartz.nip57Zaps.LnZapEvent import com.vitorpamplona.quartz.nip58Badges.award.BadgeAwardEvent +import com.vitorpamplona.quartz.nip61Nutzaps.nutzap.NutzapEvent import com.vitorpamplona.quartz.nip64Chess.challenge.accept.LiveChessGameAcceptEvent import com.vitorpamplona.quartz.nip64Chess.move.LiveChessMoveEvent import com.vitorpamplona.quartz.nip68Picture.PictureEvent @@ -79,6 +80,7 @@ import com.vitorpamplona.quartz.nip99Classifieds.ClassifiedsEvent import com.vitorpamplona.quartz.nipA0VoiceMessages.VoiceEvent import com.vitorpamplona.quartz.nipA0VoiceMessages.VoiceReplyEvent import com.vitorpamplona.quartz.nipA4PublicMessages.PublicMessageEvent +import com.vitorpamplona.quartz.nipBCOnchainZaps.zap.OnchainZapEvent import com.vitorpamplona.quartz.nipF4Podcasts.episode.PodcastEpisodeEvent import com.vitorpamplona.quartz.nipF4Podcasts.metadata.PodcastMetadataEvent import kotlinx.coroutines.flow.MutableStateFlow @@ -119,33 +121,47 @@ class NotificationFeedFilter( ) val NOTIFICATION_KINDS = - // The core subscription kinds are shared with Desktop through - // `commons/.../moderation/notifications/NotificationKinds.SUBSCRIPTION_KINDS` - // so a change on either platform automatically propagates. - // Android-only extras (badge awards, git issues/patches/PRs, - // highlights, polls, videos, voice, public messages, - // live-activities chat) stay here because Desktop has no - // rendering for those kinds today. - com.vitorpamplona.amethyst.commons.moderation.notifications.NotificationKinds - .SUBSCRIPTION_KINDS - .toSet() + - setOf( - BadgeAwardEvent.KIND, - GitIssueEvent.KIND, - GitPatchEvent.KIND, - GitPullRequestEvent.KIND, - GitPullRequestUpdateEvent.KIND, - HighlightEvent.KIND, - LiveActivitiesChatMessageEvent.KIND, - PictureEvent.KIND, - PollEvent.KIND, - ZapPollEvent.KIND, - PublicMessageEvent.KIND, - VideoNormalEvent.KIND, - VideoShortEvent.KIND, - VoiceEvent.KIND, - VoiceReplyEvent.KIND, - ) + ADDRESSABLE_KINDS + // Kinds that RENDER as a row on the Notifications tab. This is a + // display gate over whatever is already in LocalCache — it plays no + // part in relay subscriptions (those live in + // FilterNotificationsToPubkey, the chat datasources, and the wallet + // assembler). It deliberately does NOT share Desktop's + // `NotificationKinds.SUBSCRIPTION_KINDS`: that list answers "what to + // ask relays for / toast on" and includes envelope kinds like + // GiftWrap (1059), whose created_at is randomized up to 2 days back + // (NIP-59). Envelopes never render here — the unwrapped inner event + // (kind 14/15/…) is the feed row, same rule NotificationDispatcher + // applies to push. NotificationKindsContractTest pins the + // relationship between the two lists. + setOf( + BadgeAwardEvent.KIND, + ChannelMessageEvent.KIND, + ChatMessageEvent.KIND, + ChatMessageEncryptedFileHeaderEvent.KIND, + CommentEvent.KIND, + GenericRepostEvent.KIND, + GitIssueEvent.KIND, + GitPatchEvent.KIND, + GitPullRequestEvent.KIND, + GitPullRequestUpdateEvent.KIND, + HighlightEvent.KIND, + TextNoteEvent.KIND, + ReactionEvent.KIND, + RepostEvent.KIND, + LnZapEvent.KIND, + NutzapEvent.KIND, + OnchainZapEvent.KIND, + LiveActivitiesChatMessageEvent.KIND, + PictureEvent.KIND, + PollEvent.KIND, + ZapPollEvent.KIND, + PrivateDmEvent.KIND, + PublicMessageEvent.KIND, + VideoNormalEvent.KIND, + VideoShortEvent.KIND, + VoiceEvent.KIND, + VoiceReplyEvent.KIND, + ) + ADDRESSABLE_KINDS // How deep to walk a public chat reply chain looking for one of the // user's own messages. Bounds the cost on very long threads; the diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/notifications/publicMessages/NewPublicMessageScreen.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/notifications/publicMessages/NewPublicMessageScreen.kt index 06d46689e6..f10bd8db4e 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/notifications/publicMessages/NewPublicMessageScreen.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/notifications/publicMessages/NewPublicMessageScreen.kt @@ -55,6 +55,7 @@ import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.unit.dp import androidx.lifecycle.viewmodel.compose.viewModel import com.vitorpamplona.amethyst.R +import com.vitorpamplona.amethyst.commons.nip30CustomEmojis.ui.ShowEmojiSuggestionList import com.vitorpamplona.amethyst.ui.actions.MentionPreservingInputTransformation import com.vitorpamplona.amethyst.ui.actions.StrippingFailureDialog import com.vitorpamplona.amethyst.ui.actions.UrlUserTagOutputTransformation @@ -69,7 +70,6 @@ import com.vitorpamplona.amethyst.ui.navigation.topbars.PostingTopBar import com.vitorpamplona.amethyst.ui.note.NoteCompose import com.vitorpamplona.amethyst.ui.note.creators.contentWarning.ContentSensitivityExplainer import com.vitorpamplona.amethyst.ui.note.creators.contentWarning.MarkAsSensitiveButton -import com.vitorpamplona.amethyst.ui.note.creators.emojiSuggestions.ShowEmojiSuggestionList import com.vitorpamplona.amethyst.ui.note.creators.emojiSuggestions.WatchAndLoadMyEmojiList import com.vitorpamplona.amethyst.ui.note.creators.expiration.ExpirationDateButton import com.vitorpamplona.amethyst.ui.note.creators.expiration.ExpirationDatePicker diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/notifications/publicMessages/NewPublicMessageViewModel.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/notifications/publicMessages/NewPublicMessageViewModel.kt index a97e2068da..68ffbf6006 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/notifications/publicMessages/NewPublicMessageViewModel.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/notifications/publicMessages/NewPublicMessageViewModel.kt @@ -34,6 +34,7 @@ import androidx.lifecycle.viewModelScope import com.vitorpamplona.amethyst.Amethyst import com.vitorpamplona.amethyst.R import com.vitorpamplona.amethyst.commons.model.nip30CustomEmojis.EmojiPackState +import com.vitorpamplona.amethyst.commons.model.nip30CustomEmojis.EmojiSuggestionState import com.vitorpamplona.amethyst.commons.ui.text.currentWord import com.vitorpamplona.amethyst.commons.ui.text.insertUrlAtCursor import com.vitorpamplona.amethyst.commons.ui.text.replaceCurrentWord @@ -53,7 +54,6 @@ import com.vitorpamplona.amethyst.ui.actions.uploads.MediaUploadTracker import com.vitorpamplona.amethyst.ui.actions.uploads.SelectedMedia import com.vitorpamplona.amethyst.ui.actions.uploads.SelectedMediaProcessing import com.vitorpamplona.amethyst.ui.note.creators.draftTags.DraftTagState -import com.vitorpamplona.amethyst.ui.note.creators.emojiSuggestions.EmojiSuggestionState import com.vitorpamplona.amethyst.ui.note.creators.expiration.IExpiration import com.vitorpamplona.amethyst.ui.note.creators.location.ILocationGrabber import com.vitorpamplona.amethyst.ui.note.creators.messagefield.IMessageField @@ -84,8 +84,6 @@ import com.vitorpamplona.quartz.nip10Notes.content.findURLs import com.vitorpamplona.quartz.nip18Reposts.quotes.quotes import com.vitorpamplona.quartz.nip18Reposts.quotes.taggedQuoteIds import com.vitorpamplona.quartz.nip19Bech32.toNpub -import com.vitorpamplona.quartz.nip30CustomEmoji.CustomEmoji -import com.vitorpamplona.quartz.nip30CustomEmoji.EmojiUrlTag import com.vitorpamplona.quartz.nip30CustomEmoji.emojis import com.vitorpamplona.quartz.nip36SensitiveContent.contentWarning import com.vitorpamplona.quartz.nip36SensitiveContent.contentWarningReason @@ -222,7 +220,7 @@ class NewPublicMessageViewModel : this.userSuggestions = UserSuggestionState(accountVM.account, accountVM.nip05ClientBuilder()) this.emojiSuggestions?.reset() - this.emojiSuggestions = EmojiSuggestionState(accountVM.account) + this.emojiSuggestions = EmojiSuggestionState(accountVM.account.emoji) } fun load(users: Set) { @@ -393,7 +391,7 @@ class NewPublicMessageViewModel : val geoHash = (location?.value as? LocationState.LocationResult.Success)?.geoHash?.toString() val localZapRaiserAmount = if (wantsZapraiser) zapRaiserAmount.value else null - val emojis = findEmoji(tagger.message, account.emoji.myEmojis.value) + val emojis = account.emoji.findEmojiTags(tagger.message) val urls = findURLs(tagger.message) val usedAttachments = iMetaAttachments.filterIsIn(urls.toSet()) @@ -419,16 +417,6 @@ class NewPublicMessageViewModel : } } - fun findEmoji( - message: String, - myEmojiSet: List?, - ): List { - if (myEmojiSet == null) return emptyList() - return CustomEmoji.findAllEmojiCodes(message).mapNotNull { possibleEmoji -> - myEmojiSet.firstOrNull { it.code == possibleEmoji }?.let { EmojiUrlTag(it.code, it.link) } - } - } - fun upload( alt: String?, contentWarningReason: String?, @@ -633,13 +621,9 @@ class NewPublicMessageViewModel : } fun autocompleteWithEmoji(item: EmojiPackState.EmojiMedia) { - val wordToInsert = ":${item.code}:" - - message.replaceCurrentWord(wordToInsert) + emojiSuggestions?.autocompleteInto(message, item) urlPreviews.update(message.text.toString()) - emojiSuggestions?.reset() - draftTag.newVersion() } diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/profile/header/DrawAdditionalInfo.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/profile/header/DrawAdditionalInfo.kt index fd9a2d1d5f..17b801c28a 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/profile/header/DrawAdditionalInfo.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/profile/header/DrawAdditionalInfo.kt @@ -113,6 +113,10 @@ fun DrawAdditionalInfo( val showAppRecommendations by ui.showProfileAppRecommendations.collectAsStateWithLifecycle() Column(modifier = Modifier.fillMaxWidth(), verticalArrangement = SpacedBy3dp) { + // the nickname the account gave this user, on top of (not replacing) + // the profile's own display name below + UserNicknameCard(baseUser, accountViewModel) + if (displayName != null) { Row( verticalAlignment = Alignment.CenterVertically, diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/profile/header/UserNicknameCard.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/profile/header/UserNicknameCard.kt new file mode 100644 index 0000000000..a09f1d78bb --- /dev/null +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/profile/header/UserNicknameCard.kt @@ -0,0 +1,125 @@ +/* + * Copyright (c) 2025 Vitor Pamplona + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.vitorpamplona.amethyst.ui.screen.loggedIn.profile.header + +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.layout.size +import androidx.compose.material3.HorizontalDivider +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.OutlinedCard +import androidx.compose.runtime.Composable +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.text.font.FontWeight +import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.sp +import com.vitorpamplona.amethyst.R +import com.vitorpamplona.amethyst.commons.icons.symbols.Icon +import com.vitorpamplona.amethyst.commons.icons.symbols.MaterialSymbols +import com.vitorpamplona.amethyst.commons.nip85TrustedAssertions.ui.EditNicknameDialog +import com.vitorpamplona.amethyst.model.User +import com.vitorpamplona.amethyst.service.relayClient.reqCommand.user.observeUserNickname +import com.vitorpamplona.amethyst.ui.components.CreateTextWithEmoji +import com.vitorpamplona.amethyst.ui.note.creators.emojiSuggestions.WatchAndLoadMyEmojiList +import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel +import com.vitorpamplona.amethyst.ui.stringRes +import com.vitorpamplona.amethyst.ui.theme.DividerThickness +import com.vitorpamplona.amethyst.ui.theme.placeholderText + +/** + * The nickname (petname) and private note the account keeps for [baseUser], + * shown above the profile's own display name without replacing it. The lock + * marks it as private — both fields live NIP-44 encrypted in the account's + * contact card. Tapping the card opens the editor. + */ +@Composable +fun UserNicknameCard( + baseUser: User, + accountViewModel: AccountViewModel, +) { + val nickname by observeUserNickname(baseUser, accountViewModel) + val card = nickname ?: return + + val isEditDialogOpen = remember { mutableStateOf(false) } + + if (isEditDialogOpen.value) { + // keeps the account's selected emoji packs loaded for the : autocomplete + WatchAndLoadMyEmojiList(accountViewModel) + EditNicknameDialog( + user = baseUser, + contactCards = accountViewModel.account.contactCards, + onSave = { petName, summary -> accountViewModel.updateContactCardPetName(baseUser, petName, summary) }, + onDismiss = { isEditDialogOpen.value = false }, + ) + } + + OutlinedCard( + onClick = { isEditDialogOpen.value = true }, + modifier = Modifier.fillMaxWidth().padding(top = 14.dp, bottom = 3.5.dp), + ) { + Box(modifier = Modifier.fillMaxWidth()) { + Column( + modifier = Modifier.fillMaxWidth().padding(horizontal = 12.dp, vertical = 10.dp), + verticalArrangement = Arrangement.spacedBy(6.dp), + ) { + card.petName?.let { + CreateTextWithEmoji( + text = it, + tags = card.tags, + fontWeight = FontWeight.Bold, + fontSize = 20.sp, + ) + } + + if (card.petName != null && card.summary != null) { + HorizontalDivider(thickness = DividerThickness) + } + + card.summary?.let { + CreateTextWithEmoji( + text = it, + tags = card.tags, + color = MaterialTheme.colorScheme.placeholderText, + fontSize = 14.sp, + ) + } + } + + Icon( + symbol = MaterialSymbols.Lock, + contentDescription = stringRes(R.string.nickname_private), + tint = MaterialTheme.colorScheme.placeholderText, + modifier = + Modifier + .align(Alignment.TopEnd) + .padding(top = 8.dp, end = 8.dp) + .size(14.dp), + ) + } + } +} diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/profile/header/UserProfileDropDownMenu.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/profile/header/UserProfileDropDownMenu.kt index 0fc0096a51..258f7dbf82 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/profile/header/UserProfileDropDownMenu.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/profile/header/UserProfileDropDownMenu.kt @@ -22,16 +22,20 @@ package com.vitorpamplona.amethyst.ui.screen.loggedIn.profile.header import android.content.Intent import androidx.compose.runtime.Composable +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.ui.platform.LocalClipboard import androidx.compose.ui.platform.LocalContext import com.vitorpamplona.amethyst.R import com.vitorpamplona.amethyst.commons.icons.symbols.MaterialSymbols +import com.vitorpamplona.amethyst.commons.nip85TrustedAssertions.ui.EditNicknameDialog import com.vitorpamplona.amethyst.model.User import com.vitorpamplona.amethyst.ui.components.M3ActionDialog import com.vitorpamplona.amethyst.ui.components.M3ActionRow import com.vitorpamplona.amethyst.ui.components.M3ActionSection import com.vitorpamplona.amethyst.ui.components.util.setText +import com.vitorpamplona.amethyst.ui.note.creators.emojiSuggestions.WatchAndLoadMyEmojiList import com.vitorpamplona.amethyst.ui.note.externalLinkForUser import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel import com.vitorpamplona.amethyst.ui.stringRes @@ -45,6 +49,19 @@ fun UserProfileDropDownMenu( onDismiss: () -> Unit, accountViewModel: AccountViewModel, ) { + val isNicknameDialogOpen = remember { mutableStateOf(false) } + + if (isNicknameDialogOpen.value) { + // keeps the account's selected emoji packs loaded for the : autocomplete + WatchAndLoadMyEmojiList(accountViewModel) + EditNicknameDialog( + user = user, + contactCards = accountViewModel.account.contactCards, + onSave = { petName, summary -> accountViewModel.updateContactCardPetName(user, petName, summary) }, + onDismiss = { isNicknameDialogOpen.value = false }, + ) + } + if (!popupExpanded) return M3ActionDialog( @@ -88,6 +105,16 @@ fun UserProfileDropDownMenu( // Moderation section (if not self) if (accountViewModel.userProfile() != user) { + M3ActionSection { + M3ActionRow( + icon = MaterialSymbols.Edit, + text = stringRes(R.string.edit_nickname), + ) { + isNicknameDialogOpen.value = true + onDismiss() + } + } + M3ActionSection { if (accountViewModel.account.isHidden(user)) { M3ActionRow( diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/settings/ResourceUsageScreen.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/settings/ResourceUsageScreen.kt new file mode 100644 index 0000000000..4e841ad4ab --- /dev/null +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/settings/ResourceUsageScreen.kt @@ -0,0 +1,608 @@ +/* + * Copyright (c) 2025 Vitor Pamplona + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.vitorpamplona.amethyst.ui.screen.loggedIn.settings + +import androidx.annotation.StringRes +import androidx.compose.foundation.background +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.fillMaxHeight +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.height +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.rememberScrollState +import androidx.compose.foundation.shape.RoundedCornerShape +import androidx.compose.foundation.verticalScroll +import androidx.compose.material3.Button +import androidx.compose.material3.Card +import androidx.compose.material3.CardDefaults +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.Scaffold +import androidx.compose.material3.Text +import androidx.compose.material3.TextButton +import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.produceState +import androidx.compose.runtime.remember +import androidx.compose.runtime.setValue +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.draw.clip +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.res.pluralStringResource +import androidx.compose.ui.text.font.FontWeight +import androidx.compose.ui.unit.dp +import com.vitorpamplona.amethyst.Amethyst +import com.vitorpamplona.amethyst.MemorySnapshot +import com.vitorpamplona.amethyst.R +import com.vitorpamplona.amethyst.collectMemorySnapshot +import com.vitorpamplona.amethyst.model.LocalCache +import com.vitorpamplona.amethyst.service.crashreports.DEV_REPORT_PUBKEY +import com.vitorpamplona.amethyst.service.resourceusage.ResourceUsageAccountant +import com.vitorpamplona.amethyst.service.resourceusage.ResourceUsageReportAssembler +import com.vitorpamplona.amethyst.service.resourceusage.ResourceUsageReportAssembler.Companion.formatBytes +import com.vitorpamplona.amethyst.service.resourceusage.ResourceUsageReportAssembler.Companion.formatConnHours +import com.vitorpamplona.amethyst.service.resourceusage.ResourceUsageReportAssembler.Companion.formatDurationMs +import com.vitorpamplona.amethyst.service.resourceusage.UsageSummary +import com.vitorpamplona.amethyst.ui.navigation.navs.INav +import com.vitorpamplona.amethyst.ui.navigation.routes.Route +import com.vitorpamplona.amethyst.ui.navigation.routes.routeToMessage +import com.vitorpamplona.amethyst.ui.navigation.topbars.TopBarWithBackButton +import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel +import com.vitorpamplona.amethyst.ui.stringRes +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.delay +import kotlinx.coroutines.withContext +import java.util.Locale + +/** + * The resource-usage ledger: how much network, relay connection time, and + * background activity the app consumed, per subsystem — with an explicit, + * user-initiated path to DM the numbers to the developers (same NIP-17 flow + * as crash reports). Everything on this screen stays on-device until the + * user sends that DM. + * + * Layout: headline stat tiles (today) → 7-day trend chart → per-feature + * proportion bars → activity counters → live memory meters → send card. + */ +@Composable +fun ResourceUsageScreen( + accountViewModel: AccountViewModel, + nav: INav, +) { + var days by remember { mutableStateOf>?>(null) } + + LaunchedEffect(Unit) { + withContext(Dispatchers.IO) { + days = Amethyst.instance.resourceUsage.allDaysIncludingLive() + } + } + + // Live memory snapshot, refreshed only while this screen is composed. + // Collected off-main: DiskLruCache.size() contends with journal I/O. + val context = LocalContext.current + val memory by produceState(null) { + while (true) { + value = withContext(Dispatchers.IO) { collectMemorySnapshot(context) } + delay(2_000) + } + } + + Scaffold( + topBar = { TopBarWithBackButton(stringRes(id = R.string.resource_usage_title), nav) }, + ) { padding -> + Column( + modifier = + Modifier + .padding(padding) + .verticalScroll(rememberScrollState()) + .padding(horizontal = 16.dp, vertical = 12.dp), + verticalArrangement = Arrangement.spacedBy(20.dp), + ) { + val loaded = days + if (loaded == null) { + Text( + text = stringRes(R.string.resource_usage_empty), + style = MaterialTheme.typography.bodyMedium, + color = MaterialTheme.colorScheme.onSurfaceVariant, + ) + } else { + // remember(loaded): the memory produceState recomposes this + // scope every 2s, and the summaries walk every counter of + // every retained day — recompute only when the data reloads. + val today = remember(loaded) { Amethyst.instance.resourceUsage.today() } + val todaySummary = remember(loaded) { UsageSummary.from(loaded[today].orEmpty()) } + val weekSummary = remember(loaded) { UsageSummary.fromDays((today - 6..today).mapNotNull { loaded[it] }) } + + TodayTiles(todaySummary) + WeekRatesTiles(weekSummary) + if (weekSummary.totalBytes > 0) { + SettingsSection(R.string.resource_usage_trend_section) { + UsageTrendChart(loaded, today) + } + } + SubsystemSection(weekSummary) + ScreenTimeSection(weekSummary) + ActivitySection(weekSummary) + AlwaysOnServiceSection(weekSummary, nav) + TorServiceSection(weekSummary, nav) + MemorySection(memory) + SendReportSection(accountViewModel, nav, loaded, today, memory) + } + } + } +} + +/** Headline numbers for today as a 2x2 tile grid. */ +@Composable +private fun TodayTiles(s: UsageSummary) { + Column(verticalArrangement = Arrangement.spacedBy(12.dp)) { + Row(horizontalArrangement = Arrangement.spacedBy(12.dp)) { + StatTile(R.string.resource_usage_tile_cellular, formatBytes(s.mobileBytes), Modifier.weight(1f)) + StatTile(R.string.resource_usage_tile_wifi, formatBytes(s.wifiBytesBg + s.wifiBytesFg), Modifier.weight(1f)) + } + Row(horizontalArrangement = Arrangement.spacedBy(12.dp)) { + StatTile(R.string.resource_usage_tile_relay, formatConnHours(s.relayConnMs), Modifier.weight(1f)) + StatTile(R.string.resource_usage_tile_in_app, formatDurationMs(s.foregroundMs), Modifier.weight(1f)) + } + } +} + +@Composable +private fun StatTile( + @StringRes label: Int, + value: String, + modifier: Modifier = Modifier, +) { + Card( + modifier = modifier, + shape = RoundedCornerShape(20.dp), + colors = CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.surfaceContainerLow), + elevation = CardDefaults.cardElevation(defaultElevation = 0.dp), + ) { + Column( + modifier = Modifier.padding(16.dp), + verticalArrangement = Arrangement.spacedBy(4.dp), + ) { + Text( + text = stringRes(label), + style = MaterialTheme.typography.labelMedium, + color = MaterialTheme.colorScheme.onSurfaceVariant, + ) + Text( + text = value, + style = MaterialTheme.typography.titleLarge, + fontWeight = FontWeight.SemiBold, + ) + } + } +} + +/** + * 7-day rates: totals aren't judgeable, rates are. Battery %/hour of use is + * the most tangible battery number we can show; average simultaneous relay + * connections is the number a user can act on by trimming their relay list. + */ +@Composable +private fun WeekRatesTiles(s: UsageSummary) { + val fgHours = s.foregroundMs / 3_600_000.0 + val tiles = + buildList { + if (fgHours >= 0.5 && s.batteryDrainFg > 0) { + add(R.string.resource_usage_tile_battery_rate to String.format(Locale.US, "%.1f%%", s.batteryDrainFg / fgHours)) + } + if (fgHours >= 0.5) { + add(R.string.resource_usage_tile_data_rate to formatBytes(((s.mobileBytesFg + s.wifiBytesFg) / fgHours).toLong())) + } + val avgRelays = s.relayConnMs.toDouble() / (s.dayCount * ResourceUsageAccountant.DAY_MS) + if (avgRelays >= 0.5) { + add(R.string.resource_usage_tile_avg_relays to String.format(Locale.US, "%.1f", avgRelays)) + } + } + if (tiles.isEmpty()) return + Row(horizontalArrangement = Arrangement.spacedBy(12.dp)) { + tiles.forEach { (label, value) -> StatTile(label, value, Modifier.weight(1f)) } + } +} + +/** + * Ranked per-feature traffic with proportion bars (7 days). Cellular is the + * scarce resource (battery and often money), so when any cellular traffic + * exists the ranking, bars, and headline value are cellular — with the total + * as secondary context. Wi-Fi-only devices fall back to totals. + */ +@Composable +private fun SubsystemSection(week: UsageSummary) { + val cellular = week.mobileBytesPerSubsystem + if (cellular.isNotEmpty()) { + val rows = cellular.entries.sortedByDescending { it.value } + val max = rows.first().value.coerceAtLeast(1L) + SettingsSection(R.string.resource_usage_by_subsystem_cellular) { + rows.forEach { (subsystem, bytes) -> + BarRow( + label = subsystemLabel(subsystem), + value = stringRes(R.string.resource_usage_cell_of_total, formatBytes(bytes), formatBytes(week.bytesPerSubsystem[subsystem] ?: bytes)), + fraction = bytes.toFloat() / max.toFloat(), + ) + } + } + return + } + if (week.bytesPerSubsystem.isEmpty()) return + val rows = week.bytesPerSubsystem.entries.sortedByDescending { it.value } + val max = rows.first().value.coerceAtLeast(1L) + SettingsSection(R.string.resource_usage_by_subsystem) { + rows.forEach { (subsystem, bytes) -> + BarRow( + label = subsystemLabel(subsystem), + value = formatBytes(bytes), + fraction = bytes.toFloat() / max.toFloat(), + ) + } + } +} + +/** + * Where the screen-on time went (7 days). Route base names only — the + * ledger never records which profile/hashtag/thread a screen showed. + */ +@Composable +private fun ScreenTimeSection(week: UsageSummary) { + if (week.screenTimeMs.isEmpty()) return + val rows = + week.screenTimeMs.entries + .sortedByDescending { it.value } + .take(6) + val max = rows.first().value.coerceAtLeast(1L) + SettingsSection(R.string.resource_usage_screen_time_section) { + rows.forEach { (name, ms) -> + BarRow( + label = name, + value = formatDurationMs(ms), + fraction = ms.toFloat() / max.toFloat(), + ) + } + } +} + +@StringRes +private fun subsystemLabel(subsystem: String): Int = + when (subsystem) { + "relay" -> R.string.resource_usage_subsystem_relay + "image" -> R.string.resource_usage_subsystem_image + "video" -> R.string.resource_usage_subsystem_video + "uploads" -> R.string.resource_usage_subsystem_uploads + "money" -> R.string.resource_usage_subsystem_money + "nip05" -> R.string.resource_usage_subsystem_nip05 + "preview" -> R.string.resource_usage_subsystem_preview + "push" -> R.string.resource_usage_subsystem_push + else -> R.string.resource_usage_subsystem_other + } + +@Composable +private fun BarRow( + @StringRes label: Int, + value: String, + fraction: Float, + color: Color = MaterialTheme.colorScheme.primary, +) = BarRow(stringRes(label), value, fraction, color) + +/** Label + value + a thin rounded proportion bar underneath. */ +@Composable +private fun BarRow( + label: String, + value: String, + fraction: Float, + color: Color = MaterialTheme.colorScheme.primary, +) { + Column( + modifier = Modifier.fillMaxWidth().padding(horizontal = 16.dp, vertical = 10.dp), + verticalArrangement = Arrangement.spacedBy(6.dp), + ) { + Row(verticalAlignment = Alignment.CenterVertically) { + Text( + text = label, + style = MaterialTheme.typography.bodyMedium, + modifier = Modifier.weight(1f), + ) + Text( + text = value, + style = MaterialTheme.typography.bodyMedium, + fontWeight = FontWeight.SemiBold, + ) + } + Box( + modifier = + Modifier + .fillMaxWidth() + .height(8.dp) + .clip(RoundedCornerShape(4.dp)) + .background(MaterialTheme.colorScheme.surfaceVariant), + ) { + Box( + modifier = + Modifier + .fillMaxWidth(fraction.coerceIn(0f, 1f)) + .fillMaxHeight() + .clip(RoundedCornerShape(4.dp)) + .background(color), + ) + } + } +} + +/** Background/CPU activity counters for the last 7 days. */ +@Composable +private fun ActivitySection(s: UsageSummary) { + SettingsSection(R.string.resource_usage_activity_section) { + MetricRow(R.string.resource_usage_relay_time, formatConnHours(s.relayConnMs)) + SettingsDivider() + MetricRow(R.string.resource_usage_relay_time_bg_mobile, formatConnHours(s.relayConnMsMobileBg)) + SettingsDivider() + MetricRow(R.string.resource_usage_reconnects, "${s.relayConnects} (${s.relayConnectFails})") + SettingsDivider() + MetricRow(R.string.resource_usage_http_requests, s.httpRequests.toString()) + SettingsDivider() + MetricRow(R.string.resource_usage_radio_bursts, s.radioBursts.toString()) + SettingsDivider() + MetricRow(R.string.resource_usage_http_active, formatDurationMs(s.httpActiveMs)) + SettingsDivider() + MetricRow(R.string.resource_usage_media_play, formatDurationMs(s.mediaPlayMs)) + SettingsDivider() + MetricRow(R.string.resource_usage_verifies, s.verifyCount.toString()) + SettingsDivider() + MetricRow(R.string.resource_usage_cpu, formatDurationMs(s.cpuMs)) + SettingsDivider() + MetricRow(R.string.resource_usage_wakelock, formatDurationMs(s.wakelockMs)) + SettingsDivider() + MetricRow(R.string.resource_usage_worker_runs, s.workerRuns.toString()) + SettingsDivider() + MetricRow(R.string.resource_usage_app_starts, s.appStarts.toString()) + if (s.decryptCount > 0) { + SettingsDivider() + MetricRow(R.string.resource_usage_decrypts, s.decryptCount.toString()) + } + if (s.signNip46 + s.signNip55 > 0) { + SettingsDivider() + MetricRow(R.string.resource_usage_remote_signs, (s.signNip46 + s.signNip55).toString()) + } + if (s.powMs > 0) { + SettingsDivider() + MetricRow(R.string.resource_usage_pow, formatDurationMs(s.powMs)) + } + if (s.torMs > 0) { + SettingsDivider() + MetricRow(R.string.resource_usage_tor, formatDurationMs(s.torMs)) + } + if (s.callMs > 0) { + SettingsDivider() + MetricRow(R.string.resource_usage_calls, formatDurationMs(s.callMs)) + } + if (s.nestsMs > 0) { + SettingsDivider() + MetricRow(R.string.resource_usage_nests, formatDurationMs(s.nestsMs)) + } + if (s.locationMs > 0) { + SettingsDivider() + MetricRow(R.string.resource_usage_location, formatDurationMs(s.locationMs)) + } + if (s.batteryDrainFg + s.batteryDrainBg > 0) { + SettingsDivider() + MetricRow(R.string.resource_usage_battery_drain, "${s.batteryDrainFg}% / ${s.batteryDrainBg}%") + } + } +} + +@Composable +private fun MetricRow( + @StringRes label: Int, + value: String, +) { + Row( + modifier = Modifier.fillMaxWidth().padding(horizontal = 16.dp, vertical = 12.dp), + verticalAlignment = Alignment.CenterVertically, + ) { + Text( + text = stringRes(label), + style = MaterialTheme.typography.bodyMedium, + modifier = Modifier.weight(1f), + ) + Text( + text = value, + style = MaterialTheme.typography.bodyMedium, + fontWeight = FontWeight.SemiBold, + ) + } +} + +/** + * Live memory meters. The heap bar keeps the retired debug chip's status + * semantics: green below 60% of the max heap, amber to 80%, red above. + */ +@Composable +private fun MemorySection(memory: MemorySnapshot?) { + if (memory == null) return + val heapColor = + when { + memory.heapFraction > 0.80f -> Color(0xFFE53935) + memory.heapFraction > 0.60f -> Color(0xFFFFA000) + else -> Color(0xFF43A047) + } + SettingsSection(R.string.resource_usage_memory_section) { + BarRow( + label = R.string.resource_usage_memory_heap, + value = "${memory.heapUsedMb} / ${memory.heapMaxMb} MB", + fraction = memory.heapFraction, + color = heapColor, + ) + BarRow( + label = R.string.resource_usage_memory_image_cache, + value = "${memory.imageCacheUsedMb} / ${memory.imageCacheMaxMb} MB", + fraction = memory.imageCacheUsedMb.toFloat() / memory.imageCacheMaxMb.coerceAtLeast(1L).toFloat(), + ) + BarRow( + label = R.string.resource_usage_memory_image_disk, + value = "${memory.imageDiskUsedMb} / ${memory.imageDiskMaxMb} MB", + fraction = memory.imageDiskUsedMb.toFloat() / memory.imageDiskMaxMb.coerceAtLeast(1L).toFloat(), + ) + SettingsDivider() + MetricRow(R.string.resource_usage_memory_native, "${memory.nativeHeapUsedMb} MB") + SettingsDivider() + MetricRow(R.string.resource_usage_memory_notes, memory.noteCount.toString()) + SettingsDivider() + MetricRow(R.string.resource_usage_memory_users, memory.userCount.toString()) + SettingsDivider() + MetricRow(R.string.resource_usage_memory_addressables, memory.addressableCount.toString()) + SettingsDivider() + MetricRow(R.string.resource_usage_memory_chatrooms, memory.chatroomCount.toString()) + SettingsDivider() + MetricRow(R.string.resource_usage_memory_device_class, "${memory.memoryClassMb} MB") + } +} + +/** + * Decision-support card for the always-on notification service — the one + * background consumer users can directly turn off. Shows what the service + * actually costs in the last 7 days (uptime, the relay connections it holds + * while the app is closed, and the measured background battery drain) and + * links straight to the setting that controls it. + */ +@Composable +private fun AlwaysOnServiceSection( + s: UsageSummary, + nav: INav, +) { + if (s.alwaysOnMs <= 0) return + val starts = s.alwaysOnStarts.toInt() + SettingsSection(R.string.resource_usage_alwayson_section) { + Column( + modifier = Modifier.padding(16.dp), + verticalArrangement = Arrangement.spacedBy(12.dp), + ) { + Text( + text = stringRes(R.string.resource_usage_alwayson_explanation), + style = MaterialTheme.typography.bodyMedium, + color = MaterialTheme.colorScheme.onSurfaceVariant, + ) + } + MetricRow( + R.string.resource_usage_alwayson_uptime, + "${formatDurationMs(s.alwaysOnMs)} · ${pluralStringResource(R.plurals.resource_usage_alwayson_starts, starts, starts)}", + ) + SettingsDivider() + MetricRow( + R.string.resource_usage_alwayson_bg_relay, + formatConnHours(s.relayConnMsMobileBg + s.relayConnMsWifiBg), + ) + if (s.batteryDrainBg > 0) { + SettingsDivider() + MetricRow(R.string.resource_usage_alwayson_battery, "${s.batteryDrainBg}%") + } + Column(modifier = Modifier.padding(horizontal = 8.dp, vertical = 4.dp)) { + TextButton( + onClick = { nav.nav(Route.NotificationSettings) }, + modifier = Modifier.align(Alignment.End), + ) { + Text(stringRes(R.string.resource_usage_alwayson_settings_button)) + } + } + } +} + +/** + * Cost card for in-app Tor — like the always-on card: what it cost this + * week and the settings surface that controls it. + */ +@Composable +private fun TorServiceSection( + s: UsageSummary, + nav: INav, +) { + if (s.torMs <= 0) return + val starts = s.torStarts.toInt() + SettingsSection(R.string.resource_usage_tor_section) { + Column( + modifier = Modifier.padding(16.dp), + verticalArrangement = Arrangement.spacedBy(12.dp), + ) { + Text( + text = stringRes(R.string.resource_usage_tor_explanation), + style = MaterialTheme.typography.bodyMedium, + color = MaterialTheme.colorScheme.onSurfaceVariant, + ) + } + MetricRow( + R.string.resource_usage_alwayson_uptime, + "${formatDurationMs(s.torMs)} · ${pluralStringResource(R.plurals.resource_usage_alwayson_starts, starts, starts)}", + ) + Column(modifier = Modifier.padding(horizontal = 8.dp, vertical = 4.dp)) { + TextButton( + onClick = { nav.nav(Route.PrivacyOptions) }, + modifier = Modifier.align(Alignment.End), + ) { + Text(stringRes(R.string.resource_usage_tor_settings_button)) + } + } + } +} + +@Composable +private fun SendReportSection( + accountViewModel: AccountViewModel, + nav: INav, + days: Map>, + today: Long, + memory: MemorySnapshot?, +) { + SettingsSection(R.string.resource_usage_send_section) { + Column( + modifier = Modifier.padding(16.dp), + verticalArrangement = Arrangement.spacedBy(12.dp), + ) { + Text( + text = stringRes(R.string.resource_usage_send_explanation), + style = MaterialTheme.typography.bodyMedium, + color = MaterialTheme.colorScheme.onSurfaceVariant, + ) + Button( + onClick = { + val report = ResourceUsageReportAssembler().buildReport(days, today, memory) + nav.nav { + routeToMessage( + user = LocalCache.getOrCreateUser(DEV_REPORT_PUBKEY), + draftMessage = report, + accountViewModel = accountViewModel, + expiresDays = 30, + ) + } + }, + modifier = Modifier.align(Alignment.End), + ) { + Text(stringRes(R.string.resource_usage_send_button)) + } + } + } +} diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/settings/SettingsCatalogBuilder.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/settings/SettingsCatalogBuilder.kt index 3ace415fd7..5e85e88c94 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/settings/SettingsCatalogBuilder.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/settings/SettingsCatalogBuilder.kt @@ -103,6 +103,7 @@ fun buildSettingsCatalog( symEntry(R.string.calendar_reminder_settings_title, MaterialSymbols.CalendarMonth, R.string.calendar_reminder_search_keywords, Route.CalendarReminderSettings), symEntry(R.string.ots_explorer_settings, MaterialSymbols.Search, R.string.ots_explorer_search_keywords, Route.OtsSettings), symEntry(R.string.namecoin_settings, MaterialSymbols.Security, R.string.namecoin_search_keywords, Route.NamecoinSettings), + symEntry(R.string.resource_usage_title, MaterialSymbols.Bolt, R.string.resource_usage_search_keywords, Route.ResourceUsage), ), ) diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/settings/UsageTrendChart.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/settings/UsageTrendChart.kt new file mode 100644 index 0000000000..fed0882e3b --- /dev/null +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/settings/UsageTrendChart.kt @@ -0,0 +1,174 @@ +/* + * Copyright (c) 2025 Vitor Pamplona + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.vitorpamplona.amethyst.ui.screen.loggedIn.settings + +import androidx.annotation.StringRes +import androidx.compose.foundation.background +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.height +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.layout.size +import androidx.compose.foundation.shape.CircleShape +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.runtime.remember +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.graphics.luminance +import androidx.compose.ui.unit.dp +import com.patrykandpatrick.vico.compose.cartesian.CartesianChartHost +import com.patrykandpatrick.vico.compose.cartesian.CartesianMeasuringContext +import com.patrykandpatrick.vico.compose.cartesian.axis.Axis +import com.patrykandpatrick.vico.compose.cartesian.axis.HorizontalAxis +import com.patrykandpatrick.vico.compose.cartesian.axis.VerticalAxis +import com.patrykandpatrick.vico.compose.cartesian.data.CartesianChartModel +import com.patrykandpatrick.vico.compose.cartesian.data.CartesianValueFormatter +import com.patrykandpatrick.vico.compose.cartesian.data.LineCartesianLayerModel +import com.patrykandpatrick.vico.compose.cartesian.layer.LineCartesianLayer +import com.patrykandpatrick.vico.compose.cartesian.rememberCartesianChart +import com.vitorpamplona.amethyst.R +import com.vitorpamplona.amethyst.service.resourceusage.UsageSummary +import com.vitorpamplona.amethyst.ui.screen.loggedIn.notifications.chart.LastWeekLabelFormatter +import com.vitorpamplona.amethyst.ui.screen.loggedIn.notifications.chart.makeLine +import com.vitorpamplona.amethyst.ui.stringRes +import com.vitorpamplona.amethyst.ui.theme.BitcoinOrange +import com.vitorpamplona.amethyst.ui.theme.RoyalBlue +import kotlin.math.roundToInt + +/** + * Cellular-on-dark-surfaces variant of BitcoinOrange. The light/dark pairs + * (#F7931A + #4169E1 on light; #C77414 + #4169E1 on dark) were validated for + * CVD separation, lightness band, and chroma with the dataviz palette checks; + * the light orange's low surface contrast is relieved by the text legend. + */ +private val CellularOnDark = Color(0xFFC77414) + +/** + * 7-day data-per-day trend: one line per network class, MB per day, today at + * the right edge. Reuses the notification chart's Vico idioms ([makeLine], + * [LastWeekLabelFormatter], x = -6..0 with today at 0). + */ +@Composable +fun UsageTrendChart( + days: Map>, + today: Long, +) { + val isDark = MaterialTheme.colorScheme.background.luminance() < 0.5f + val cellularColor = if (isDark) CellularOnDark else BitcoinOrange + val wifiColor = RoyalBlue + + val model = + remember(days, today) { + val xs = (-6..0).toList() + val cellular = + xs.map { offset -> + UsageSummary.from(days[today + offset].orEmpty()).mobileBytes.toMb() + } + val wifi = + xs.map { offset -> + val s = UsageSummary.from(days[today + offset].orEmpty()) + (s.wifiBytesBg + s.wifiBytesFg).toMb() + } + CartesianChartModel( + LineCartesianLayerModel.build { + series(xs, cellular) + series(xs, wifi) + }, + ) + } + + val chart = + rememberCartesianChart( + layers = + arrayOf( + LineCartesianLayer( + LineCartesianLayer.LineProvider.series( + makeLine(cellularColor), + makeLine(wifiColor), + ), + ), + ), + startAxis = + VerticalAxis.rememberStart( + valueFormatter = MegabyteAxisFormatter, + itemPlacer = VerticalAxis.ItemPlacer.count({ 5 }), + ), + bottomAxis = + HorizontalAxis.rememberBottom( + valueFormatter = LastWeekLabelFormatter(), + ), + ) + + Column( + modifier = Modifier.padding(horizontal = 16.dp, vertical = 12.dp), + verticalArrangement = Arrangement.spacedBy(10.dp), + ) { + CartesianChartHost( + chart = chart, + model = model, + modifier = Modifier.fillMaxWidth().height(170.dp), + ) + Row(horizontalArrangement = Arrangement.spacedBy(16.dp)) { + LegendEntry(cellularColor, R.string.resource_usage_legend_cellular) + LegendEntry(wifiColor, R.string.resource_usage_legend_wifi) + } + } +} + +@Composable +private fun LegendEntry( + color: Color, + @StringRes label: Int, +) { + Row( + verticalAlignment = Alignment.CenterVertically, + horizontalArrangement = Arrangement.spacedBy(6.dp), + ) { + Box(Modifier.size(10.dp).background(color, CircleShape)) + Text( + text = stringRes(label), + style = MaterialTheme.typography.labelMedium, + color = MaterialTheme.colorScheme.onSurfaceVariant, + ) + } +} + +private fun Long.toMb(): Float = this / 1_048_576f + +private val MegabyteAxisFormatter = + object : CartesianValueFormatter { + override fun format( + context: CartesianMeasuringContext, + value: Double, + verticalAxisPosition: Axis.Position.Vertical?, + ): CharSequence = + if (value >= 1024) { + "%.1fG".format(value / 1024.0) + } else { + "${value.roundToInt()}M" + } + } diff --git a/amethyst/src/main/res/values-ar-rSA/strings.xml b/amethyst/src/main/res/values-ar-rSA/strings.xml index fe16a886ea..920e74aecc 100644 --- a/amethyst/src/main/res/values-ar-rSA/strings.xml +++ b/amethyst/src/main/res/values-ar-rSA/strings.xml @@ -2823,6 +2823,7 @@ إجراءات الوسائط التشغيل تلقائي + البث إلى جهاز إيقاف البث diff --git a/amethyst/src/main/res/values-bn-rBD/strings.xml b/amethyst/src/main/res/values-bn-rBD/strings.xml index edc69f1593..399854cf01 100644 --- a/amethyst/src/main/res/values-bn-rBD/strings.xml +++ b/amethyst/src/main/res/values-bn-rBD/strings.xml @@ -2702,6 +2702,7 @@ মিডিয়া অ্যাকশন প্লেব্যাক স্বয়ংক্রিয় + ডিভাইসে কাস্ট করুন কাস্টিং বন্ধ করুন diff --git a/amethyst/src/main/res/values-cs/strings.xml b/amethyst/src/main/res/values-cs/strings.xml index b27e809d56..d60ee8dc55 100644 --- a/amethyst/src/main/res/values-cs/strings.xml +++ b/amethyst/src/main/res/values-cs/strings.xml @@ -3300,6 +3300,7 @@ Akce médií Přehrávání Auto + Odeslat do zařízení Ukončit odesílání diff --git a/amethyst/src/main/res/values-de-rDE/strings.xml b/amethyst/src/main/res/values-de-rDE/strings.xml index 1f106783f7..be4bfcb024 100644 --- a/amethyst/src/main/res/values-de-rDE/strings.xml +++ b/amethyst/src/main/res/values-de-rDE/strings.xml @@ -3168,6 +3168,7 @@ Medienaktionen Wiedergabe Auto + An Gerät streamen Streaming beenden diff --git a/amethyst/src/main/res/values-el-rGR/strings.xml b/amethyst/src/main/res/values-el-rGR/strings.xml index e989bff3a5..2844d47390 100644 --- a/amethyst/src/main/res/values-el-rGR/strings.xml +++ b/amethyst/src/main/res/values-el-rGR/strings.xml @@ -2647,6 +2647,7 @@ Ενέργειες Πολυμέσων Αναπαραγωγή Αυτόματο + Μετάδοση σε συσκευή Διακοπή μετάδοσης diff --git a/amethyst/src/main/res/values-en-rGB/strings.xml b/amethyst/src/main/res/values-en-rGB/strings.xml index 132d63dc73..eda9fdf794 100644 --- a/amethyst/src/main/res/values-en-rGB/strings.xml +++ b/amethyst/src/main/res/values-en-rGB/strings.xml @@ -30,6 +30,7 @@ + diff --git a/amethyst/src/main/res/values-eo-rUY/strings.xml b/amethyst/src/main/res/values-eo-rUY/strings.xml index ce414fabc8..418d68e31c 100644 --- a/amethyst/src/main/res/values-eo-rUY/strings.xml +++ b/amethyst/src/main/res/values-eo-rUY/strings.xml @@ -2704,6 +2704,7 @@ Mediaj Agoj Reproduktado Aŭtomata + Transigi al aparato Ĉesi transiĝon diff --git a/amethyst/src/main/res/values-es-rES/strings.xml b/amethyst/src/main/res/values-es-rES/strings.xml index 8a758dad23..3b8df32c0f 100644 --- a/amethyst/src/main/res/values-es-rES/strings.xml +++ b/amethyst/src/main/res/values-es-rES/strings.xml @@ -2769,6 +2769,7 @@ Acciones de medios Reproducción Automático + Emitir al dispositivo Detener emisión diff --git a/amethyst/src/main/res/values-es-rMX/strings.xml b/amethyst/src/main/res/values-es-rMX/strings.xml index 6c76d29dcc..2c9f2c92f3 100644 --- a/amethyst/src/main/res/values-es-rMX/strings.xml +++ b/amethyst/src/main/res/values-es-rMX/strings.xml @@ -2760,6 +2760,7 @@ Acciones de medios Reproducción Automático + Emitir al dispositivo Detener emisión diff --git a/amethyst/src/main/res/values-es-rUS/strings.xml b/amethyst/src/main/res/values-es-rUS/strings.xml index feb5c202e2..7835174c36 100644 --- a/amethyst/src/main/res/values-es-rUS/strings.xml +++ b/amethyst/src/main/res/values-es-rUS/strings.xml @@ -2760,6 +2760,7 @@ Acciones de medios Reproducción Automático + Emitir al dispositivo Detener emisión diff --git a/amethyst/src/main/res/values-fa-rIR/strings.xml b/amethyst/src/main/res/values-fa-rIR/strings.xml index 429bc76504..201d7e725d 100644 --- a/amethyst/src/main/res/values-fa-rIR/strings.xml +++ b/amethyst/src/main/res/values-fa-rIR/strings.xml @@ -2717,6 +2717,7 @@ اقدامات رسانه پخش خودکار + پخش روی دستگاه توقف پخش diff --git a/amethyst/src/main/res/values-fi-rFI/strings.xml b/amethyst/src/main/res/values-fi-rFI/strings.xml index 7ce8e75691..c2256d196a 100644 --- a/amethyst/src/main/res/values-fi-rFI/strings.xml +++ b/amethyst/src/main/res/values-fi-rFI/strings.xml @@ -2678,6 +2678,7 @@ Median toiminnot Toisto Automaattinen + Suoratoista laitteeseen Lopeta suoratoisto diff --git a/amethyst/src/main/res/values-fr-rCA/strings.xml b/amethyst/src/main/res/values-fr-rCA/strings.xml index 9a98dbfd52..5cfeafd164 100644 --- a/amethyst/src/main/res/values-fr-rCA/strings.xml +++ b/amethyst/src/main/res/values-fr-rCA/strings.xml @@ -2606,6 +2606,7 @@ Actions sur le profil Actions sur le média Lecture + Diffuser sur l\'appareil Arrêter la diffusion diff --git a/amethyst/src/main/res/values-fr-rFR/strings.xml b/amethyst/src/main/res/values-fr-rFR/strings.xml index 3b445362af..7e01e4ac0c 100644 --- a/amethyst/src/main/res/values-fr-rFR/strings.xml +++ b/amethyst/src/main/res/values-fr-rFR/strings.xml @@ -2982,6 +2982,7 @@ Actions sur le média Lecture Auto + Diffuser sur l\'appareil Arrêter la diffusion diff --git a/amethyst/src/main/res/values-hi-rIN/strings.xml b/amethyst/src/main/res/values-hi-rIN/strings.xml index 1124b60683..4b2fd9b00d 100644 --- a/amethyst/src/main/res/values-hi-rIN/strings.xml +++ b/amethyst/src/main/res/values-hi-rIN/strings.xml @@ -3215,6 +3215,7 @@ अभिलेख कार्य चालन स्वचालित + यन्त्र पर निक्षेपण निक्षेपण रोकें diff --git a/amethyst/src/main/res/values-hu-rHU/strings.xml b/amethyst/src/main/res/values-hu-rHU/strings.xml index 517a94c213..93a41749de 100644 --- a/amethyst/src/main/res/values-hu-rHU/strings.xml +++ b/amethyst/src/main/res/values-hu-rHU/strings.xml @@ -3216,6 +3216,7 @@ Médiaműveletek Lejátszás Automatikus + Közvetítés eszközre Közvetítés leállítása diff --git a/amethyst/src/main/res/values-in-rID/strings.xml b/amethyst/src/main/res/values-in-rID/strings.xml index 55c2791a6f..866eac29d9 100644 --- a/amethyst/src/main/res/values-in-rID/strings.xml +++ b/amethyst/src/main/res/values-in-rID/strings.xml @@ -2606,6 +2606,7 @@ Seharusnya %3$s Tindakan Media Pemutaran Otomatis + Tampilkan ke perangkat Hentikan penayangan diff --git a/amethyst/src/main/res/values-it-rIT/strings.xml b/amethyst/src/main/res/values-it-rIT/strings.xml index 11279feaef..f0d0a2a7b5 100644 --- a/amethyst/src/main/res/values-it-rIT/strings.xml +++ b/amethyst/src/main/res/values-it-rIT/strings.xml @@ -2645,6 +2645,7 @@ Azioni profilo Azioni media Riproduzione + Trasmetti al dispositivo Interrompi trasmissione diff --git a/amethyst/src/main/res/values-ja-rJP/strings.xml b/amethyst/src/main/res/values-ja-rJP/strings.xml index 033213ceb3..5d772688c6 100644 --- a/amethyst/src/main/res/values-ja-rJP/strings.xml +++ b/amethyst/src/main/res/values-ja-rJP/strings.xml @@ -2660,6 +2660,7 @@ メディアのアクション 再生 自動 + デバイスにキャスト キャストを停止 diff --git a/amethyst/src/main/res/values-ko-rKR/strings.xml b/amethyst/src/main/res/values-ko-rKR/strings.xml index d513de6d20..2c350714e7 100644 --- a/amethyst/src/main/res/values-ko-rKR/strings.xml +++ b/amethyst/src/main/res/values-ko-rKR/strings.xml @@ -2647,6 +2647,7 @@ 미디어 작업 재생 자동 + 기기로 캐스트 캐스트 중지 diff --git a/amethyst/src/main/res/values-lv-rLV/strings.xml b/amethyst/src/main/res/values-lv-rLV/strings.xml index 567924d2d6..947c7a7461 100644 --- a/amethyst/src/main/res/values-lv-rLV/strings.xml +++ b/amethyst/src/main/res/values-lv-rLV/strings.xml @@ -2720,6 +2720,7 @@ Multivides darbības Atskaņošana Automātiski + Apraidīt uz ierīci Pārtraukt apraidi diff --git a/amethyst/src/main/res/values-nl-rNL/strings.xml b/amethyst/src/main/res/values-nl-rNL/strings.xml index 4bed90f621..bb876140cb 100644 --- a/amethyst/src/main/res/values-nl-rNL/strings.xml +++ b/amethyst/src/main/res/values-nl-rNL/strings.xml @@ -3051,6 +3051,7 @@ Media-acties Afspelen Auto + Casten naar apparaat Casten stoppen diff --git a/amethyst/src/main/res/values-pl-rPL/strings.xml b/amethyst/src/main/res/values-pl-rPL/strings.xml index 23c0a216cf..5137fd21d6 100644 --- a/amethyst/src/main/res/values-pl-rPL/strings.xml +++ b/amethyst/src/main/res/values-pl-rPL/strings.xml @@ -3301,6 +3301,7 @@ Zaplanowane posty z innych kont nie zostaną opublikowane, dopóki to konto jest Akcje multimediów Odtwarzanie Automatycznie + Zrzuć na urządzenie Przestań zrzucać diff --git a/amethyst/src/main/res/values-pt-rBR/strings.xml b/amethyst/src/main/res/values-pt-rBR/strings.xml index 4b0e0381de..fa993c1730 100644 --- a/amethyst/src/main/res/values-pt-rBR/strings.xml +++ b/amethyst/src/main/res/values-pt-rBR/strings.xml @@ -3164,6 +3164,7 @@ Ações de mídia Reprodução Auto + Transmitir para dispositivo Parar transmissão diff --git a/amethyst/src/main/res/values-pt-rPT/strings.xml b/amethyst/src/main/res/values-pt-rPT/strings.xml index d06e8cbdb8..77946bedcb 100644 --- a/amethyst/src/main/res/values-pt-rPT/strings.xml +++ b/amethyst/src/main/res/values-pt-rPT/strings.xml @@ -2664,6 +2664,7 @@ Ações do perfil Ações de mídia Reprodução + Transmitir para dispositivo Parar transmissão diff --git a/amethyst/src/main/res/values-ru-rRU/strings.xml b/amethyst/src/main/res/values-ru-rRU/strings.xml index 541080ba25..ce0893cfb8 100644 --- a/amethyst/src/main/res/values-ru-rRU/strings.xml +++ b/amethyst/src/main/res/values-ru-rRU/strings.xml @@ -2742,6 +2742,7 @@ Действия с медиа Воспроизведение Авто + Трансляция на устройство Остановить трансляцию diff --git a/amethyst/src/main/res/values-ru-rUA/strings.xml b/amethyst/src/main/res/values-ru-rUA/strings.xml index 63c1339889..9f41ce3df0 100644 --- a/amethyst/src/main/res/values-ru-rUA/strings.xml +++ b/amethyst/src/main/res/values-ru-rUA/strings.xml @@ -2725,6 +2725,7 @@ Действия с медиа Воспроизведение Авто + Трансляция на устройство Остановить трансляцию diff --git a/amethyst/src/main/res/values-sl-rSI/strings.xml b/amethyst/src/main/res/values-sl-rSI/strings.xml index 53783ca50f..42ecf5cf1d 100644 --- a/amethyst/src/main/res/values-sl-rSI/strings.xml +++ b/amethyst/src/main/res/values-sl-rSI/strings.xml @@ -3316,6 +3316,7 @@ Za ohranitev zasebnosti to denarnico polni in prazni prek ne-zasebnih računov, Možnosti predstavnosti Predvajaj Samodejno + Prenesi v napravo Prenehaj s prenašanjem diff --git a/amethyst/src/main/res/values-sr-rSP/strings.xml b/amethyst/src/main/res/values-sr-rSP/strings.xml index b068cb0758..49b7601369 100644 --- a/amethyst/src/main/res/values-sr-rSP/strings.xml +++ b/amethyst/src/main/res/values-sr-rSP/strings.xml @@ -2703,6 +2703,7 @@ Radnje sa medijima Reprodukcija Automatski + Prikaži na uređaju Zaustavi prikazivanje diff --git a/amethyst/src/main/res/values-sv-rSE/strings.xml b/amethyst/src/main/res/values-sv-rSE/strings.xml index 734be267e9..664c06a385 100644 --- a/amethyst/src/main/res/values-sv-rSE/strings.xml +++ b/amethyst/src/main/res/values-sv-rSE/strings.xml @@ -3172,6 +3172,7 @@ Medieåtgärder Uppspelning Auto + Casta till enhet Sluta casta diff --git a/amethyst/src/main/res/values-sw/strings.xml b/amethyst/src/main/res/values-sw/strings.xml index fda33de346..c1758eb485 100644 --- a/amethyst/src/main/res/values-sw/strings.xml +++ b/amethyst/src/main/res/values-sw/strings.xml @@ -2684,6 +2684,7 @@ Vitendo vya Midia Uchezaji Otomatiki + Tuma kwenye kifaa Acha kutuma diff --git a/amethyst/src/main/res/values-ta-rIN/strings.xml b/amethyst/src/main/res/values-ta-rIN/strings.xml index ac6c3e80b9..e77b264334 100644 --- a/amethyst/src/main/res/values-ta-rIN/strings.xml +++ b/amethyst/src/main/res/values-ta-rIN/strings.xml @@ -2673,6 +2673,7 @@ ஊடக செயல்கள் இயக்கம் தானியங்கி + சாதனத்திற்கு அனுப்பவும் அனுப்புவதை நிறுத்தவும் diff --git a/amethyst/src/main/res/values-th-rTH/strings.xml b/amethyst/src/main/res/values-th-rTH/strings.xml index 2779a9625d..49b6c3d5ac 100644 --- a/amethyst/src/main/res/values-th-rTH/strings.xml +++ b/amethyst/src/main/res/values-th-rTH/strings.xml @@ -2511,6 +2511,7 @@ การดำเนินการกับสื่อ การเล่น อัตโนมัติ + แคสต์ไปยังอุปกรณ์ หยุดการแคสต์ diff --git a/amethyst/src/main/res/values-tr-rTR/strings.xml b/amethyst/src/main/res/values-tr-rTR/strings.xml index b3b34cb392..c1ebcd1dff 100644 --- a/amethyst/src/main/res/values-tr-rTR/strings.xml +++ b/amethyst/src/main/res/values-tr-rTR/strings.xml @@ -2685,6 +2685,7 @@ Medya İşlemleri Oynatma Otomatik + Cihaza yayınla Yayını durdur diff --git a/amethyst/src/main/res/values-uk-rUA/strings.xml b/amethyst/src/main/res/values-uk-rUA/strings.xml index fe1cde2fcf..b6211b0f0d 100644 --- a/amethyst/src/main/res/values-uk-rUA/strings.xml +++ b/amethyst/src/main/res/values-uk-rUA/strings.xml @@ -2733,6 +2733,7 @@ Дії з медіа Відтворення Авто + Трансляція на пристрій Зупинити трансляцію diff --git a/amethyst/src/main/res/values-uz-rUZ/strings.xml b/amethyst/src/main/res/values-uz-rUZ/strings.xml index 5e41ccb79e..6fe092d19e 100644 --- a/amethyst/src/main/res/values-uz-rUZ/strings.xml +++ b/amethyst/src/main/res/values-uz-rUZ/strings.xml @@ -2689,6 +2689,7 @@ Media amallar Ijro etish Avtomatik + Qurilmaga uzatish Uzatishni to\'xtatish diff --git a/amethyst/src/main/res/values-vi-rVN/strings.xml b/amethyst/src/main/res/values-vi-rVN/strings.xml index d153949ae2..9e72b7f0ad 100644 --- a/amethyst/src/main/res/values-vi-rVN/strings.xml +++ b/amethyst/src/main/res/values-vi-rVN/strings.xml @@ -2630,6 +2630,7 @@ Thao tác phương tiện Phát lại Tự động + Chiếu đến thiết bị Dừng chiếu diff --git a/amethyst/src/main/res/values-zh-rCN/strings.xml b/amethyst/src/main/res/values-zh-rCN/strings.xml index ff2d4ec596..23ab209d4c 100644 --- a/amethyst/src/main/res/values-zh-rCN/strings.xml +++ b/amethyst/src/main/res/values-zh-rCN/strings.xml @@ -1184,6 +1184,8 @@ 尚无接收方:只有您才能看到此笔记。添加分享对象。 添加 从通知中删除用户 + 通知中。轻触静音此用户的通知 + 已静音。轻触再次通知此用户 搜索并添加要通知的用户 在这里不是书签 从列表中删除书签 @@ -3171,6 +3173,7 @@ 媒体操作 播放 自动 + 投射到设备 停止投影 diff --git a/amethyst/src/main/res/values-zh-rHK/strings.xml b/amethyst/src/main/res/values-zh-rHK/strings.xml index 2afb33976f..b93c51de96 100644 --- a/amethyst/src/main/res/values-zh-rHK/strings.xml +++ b/amethyst/src/main/res/values-zh-rHK/strings.xml @@ -2699,6 +2699,7 @@ 媒体操作 播放 自动 + 投射到设备 停止投影 diff --git a/amethyst/src/main/res/values-zh-rSG/strings.xml b/amethyst/src/main/res/values-zh-rSG/strings.xml index 6e804d6206..8436609a81 100644 --- a/amethyst/src/main/res/values-zh-rSG/strings.xml +++ b/amethyst/src/main/res/values-zh-rSG/strings.xml @@ -2699,6 +2699,7 @@ 媒体操作 播放 自动 + 投射到设备 停止投影 diff --git a/amethyst/src/main/res/values-zh-rTW/strings.xml b/amethyst/src/main/res/values-zh-rTW/strings.xml index accf6196d6..9c9f7e0b50 100644 --- a/amethyst/src/main/res/values-zh-rTW/strings.xml +++ b/amethyst/src/main/res/values-zh-rTW/strings.xml @@ -2664,6 +2664,7 @@ 媒體操作 播放 自動 + 投放至裝置 停止投放 diff --git a/amethyst/src/main/res/values/strings.xml b/amethyst/src/main/res/values/strings.xml index 7a864a29f1..fc352bc4ad 100644 --- a/amethyst/src/main/res/values/strings.xml +++ b/amethyst/src/main/res/values/strings.xml @@ -1243,6 +1243,8 @@ No receivers yet: only you will be able to see this note. Add people to share it with. Add Remove user from notifications + Notifying. Tap to mute the notification for this user + Muted. Tap to notify this user again Search and add a user to notify is not a bookmark here Remove bookmark from list @@ -3186,6 +3188,102 @@ Crash Report found Would you like to send the recent crash report to Amethyst in a DM? No personal information will be shared Send it + + App resource usage + battery data usage consumption power network diagnostics ledger + Today + Last 7 days + Cellular today + Wi-Fi today + Relay time today + In app today + Data per day + Cellular (MB) + Wi-Fi (MB) + Activity (7 days) + Cellular data (background) + Cellular data (in app) + Wi-Fi data + Relay connection time + \u2026 backgrounded on cellular + Notification processing (CPU awake) + Background jobs run + Relay reconnections (failed) + Web requests + Radio wake-ups for downloads (est.) + Active transfer time + Media playback time + Signatures verified + Processor time used + Time in app + App process starts + Messages decrypted + Remote signatures + Proof-of-work mining + Tor uptime + Time in calls + Time in audio rooms + Location listening + Battery used (in app / background) + Background notification service + Keeps connections to your relays open while the app is closed, so notifications arrive without Google’s push servers. While enabled, this is usually the largest background battery cost. If battery matters more to you than instant notifications, you can change the delivery method or turn it off. + Running time + + %1$d start + %1$d starts + + Relay connections held while closed + Battery drained meanwhile (whole device) + Change notification settings + Privacy options + Battery / hour in app + Data / hour in app + Avg. relay connections + Cellular data by feature (7 days) + %1$s · %2$s total + Time by screen (7 days) + Built-in Tor + Routes the app’s traffic through the Tor network for privacy. While running it spends extra battery on encryption and keep-alive traffic, and every start pays a bootstrap. If your threat model allows, Tor use can be adjusted or turned off. + Data by feature (7 days) + Relay sync + Images + Video & audio + Uploads + Wallet & zaps + Address verification + Link previews + Push registration + Other + No usage recorded yet. + Memory right now + App memory (heap) + Native memory + Image cache (RAM) + Image cache (disk) + Notes in memory + Profiles in memory + Addressable events in memory + Chatroom lists in memory + Device memory class + Share with the developers + If Amethyst seems to drain battery or data, you can send this report to the developers in an encrypted DM. It contains only the numbers on this screen and the technical counters behind them \u2014 no posts, contacts, or browsing details. Nothing is sent until you tap Send in the message screen. + Send report via DM + High resource usage detected + Amethyst consumed more than expected recently: %1$s. Would you like to send a usage report to the developers in an encrypted DM? You will see the full report before anything is sent. + %1$s of cellular data in the background in one day + %1$s of relay connections while backgrounded on cellular in one day + the CPU was held awake for %1$s processing notifications in one day + + the app process restarted %1$d time in one day + the app process restarted %1$d times in one day + + + the app reconnected to relays %1$d time in one day + the app reconnected to relays %1$d times in one day + + Review & send + Not now + Don\u2019t ask again This message will disappear in %1$s Select Signer @@ -3549,6 +3647,10 @@ Playback Auto + + Edit nickname + Only visible to you + Cast to device Stop casting diff --git a/amethyst/src/test/java/com/vitorpamplona/amethyst/calendar/CalendarReminderCandidatesTest.kt b/amethyst/src/test/java/com/vitorpamplona/amethyst/calendar/CalendarReminderCandidatesTest.kt new file mode 100644 index 0000000000..74347b6d61 --- /dev/null +++ b/amethyst/src/test/java/com/vitorpamplona/amethyst/calendar/CalendarReminderCandidatesTest.kt @@ -0,0 +1,149 @@ +/* + * Copyright (c) 2025 Vitor Pamplona + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.vitorpamplona.amethyst.calendar + +import com.vitorpamplona.amethyst.model.LocalCache +import com.vitorpamplona.amethyst.service.calendar.CalendarReminderWorker +import com.vitorpamplona.quartz.nip52Calendar.appt.time.CalendarTimeSlotEvent +import com.vitorpamplona.quartz.nip52Calendar.rsvp.CalendarRSVPEvent +import org.junit.Assert.assertFalse +import org.junit.Assert.assertTrue +import org.junit.Test + +/** + * The predicate that decides whether the CalendarReminderWorker's periodic + * chain may cancel itself. Getting it wrong in either direction is a bug: + * a false "could fire" keeps waking the process forever; a false "can't fire" + * silently kills a reminder the user RSVP'd to. + */ +class CalendarReminderCandidatesTest { + private val now = 2_000_000_000L + + // Unique per-test-class identities so the shared LocalCache singleton + // doesn't collide with other suites running in the same JVM. + private val organizer = "b0b0000000000000000000000000000000000000000000000000000000000001" + private val attendee = "a11ce00000000000000000000000000000000000000000000000000000000002" + + private var eventSeq = 0 + + private fun timeSlot( + dTag: String, + startSeconds: Long?, + ) = CalendarTimeSlotEvent( + id = "c0ffee%058d".format(++eventSeq), + pubKey = organizer, + createdAt = now - 1000, + tags = + if (startSeconds != null) { + arrayOf(arrayOf("d", dTag), arrayOf("start", startSeconds.toString())) + } else { + arrayOf(arrayOf("d", dTag)) + }, + content = "", + sig = "sig", + ) + + private fun rsvp( + dTag: String, + targetDTag: String?, + status: String = "accepted", + ) = CalendarRSVPEvent( + id = "faced%059d".format(++eventSeq), + pubKey = attendee, + createdAt = now - 900, + tags = + buildList { + add(arrayOf("d", dTag)) + add(arrayOf("status", status)) + if (targetDTag != null) { + add(arrayOf("a", "${CalendarTimeSlotEvent.KIND}:$organizer:$targetDTag")) + } + }.toTypedArray(), + content = "", + sig = "sig", + ) + + @Test + fun acceptedRsvpsInCache_returnsAcceptedAndSkipsDeclined() { + val accepted = rsvp("cand-accepted", "cand-target-1") + val declined = rsvp("cand-declined", "cand-target-2", status = "declined") + LocalCache.justConsume(accepted, null, true) + LocalCache.justConsume(declined, null, true) + + val found = CalendarReminderWorker.acceptedRsvpsInCache() + assertTrue("accepted RSVP must be found", found.any { it.dTag() == "cand-accepted" }) + assertFalse("declined RSVP must be skipped", found.any { it.dTag() == "cand-declined" }) + } + + @Test + fun futureTarget_couldStillFire() { + val slot = timeSlot("cand-future", startSeconds = now + 3600) + val r = rsvp("cand-rsvp-future", "cand-future") + LocalCache.justConsume(slot, null, true) + LocalCache.justConsume(r, null, true) + + assertTrue(CalendarReminderWorker.couldStillFire(listOf(r), now)) + } + + @Test + fun pastTarget_cannotFireAnymore() { + val slot = timeSlot("cand-past", startSeconds = now - 3600) + val r = rsvp("cand-rsvp-past", "cand-past") + LocalCache.justConsume(slot, null, true) + LocalCache.justConsume(r, null, true) + + assertFalse(CalendarReminderWorker.couldStillFire(listOf(r), now)) + } + + @Test + fun unresolvedTarget_keepsTheChainAlive() { + // The RSVP points at an event the cache hasn't fetched yet: the start + // is unknown, so the worker must NOT cancel its chain. + val r = rsvp("cand-rsvp-unresolved", "cand-never-fetched") + LocalCache.justConsume(r, null, true) + + assertTrue(CalendarReminderWorker.couldStillFire(listOf(r), now)) + } + + @Test + fun targetWithoutStart_keepsTheChainAlive() { + // Target resolved but carries no start tag: still unknown, keep alive. + val slot = timeSlot("cand-no-start", startSeconds = null) + val r = rsvp("cand-rsvp-no-start", "cand-no-start") + LocalCache.justConsume(slot, null, true) + LocalCache.justConsume(r, null, true) + + assertTrue(CalendarReminderWorker.couldStillFire(listOf(r), now)) + } + + @Test + fun rsvpWithoutTargetAddress_doesNotKeepTheChainAlive() { + val r = rsvp("cand-rsvp-no-a-tag", targetDTag = null) + LocalCache.justConsume(r, null, true) + + assertFalse(CalendarReminderWorker.couldStillFire(listOf(r), now)) + } + + @Test + fun emptyCache_doesNotKeepTheChainAlive() { + assertFalse(CalendarReminderWorker.couldStillFire(emptyList(), now)) + } +} diff --git a/amethyst/src/test/java/com/vitorpamplona/amethyst/service/playback/playerPool/repeat/AutoReplayLimiterTest.kt b/amethyst/src/test/java/com/vitorpamplona/amethyst/service/playback/playerPool/repeat/AutoReplayLimiterTest.kt new file mode 100644 index 0000000000..891c0d5140 --- /dev/null +++ b/amethyst/src/test/java/com/vitorpamplona/amethyst/service/playback/playerPool/repeat/AutoReplayLimiterTest.kt @@ -0,0 +1,91 @@ +/* + * Copyright (c) 2025 Vitor Pamplona + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.vitorpamplona.amethyst.service.playback.playerPool.repeat + +import androidx.media3.common.Player +import org.junit.Assert.assertEquals +import org.junit.Test + +class AutoReplayLimiterTest { + private var pauseCalls = 0 + private val limiter = AutoReplayLimiter(maxAutoPlays = 5) { pauseCalls++ } + + private fun startPlaying() = limiter.onPlayWhenReadyChanged(true, Player.PLAY_WHEN_READY_CHANGE_REASON_USER_REQUEST) + + private fun completeOnePlay() = limiter.onMediaItemTransition(null, Player.MEDIA_ITEM_TRANSITION_REASON_REPEAT) + + private fun newMediaItem() = limiter.onMediaItemTransition(null, Player.MEDIA_ITEM_TRANSITION_REASON_PLAYLIST_CHANGED) + + @Test + fun pausesWhenTheFifthPlayCompletes() { + startPlaying() + + // plays 1..4 complete: repeat transitions 1..4 start plays 2..5 + repeat(4) { completeOnePlay() } + assertEquals(0, pauseCalls) + + // play 5 completes: the 5th repeat transition would start play 6 + completeOnePlay() + assertEquals(1, pauseCalls) + } + + @Test + fun pressingPlayGrantsAFreshAllowance() { + startPlaying() + repeat(5) { completeOnePlay() } + assertEquals(1, pauseCalls) + + // user presses play again + startPlaying() + repeat(4) { completeOnePlay() } + assertEquals(1, pauseCalls) + + completeOnePlay() + assertEquals(2, pauseCalls) + } + + @Test + fun switchingMediaItemsResetsTheCount() { + startPlaying() + repeat(4) { completeOnePlay() } + + // the pooled player is handed a different video + newMediaItem() + + repeat(4) { completeOnePlay() } + assertEquals(0, pauseCalls) + + completeOnePlay() + assertEquals(1, pauseCalls) + } + + @Test + fun pausingMidPlayDoesNotResetTheCount() { + startPlaying() + repeat(3) { completeOnePlay() } + + // a pause alone (e.g. scrolled off screen) does not reset; only a resume does + limiter.onPlayWhenReadyChanged(false, Player.PLAY_WHEN_READY_CHANGE_REASON_USER_REQUEST) + + repeat(2) { completeOnePlay() } + assertEquals(1, pauseCalls) + } +} diff --git a/amethyst/src/test/java/com/vitorpamplona/amethyst/service/resourceusage/ResourceUsageLedgerTest.kt b/amethyst/src/test/java/com/vitorpamplona/amethyst/service/resourceusage/ResourceUsageLedgerTest.kt new file mode 100644 index 0000000000..6be24d2942 --- /dev/null +++ b/amethyst/src/test/java/com/vitorpamplona/amethyst/service/resourceusage/ResourceUsageLedgerTest.kt @@ -0,0 +1,778 @@ +/* + * Copyright (c) 2025 Vitor Pamplona + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.vitorpamplona.amethyst.service.resourceusage + +import com.vitorpamplona.amethyst.service.playback.playerPool.MediaPlayTimeTracker +import com.vitorpamplona.quartz.nip01Core.core.Event +import com.vitorpamplona.quartz.nip01Core.core.HexKey +import com.vitorpamplona.quartz.nip01Core.signers.NostrSigner +import com.vitorpamplona.quartz.nip57Zaps.LnZapPrivateEvent +import com.vitorpamplona.quartz.nip57Zaps.LnZapRequestEvent +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.test.runTest +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertNull +import org.junit.Assert.assertTrue +import org.junit.Before +import org.junit.Rule +import org.junit.Test +import org.junit.rules.TemporaryFolder +import java.io.File + +class ResourceUsageStoreTest { + @get:Rule + val temp = TemporaryFolder() + + private lateinit var file: File + + @Before + fun setUp() { + file = File(temp.root, ResourceUsageStore.FILE_NAME) + } + + @Test + fun mergesAndReloadsFromDisk() = + runTest { + val store = ResourceUsageStore(file) + store.mergeInto(100, mapOf("a" to 5L, "b" to 2L)) + store.mergeInto(100, mapOf("a" to 3L)) + store.mergeInto(101, mapOf("a" to 1L)) + + val reloaded = ResourceUsageStore(file).allDays() + assertEquals(8L, reloaded[100]?.get("a")) + assertEquals(2L, reloaded[100]?.get("b")) + assertEquals(1L, reloaded[101]?.get("a")) + } + + @Test + fun prunesBucketsOlderThanKeepDays() = + runTest { + val store = ResourceUsageStore(file, keepDays = 7) + store.mergeInto(100, mapOf("a" to 1L)) + store.mergeInto(110, mapOf("a" to 1L)) + + val days = store.allDays() + assertNull("day 100 is older than 110-7 and must be pruned", days[100]) + assertEquals(1L, days[110]?.get("a")) + } + + @Test + fun alertStateRoundTrips() = + runTest { + val store = ResourceUsageStore(file) + assertEquals(0L, store.lastAlertAtSec()) + assertFalse(store.alertsOptOut()) + + store.markAlertPrompted(12345L) + store.setAlertsOptOut(true) + + val reloaded = ResourceUsageStore(file) + assertEquals(12345L, reloaded.lastAlertAtSec()) + assertTrue(reloaded.alertsOptOut()) + } +} + +class ResourceUsageAccountantTest { + @get:Rule + val temp = TemporaryFolder() + + @Test + fun accumulatesAndFlushesIntoTheRightDay() = + runTest { + val store = ResourceUsageStore(File(temp.root, "u.json")) + var day = 200L + val accountant = ResourceUsageAccountant(store, backgroundScope, epochDay = { day }) + + accountant.add("x", 5) + accountant.add("x", 5) + accountant.flush() + day = 201L + accountant.add("x", 7) + accountant.flush() + + val days = store.allDays() + assertEquals(10L, days[200]?.get("x")) + assertEquals(7L, days[201]?.get("x")) + } + + @Test + fun liveCountersAreVisibleBeforeFlush() = + runTest { + val store = ResourceUsageStore(File(temp.root, "u.json")) + val accountant = ResourceUsageAccountant(store, backgroundScope, epochDay = { 300L }) + + accountant.add("x", 42) + val days = accountant.allDaysIncludingLive() + assertEquals(42L, days[300]?.get("x")) + // and not yet on disk + assertNull(store.allDays()[300]) + } + + @Test + fun countersKeepAccumulatingAfterADrain() = + runTest { + val store = ResourceUsageStore(File(temp.root, "u.json")) + val accountant = ResourceUsageAccountant(store, backgroundScope, epochDay = { 200L }) + + accountant.add("x", 5) + accountant.flush() + accountant.add("x", 7) + accountant.flush() + + assertEquals(12L, store.allDays()[200]?.get("x")) + } + + @Test + fun hookAddsAreDrainedInPlaceAndNeverRearmTheFlushLoop() = + runTest { + val store = ResourceUsageStore(File(temp.root, "u.json")) + val accountant = ResourceUsageAccountant(store, backgroundScope, epochDay = { 300L }) + var hookRuns = 0 + accountant.addPreFlushHook { + hookRuns++ + accountant.add("hook.counter", 1) + } + + accountant.add("x", 1) + testScheduler.advanceTimeBy(31_000) + testScheduler.runCurrent() + + assertEquals("the one debounced flush ran its hooks once", 1, hookRuns) + assertEquals(1L, store.allDays()[300]?.get("hook.counter")) + + // If hook adds re-armed the debounce, more flushes would fire and + // the hook counter would keep growing without any real activity. + testScheduler.advanceTimeBy(300_000) + testScheduler.runCurrent() + assertEquals("no self-perpetuating flush loop", 1, hookRuns) + assertEquals(1L, store.allDays()[300]?.get("hook.counter")) + } + + @Test + fun preFlushHooksRunOnFlushAndRead() = + runTest { + val store = ResourceUsageStore(File(temp.root, "u.json")) + val accountant = ResourceUsageAccountant(store, backgroundScope, epochDay = { 300L }) + var hookRuns = 0 + accountant.addPreFlushHook { hookRuns++ } + + accountant.flush() + accountant.allDaysIncludingLive() + assertEquals(2, hookRuns) + } +} + +class RelayConnectionTimeIntegratorTest { + @get:Rule + val temp = TemporaryFolder() + + @Test + fun integratesConnectionTimeAcrossStateChanges() = + runTest { + val store = ResourceUsageStore(File(temp.root, "u.json")) + val accountant = ResourceUsageAccountant(store, backgroundScope, epochDay = { 1L }) + + var now = 0L + val count = MutableStateFlow(0) + val mobile = MutableStateFlow(false) + val fg = MutableStateFlow(true) + + val integrator = + RelayConnectionTimeIntegrator( + connectedCount = count, + isMobile = mobile, + isForeground = fg, + accountant = accountant, + nowMs = { now }, + ) + val job = integrator.start(backgroundScope) + testScheduler.runCurrent() + + // 5 relays connected on wifi foreground for 10s + count.value = 5 + testScheduler.runCurrent() + now = 10_000L + + // switch to cellular background: closes the wifi segment + mobile.value = true + fg.value = false + testScheduler.runCurrent() + + // 5 relays on cellular background for 20s, then all disconnect + now = 30_000L + count.value = 0 + testScheduler.runCurrent() + + val counters = accountant.allDaysIncludingLive()[1L].orEmpty() + assertEquals(5 * 10_000L, counters[UsageKeys.relayConnMs(mobile = false, foreground = true)]) + assertEquals(5 * 20_000L, counters[UsageKeys.relayConnMs(mobile = true, foreground = false)]) + job.cancel() + } + + @Test + fun closeOpenSegmentAccountsLongStableSessions() = + runTest { + val store = ResourceUsageStore(File(temp.root, "u.json")) + val accountant = ResourceUsageAccountant(store, backgroundScope, epochDay = { 1L }) + + var now = 0L + val count = MutableStateFlow(3) + val integrator = + RelayConnectionTimeIntegrator( + connectedCount = count, + isMobile = MutableStateFlow(true), + isForeground = MutableStateFlow(false), + accountant = accountant, + nowMs = { now }, + ) + val job = integrator.start(backgroundScope) + testScheduler.runCurrent() + + // hours pass with no state change at all (always-on background) + now = 2 * 60 * 60 * 1000L + val counters = accountant.allDaysIncludingLive()[1L].orEmpty() + assertEquals( + "reading the ledger must account the still-open segment", + 3 * 2 * 60 * 60 * 1000L, + counters[UsageKeys.relayConnMs(mobile = true, foreground = false)], + ) + job.cancel() + } +} + +class ProcessCpuSamplerTest { + @get:Rule + val temp = TemporaryFolder() + + @Test + fun accountsCpuDeltasAcrossSamples() = + runTest { + val store = ResourceUsageStore(File(temp.root, "u.json")) + val accountant = ResourceUsageAccountant(store, backgroundScope, epochDay = { 1L }) + var cpu = 1_000L + val sampler = ProcessCpuSampler(accountant) { cpu } + + cpu = 1_500L + sampler.sample() + cpu = 1_800L + sampler.sample() + + val counters = accountant.allDaysIncludingLive()[1L].orEmpty() + assertEquals(800L, counters[UsageKeys.CPU_MS]) + } +} + +class ForegroundTimeIntegratorTest { + @get:Rule + val temp = TemporaryFolder() + + @Test + fun accountsOnlyForegroundTime() = + runTest { + val store = ResourceUsageStore(File(temp.root, "u.json")) + val accountant = ResourceUsageAccountant(store, backgroundScope, epochDay = { 1L }) + var now = 0L + val fg = MutableStateFlow(false) + val integrator = ForegroundTimeIntegrator(fg, accountant) { now } + val job = integrator.start(backgroundScope) + testScheduler.runCurrent() + + // 60s in background — must not count + now = 60_000L + fg.value = true + testScheduler.runCurrent() + + // 30s in foreground — counts + now = 90_000L + fg.value = false + testScheduler.runCurrent() + + // 60s more in background, then read: still 30s + now = 150_000L + val counters = accountant.allDaysIncludingLive()[1L].orEmpty() + assertEquals(30_000L, counters[UsageKeys.APP_FG_MS]) + job.cancel() + } + + @Test + fun openForegroundSegmentIsAccountedOnRead() = + runTest { + val store = ResourceUsageStore(File(temp.root, "u.json")) + val accountant = ResourceUsageAccountant(store, backgroundScope, epochDay = { 1L }) + var now = 0L + val fg = MutableStateFlow(true) + val integrator = ForegroundTimeIntegrator(fg, accountant) { now } + val job = integrator.start(backgroundScope) + testScheduler.runCurrent() + + now = 45_000L + val counters = accountant.allDaysIncludingLive()[1L].orEmpty() + assertEquals(45_000L, counters[UsageKeys.APP_FG_MS]) + job.cancel() + } +} + +class RadioBurstEstimatorTest { + @get:Rule + val temp = TemporaryFolder() + + @Test + fun countsBurstsOnlyAfterRadioIdleGaps() = + runTest { + val store = ResourceUsageStore(File(temp.root, "u.json")) + val accountant = ResourceUsageAccountant(store, backgroundScope, epochDay = { 1L }) + var now = 100_000L + val estimator = + RadioBurstEstimator( + accountant = accountant, + isMobile = { true }, + isForeground = { false }, + nowMs = { now }, + ) + + estimator.onHttpActivity() // first activity = one burst + now += 2_000 + estimator.onHttpActivity() // 2s later: same burst + now += RadioBurstEstimator.BURST_GAP_MS + 1 + estimator.onHttpActivity() // >10s of silence: new burst + now += 500 + estimator.onHttpActivity() // same burst + + val counters = accountant.allDaysIncludingLive()[1L].orEmpty() + assertEquals(2L, counters[UsageKeys.radioBursts(mobile = true, foreground = false)]) + } +} + +class LoopbackExclusionTest { + @Test + fun loopbackHostsAreRecognizedAndRealHostsAreNot() { + assertTrue(UsageCountingInterceptor.isLoopback("127.0.0.1")) + assertTrue(UsageCountingInterceptor.isLoopback("127.4.5.6")) + assertTrue(UsageCountingInterceptor.isLoopback("localhost")) + assertTrue(UsageCountingInterceptor.isLoopback("::1")) + assertFalse(UsageCountingInterceptor.isLoopback("relay.example.com")) + assertFalse(UsageCountingInterceptor.isLoopback("192.168.1.10")) + assertFalse(UsageCountingInterceptor.isLoopback("128.0.0.1")) + } +} + +class MediaPlayTimeTrackerTest { + @Test + fun accumulatesOnlyWhilePlaying() { + var now = 0L + var played = 0L + val tracker = MediaPlayTimeTracker(onPlayed = { played += it }, nowMs = { now }) + + tracker.onIsPlayingChanged(true) + now = 30_000L + tracker.onIsPlayingChanged(false) + + now = 100_000L // paused time must not count + tracker.onIsPlayingChanged(true) + now = 105_000L + tracker.onIsPlayingChanged(false) + tracker.onIsPlayingChanged(false) // duplicate stop is a no-op + + assertEquals(35_000L, played) + } +} + +class SessionTimeIntegratorTest { + @get:Rule + val temp = TemporaryFolder() + + @Test + fun accountsActiveTimeAndCountsActivations() = + runTest { + val store = ResourceUsageStore(File(temp.root, "u.json")) + val accountant = ResourceUsageAccountant(store, backgroundScope, epochDay = { 100L }) + var clock = 0L + val session = SessionTimeIntegrator(accountant, "s.ms", "s.starts", nowMs = { clock }) + + session.setActive(true) + clock += 5_000 + session.setActive(false) + clock += 60_000 // idle time must not count + session.setActive(true) + clock += 1_000 + session.setActive(false) + + val today = accountant.allDaysIncludingLive()[100].orEmpty() + assertEquals(6_000L, today["s.ms"]) + assertEquals(2L, today["s.starts"]) + } + + @Test + fun repeatedActivationsDoNotDoubleCountStarts() = + runTest { + val store = ResourceUsageStore(File(temp.root, "u.json")) + val accountant = ResourceUsageAccountant(store, backgroundScope, epochDay = { 100L }) + var clock = 0L + val session = SessionTimeIntegrator(accountant, "s.ms", "s.starts", nowMs = { clock }) + + session.setActive(true) + clock += 1_000 + session.setActive(true) + clock += 1_000 + session.setActive(false) + + val today = accountant.allDaysIncludingLive()[100].orEmpty() + assertEquals(2_000L, today["s.ms"]) + assertEquals(1L, today["s.starts"]) + } + + @Test + fun openSegmentIsAccountedOnFlushWithoutClosing() = + runTest { + val store = ResourceUsageStore(File(temp.root, "u.json")) + val accountant = ResourceUsageAccountant(store, backgroundScope, epochDay = { 100L }) + var clock = 0L + val session = SessionTimeIntegrator(accountant, "s.ms", nowMs = { clock }) + session.registerFlushHook() + + session.setActive(true) + clock += 120_000 + val mid = accountant.allDaysIncludingLive()[100].orEmpty() + assertEquals("multi-hour stable sessions account without a transition", 120_000L, mid["s.ms"]) + + clock += 30_000 + session.setActive(false) + val end = accountant.allDaysIncludingLive()[100].orEmpty() + assertEquals(150_000L, end["s.ms"]) + } +} + +class BatteryDrainSamplerTest { + @get:Rule + val temp = TemporaryFolder() + + private fun harness(scope: CoroutineScope): Triple { + val store = ResourceUsageStore(File(temp.root, "u.json")) + val accountant = ResourceUsageAccountant(store, scope, epochDay = { 100L }) + val controls = Controls() + val sampler = + BatteryDrainSampler( + accountant = accountant, + capacityPct = { controls.pct }, + isCharging = { controls.charging }, + isForeground = { controls.foreground }, + ) + return Triple(accountant, sampler, controls) + } + + private class Controls { + var pct: Int? = 90 + var charging = false + var foreground = true + } + + @Test + fun firstSampleOnlyEstablishesTheBaseline() = + runTest { + val (accountant, sampler, _) = harness(backgroundScope) + sampler.sample() + assertNull(accountant.allDaysIncludingLive()[100]?.get(UsageKeys.BATTERY_DRAIN_FG)) + } + + @Test + fun dischargeDropsAreAccountedByVisibility() = + runTest { + val (accountant, sampler, controls) = harness(backgroundScope) + sampler.sample() // baseline 90, discharging + controls.pct = 87 + sampler.sample() // -3 while foreground + controls.foreground = false + controls.pct = 85 + sampler.sample() // -2 while background + + val today = accountant.allDaysIncludingLive()[100].orEmpty() + assertEquals(3L, today[UsageKeys.BATTERY_DRAIN_FG]) + assertEquals(2L, today[UsageKeys.BATTERY_DRAIN_BG]) + } + + @Test + fun intervalsTouchingAChargerAreSkipped() = + runTest { + val (accountant, sampler, controls) = harness(backgroundScope) + sampler.sample() // baseline 90, discharging + controls.charging = true + controls.pct = 80 // weird drop while charging: ignore + sampler.sample() + controls.charging = false + controls.pct = 78 // first discharging interval after charging: baseline only + sampler.sample() + controls.pct = 77 + sampler.sample() // clean discharging interval: -1 + + val today = accountant.allDaysIncludingLive()[100].orEmpty() + assertEquals(1L, today[UsageKeys.BATTERY_DRAIN_FG]) + } + + @Test + fun aLevelIncreaseResetsTheBaselineWithoutAccounting() = + runTest { + val (accountant, sampler, controls) = harness(backgroundScope) + sampler.sample() // baseline 90 + controls.pct = 95 // e.g. charged while the process was frozen + sampler.sample() + controls.pct = 94 + sampler.sample() // -1 + + val today = accountant.allDaysIncludingLive()[100].orEmpty() + assertEquals(1L, today[UsageKeys.BATTERY_DRAIN_FG]) + } +} + +class MeteringNostrSignerTest { + @get:Rule + val temp = TemporaryFolder() + + /** Pure-Kotlin fake so the test never touches secp256k1 JNI. */ + private class FakeSigner : NostrSigner("aa".repeat(32)) { + override fun isWriteable() = true + + override suspend fun sign( + createdAt: Long, + kind: Int, + tags: Array>, + content: String, + ): T = throw UnsupportedOperationException("fake") + + override suspend fun nip04Encrypt( + plaintext: String, + toPublicKey: HexKey, + ) = "enc04" + + override suspend fun nip04Decrypt( + ciphertext: String, + fromPublicKey: HexKey, + ) = "dec04" + + override suspend fun nip44Encrypt( + plaintext: String, + toPublicKey: HexKey, + ) = "enc44" + + override suspend fun nip44Decrypt( + ciphertext: String, + fromPublicKey: HexKey, + ) = "dec44" + + override suspend fun decryptZapEvent(event: LnZapRequestEvent): LnZapPrivateEvent = throw UnsupportedOperationException("fake") + + override suspend fun deriveKey(nonce: HexKey): HexKey = "bb".repeat(32) + + override suspend fun signPsbt(psbtHex: String): String = psbtHex + + override fun hasForegroundSupport() = true + } + + @Test + fun countsSignsAndCryptoOpsWithoutChangingResults() = + runTest { + val store = ResourceUsageStore(File(temp.root, "u.json")) + val accountant = ResourceUsageAccountant(store, backgroundScope, epochDay = { 100L }) + val metered = MeteringNostrSigner(FakeSigner(), accountant) + + assertEquals("dec44", metered.nip44Decrypt("x", "aa".repeat(32))) + assertEquals("dec04", metered.nip04Decrypt("x", "aa".repeat(32))) + assertEquals("enc44", metered.nip44Encrypt("x", "aa".repeat(32))) + runCatching { metered.sign(0L, 1, arrayOf(), "hello") } + + val today = accountant.allDaysIncludingLive()[100].orEmpty() + assertEquals(2L, today[UsageKeys.DECRYPT_COUNT]) + assertEquals(1L, today[UsageKeys.ENCRYPT_COUNT]) + assertEquals(1L, today[UsageKeys.signs(UsageKeys.SIGNER_LOCAL)]) + assertNull("non-local signers must not pollute crypto CPU time", today[UsageKeys.DECRYPT_US]) + } + + @Test + fun innermostSignerUnwrapsTheMeteringDecorator() = + runTest { + val store = ResourceUsageStore(File(temp.root, "u.json")) + val accountant = ResourceUsageAccountant(store, backgroundScope, epochDay = { 100L }) + val raw = FakeSigner() + val metered = MeteringNostrSigner(raw, accountant) + assertEquals(raw, metered.innermostSigner()) + } +} + +class ScreenTimeIntegratorTest { + @get:Rule + val temp = TemporaryFolder() + + @Test + fun routeNamesLoseTheirArgumentsBeforeAnythingIsRecorded() { + assertEquals("Profile", ScreenTimeIntegrator.screenNameOf("com.vitorpamplona.amethyst.ui.navigation.routes.Route.Profile/{userId}")) + assertEquals("Hashtag", ScreenTimeIntegrator.screenNameOf("routes.Route.Hashtag/{tag}?extra={extra}")) + assertEquals("Home", ScreenTimeIntegrator.screenNameOf("routes.Route.Home")) + assertNull(ScreenTimeIntegrator.screenNameOf(null)) + assertNull(ScreenTimeIntegrator.screenNameOf("")) + } + + @Test + fun accountsScreenTimeOnlyWhileForeground() = + runTest { + val store = ResourceUsageStore(File(temp.root, "u.json")) + val accountant = ResourceUsageAccountant(store, backgroundScope, epochDay = { 100L }) + var clock = 0L + val isForeground = MutableStateFlow(true) + val integrator = ScreenTimeIntegrator(accountant, nowMs = { clock }) + integrator.start(backgroundScope, isForeground) + testScheduler.runCurrent() + + integrator.onScreen("Home") + testScheduler.runCurrent() + clock += 5_000 + integrator.onScreen("Video") + testScheduler.runCurrent() + clock += 3_000 + isForeground.value = false // backgrounded on Video: segment closes + testScheduler.runCurrent() + clock += 60_000 // background time must not count + isForeground.value = true + testScheduler.runCurrent() + clock += 2_000 + integrator.onScreen(null) + testScheduler.runCurrent() + + val today = accountant.allDaysIncludingLive()[100].orEmpty() + assertEquals(5_000L, today[UsageKeys.screenMs("Home")]) + assertEquals(5_000L, today[UsageKeys.screenMs("Video")]) + } +} + +class UsageSummaryMapsTest { + @Test + fun screenTimeAndCellularMapsAreExtractedFromCounters() { + val s = + UsageSummary.from( + mapOf( + UsageKeys.screenMs("Home") to 10_000L, + UsageKeys.screenMs("Video") to 5_000L, + UsageKeys.net(UsageKeys.ROLE_IMAGE, mobile = true, foreground = true, received = true) to 100L, + UsageKeys.net(UsageKeys.ROLE_IMAGE, mobile = false, foreground = true, received = true) to 900L, + ), + ) + assertEquals(10_000L, s.screenTimeMs["Home"]) + assertEquals(5_000L, s.screenTimeMs["Video"]) + assertEquals(100L, s.mobileBytesPerSubsystem[UsageKeys.ROLE_IMAGE]) + assertEquals(1_000L, s.bytesPerSubsystem[UsageKeys.ROLE_IMAGE]) + } +} + +class ResourceUsageAlertsTest { + private fun day(vararg counters: Pair) = mapOf(*counters) + + @Test + fun quietUsageDoesNotAlert() { + val days = + mapOf( + 9L to + day( + UsageKeys.net(UsageKeys.ROLE_IMAGE, mobile = true, foreground = false, received = true) to 1024L * 1024L, + UsageKeys.relayConnMs(mobile = true, foreground = false) to 60L * 60L * 1000L, + ), + ) + assertNull(ResourceUsageAlerts.evaluate(days, today = 10L)) + } + + @Test + fun backgroundMobileDataCrossingThresholdAlerts() { + val days = + mapOf( + 9L to + day( + UsageKeys.net(UsageKeys.ROLE_VIDEO, mobile = true, foreground = false, received = true) to + ResourceUsageAlerts.BG_MOBILE_BYTES_PER_DAY + 1, + ), + ) + val alert = ResourceUsageAlerts.evaluate(days, today = 10L) + assertEquals(ResourceUsageAlerts.Reason.BACKGROUND_MOBILE_DATA, alert?.reason) + assertEquals(9L, alert?.day) + } + + @Test + fun foregroundMobileDataDoesNotTripTheBackgroundThreshold() { + val days = + mapOf( + 9L to + day( + UsageKeys.net(UsageKeys.ROLE_VIDEO, mobile = true, foreground = true, received = true) to + ResourceUsageAlerts.BG_MOBILE_BYTES_PER_DAY * 10, + ), + ) + assertNull(ResourceUsageAlerts.evaluate(days, today = 10L)) + } + + @Test + fun connectionTimeCounterDoesNotLeakIntoByteThreshold() { + // relay.connms keys carry mobile+bg dims but are milliseconds, not + // bytes: they must never count toward the data threshold. + val days = + mapOf( + 9L to + day( + UsageKeys.relayConnMs(mobile = true, foreground = false) to + ResourceUsageAlerts.BG_MOBILE_BYTES_PER_DAY * 100, + ), + ) + val alert = ResourceUsageAlerts.evaluate(days, today = 10L) + assertEquals(ResourceUsageAlerts.Reason.BACKGROUND_MOBILE_CONNECTION_TIME, alert?.reason) + } + + @Test + fun todayIsCheckedWhenYesterdayIsQuiet() { + val days = + mapOf( + 10L to day(UsageKeys.APP_STARTS to ResourceUsageAlerts.APP_STARTS_PER_DAY + 1), + ) + val alert = ResourceUsageAlerts.evaluate(days, today = 10L) + assertEquals(ResourceUsageAlerts.Reason.PROCESS_CHURN, alert?.reason) + } + + @Test + fun reconnectChurnAlerts() { + val days = + mapOf( + 9L to + day( + UsageKeys.relayConnects(mobile = true, foreground = false) to 3_000L, + UsageKeys.relayConnects(mobile = false, foreground = true) to + ResourceUsageAlerts.RELAY_CONNECTS_PER_DAY - 2_000L, + ), + ) + val alert = ResourceUsageAlerts.evaluate(days, today = 10L) + assertEquals(ResourceUsageAlerts.Reason.RECONNECT_CHURN, alert?.reason) + } + + @Test + fun promptRateLimiting() { + val now = 1_000_000L + val week = ResourceUsageAlerts.MIN_DAYS_BETWEEN_PROMPTS * 24 * 60 * 60 + assertTrue(ResourceUsageAlerts.shouldPrompt(lastAlertAtSec = 0, optOut = false, nowSec = now)) + assertFalse(ResourceUsageAlerts.shouldPrompt(lastAlertAtSec = now - week + 10, optOut = false, nowSec = now)) + assertTrue(ResourceUsageAlerts.shouldPrompt(lastAlertAtSec = now - week - 10, optOut = false, nowSec = now)) + assertFalse(ResourceUsageAlerts.shouldPrompt(lastAlertAtSec = 0, optOut = true, nowSec = now)) + } +} diff --git a/amethyst/src/test/java/com/vitorpamplona/amethyst/service/scheduledposts/ScheduledPostStoreTest.kt b/amethyst/src/test/java/com/vitorpamplona/amethyst/service/scheduledposts/ScheduledPostStoreTest.kt index ea6e0a09a3..4389ea6b78 100644 --- a/amethyst/src/test/java/com/vitorpamplona/amethyst/service/scheduledposts/ScheduledPostStoreTest.kt +++ b/amethyst/src/test/java/com/vitorpamplona/amethyst/service/scheduledposts/ScheduledPostStoreTest.kt @@ -60,6 +60,22 @@ class ScheduledPostStoreTest { createdAtSec = 500, ) + @Test + fun stalePublishingClaimsAreReleasedOnLoad() = + runTest { + val store = newStore() + store.add(samplePost(id = "a")) + val claimed = store.claimDuePosts(nowSec = 2_000) + assertEquals(1, claimed.size) + assertEquals(ScheduledPostStatus.PUBLISHING, store.list().single().status) + + // A fresh store (new process) finds the on-disk PUBLISHING row: the + // worker that claimed it died with the old process, so the claim + // must be released for the next cycle to retry. + val reloaded = newStore().list().single() + assertEquals(ScheduledPostStatus.PENDING, reloaded.status) + } + @Test fun add_persists_to_disk() = runTest { diff --git a/amethyst/src/test/java/com/vitorpamplona/amethyst/service/scheduledposts/ScheduledPostWorkGateTest.kt b/amethyst/src/test/java/com/vitorpamplona/amethyst/service/scheduledposts/ScheduledPostWorkGateTest.kt new file mode 100644 index 0000000000..85f85d0515 --- /dev/null +++ b/amethyst/src/test/java/com/vitorpamplona/amethyst/service/scheduledposts/ScheduledPostWorkGateTest.kt @@ -0,0 +1,171 @@ +/* + * Copyright (c) 2025 Vitor Pamplona + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.vitorpamplona.amethyst.service.scheduledposts + +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.runBlocking +import kotlinx.coroutines.test.TestScope +import kotlinx.coroutines.test.runCurrent +import kotlinx.coroutines.test.runTest +import org.junit.Assert.assertEquals +import org.junit.Before +import org.junit.Rule +import org.junit.Test +import org.junit.rules.TemporaryFolder +import java.io.File + +/** + * The gate must keep the periodic worker enqueued exactly while a PENDING + * post exists — and, critically, must never act on the store flow's empty + * placeholder value before the disk load completes (which would cancel + * scheduled work an existing pending post still needs). + */ +@OptIn(ExperimentalCoroutinesApi::class) +class ScheduledPostWorkGateTest { + @get:Rule + val temp = TemporaryFolder() + + private lateinit var file: File + + /** Chronological record of gate decisions: true = schedule, false = cancel. */ + private val decisions = mutableListOf() + + @Before + fun setUp() { + file = File(temp.root, "scheduled_posts.json") + decisions.clear() + } + + private fun newStore() = ScheduledPostStore(file) + + private fun TestScope.startGate(store: ScheduledPostStore) = + ScheduledPostWorkGate( + store = store, + scope = backgroundScope, + onPendingWork = { decisions.add(true) }, + onNoPendingWork = { decisions.add(false) }, + ).start() + + private fun samplePost( + id: String = "id-1", + publishAtSec: Long = 4_000_000_000, + ) = ScheduledPost( + id = id, + accountPubkey = "pk1", + signedEventJson = "{}", + relayUrls = listOf("wss://relay.example/"), + extraEventsJson = emptyList(), + publishAtSec = publishAtSec, + createdAtSec = 500, + ) + + @Test + fun emptyStore_cancelsOnceAndOnlyOnce() = + runTest { + startGate(newStore()) + runCurrent() + assertEquals(listOf(false), decisions) + } + + @Test + fun pendingPostOnDisk_schedulesWithoutSpuriousCancelFirst() = + runTest { + // Persist a pending post in a previous "process". + runBlocking { newStore().add(samplePost()) } + + // Fresh store (flow starts as the empty placeholder). The gate must + // load from disk before collecting: the FIRST decision must be + // "schedule", never a cancel from the placeholder value. + startGate(newStore()) + runCurrent() + assertEquals(listOf(true), decisions) + } + + @Test + fun addThenDrainThenAddAgain_togglesTheWorker() = + runTest { + val store = newStore() + startGate(store) + runCurrent() + assertEquals(listOf(false), decisions) + + store.add(samplePost(id = "a")) + runCurrent() + assertEquals(listOf(false, true), decisions) + + // A second pending post must not re-fire (distinctUntilChanged). + store.add(samplePost(id = "b")) + runCurrent() + assertEquals(listOf(false, true), decisions) + + // Draining both pending posts cancels the periodic chain. + store.markSent("a") + runCurrent() + assertEquals(listOf(false, true), decisions) + store.markFailed("b", "boom") + runCurrent() + assertEquals(listOf(false, true, false), decisions) + + // A new post re-schedules. + store.add(samplePost(id = "c")) + runCurrent() + assertEquals(listOf(false, true, false, true), decisions) + } + + @Test + fun claimingTheLastPendingPost_doesNotCancelTheRunningWorker() = + runTest { + val store = newStore() + store.add(samplePost(id = "a", publishAtSec = 1_000)) + startGate(store) + runCurrent() + assertEquals(listOf(true), decisions) + + // The periodic worker claims the post (PENDING -> PUBLISHING) and + // is now mid-publish. Cancelling here would kill the very worker + // holding the claim and strand the post in PUBLISHING forever. + store.claimDuePosts(nowSec = 2_000) + runCurrent() + assertEquals("a claim must never cancel the chain", listOf(true), decisions) + + // Only the publish actually finishing drains the gate. + store.markSent("a") + runCurrent() + assertEquals(listOf(true, false), decisions) + } + + @Test + fun publishNowOnFailedPost_reschedulesTheWorker() = + runTest { + val store = newStore() + store.add(samplePost(id = "a")) + startGate(store) + runCurrent() + store.markFailed("a", "relay down") + runCurrent() + assertEquals(listOf(true, false), decisions) + + // Retry flips the post back to PENDING; the worker must come back. + store.publishNow("a") + runCurrent() + assertEquals(listOf(true, false, true), decisions) + } +} diff --git a/amethyst/src/test/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/notifications/dal/NotificationKindsContractTest.kt b/amethyst/src/test/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/notifications/dal/NotificationKindsContractTest.kt new file mode 100644 index 0000000000..0e49022cf9 --- /dev/null +++ b/amethyst/src/test/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/notifications/dal/NotificationKindsContractTest.kt @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2025 Vitor Pamplona + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.vitorpamplona.amethyst.ui.screen.loggedIn.notifications.dal + +import com.vitorpamplona.amethyst.commons.moderation.notifications.NotificationKinds +import com.vitorpamplona.quartz.nip59Giftwrap.wraps.EphemeralGiftWrapEvent +import com.vitorpamplona.quartz.nip59Giftwrap.wraps.GiftWrapEvent +import org.junit.Assert.assertTrue +import org.junit.Test + +/** + * Pins the relationship between the two independently-maintained kind lists: + * + * - `NotificationKinds.SUBSCRIPTION_KINDS` (commons) — what Desktop asks + * relays for and toasts on. May contain ENVELOPE kinds (gift wraps) whose + * created_at is randomized per NIP-59, because Desktop surfaces the wrap + * itself as a DM row. + * - `NotificationFeedFilter.NOTIFICATION_KINDS` (Android) — what renders as + * a row on the Notifications tab. Envelopes must never appear here; the + * unwrapped inner event is the row. + * + * The lists were briefly coupled (NOTIFICATION_KINDS spread + * SUBSCRIPTION_KINDS), which silently pulled the kind-1059 wrap into the + * Android feed. They are now maintained separately; this test is the tripwire + * that keeps them from drifting apart unintentionally in either direction. + */ +class NotificationKindsContractTest { + private val envelopeKinds = setOf(GiftWrapEvent.KIND, EphemeralGiftWrapEvent.KIND) + + @Test + fun `envelope kinds never render on the Android notifications tab`() { + val leaked = envelopeKinds.intersect(NotificationFeedFilter.NOTIFICATION_KINDS.toSet()) + assertTrue( + "Envelope kinds $leaked are in NOTIFICATION_KINDS. Wraps have a " + + "randomized created_at (NIP-59) and no decryptable payload to render — " + + "the unwrapped inner event is the feed row. If a new envelope kind is " + + "intentional, unwrap it instead of displaying it.", + leaked.isEmpty(), + ) + } + + @Test + fun `every kind desktop notifies on is displayable on Android or a known envelope`() { + val unaccounted = + NotificationKinds.SUBSCRIPTION_KINDS.toSet() - + NotificationFeedFilter.NOTIFICATION_KINDS.toSet() - + envelopeKinds + + assertTrue( + "Kinds $unaccounted were added to the shared SUBSCRIPTION_KINDS but are " + + "neither displayable on the Android notifications tab nor a known " + + "envelope kind. Either add them to NOTIFICATION_KINDS (if Android " + + "should render them) or to envelopeKinds in this test (if they only " + + "deliver an inner payload).", + unaccounted.isEmpty(), + ) + } +} diff --git a/commons/src/commonMain/composeResources/font/material_symbols_outlined.ttf b/commons/src/commonMain/composeResources/font/material_symbols_outlined.ttf index aed61e63f4..590cbff2c3 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/commons/src/commonMain/composeResources/values/strings.xml b/commons/src/commonMain/composeResources/values/strings.xml index f7cbf115a9..e50c831382 100644 --- a/commons/src/commonMain/composeResources/values/strings.xml +++ b/commons/src/commonMain/composeResources/values/strings.xml @@ -90,4 +90,15 @@ Source: Servers: Open + + + Use direct URL + + + Nickname + Shown to you instead of this user\'s name, everywhere in the app. It\'s saved encrypted in your contact card: only you can read it. Type : to use your custom emojis. + Nickname + Private note about this user + Save + Cancel diff --git a/commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/icons/symbols/MaterialSymbols.kt b/commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/icons/symbols/MaterialSymbols.kt index 714013ec98..80bac6a653 100644 --- a/commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/icons/symbols/MaterialSymbols.kt +++ b/commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/icons/symbols/MaterialSymbols.kt @@ -165,6 +165,7 @@ object MaterialSymbols { val NoAccounts = MaterialSymbol("\uF03E") val NoEncryption = MaterialSymbol("\uF03F") val Notifications = MaterialSymbol("\uE7F5") + val NotificationsOff = MaterialSymbol("\uE7F6") val Numbers = MaterialSymbol("\uEAC7") val OpenInBrowser = MaterialSymbol("\uE89D") val OpenInFull = MaterialSymbol("\uF1CE") diff --git a/commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/model/nip30CustomEmojis/EmojiPackState.kt b/commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/model/nip30CustomEmojis/EmojiPackState.kt index f8f77d36c9..decf5f345e 100644 --- a/commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/model/nip30CustomEmojis/EmojiPackState.kt +++ b/commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/model/nip30CustomEmojis/EmojiPackState.kt @@ -25,6 +25,7 @@ import com.vitorpamplona.amethyst.commons.model.NoteState import com.vitorpamplona.amethyst.commons.model.cache.ICacheProvider import com.vitorpamplona.quartz.nip01Core.signers.NostrSigner import com.vitorpamplona.quartz.nip01Core.tags.aTag.taggedAddresses +import com.vitorpamplona.quartz.nip30CustomEmoji.CustomEmoji import com.vitorpamplona.quartz.nip30CustomEmoji.EmojiUrlTag import com.vitorpamplona.quartz.nip30CustomEmoji.pack.EmojiPackEvent import com.vitorpamplona.quartz.nip30CustomEmoji.selection.EmojiPackSelectionEvent @@ -125,6 +126,23 @@ class EmojiPackState( emptyList(), ) + /** + * Resolves every `:shortcode:` in [message] against the account's selected + * emoji packs, returning the NIP-30 `emoji` tags an event needs to carry for + * the codes to render. Unknown codes are simply skipped. + */ + fun findEmojiTags(message: String): List { + val myEmojiSet = myEmojis.value + if (myEmojiSet.isEmpty()) return emptyList() + val byCode = myEmojiSet.associateBy { it.code } + return CustomEmoji + .findAllEmojiCodes(message) + .distinct() + .mapNotNull { code -> + byCode[code]?.let { EmojiUrlTag(it.code, it.link) } + } + } + suspend fun addEmojiPack(emojiPack: Note): EmojiPackSelectionEvent { val emojiPackEvent = emojiPack.event if (emojiPackEvent !is EmojiPackEvent) throw IllegalArgumentException("Note is not an EmojiPackEvent; cannot add to emoji list.") diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/note/creators/emojiSuggestions/EmojiSuggestionState.kt b/commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/model/nip30CustomEmojis/EmojiSuggestionState.kt similarity index 73% rename from amethyst/src/main/java/com/vitorpamplona/amethyst/ui/note/creators/emojiSuggestions/EmojiSuggestionState.kt rename to commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/model/nip30CustomEmojis/EmojiSuggestionState.kt index 67fc66229b..0f5e457153 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/note/creators/emojiSuggestions/EmojiSuggestionState.kt +++ b/commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/model/nip30CustomEmojis/EmojiSuggestionState.kt @@ -18,25 +18,30 @@ * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -package com.vitorpamplona.amethyst.ui.note.creators.emojiSuggestions +package com.vitorpamplona.amethyst.commons.model.nip30CustomEmojis +import androidx.compose.foundation.text.input.TextFieldState import androidx.compose.runtime.Stable -import com.vitorpamplona.amethyst.commons.model.nip30CustomEmojis.EmojiPackState -import com.vitorpamplona.amethyst.model.Account +import com.vitorpamplona.amethyst.commons.ui.text.replaceCurrentWord import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.IO import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.flowOn +/** + * Backs the `:shortcode:` autocomplete in text fields: feed the word under the + * cursor into [processCurrentWord] and collect [results] for the matching + * emojis from the account's selected packs. + */ @Stable class EmojiSuggestionState( - val account: Account, + val emojiPacks: EmojiPackState, ) { val search: MutableStateFlow = MutableStateFlow("") val results: Flow> = - account - .emoji.myEmojis + emojiPacks.myEmojis .combine(search) { list, search -> if (search.length == 1) { list @@ -63,4 +68,16 @@ class EmojiSuggestionState( } } } + + /** + * Completes the word under the cursor in [field] with the selected emoji's + * `:shortcode:` and closes the suggestion list. + */ + fun autocompleteInto( + field: TextFieldState, + item: EmojiPackState.EmojiMedia, + ) { + field.replaceCurrentWord(":${item.code}:") + reset() + } } diff --git a/commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/model/nip85TrustedAssertions/ContactCardDecryptionCache.kt b/commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/model/nip85TrustedAssertions/ContactCardDecryptionCache.kt new file mode 100644 index 0000000000..e6e5477791 --- /dev/null +++ b/commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/model/nip85TrustedAssertions/ContactCardDecryptionCache.kt @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2025 Vitor Pamplona + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.vitorpamplona.amethyst.commons.model.nip85TrustedAssertions + +import com.vitorpamplona.amethyst.commons.model.toImmutableListOfLists +import com.vitorpamplona.quartz.nip01Core.core.TagArray +import com.vitorpamplona.quartz.nip01Core.signers.NostrSigner +import com.vitorpamplona.quartz.nip51Lists.PrivateTagArrayEventCache +import com.vitorpamplona.quartz.nip85TrustedAssertions.users.ContactCardEvent +import com.vitorpamplona.quartz.nip85TrustedAssertions.users.petName +import com.vitorpamplona.quartz.nip85TrustedAssertions.users.summary + +/** + * Decrypts and caches the NIP-44 private tags of the account's own kind:30382 + * contact cards. Petname and summary always live in the encrypted part, so + * reading them requires the account's main key ([signer]); cards signed by any + * other key never decrypt here. + */ +class ContactCardDecryptionCache( + val signer: NostrSigner, +) { + val cachedPrivateCards = PrivateTagArrayEventCache(signer, cacheSize = 100) + + suspend fun petName(event: ContactCardEvent) = cachedPrivateCards.mergeTagList(event).petName() + + suspend fun summary(event: ContactCardEvent) = cachedPrivateCards.mergeTagList(event).summary() + + /** + * The decrypted petname and summary plus the card's full decrypted tag list, + * so renderers can resolve the NIP-30 `emoji` mappings stored alongside them. + */ + suspend fun nickname(event: ContactCardEvent): Nickname? = cachedPrivateCards.mergeTagList(event).toNickname() + + /** + * Synchronous variant that only reads an already-decrypted card, for use as + * the immediate value of UI flows. Returns null until the suspend path has + * decrypted the card once. + */ + fun cachedNickname(event: ContactCardEvent): Nickname? = cachedPrivateCards.mergeTagListPrecached(event).toNickname() + + private fun TagArray.toNickname(): Nickname? { + val name = petName() + val summary = summary() + if (name == null && summary == null) return null + return Nickname(name, summary, toImmutableListOfLists()) + } +} diff --git a/commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/model/nip85TrustedAssertions/ContactCardsState.kt b/commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/model/nip85TrustedAssertions/ContactCardsState.kt new file mode 100644 index 0000000000..f643429c5c --- /dev/null +++ b/commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/model/nip85TrustedAssertions/ContactCardsState.kt @@ -0,0 +1,171 @@ +/* + * Copyright (c) 2025 Vitor Pamplona + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.vitorpamplona.amethyst.commons.model.nip85TrustedAssertions + +import androidx.compose.runtime.Stable +import com.vitorpamplona.amethyst.commons.model.AddressableNote +import com.vitorpamplona.amethyst.commons.model.User +import com.vitorpamplona.amethyst.commons.model.cache.ICacheProvider +import com.vitorpamplona.amethyst.commons.model.nip30CustomEmojis.EmojiPackState +import com.vitorpamplona.quartz.nip01Core.core.Address +import com.vitorpamplona.quartz.nip01Core.core.HexKey +import com.vitorpamplona.quartz.nip01Core.signers.NostrSigner +import com.vitorpamplona.quartz.nip85TrustedAssertions.users.ContactCardEvent +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.IO +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.combine +import kotlinx.coroutines.flow.distinctUntilChanged +import kotlinx.coroutines.flow.flatMapLatest +import kotlinx.coroutines.flow.flowOf +import kotlinx.coroutines.flow.flowOn +import kotlinx.coroutines.flow.map +import kotlinx.coroutines.flow.mapLatest + +/** + * The account's own kind:30382 contact cards — one card per target user, signed + * by the account's main key. This is how the user nicknames other users: the + * petname and summary always live in the card's NIP-44 encrypted content. + * + * The same kind is used by trust providers for WoT scores; those cards are + * ignored here because everything is keyed on the card author being the + * account itself ([signer]'s pubkey). + */ +@Stable +class ContactCardsState( + val signer: NostrSigner, + val cache: ICacheProvider, + val decryptionCache: ContactCardDecryptionCache, + val emojiPacks: EmojiPackState, +) { + private val accountUser: User? by lazy { cache.getOrCreateUser(signer.pubKey) } + + fun createCardAddress(target: HexKey): Address = ContactCardEvent.createAddress(signer.pubKey, target) + + fun getCardNote(target: HexKey): AddressableNote = cache.getOrCreateAddressableNote(createCardAddress(target)) + + fun getCard(target: HexKey): ContactCardEvent? = getCardNote(target).event as? ContactCardEvent + + /** + * The account's own card about [target], as attached to the target user's + * [UserCardsCache] when the event is consumed. `cards()` lazily allocates the + * per-user cache (like `metadata()` does) so a card arriving later is seen. + */ + @OptIn(ExperimentalCoroutinesApi::class) + fun myCardFlow(target: User): Flow = + target + .cards() + .receivedCards + .map { it[accountUser] } + .distinctUntilChanged() + .flatMapLatest { note -> + note + ?.flow() + ?.metadata + ?.stateFlow + ?.map { it.note.event as? ContactCardEvent } + ?: flowOf(null) + } + + /** + * The nickname (petname + private summary) the account gave [target], + * decrypted from the card's content, along with the card's tags so + * `:shortcode:` custom emojis resolve. + */ + @OptIn(ExperimentalCoroutinesApi::class) + fun nicknameFlow(target: User): Flow = + myCardFlow(target) + .mapLatest { card -> card?.let { decryptionCache.nickname(it) } } + .distinctUntilChanged() + .flowOn(Dispatchers.IO) + + /** + * Synchronously returns the nickname for [target] when its card is already + * decrypted (or has none to decrypt). Cheap enough for initial values of UI + * flows: a map read plus the decryption cache lookup, no crypto. + */ + fun cachedNickname(target: User): Nickname? { + val card = + target + .cardsOrNull() + ?.receivedCards + ?.value + ?.get(accountUser) + ?.event as? ContactCardEvent ?: return null + return decryptionCache.cachedNickname(card) + } + + /** + * The name to render for [target], per the NIP-81 policy: the nickname the + * account gave them wins over the profile's own display name, falling back + * to the short npub when neither exists. + */ + fun displayNameFlow(target: User): Flow = + combine( + target.metadata().flow, + nicknameFlow(target), + ) { info, nickname -> + nickname?.petName ?: info?.info?.bestName() ?: target.pubkeyDisplayHex() + }.distinctUntilChanged() + + /** Synchronous first value for [displayNameFlow], from already-decrypted data. */ + fun cachedDisplayName(target: User): String = cachedNickname(target)?.petName ?: target.toBestDisplayName() + + suspend fun petName(target: HexKey): String? = getCard(target)?.let { decryptionCache.petName(it) } + + suspend fun summary(target: HexKey): String? = getCard(target)?.let { decryptionCache.summary(it) } + + /** + * Builds the new signed card for [target] with the given petname and summary + * (`null` clears a field), preserving every other tag of an existing card. + * Any `:shortcode:` from the account's emoji packs gets its NIP-30 emoji + * mapping embedded; everything is stored NIP-44 encrypted. The caller is + * responsible for publishing it. + */ + suspend fun updatePetNameAndSummary( + target: HexKey, + petName: String?, + summary: String?, + ): ContactCardEvent { + val emojis = emojiPacks.findEmojiTags(listOfNotNull(petName, summary).joinToString(" ")) + val existing = getCard(target) + return if (existing != null) { + signer.sign( + ContactCardEvent.updatePetNameAndSummary( + earlierVersion = existing, + petName = petName, + summary = summary, + emojis = emojis, + signer = signer, + ), + ) + } else { + ContactCardEvent.create( + targetUser = target, + petName = petName, + summary = summary, + emojis = emojis, + signer = signer, + ) + } + } +} diff --git a/commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/model/nip85TrustedAssertions/Nickname.kt b/commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/model/nip85TrustedAssertions/Nickname.kt new file mode 100644 index 0000000000..1d678013a8 --- /dev/null +++ b/commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/model/nip85TrustedAssertions/Nickname.kt @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2025 Vitor Pamplona + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.vitorpamplona.amethyst.commons.model.nip85TrustedAssertions + +import androidx.compose.runtime.Immutable +import com.vitorpamplona.amethyst.commons.model.ImmutableListOfLists + +/** + * The decrypted private fields of the account's contact card about a user: the + * nickname (petname) and the private note (summary), plus the card's decrypted + * tag list so renderers can resolve any NIP-30 `:shortcode:` custom emojis they + * use (the `emoji` mappings live encrypted next to them). Only built when at + * least one of the two fields is present. + */ +@Immutable +class Nickname( + val petName: String?, + val summary: String?, + val tags: ImmutableListOfLists, +) { + // content equality so flow distinctUntilChanged() dedupes re-decryptions of + // the same card (ImmutableListOfLists itself compares by identity) + override fun equals(other: Any?): Boolean = + other is Nickname && + petName == other.petName && + summary == other.summary && + tags.lists.contentDeepEquals(other.tags.lists) + + override fun hashCode(): Int = 31 * (31 * petName.hashCode() + summary.hashCode()) + tags.contentHash() +} diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/note/creators/emojiSuggestions/ShowEmojiSuggestionList.kt b/commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/nip30CustomEmojis/ui/ShowEmojiSuggestionList.kt similarity index 84% rename from amethyst/src/main/java/com/vitorpamplona/amethyst/ui/note/creators/emojiSuggestions/ShowEmojiSuggestionList.kt rename to commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/nip30CustomEmojis/ui/ShowEmojiSuggestionList.kt index b6e6ec3e8a..61d0048193 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/note/creators/emojiSuggestions/ShowEmojiSuggestionList.kt +++ b/commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/nip30CustomEmojis/ui/ShowEmojiSuggestionList.kt @@ -18,7 +18,7 @@ * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -package com.vitorpamplona.amethyst.ui.note.creators.emojiSuggestions +package com.vitorpamplona.amethyst.commons.nip30CustomEmojis.ui import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.Arrangement.spacedBy @@ -39,14 +39,17 @@ import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.unit.dp import androidx.lifecycle.compose.collectAsStateWithLifecycle import coil3.compose.AsyncImage -import com.vitorpamplona.amethyst.R import com.vitorpamplona.amethyst.commons.icons.symbols.Icon import com.vitorpamplona.amethyst.commons.icons.symbols.MaterialSymbols import com.vitorpamplona.amethyst.commons.model.nip30CustomEmojis.EmojiPackState -import com.vitorpamplona.amethyst.ui.stringRes -import com.vitorpamplona.amethyst.ui.theme.DividerThickness -import com.vitorpamplona.amethyst.ui.theme.Size10dp -import com.vitorpamplona.amethyst.ui.theme.Size40Modifier +import com.vitorpamplona.amethyst.commons.model.nip30CustomEmojis.EmojiSuggestionState +import com.vitorpamplona.amethyst.commons.resources.Res +import com.vitorpamplona.amethyst.commons.resources.use_direct_url +import org.jetbrains.compose.resources.stringResource + +private val DividerThickness = 0.25.dp +private val RowSpacing = 10.dp +private val EmojiSize = Modifier.size(40.dp) @Composable fun ShowEmojiSuggestionList( @@ -72,23 +75,23 @@ fun ShowEmojiSuggestionList( bottom = 10.dp, ), verticalAlignment = Alignment.CenterVertically, - horizontalArrangement = spacedBy(Size10dp), + horizontalArrangement = spacedBy(RowSpacing), ) { AsyncImage( it.link, contentDescription = it.code, - modifier = Size40Modifier, + modifier = EmojiSize, ) Text(it.code, fontWeight = FontWeight.Bold, modifier = Modifier.weight(1f)) IconButton( - modifier = Size40Modifier, + modifier = EmojiSize, onClick = { onFullSize(it) }, ) { Icon( symbol = MaterialSymbols.OpenInFull, - contentDescription = stringRes(R.string.use_direct_url), + contentDescription = stringResource(Res.string.use_direct_url), modifier = Modifier.size(20.dp), ) } diff --git a/commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/nip85TrustedAssertions/ui/EditNicknameDialog.kt b/commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/nip85TrustedAssertions/ui/EditNicknameDialog.kt new file mode 100644 index 0000000000..b24c98cbb5 --- /dev/null +++ b/commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/nip85TrustedAssertions/ui/EditNicknameDialog.kt @@ -0,0 +1,172 @@ +/* + * Copyright (c) 2025 Vitor Pamplona + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.vitorpamplona.amethyst.commons.nip85TrustedAssertions.ui + +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.heightIn +import androidx.compose.foundation.text.input.TextFieldLineLimits +import androidx.compose.foundation.text.input.TextFieldState +import androidx.compose.foundation.text.input.rememberTextFieldState +import androidx.compose.foundation.text.input.setTextAndPlaceCursorAtEnd +import androidx.compose.material3.AlertDialog +import androidx.compose.material3.Button +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.OutlinedTextField +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.snapshotFlow +import androidx.compose.ui.Modifier +import androidx.compose.ui.unit.dp +import com.vitorpamplona.amethyst.commons.model.User +import com.vitorpamplona.amethyst.commons.model.nip30CustomEmojis.EmojiSuggestionState +import com.vitorpamplona.amethyst.commons.model.nip85TrustedAssertions.ContactCardsState +import com.vitorpamplona.amethyst.commons.nip30CustomEmojis.ui.ShowEmojiSuggestionList +import com.vitorpamplona.amethyst.commons.resources.Res +import com.vitorpamplona.amethyst.commons.resources.nickname_cancel +import com.vitorpamplona.amethyst.commons.resources.nickname_dialog_explainer +import com.vitorpamplona.amethyst.commons.resources.nickname_dialog_title +import com.vitorpamplona.amethyst.commons.resources.nickname_label +import com.vitorpamplona.amethyst.commons.resources.nickname_save +import com.vitorpamplona.amethyst.commons.resources.nickname_summary_label +import com.vitorpamplona.amethyst.commons.ui.text.currentWord +import kotlinx.coroutines.launch +import org.jetbrains.compose.resources.stringResource + +/** + * Edits the nickname (petname) and private note (summary) the account keeps for + * [user] in its own kind:30382 contact card. Both fields are saved NIP-44 + * encrypted, so only this account can read them. Blank fields clear the value. + * + * Typing `:` offers the account's NIP-30 custom emojis; the mappings for any + * shortcode used are embedded (also encrypted) so the nickname renders with them. + * + * Shared by every front end: the caller supplies the account's [contactCards] + * and publishes the result in [onSave] (e.g. through its outbox relays). On + * Android, compose `WatchAndLoadMyEmojiList` alongside so the emoji packs load. + */ +@Composable +fun EditNicknameDialog( + user: User, + contactCards: ContactCardsState, + onSave: (petName: String?, summary: String?) -> Unit, + onDismiss: () -> Unit, +) { + val nickname = rememberTextFieldState() + val summary = rememberTextFieldState() + val emojiSuggestions = remember(contactCards) { EmojiSuggestionState(contactCards.emojiPacks) } + // which field the emoji autocomplete should insert into: the last one edited + val emojiTarget = remember { mutableStateOf(null) } + + // Prefill with the card's current encrypted values, if any. Decryption can + // be slow on external signers, so don't clobber anything already typed. + LaunchedEffect(user) { + contactCards + .petName(user.pubkeyHex) + ?.takeIf { nickname.text.isEmpty() } + ?.let { nickname.setTextAndPlaceCursorAtEnd(it) } + contactCards + .summary(user.pubkeyHex) + ?.takeIf { summary.text.isEmpty() } + ?.let { summary.setTextAndPlaceCursorAtEnd(it) } + } + + // Feed the word under the cursor of the last-edited field to the autocomplete. + LaunchedEffect(nickname, summary) { + fun watch(field: TextFieldState) { + emojiTarget.value = field + if (field.selection.collapsed) { + emojiSuggestions.processCurrentWord(field.currentWord()) + } + } + launch { snapshotFlow { nickname.text }.collect { watch(nickname) } } + launch { snapshotFlow { summary.text }.collect { watch(summary) } } + } + + AlertDialog( + onDismissRequest = onDismiss, + title = { + Text(text = stringResource(Res.string.nickname_dialog_title)) + }, + text = { + Column( + verticalArrangement = Arrangement.spacedBy(10.dp), + ) { + Text( + text = stringResource(Res.string.nickname_dialog_explainer), + color = MaterialTheme.colorScheme.onSurfaceVariant, + ) + OutlinedTextField( + state = nickname, + lineLimits = TextFieldLineLimits.SingleLine, + label = { + Text(text = stringResource(Res.string.nickname_label)) + }, + ) + OutlinedTextField( + state = summary, + label = { + Text(text = stringResource(Res.string.nickname_summary_label)) + }, + ) + ShowEmojiSuggestionList( + emojiSuggestions, + onSelect = { emoji -> + emojiTarget.value?.let { emojiSuggestions.autocompleteInto(it, emoji) } + }, + onFullSize = { emoji -> + emojiTarget.value?.let { emojiSuggestions.autocompleteInto(it, emoji) } + }, + modifier = Modifier.heightIn(max = 200.dp), + ) + } + }, + confirmButton = { + Button( + onClick = { + onSave( + nickname.text + .toString() + .trim() + .ifBlank { null }, + summary.text + .toString() + .trim() + .ifBlank { null }, + ) + onDismiss() + }, + ) { + Text(stringResource(Res.string.nickname_save)) + } + }, + dismissButton = { + Button( + onClick = onDismiss, + ) { + Text(stringResource(Res.string.nickname_cancel)) + } + }, + ) +} diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/relayClient/reqCommand/user/watchers/FilterContactCardsToKey.kt b/commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/relayClient/assemblers/ContactCardFilters.kt similarity index 65% rename from amethyst/src/main/java/com/vitorpamplona/amethyst/service/relayClient/reqCommand/user/watchers/FilterContactCardsToKey.kt rename to commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/relayClient/assemblers/ContactCardFilters.kt index 8013013f99..9eab40c51a 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/relayClient/reqCommand/user/watchers/FilterContactCardsToKey.kt +++ b/commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/relayClient/assemblers/ContactCardFilters.kt @@ -18,16 +18,22 @@ * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -package com.vitorpamplona.amethyst.service.relayClient.reqCommand.user.watchers +package com.vitorpamplona.amethyst.commons.relayClient.assemblers import com.vitorpamplona.quartz.nip01Core.core.HexKey import com.vitorpamplona.quartz.nip01Core.relay.client.pool.RelayBasedFilter import com.vitorpamplona.quartz.nip01Core.relay.filters.Filter import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl +import com.vitorpamplona.quartz.nip01Core.tags.dTag.DTag import com.vitorpamplona.quartz.nip85TrustedAssertions.users.ContactCardEvent val ContactCardKindList = listOf(ContactCardEvent.KIND) +/** + * Kind:30382 cards *about* [targets], written by [trustedAccounts] (the account + * itself plus its WoT trust providers). Fetches nicknames and scores for the + * users currently on screen. + */ fun filterContactCardsToTargetKeysFromTrustedAccountsInTheRelay( targets: Set, trustedAccounts: List, @@ -41,8 +47,31 @@ fun filterContactCardsToTargetKeysFromTrustedAccountsInTheRelay( Filter( kinds = ContactCardKindList, authors = trustedAccounts, - tags = mapOf("d" to targets.sorted()), + // kind:30382 addresses the target user in the d-tag + tags = mapOf(DTag.TAG_NAME to targets.sorted()), since = since, ), ) } + +/** + * Every kind:30382 card *written by* [author] — the account's own nicknames — + * for the bulk download at login from the account's relays. Addressable events: + * one card per target user, hence the larger limit. + */ +fun filterContactCardsByAuthorInTheRelay( + relay: NormalizedRelayUrl, + author: HexKey, + since: Long?, + limit: Int = 500, +): RelayBasedFilter = + RelayBasedFilter( + relay = relay, + filter = + Filter( + kinds = ContactCardKindList, + authors = listOf(author), + limit = limit, + since = since, + ), + ) diff --git a/desktopApp/packaging/flatpak/README.md b/desktopApp/packaging/flatpak/README.md index 12bf3f3c7a..552fba7e42 100644 --- a/desktopApp/packaging/flatpak/README.md +++ b/desktopApp/packaging/flatpak/README.md @@ -1,44 +1,71 @@ -# Flathub packaging for Amethyst Desktop +# Flatpak packaging for Amethyst Desktop -This directory contains the Flatpak manifest and associated metadata for -publishing Amethyst Desktop on Flathub. +This directory contains the Flatpak manifest and associated metadata. It is +used two ways: + +1. **Release CI** (`.github/workflows/create-release.yml`, `linux-portable` + leg) builds a single-file bundle from it on every tag and attaches it to + the GitHub Release as `amethyst-desktop--linux-x64.flatpak`. +2. **Flathub submission** — the `flathub/` subdirectory holds a + submission-ready variant that builds from the published GitHub Release + tarball (Flathub's build servers must fetch sources themselves; the + local `type: dir` tree used by CI is not allowed there). ## Files -- `com.vitorpamplona.amethyst.Desktop.yml` — Flatpak manifest -- `com.vitorpamplona.amethyst.Desktop.metainfo.xml` — AppStream metadata - (categories, license, screenshots — needs screenshots added before - submission) +- `com.vitorpamplona.amethyst.Desktop.yml` — Flatpak manifest. It packages + the **prebuilt** jpackage tree from + `./gradlew :desktopApp:createReleaseDistributable` (which bundles its own + trimmed JRE — that's why there is no openjdk module/sdk-extension). +- `com.vitorpamplona.amethyst.Desktop.metainfo.xml` — AppStream metadata. + Release CI injects the `` entry for the version being built + (the checked-in file deliberately carries none); screenshots still need + to be added before any Flathub submission. - `com.vitorpamplona.amethyst.Desktop.desktop` — XDG desktop entry -- `icons/256/com.vitorpamplona.amethyst.Desktop.png` — TODO: copy a 256x256 - PNG icon from `desktopApp/src/jvmMain/resources/icon.png` before - submission - -## Build prerequisites - -- `./gradlew :desktopApp:createReleaseDistributable` — produces - `desktopApp/build/compose/binaries/main-release/app/Amethyst/` -- `flatpak install org.freedesktop.Platform//24.08 org.freedesktop.Sdk//24.08 org.freedesktop.Sdk.Extension.openjdk21//24.08` +- `icons/512/com.vitorpamplona.amethyst.Desktop.png` — 512x512 icon (copy of + `desktopApp/src/jvmMain/resources/icon.png`) +- `flathub/` — self-contained, copy-ready Flathub submission dir: its own + manifest (archive source pinned to the release tarball URL + sha256, with + `x-checker-data` so Flathub's update bot bumps it), its own metainfo + (carries the permanent `` history Flathub requires), desktop + entry, icon, and `flathub.json` (`only-arches: x86_64` — we publish no + aarch64 tarball, and jpackage can't cross-compile one) ## Local build ```bash +# 1. Build the app tree the manifest packages +./gradlew :desktopApp:createReleaseDistributable + +# 2. Tooling + Flathub remote (one-time) +sudo apt-get install -y flatpak flatpak-builder # or distro equivalent +flatpak remote-add --user --if-not-exists flathub https://dl.flathub.org/repo/flathub.flatpakrepo + +# 3. Build + install locally cd desktopApp/packaging/flatpak -flatpak-builder --user --install --force-clean build-dir com.vitorpamplona.amethyst.Desktop.yml +flatpak-builder --user --install --install-deps-from=flathub --force-clean \ + build-dir com.vitorpamplona.amethyst.Desktop.yml flatpak run com.vitorpamplona.amethyst.Desktop ``` -## Submission to Flathub +To produce the distributable single-file bundle instead (what CI ships): -Follow https://docs.flathub.org/docs/for-app-authors/submission +```bash +flatpak-builder --user --install-deps-from=flathub --force-clean \ + --repo=repo build-dir com.vitorpamplona.amethyst.Desktop.yml +flatpak build-bundle repo amethyst.flatpak com.vitorpamplona.amethyst.Desktop \ + --runtime-repo=https://dl.flathub.org/repo/flathub.flatpakrepo +``` -1. Fork `flathub/flathub` on GitHub. -2. Branch from `new-pr` (NOT `master`). -3. Copy this manifest + AppStream + desktop into a new directory matching - the app id. -4. Open a PR titled "Add com.vitorpamplona.amethyst.Desktop". -5. After merge, a per-app repo is created with write access for ongoing - updates. +Installing the bundle: `flatpak install --user ./amethyst.flatpak`. The +`--runtime-repo` baked in above lets flatpak fetch the freedesktop runtime +from Flathub automatically on the user's machine. + +## Sandbox notes + +- `--socket=x11` (not wayland/fallback-x11): Compose Desktop renders through + AWT/skiko, which is X11-only on Linux and runs under XWayland on Wayland + sessions. ## Codec coverage @@ -47,6 +74,31 @@ Follow https://docs.flathub.org/docs/for-app-authors/submission - HLS, H.264, AAC, MP3, Opus: covered by the GStreamer plugin set in `org.freedesktop.Platform 24.08` itself. +## Submission to Flathub + +Follow https://docs.flathub.org/docs/for-app-authors/submission — no +separate Flathub account exists; everything runs through GitHub PRs. + +**Remaining blocker before submitting:** at least one screenshot with a +publicly reachable URL in `flathub/….metainfo.xml` (the Flathub linter +rejects the current empty placeholder). + +1. Fork `flathub/flathub` on GitHub. +2. Branch from `new-pr` (NOT `master`). +3. Copy the contents of `flathub/` verbatim into a new top-level directory + named `com.vitorpamplona.amethyst.Desktop`. +4. Open a PR titled "Add com.vitorpamplona.amethyst.Desktop". +5. After merge, Flathub creates a per-app repo + (`flathub/com.vitorpamplona.amethyst.Desktop`) with write access for + ongoing updates. Its `x-checker-data` makes + flatpak-external-data-checker open update PRs there automatically on + each new GitHub Release (bumping url/sha256 and appending the metainfo + `` entry). + +To test the Flathub variant locally, run the same flatpak-builder commands +as above from inside `flathub/` — it downloads the pinned release tarball +instead of using a local Gradle build. + ## License metadata The manifest declares the binary as diff --git a/desktopApp/packaging/flatpak/com.vitorpamplona.amethyst.Desktop.yml b/desktopApp/packaging/flatpak/com.vitorpamplona.amethyst.Desktop.yml index 8ed6f86207..79f44e226b 100644 --- a/desktopApp/packaging/flatpak/com.vitorpamplona.amethyst.Desktop.yml +++ b/desktopApp/packaging/flatpak/com.vitorpamplona.amethyst.Desktop.yml @@ -8,8 +8,6 @@ app-id: com.vitorpamplona.amethyst.Desktop runtime: org.freedesktop.Platform runtime-version: '24.08' sdk: org.freedesktop.Sdk -sdk-extensions: - - org.freedesktop.Sdk.Extension.openjdk21 command: amethyst-desktop add-extensions: @@ -26,8 +24,11 @@ cleanup-commands: finish-args: - --share=network - --share=ipc - - --socket=fallback-x11 - - --socket=wayland + # Compose Desktop renders through AWT/skiko, which is X11-only on Linux + # (runs under XWayland on Wayland sessions). fallback-x11 + wayland would + # leave the app without a usable display socket on Wayland sessions, so + # grant x11 unconditionally. + - --socket=x11 - --socket=pulseaudio - --device=dri - --filesystem=xdg-download @@ -37,12 +38,10 @@ finish-args: # GStreamer plugin/cache paths (org.freedesktop.Platform exposes them by default). - --env=GST_PLUGIN_SYSTEM_PATH=/usr/lib/x86_64-linux-gnu/gstreamer-1.0 +# No openjdk module / sdk-extension: the jpackage tree copied below already +# bundles its own trimmed JRE under Amethyst/lib/runtime/, so installing a +# second JRE at /app/jre would only bloat the bundle. modules: - - name: openjdk - buildsystem: simple - build-commands: - - /usr/lib/sdk/openjdk21/install.sh - - name: amethyst-desktop buildsystem: simple build-commands: @@ -53,7 +52,7 @@ modules: - install -Dm755 amethyst-desktop.sh /app/bin/amethyst-desktop - install -Dm644 com.vitorpamplona.amethyst.Desktop.metainfo.xml -t /app/share/metainfo/ - install -Dm644 com.vitorpamplona.amethyst.Desktop.desktop -t /app/share/applications/ - - install -Dm644 icons/256/com.vitorpamplona.amethyst.Desktop.png -t /app/share/icons/hicolor/256x256/apps/ + - install -Dm644 icons/512/com.vitorpamplona.amethyst.Desktop.png -t /app/share/icons/hicolor/512x512/apps/ sources: # Built artifact from `./gradlew :desktopApp:createReleaseDistributable`. # Path matches Compose Multiplatform 1.11's output layout. @@ -73,5 +72,6 @@ modules: - type: file path: com.vitorpamplona.amethyst.Desktop.desktop - - type: file - path: icons/256/com.vitorpamplona.amethyst.Desktop.png + - type: dir + path: icons + dest: icons diff --git a/desktopApp/packaging/flatpak/flathub/com.vitorpamplona.amethyst.Desktop.desktop b/desktopApp/packaging/flatpak/flathub/com.vitorpamplona.amethyst.Desktop.desktop new file mode 100644 index 0000000000..cc52ce21e4 --- /dev/null +++ b/desktopApp/packaging/flatpak/flathub/com.vitorpamplona.amethyst.Desktop.desktop @@ -0,0 +1,9 @@ +[Desktop Entry] +Type=Application +Name=Amethyst Desktop +Comment=Nostr client for desktop +Categories=Network;InstantMessaging; +Exec=amethyst-desktop +Icon=com.vitorpamplona.amethyst.Desktop +Terminal=false +StartupWMClass=Amethyst diff --git a/desktopApp/packaging/flatpak/flathub/com.vitorpamplona.amethyst.Desktop.metainfo.xml b/desktopApp/packaging/flatpak/flathub/com.vitorpamplona.amethyst.Desktop.metainfo.xml new file mode 100644 index 0000000000..a203d09e68 --- /dev/null +++ b/desktopApp/packaging/flatpak/flathub/com.vitorpamplona.amethyst.Desktop.metainfo.xml @@ -0,0 +1,52 @@ + + + com.vitorpamplona.amethyst.Desktop + CC0-1.0 + MIT AND LGPL-2.1-or-later AND BSD-2-Clause AND Apache-2.0 + Amethyst Desktop + Nostr client for desktop + + +

+ Amethyst Desktop is a desktop client for the Nostr protocol. + Browse feeds, post notes, send zaps (Lightning Network), and + participate in NIP-53 live audio rooms. +

+
+ + com.vitorpamplona.amethyst.Desktop.desktop + + https://github.com/vitorpamplona/amethyst + https://github.com/vitorpamplona/amethyst/issues + https://github.com/vitorpamplona/amethyst + + + Vitor Pamplona + + + + + + Network + InstantMessaging + + + + + + + + + + +
diff --git a/desktopApp/packaging/flatpak/flathub/com.vitorpamplona.amethyst.Desktop.yml b/desktopApp/packaging/flatpak/flathub/com.vitorpamplona.amethyst.Desktop.yml new file mode 100644 index 0000000000..54faa72fe1 --- /dev/null +++ b/desktopApp/packaging/flatpak/flathub/com.vitorpamplona.amethyst.Desktop.yml @@ -0,0 +1,98 @@ +# Flathub submission manifest for Amethyst Desktop. +# +# Unlike ../com.vitorpamplona.amethyst.Desktop.yml (which packages the locally +# built tree for release CI), this variant builds from the published GitHub +# Release tarball — Flathub's build servers must be able to fetch every source +# themselves. This directory is copy-ready: everything in it (manifest, +# metainfo, desktop entry, icon, flathub.json) is exactly what gets committed +# to the flathub per-app repo at submission time. +# +# Update flow after each Amethyst release: bump url + sha256 on the archive +# source and append a entry to the metainfo. The x-checker-data +# block below lets Flathub's flatpak-external-data-checker bot do both +# automatically once the app is published. + +app-id: com.vitorpamplona.amethyst.Desktop +runtime: org.freedesktop.Platform +runtime-version: '24.08' +sdk: org.freedesktop.Sdk +command: amethyst-desktop + +add-extensions: + org.freedesktop.Platform.ffmpeg-full: + directory: lib/ffmpeg + version: '24.08' + add-ld-path: . + autodownload: true + autodelete: false + +cleanup-commands: + - mkdir -p /app/lib/ffmpeg + +finish-args: + - --share=network + - --share=ipc + # Compose Desktop renders through AWT/skiko, which is X11-only on Linux + # (runs under XWayland on Wayland sessions). fallback-x11 + wayland would + # leave the app without a usable display socket on Wayland sessions, so + # grant x11 unconditionally. + - --socket=x11 + - --socket=pulseaudio + - --device=dri + - --filesystem=xdg-download + - --filesystem=xdg-pictures + - --talk-name=org.freedesktop.Notifications + - --talk-name=org.freedesktop.secrets + # GStreamer plugin/cache paths (org.freedesktop.Platform exposes them by default). + - --env=GST_PLUGIN_SYSTEM_PATH=/usr/lib/x86_64-linux-gnu/gstreamer-1.0 + +# No openjdk module / sdk-extension: the jpackage tree in the release tarball +# already bundles its own trimmed JRE under Amethyst/lib/runtime/. +modules: + - name: amethyst-desktop + buildsystem: simple + build-commands: + # Drop the jpackage-emitted self-contained tree into /app/lib/Amethyst. + # Wrapper at /app/bin/amethyst-desktop forwards args. + - mkdir -p /app/lib/Amethyst + - cp -r ./Amethyst/* /app/lib/Amethyst/ + - install -Dm755 amethyst-desktop.sh /app/bin/amethyst-desktop + - install -Dm644 com.vitorpamplona.amethyst.Desktop.metainfo.xml -t /app/share/metainfo/ + - install -Dm644 com.vitorpamplona.amethyst.Desktop.desktop -t /app/share/applications/ + - install -Dm644 icons/512/com.vitorpamplona.amethyst.Desktop.png -t /app/share/icons/hicolor/512x512/apps/ + sources: + # jpackage self-contained tree published by release CI. The tarball + # carries a single Amethyst/ top-level dir — keep it (strip-components + # defaults to 1, which would flatten it away). + - type: archive + url: https://github.com/vitorpamplona/amethyst/releases/download/v1.12.6/amethyst-desktop-1.12.6-linux-x64.tar.gz + sha256: 42250027857b37f184ebca52bf6124ff6524be4bc15fedf819e4acaca8bc9538 + strip-components: 0 + # flatpak-external-data-checker: watches GitHub Releases and opens + # update PRs against the flathub repo (bumps url/sha256 above and, + # via is-main-source, appends the entry to the metainfo). + x-checker-data: + type: json + is-main-source: true + url: https://api.github.com/repos/vitorpamplona/amethyst/releases/latest + version-query: .tag_name | sub("^v"; "") + timestamp-query: .published_at + url-query: '"https://github.com/vitorpamplona/amethyst/releases/download/" + + .tag_name + "/amethyst-desktop-" + (.tag_name | sub("^v"; "")) + + "-linux-x64.tar.gz"' + + - type: script + dest-filename: amethyst-desktop.sh + commands: + - "#!/bin/sh" + - exec /app/lib/Amethyst/bin/Amethyst "$@" + + - type: file + path: com.vitorpamplona.amethyst.Desktop.metainfo.xml + + - type: file + path: com.vitorpamplona.amethyst.Desktop.desktop + + - type: dir + path: icons + dest: icons diff --git a/desktopApp/packaging/flatpak/flathub/flathub.json b/desktopApp/packaging/flatpak/flathub/flathub.json new file mode 100644 index 0000000000..c75b742acf --- /dev/null +++ b/desktopApp/packaging/flatpak/flathub/flathub.json @@ -0,0 +1,3 @@ +{ + "only-arches": ["x86_64"] +} diff --git a/desktopApp/packaging/flatpak/flathub/icons/512/com.vitorpamplona.amethyst.Desktop.png b/desktopApp/packaging/flatpak/flathub/icons/512/com.vitorpamplona.amethyst.Desktop.png new file mode 100644 index 0000000000..ffe89a16f0 Binary files /dev/null and b/desktopApp/packaging/flatpak/flathub/icons/512/com.vitorpamplona.amethyst.Desktop.png differ diff --git a/desktopApp/packaging/flatpak/icons/512/com.vitorpamplona.amethyst.Desktop.png b/desktopApp/packaging/flatpak/icons/512/com.vitorpamplona.amethyst.Desktop.png new file mode 100644 index 0000000000..ffe89a16f0 Binary files /dev/null and b/desktopApp/packaging/flatpak/icons/512/com.vitorpamplona.amethyst.Desktop.png differ diff --git a/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip01Core/relay/client/NostrClient.kt b/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip01Core/relay/client/NostrClient.kt index 98d8f6a377..2e98749f34 100644 --- a/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip01Core/relay/client/NostrClient.kt +++ b/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip01Core/relay/client/NostrClient.kt @@ -48,6 +48,7 @@ import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.SharingStarted import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.debounce +import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.flow.sample import kotlinx.coroutines.flow.stateIn @@ -107,7 +108,12 @@ class NostrClient( // new filters will be sent to the relays and a potential reconnect can // be triggered. // Default: STARTS active - private var isActive = true + // + // A StateFlow (not a plain Boolean) so the keep-alive loop below can + // suspend without any scheduled timer while the client is inactive — + // e.g. the whole time the app sits in the background after the host + // calls [disconnect]. + private val isActiveFlow = MutableStateFlow(true) /** * Whatches for any changes in the relay list from subscriptions or outbox @@ -132,16 +138,16 @@ class NostrClient( // Reconnects all relays that may have disconnected override fun connect() { - isActive = true + isActiveFlow.value = true relayPool.connect() } override fun disconnect() { - isActive = false + isActiveFlow.value = false relayPool.disconnect() } - override fun isActive() = isActive + override fun isActive() = isActiveFlow.value class Reconnect( val onlyIfChanged: Boolean, @@ -173,12 +179,19 @@ class NostrClient( * error code) would stay disconnected forever in the absence of any * subscription change. The per-relay [BasicRelayClient] backoff still * gates the actual reconnect attempt, so dead relays are not hammered. + * + * While the client is inactive (the host called [disconnect], e.g. the + * app moved to the background) the loop suspends on [isActiveFlow] + * instead of ticking: no timer fires at all until [connect] flips the + * flag back, saving periodic CPU wake-ups for the whole background + * stretch. */ private val keepAliveJob = scope.launch { while (true) { + isActiveFlow.first { it } delay(KEEP_ALIVE_INTERVAL_MS) - if (this@NostrClient.isActive) { + if (this@NostrClient.isActive()) { relayPool.reconnectIfNeedsTo(ignoreRetryDelays = false) } } @@ -202,7 +215,7 @@ class NostrClient( ) { val relaysToUpdate = activeRequests.addOrUpdate(subId, filters, listener) - if (isActive) { + if (isActive()) { activeRequests.sendToRelayIfChanged(subId, relaysToUpdate) { relay, cmd -> if (cmd is CloseCmd || cmd is AuthCmd) { relayPool.sendIfConnected(relay, cmd) @@ -225,7 +238,7 @@ class NostrClient( ) { val relaysToUpdate = activeCounts.addOrUpdate(subId, filters) - if (isActive) { + if (isActive()) { activeCounts.sendToRelayIfChanged(subId, relaysToUpdate) { relay, cmd -> if (cmd is CloseCmd || cmd is AuthCmd) { relayPool.sendIfConnected(relay, cmd) @@ -245,7 +258,7 @@ class NostrClient( ) { val relaysToUpdate = eventOutbox.markAsSending(event, relayList) - if (isActive) { + if (isActive()) { eventOutbox.sendToRelayIfChanged(event, relaysToUpdate, relayPool::sendOrConnectAndSync) // wakes up all the other relays @@ -257,14 +270,14 @@ class NostrClient( val relaysToUpdateReqs = activeRequests.remove(subId) val relaysToUpdateCounts = activeCounts.remove(subId) - if (isActive) { + if (isActive()) { activeRequests.sendToRelayIfChanged(subId, relaysToUpdateReqs, relayPool::sendIfConnected) activeCounts.sendToRelayIfChanged(subId, relaysToUpdateCounts, relayPool::sendIfConnected) } } override fun syncFilters(relay: IRelayClient) { - if (isActive) { + if (isActive()) { scope.launch { activeRequests.syncState(relay.url, relay::sendOrConnectAndSync) activeCounts.syncState(relay.url, relay::sendOrConnectAndSync) @@ -337,7 +350,7 @@ class NostrClient( // The reconnect path is debounced and goes through // reconnectIfNeedsTo, which respects each relay's exponential // backoff so we don't hammer dead relays. - if (isActive && relay.url in allRelays.value) { + if (isActive() && relay.url in allRelays.value) { reconnect(onlyIfChanged = true) } } diff --git a/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip72ModCommunities/approval/CommunityPostApprovalEvent.kt b/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip72ModCommunities/approval/CommunityPostApprovalEvent.kt index 3347b88a9e..9414dfe67c 100644 --- a/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip72ModCommunities/approval/CommunityPostApprovalEvent.kt +++ b/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip72ModCommunities/approval/CommunityPostApprovalEvent.kt @@ -70,7 +70,9 @@ class CommunityPostApprovalEvent( fun containedPost(): Event? = try { - content.ifBlank { null }?.let { fromJson(it) } + // Only attempts to parse contents that could be an event json. + // Clients in the wild put all sorts of non-event text in here. + content.trim().takeIf { it.startsWith("{") }?.let { fromJson(it) } } catch (e: Exception) { Log.w( "CommunityPostEvent", diff --git a/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip85TrustedAssertions/users/ContactCardEvent.kt b/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip85TrustedAssertions/users/ContactCardEvent.kt index 24b53cac48..6f276a3716 100644 --- a/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip85TrustedAssertions/users/ContactCardEvent.kt +++ b/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip85TrustedAssertions/users/ContactCardEvent.kt @@ -25,31 +25,20 @@ import com.vitorpamplona.quartz.nip01Core.core.Address import com.vitorpamplona.quartz.nip01Core.core.HexKey import com.vitorpamplona.quartz.nip01Core.core.TagArrayBuilder import com.vitorpamplona.quartz.nip01Core.core.tagArray +import com.vitorpamplona.quartz.nip01Core.signers.EventTemplate import com.vitorpamplona.quartz.nip01Core.signers.NostrSigner +import com.vitorpamplona.quartz.nip01Core.signers.SignerExceptions +import com.vitorpamplona.quartz.nip01Core.signers.eventTemplate import com.vitorpamplona.quartz.nip01Core.tags.aTag.ATag import com.vitorpamplona.quartz.nip01Core.tags.dTag.dTag +import com.vitorpamplona.quartz.nip30CustomEmoji.EmojiUrlTag +import com.vitorpamplona.quartz.nip30CustomEmoji.emojis import com.vitorpamplona.quartz.nip50Search.SearchableEvent import com.vitorpamplona.quartz.nip51Lists.PrivateTagArrayEvent import com.vitorpamplona.quartz.nip51Lists.encryption.PrivateTagsInContent -import com.vitorpamplona.quartz.nip85TrustedAssertions.users.tags.ActiveHoursEndTag -import com.vitorpamplona.quartz.nip85TrustedAssertions.users.tags.ActiveHoursStartTag -import com.vitorpamplona.quartz.nip85TrustedAssertions.users.tags.FirstCreatedAtTag -import com.vitorpamplona.quartz.nip85TrustedAssertions.users.tags.FollowerCountTag +import com.vitorpamplona.quartz.nip51Lists.remove import com.vitorpamplona.quartz.nip85TrustedAssertions.users.tags.PetNameTag -import com.vitorpamplona.quartz.nip85TrustedAssertions.users.tags.PostCountTag -import com.vitorpamplona.quartz.nip85TrustedAssertions.users.tags.RankTag -import com.vitorpamplona.quartz.nip85TrustedAssertions.users.tags.ReactionsCountTag -import com.vitorpamplona.quartz.nip85TrustedAssertions.users.tags.ReplyCountTag -import com.vitorpamplona.quartz.nip85TrustedAssertions.users.tags.ReportsCountReceivedTag -import com.vitorpamplona.quartz.nip85TrustedAssertions.users.tags.ReportsCountSentTag import com.vitorpamplona.quartz.nip85TrustedAssertions.users.tags.SummaryTag -import com.vitorpamplona.quartz.nip85TrustedAssertions.users.tags.TopicTag -import com.vitorpamplona.quartz.nip85TrustedAssertions.users.tags.ZapAmountReceivedTag -import com.vitorpamplona.quartz.nip85TrustedAssertions.users.tags.ZapAmountSentTag -import com.vitorpamplona.quartz.nip85TrustedAssertions.users.tags.ZapAvgAmountDayReceivedTag -import com.vitorpamplona.quartz.nip85TrustedAssertions.users.tags.ZapAvgAmountDaySentTag -import com.vitorpamplona.quartz.nip85TrustedAssertions.users.tags.ZapCountReceivedTag -import com.vitorpamplona.quartz.nip85TrustedAssertions.users.tags.ZapCountSentTag import com.vitorpamplona.quartz.utils.TimeUtils @Immutable @@ -68,43 +57,43 @@ class ContactCardEvent( fun aboutUser() = tags.dTag() - fun rank() = tags.firstNotNullOfOrNull(RankTag::parse) + fun rank() = tags.rank() - fun followerCount() = tags.firstNotNullOfOrNull(FollowerCountTag::parse) + fun followerCount() = tags.followerCount() - fun firstCreatedAt() = tags.firstNotNullOfOrNull(FirstCreatedAtTag::parse) + fun firstCreatedAt() = tags.firstCreatedAt() - fun postCount() = tags.firstNotNullOfOrNull(PostCountTag::parse) + fun postCount() = tags.postCount() - fun replyCount() = tags.firstNotNullOfOrNull(ReplyCountTag::parse) + fun replyCount() = tags.replyCount() - fun reactionsCount() = tags.firstNotNullOfOrNull(ReactionsCountTag::parse) + fun reactionsCount() = tags.reactionsCount() - fun zapAmountReceived() = tags.firstNotNullOfOrNull(ZapAmountReceivedTag::parse) + fun zapAmountReceived() = tags.zapAmountReceived() - fun zapAmountSent() = tags.firstNotNullOfOrNull(ZapAmountSentTag::parse) + fun zapAmountSent() = tags.zapAmountSent() - fun zapCountReceived() = tags.firstNotNullOfOrNull(ZapCountReceivedTag::parse) + fun zapCountReceived() = tags.zapCountReceived() - fun zapCountSent() = tags.firstNotNullOfOrNull(ZapCountSentTag::parse) + fun zapCountSent() = tags.zapCountSent() - fun zapAvgAmountDayReceived() = tags.firstNotNullOfOrNull(ZapAvgAmountDayReceivedTag::parse) + fun zapAvgAmountDayReceived() = tags.zapAvgAmountDayReceived() - fun zapAvgAmountDaySent() = tags.firstNotNullOfOrNull(ZapAvgAmountDaySentTag::parse) + fun zapAvgAmountDaySent() = tags.zapAvgAmountDaySent() - fun reportsCountReceived() = tags.firstNotNullOfOrNull(ReportsCountReceivedTag::parse) + fun reportsCountReceived() = tags.reportsCountReceived() - fun reportsCountSent() = tags.firstNotNullOfOrNull(ReportsCountSentTag::parse) + fun reportsCountSent() = tags.reportsCountSent() - fun topics() = tags.mapNotNull(TopicTag::parse) + fun topics() = tags.topics() - fun activeHoursStart() = tags.firstNotNullOfOrNull(ActiveHoursStartTag::parse) + fun activeHoursStart() = tags.activeHoursStart() - fun activeHoursEnd() = tags.firstNotNullOfOrNull(ActiveHoursEndTag::parse) + fun activeHoursEnd() = tags.activeHoursEnd() - fun petName() = tags.firstNotNullOfOrNull(PetNameTag::parse) + fun petName() = tags.petName() - fun summary() = tags.firstNotNullOfOrNull(SummaryTag::parse) + fun summary() = tags.summary() companion object { const val KIND = 30382 @@ -123,26 +112,90 @@ class ContactCardEvent( targetUser: HexKey, petName: String? = null, summary: String? = null, + emojis: List = emptyList(), signer: NostrSigner, createdAt: Long = TimeUtils.now(), publicInitializer: TagArrayBuilder.() -> Unit = {}, privateInitializer: TagArrayBuilder.() -> Unit = {}, - ): ContactCardEvent { - val publicTags = - tagArray { - dTag(targetUser) - publicInitializer() - } + ): ContactCardEvent = signer.sign(build(targetUser, petName, summary, emojis, signer, createdAt, publicInitializer, privateInitializer)) + /** + * Unsigned template for a new card about [targetUser]. The petname, summary + * and the NIP-30 emoji mappings their shortcodes use always go in the NIP-44 + * encrypted content ([signer] only encrypts here; the caller signs). + */ + suspend fun build( + targetUser: HexKey, + petName: String? = null, + summary: String? = null, + emojis: List = emptyList(), + signer: NostrSigner, + createdAt: Long = TimeUtils.now(), + publicInitializer: TagArrayBuilder.() -> Unit = {}, + privateInitializer: TagArrayBuilder.() -> Unit = {}, + ): EventTemplate { val privateTags = tagArray { petName?.let { petName(it) } summary?.let { summary(it) } + emojis(emojis) privateInitializer() } - val encryptedContent = PrivateTagsInContent.encryptNip44(privateTags, signer) - return signer.sign(createdAt, KIND, publicTags, encryptedContent) + return eventTemplate( + kind = KIND, + description = PrivateTagsInContent.encryptNip44(privateTags, signer), + createdAt = createdAt, + ) { + dTag(targetUser) + publicInitializer() + } + } + + /** + * Unsigned template that replaces the petname, summary and their NIP-30 + * custom emoji mappings on an existing card, keeping every other public and + * private tag intact. All of them always live in the NIP-44 encrypted + * content — any stray public petname/summary copy is stripped. A `null` + * value removes the field; the private `emoji` tag set is replaced + * wholesale since it only exists to render the petname/summary shortcodes. + * [signer] only decrypts/encrypts here; the caller signs the template. + */ + suspend fun updatePetNameAndSummary( + earlierVersion: ContactCardEvent, + petName: String? = null, + summary: String? = null, + emojis: List = emptyList(), + signer: NostrSigner, + createdAt: Long = TimeUtils.now(), + ): EventTemplate { + val privateTags = + earlierVersion.privateTags(signer) + ?: throw SignerExceptions.UnauthorizedDecryptionException() + + var newPrivateTags = + privateTags + .remove(arrayOf(PetNameTag.TAG_NAME)) + .remove(arrayOf(SummaryTag.TAG_NAME)) + .remove(arrayOf(EmojiUrlTag.TAG_NAME)) + + petName?.let { newPrivateTags = newPrivateTags.plus(PetNameTag.assemble(it)) } + summary?.let { newPrivateTags = newPrivateTags.plus(SummaryTag.assemble(it)) } + if (emojis.isNotEmpty()) { + newPrivateTags = newPrivateTags.plus(emojis.map { it.toTagArray() }) + } + + val newPublicTags = + earlierVersion.tags + .remove(arrayOf(PetNameTag.TAG_NAME)) + .remove(arrayOf(SummaryTag.TAG_NAME)) + + return EventTemplate( + createdAt = createdAt, + kind = KIND, + tags = newPublicTags, + content = PrivateTagsInContent.encryptNip44(newPrivateTags, signer), + ) } } } diff --git a/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip85TrustedAssertions/users/TagArrayBuilderExt.kt b/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip85TrustedAssertions/users/TagArrayBuilderExt.kt index 5c050c482d..88d87d7359 100644 --- a/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip85TrustedAssertions/users/TagArrayBuilderExt.kt +++ b/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip85TrustedAssertions/users/TagArrayBuilderExt.kt @@ -41,40 +41,40 @@ import com.vitorpamplona.quartz.nip85TrustedAssertions.users.tags.ZapAvgAmountDa import com.vitorpamplona.quartz.nip85TrustedAssertions.users.tags.ZapCountReceivedTag import com.vitorpamplona.quartz.nip85TrustedAssertions.users.tags.ZapCountSentTag -fun TagArrayBuilder.rank(rank: Int) = add(RankTag.assemble(rank)) +fun TagArrayBuilder.rank(rank: Int) = addUnique(RankTag.assemble(rank)) -fun TagArrayBuilder.followers(count: Int) = add(FollowerCountTag.assemble(count)) +fun TagArrayBuilder.followers(count: Int) = addUnique(FollowerCountTag.assemble(count)) -fun TagArrayBuilder.firstCreatedAt(timestamp: Long) = add(FirstCreatedAtTag.assemble(timestamp)) +fun TagArrayBuilder.firstCreatedAt(timestamp: Long) = addUnique(FirstCreatedAtTag.assemble(timestamp)) -fun TagArrayBuilder.postCount(count: Int) = add(PostCountTag.assemble(count)) +fun TagArrayBuilder.postCount(count: Int) = addUnique(PostCountTag.assemble(count)) -fun TagArrayBuilder.replyCount(count: Int) = add(ReplyCountTag.assemble(count)) +fun TagArrayBuilder.replyCount(count: Int) = addUnique(ReplyCountTag.assemble(count)) -fun TagArrayBuilder.reactionsCount(count: Int) = add(ReactionsCountTag.assemble(count)) +fun TagArrayBuilder.reactionsCount(count: Int) = addUnique(ReactionsCountTag.assemble(count)) -fun TagArrayBuilder.zapAmountReceived(sats: Long) = add(ZapAmountReceivedTag.assemble(sats)) +fun TagArrayBuilder.zapAmountReceived(sats: Long) = addUnique(ZapAmountReceivedTag.assemble(sats)) -fun TagArrayBuilder.zapAmountSent(sats: Long) = add(ZapAmountSentTag.assemble(sats)) +fun TagArrayBuilder.zapAmountSent(sats: Long) = addUnique(ZapAmountSentTag.assemble(sats)) -fun TagArrayBuilder.zapCountReceived(count: Int) = add(ZapCountReceivedTag.assemble(count)) +fun TagArrayBuilder.zapCountReceived(count: Int) = addUnique(ZapCountReceivedTag.assemble(count)) -fun TagArrayBuilder.zapCountSent(count: Int) = add(ZapCountSentTag.assemble(count)) +fun TagArrayBuilder.zapCountSent(count: Int) = addUnique(ZapCountSentTag.assemble(count)) -fun TagArrayBuilder.zapAvgAmountDayReceived(sats: Long) = add(ZapAvgAmountDayReceivedTag.assemble(sats)) +fun TagArrayBuilder.zapAvgAmountDayReceived(sats: Long) = addUnique(ZapAvgAmountDayReceivedTag.assemble(sats)) -fun TagArrayBuilder.zapAvgAmountDaySent(sats: Long) = add(ZapAvgAmountDaySentTag.assemble(sats)) +fun TagArrayBuilder.zapAvgAmountDaySent(sats: Long) = addUnique(ZapAvgAmountDaySentTag.assemble(sats)) -fun TagArrayBuilder.reportsCountReceived(count: Int) = add(ReportsCountReceivedTag.assemble(count)) +fun TagArrayBuilder.reportsCountReceived(count: Int) = addUnique(ReportsCountReceivedTag.assemble(count)) -fun TagArrayBuilder.reportsCountSent(count: Int) = add(ReportsCountSentTag.assemble(count)) +fun TagArrayBuilder.reportsCountSent(count: Int) = addUnique(ReportsCountSentTag.assemble(count)) fun TagArrayBuilder.topic(topic: String) = add(TopicTag.assemble(topic)) -fun TagArrayBuilder.activeHoursStart(hour: Int) = add(ActiveHoursStartTag.assemble(hour)) +fun TagArrayBuilder.activeHoursStart(hour: Int) = addUnique(ActiveHoursStartTag.assemble(hour)) -fun TagArrayBuilder.activeHoursEnd(hour: Int) = add(ActiveHoursEndTag.assemble(hour)) +fun TagArrayBuilder.activeHoursEnd(hour: Int) = addUnique(ActiveHoursEndTag.assemble(hour)) -fun TagArrayBuilder.petName(name: String) = add(PetNameTag.assemble(name)) +fun TagArrayBuilder.petName(name: String) = addUnique(PetNameTag.assemble(name)) -fun TagArrayBuilder.summary(summary: String) = add(SummaryTag.assemble(summary)) +fun TagArrayBuilder.summary(summary: String) = addUnique(SummaryTag.assemble(summary)) diff --git a/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip85TrustedAssertions/users/TagArrayExt.kt b/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip85TrustedAssertions/users/TagArrayExt.kt new file mode 100644 index 0000000000..fad3a43cc8 --- /dev/null +++ b/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip85TrustedAssertions/users/TagArrayExt.kt @@ -0,0 +1,81 @@ +/* + * Copyright (c) 2025 Vitor Pamplona + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.vitorpamplona.quartz.nip85TrustedAssertions.users + +import com.vitorpamplona.quartz.nip01Core.core.TagArray +import com.vitorpamplona.quartz.nip01Core.core.fastFirstNotNullOfOrNull +import com.vitorpamplona.quartz.nip85TrustedAssertions.users.tags.ActiveHoursEndTag +import com.vitorpamplona.quartz.nip85TrustedAssertions.users.tags.ActiveHoursStartTag +import com.vitorpamplona.quartz.nip85TrustedAssertions.users.tags.FirstCreatedAtTag +import com.vitorpamplona.quartz.nip85TrustedAssertions.users.tags.FollowerCountTag +import com.vitorpamplona.quartz.nip85TrustedAssertions.users.tags.PetNameTag +import com.vitorpamplona.quartz.nip85TrustedAssertions.users.tags.PostCountTag +import com.vitorpamplona.quartz.nip85TrustedAssertions.users.tags.RankTag +import com.vitorpamplona.quartz.nip85TrustedAssertions.users.tags.ReactionsCountTag +import com.vitorpamplona.quartz.nip85TrustedAssertions.users.tags.ReplyCountTag +import com.vitorpamplona.quartz.nip85TrustedAssertions.users.tags.ReportsCountReceivedTag +import com.vitorpamplona.quartz.nip85TrustedAssertions.users.tags.ReportsCountSentTag +import com.vitorpamplona.quartz.nip85TrustedAssertions.users.tags.SummaryTag +import com.vitorpamplona.quartz.nip85TrustedAssertions.users.tags.TopicTag +import com.vitorpamplona.quartz.nip85TrustedAssertions.users.tags.ZapAmountReceivedTag +import com.vitorpamplona.quartz.nip85TrustedAssertions.users.tags.ZapAmountSentTag +import com.vitorpamplona.quartz.nip85TrustedAssertions.users.tags.ZapAvgAmountDayReceivedTag +import com.vitorpamplona.quartz.nip85TrustedAssertions.users.tags.ZapAvgAmountDaySentTag +import com.vitorpamplona.quartz.nip85TrustedAssertions.users.tags.ZapCountReceivedTag +import com.vitorpamplona.quartz.nip85TrustedAssertions.users.tags.ZapCountSentTag + +fun TagArray.rank() = fastFirstNotNullOfOrNull(RankTag::parse) + +fun TagArray.followerCount() = fastFirstNotNullOfOrNull(FollowerCountTag::parse) + +fun TagArray.firstCreatedAt() = fastFirstNotNullOfOrNull(FirstCreatedAtTag::parse) + +fun TagArray.postCount() = fastFirstNotNullOfOrNull(PostCountTag::parse) + +fun TagArray.replyCount() = fastFirstNotNullOfOrNull(ReplyCountTag::parse) + +fun TagArray.reactionsCount() = fastFirstNotNullOfOrNull(ReactionsCountTag::parse) + +fun TagArray.zapAmountReceived() = fastFirstNotNullOfOrNull(ZapAmountReceivedTag::parse) + +fun TagArray.zapAmountSent() = fastFirstNotNullOfOrNull(ZapAmountSentTag::parse) + +fun TagArray.zapCountReceived() = fastFirstNotNullOfOrNull(ZapCountReceivedTag::parse) + +fun TagArray.zapCountSent() = fastFirstNotNullOfOrNull(ZapCountSentTag::parse) + +fun TagArray.zapAvgAmountDayReceived() = fastFirstNotNullOfOrNull(ZapAvgAmountDayReceivedTag::parse) + +fun TagArray.zapAvgAmountDaySent() = fastFirstNotNullOfOrNull(ZapAvgAmountDaySentTag::parse) + +fun TagArray.reportsCountReceived() = fastFirstNotNullOfOrNull(ReportsCountReceivedTag::parse) + +fun TagArray.reportsCountSent() = fastFirstNotNullOfOrNull(ReportsCountSentTag::parse) + +fun TagArray.topics() = mapNotNull(TopicTag::parse) + +fun TagArray.activeHoursStart() = fastFirstNotNullOfOrNull(ActiveHoursStartTag::parse) + +fun TagArray.activeHoursEnd() = fastFirstNotNullOfOrNull(ActiveHoursEndTag::parse) + +fun TagArray.petName() = fastFirstNotNullOfOrNull(PetNameTag::parse) + +fun TagArray.summary() = fastFirstNotNullOfOrNull(SummaryTag::parse) diff --git a/quartz/src/commonTest/kotlin/com/vitorpamplona/quartz/experimental/nip85TrustedAssertions/ContactCardPetNameTest.kt b/quartz/src/commonTest/kotlin/com/vitorpamplona/quartz/experimental/nip85TrustedAssertions/ContactCardPetNameTest.kt new file mode 100644 index 0000000000..17d1f2d14b --- /dev/null +++ b/quartz/src/commonTest/kotlin/com/vitorpamplona/quartz/experimental/nip85TrustedAssertions/ContactCardPetNameTest.kt @@ -0,0 +1,185 @@ +/* + * Copyright (c) 2025 Vitor Pamplona + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.vitorpamplona.quartz.experimental.nip85TrustedAssertions + +import com.vitorpamplona.quartz.nip01Core.crypto.KeyPair +import com.vitorpamplona.quartz.nip01Core.signers.NostrSignerInternal +import com.vitorpamplona.quartz.nip30CustomEmoji.EmojiUrlTag +import com.vitorpamplona.quartz.nip85TrustedAssertions.users.ContactCardEvent +import com.vitorpamplona.quartz.nip85TrustedAssertions.users.tags.PetNameTag +import com.vitorpamplona.quartz.nip85TrustedAssertions.users.tags.SummaryTag +import kotlinx.coroutines.test.runTest +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertNull +import kotlin.test.assertTrue + +class ContactCardPetNameTest { + val signer = NostrSignerInternal(KeyPair()) + val targetUser = "e88a691e98d9987c964521dff60025f60700378a4879180dcbbb4a5027850411" + + private suspend fun ContactCardEvent.privatePetName() = privateTags(signer)?.firstNotNullOfOrNull(PetNameTag::parse) + + private suspend fun ContactCardEvent.privateSummary() = privateTags(signer)?.firstNotNullOfOrNull(SummaryTag::parse) + + @Test + fun createKeepsPetNameAndSummaryEncrypted() = + runTest { + val card = + ContactCardEvent.create( + targetUser = targetUser, + petName = "Bob from work", + summary = "Met at the conference", + signer = signer, + ) + + assertEquals(targetUser, card.aboutUser()) + + // never in the public tags + assertNull(card.petName()) + assertNull(card.summary()) + + // always in the encrypted content + assertEquals("Bob from work", card.privatePetName()) + assertEquals("Met at the conference", card.privateSummary()) + } + + @Test + fun updateReplacesPetNameAndKeepsOtherPrivateTags() = + runTest { + val card = + ContactCardEvent.create( + targetUser = targetUser, + petName = "Bob", + summary = "old summary", + signer = signer, + privateInitializer = { add(arrayOf("t", "friend")) }, + publicInitializer = { add(arrayOf("n", "follow")) }, + ) + + val updated = + signer.sign( + ContactCardEvent.updatePetNameAndSummary( + earlierVersion = card, + petName = "Bobby", + summary = "new summary", + signer = signer, + ), + ) + + assertEquals(targetUser, updated.aboutUser()) + assertEquals("Bobby", updated.privatePetName()) + assertEquals("new summary", updated.privateSummary()) + + // other tags survive on both sides + assertTrue(updated.privateTags(signer)!!.any { it.size > 1 && it[0] == "t" && it[1] == "friend" }) + assertTrue(updated.tags.any { it.size > 1 && it[0] == "n" && it[1] == "follow" }) + + // still nothing leaked publicly + assertNull(updated.petName()) + assertNull(updated.summary()) + } + + @Test + fun updateWithNullsClearsBothFields() = + runTest { + val card = + ContactCardEvent.create( + targetUser = targetUser, + petName = "Bob", + summary = "summary", + signer = signer, + ) + + val cleared = + signer.sign( + ContactCardEvent.updatePetNameAndSummary( + earlierVersion = card, + petName = null, + summary = null, + signer = signer, + ), + ) + + assertNull(cleared.privatePetName()) + assertNull(cleared.privateSummary()) + } + + @Test + fun updateReplacesCustomEmojiMappings() = + runTest { + val oldEmoji = EmojiUrlTag("wave", "https://old.example/wave.png") + val newEmoji = EmojiUrlTag("soapbox", "https://new.example/soapbox.png") + + val card = + ContactCardEvent.create( + targetUser = targetUser, + petName = "Bob :wave:", + emojis = listOf(oldEmoji), + signer = signer, + ) + assertEquals(listOf(oldEmoji), card.privateTags(signer)!!.mapNotNull(EmojiUrlTag::parse)) + + val updated = + signer.sign( + ContactCardEvent.updatePetNameAndSummary( + earlierVersion = card, + petName = "Bob :soapbox:", + emojis = listOf(newEmoji), + signer = signer, + ), + ) + + assertEquals("Bob :soapbox:", updated.privatePetName()) + // the emoji set is replaced wholesale, still encrypted + assertEquals(listOf(newEmoji), updated.privateTags(signer)!!.mapNotNull(EmojiUrlTag::parse)) + assertTrue(updated.tags.none { it.size > 0 && it[0] == EmojiUrlTag.TAG_NAME }) + } + + @Test + fun updateStripsLegacyPublicCopies() = + runTest { + // a card that (incorrectly) carries public petname/summary tags + val card = + ContactCardEvent.create( + targetUser = targetUser, + signer = signer, + publicInitializer = { + add(PetNameTag.assemble("public bob")) + add(SummaryTag.assemble("public summary")) + }, + ) + assertEquals("public bob", card.petName()) + + val updated = + signer.sign( + ContactCardEvent.updatePetNameAndSummary( + earlierVersion = card, + petName = "private bob", + signer = signer, + ), + ) + + assertNull(updated.petName()) + assertNull(updated.summary()) + assertEquals("private bob", updated.privatePetName()) + } +} diff --git a/quartz/src/commonTest/kotlin/com/vitorpamplona/quartz/nip72ModCommunities/approval/CommunityPostApprovalEventTest.kt b/quartz/src/commonTest/kotlin/com/vitorpamplona/quartz/nip72ModCommunities/approval/CommunityPostApprovalEventTest.kt new file mode 100644 index 0000000000..488764e280 --- /dev/null +++ b/quartz/src/commonTest/kotlin/com/vitorpamplona/quartz/nip72ModCommunities/approval/CommunityPostApprovalEventTest.kt @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2025 Vitor Pamplona + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.vitorpamplona.quartz.nip72ModCommunities.approval + +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertNull + +class CommunityPostApprovalEventTest { + private fun approvalWith(content: String) = + CommunityPostApprovalEvent( + id = "00".repeat(32), + pubKey = "11".repeat(32), + createdAt = 1_700_000_000L, + tags = arrayOf(arrayOf("a", "34550:${"11".repeat(32)}:community")), + content = content, + sig = "22".repeat(64), + ) + + @Test + fun containedPostParsesEmbeddedEventJson() { + val embedded = + """{"id":"${"33".repeat(32)}","pubkey":"${"44".repeat(32)}","created_at":1700000000,"kind":1,"tags":[],"content":"hello","sig":"${"55".repeat(64)}"}""" + + val post = approvalWith(embedded).containedPost() + + assertEquals("33".repeat(32), post?.id) + assertEquals(1, post?.kind) + assertEquals("hello", post?.content) + } + + @Test + fun containedPostIgnoresBlankContent() { + assertNull(approvalWith("").containedPost()) + assertNull(approvalWith(" ").containedPost()) + } + + /** + * Clients in the wild fill approval contents with plain text (e.g. braille + * ASCII art) instead of the NIP-72 stringified event. These must be skipped + * without even attempting a JSON parse. + */ + @Test + fun containedPostIgnoresNonJsonContent() { + assertNull(approvalWith("⠀⠀⠀⠐⣖⠢⢤⣄⡀⠀").containedPost()) + assertNull(approvalWith("Approved by moderator").containedPost()) + } + + /** Valid JSON that isn't a Nostr event (no pubkey field) must return null. */ + @Test + fun containedPostIgnoresNonEventJson() { + assertNull(approvalWith("""{"approvedEvent":{"kind":1111},"moderation":{"action":"approve"}}""").containedPost()) + } +} diff --git a/quartz/src/jvmTest/kotlin/com/vitorpamplona/quartz/nip01Core/relay/client/NostrClientKeepAliveTest.kt b/quartz/src/jvmTest/kotlin/com/vitorpamplona/quartz/nip01Core/relay/client/NostrClientKeepAliveTest.kt new file mode 100644 index 0000000000..66137b9ff8 --- /dev/null +++ b/quartz/src/jvmTest/kotlin/com/vitorpamplona/quartz/nip01Core/relay/client/NostrClientKeepAliveTest.kt @@ -0,0 +1,154 @@ +/* + * Copyright (c) 2025 Vitor Pamplona + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.vitorpamplona.quartz.nip01Core.relay.client + +import com.vitorpamplona.quartz.nip01Core.relay.client.single.basic.FakeWebsocketBuilder +import com.vitorpamplona.quartz.nip01Core.relay.filters.Filter +import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.test.TestScope +import kotlinx.coroutines.test.advanceTimeBy +import kotlinx.coroutines.test.runCurrent +import kotlinx.coroutines.test.runTest +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertTrue + +/** + * Behavioral contract of the keep-alive sweep after it was changed to suspend + * on the client's active-state flow instead of ticking unconditionally: + * + * 1. while ACTIVE, a relay closed by the server is redialed within one 60s + * sweep interval (once the per-relay backoff gate opens); + * 2. while INACTIVE (host called [NostrClient.disconnect], i.e. the app went + * to the background), no amount of elapsed time produces a dial; + * 3. after [NostrClient.connect] reactivates the client, redialing works + * again — a bug in the suspend/resume logic would leave every + * server-dropped relay disconnected forever. + * + * Coroutine timers (keep-alive 60s, reconnect debounce 200ms) run on the test + * scheduler's virtual clock; the per-relay backoff gate compares real wall + * seconds (integer granularity), hence the short real sleeps before each + * expected redial. + * + * Harness note: every socket the client dials must eventually be driven to + * onOpen/onClosed — a [FakeWebsocketBuilder] socket that is never completed + * leaves the relay in "connecting" forever and blocks all later dials, which + * is exactly what a production dial never does. [settleDisconnected] flushes + * pending debounced reconnects and completes any socket they may open. + */ +@OptIn(ExperimentalCoroutinesApi::class) +class NostrClientKeepAliveTest { + private val url = NormalizedRelayUrl("wss://keepalive.example.com") + + /** + * Flushes pending sub-second timers (the 200ms reconnect debounce) and, + * if a flush dialed a new socket, completes its lifecycle so the relay + * ends DISCONNECTED with no pending timers besides the keep-alive sweep. + */ + private fun TestScope.settleDisconnected(builder: FakeWebsocketBuilder) { + repeat(4) { + val before = builder.connectAttempts + advanceTimeBy(500) + runCurrent() + if (builder.connectAttempts == before) return + builder.lastListener.onOpen(50, false) + builder.lastListener.onClosed(1000, "test settle") + } + } + + @Test + fun keepAliveSweepPausesWhileInactiveAndResumesAfterReconnect() = + runTest { + val builder = FakeWebsocketBuilder() + val client = NostrClient(builder, this) + try { + // Flush refreshConnection's initial emission against the + // still-empty pool so it can't dial later. + advanceTimeBy(500) + runCurrent() + + // Subscribing dials the relay immediately. + client.subscribe("sub", mapOf(url to listOf(Filter()))) + assertEquals(1, builder.connectAttempts) + + // Server closes the established connection. While ACTIVE the + // client must redial within one keep-alive interval. + builder.lastListener.onOpen(50, false) + builder.lastListener.onClosed(1000, "server closed") + settleDisconnected(builder) + val beforeActiveSweep = builder.connectAttempts + Thread.sleep(3_500) + advanceTimeBy(61_000) + runCurrent() + assertTrue( + builder.connectAttempts > beforeActiveSweep, + "active client should redial a server-closed relay within one sweep, " + + "got ${builder.connectAttempts} attempts (baseline $beforeActiveSweep)", + ) + + // Leave the relay cleanly disconnected, then deactivate. + builder.lastListener.onOpen(50, false) + builder.lastListener.onClosed(1000, "server closed") + settleDisconnected(builder) + client.disconnect() + val whileInactiveBaseline = builder.connectAttempts + + // The relay's backoff gate is reset by disconnect(), so any + // stray sweep tick WOULD dial — this fails if the sweep keeps + // running (or anything else dials) while inactive. + Thread.sleep(3_500) + advanceTimeBy(30 * 60_000) + runCurrent() + assertEquals( + whileInactiveBaseline, + builder.connectAttempts, + "no dial may happen while the client is inactive", + ) + + // Reactivation dials the pool immediately... + client.connect() + runCurrent() + assertEquals( + whileInactiveBaseline + 1, + builder.connectAttempts, + "connect() should redial the pool immediately", + ) + + // ...and after the pause a server-closed relay must again be + // redialed within one sweep — fails if the sweep never + // resumes after the inactive stretch. + builder.lastListener.onOpen(50, false) + builder.lastListener.onClosed(1000, "server closed") + settleDisconnected(builder) + val beforeResumedSweep = builder.connectAttempts + Thread.sleep(3_500) + advanceTimeBy(61_000) + runCurrent() + assertTrue( + builder.connectAttempts > beforeResumedSweep, + "sweep did not resume after connect(); a server-dropped relay would stay disconnected forever", + ) + } finally { + client.close() + } + } +} diff --git a/scripts/asset-name.sh b/scripts/asset-name.sh index 6e82288085..908e51d565 100755 --- a/scripts/asset-name.sh +++ b/scripts/asset-name.sh @@ -8,7 +8,7 @@ # = tag stripped of leading 'v' (e.g. "1.08.0") # = macos | windows | linux # = x64 | arm64 -# = dmg | msi | zip | deb | rpm | AppImage | tar.gz +# = dmg | msi | zip | deb | rpm | AppImage | flatpak | tar.gz # # Consumed by: .github/workflows/create-release.yml, .github/workflows/bump-*.yml, # BUILDING.md, local release runbooks. @@ -26,6 +26,7 @@ # amethyst-desktop-1.08.0-linux-x64.deb # amethyst-desktop-1.08.0-linux-x64.rpm # amethyst-desktop-1.08.0-linux-x64.AppImage +# amethyst-desktop-1.08.0-linux-x64.flatpak # amethyst-desktop-1.08.0-linux-x64.tar.gz # amy-1.08.0-macos-arm64.tar.gz # amy-1.08.0-macos-x64.tar.gz @@ -74,6 +75,7 @@ collect_assets() { desktopApp/build/compose/binaries/main-release/deb/*.deb \ desktopApp/build/compose/binaries/main-release/rpm/*.rpm \ desktopApp/build/appimage/*.AppImage \ + desktopApp/build/flatpak/*.flatpak \ desktopApp/build/portable/*.tar.gz \ desktopApp/build/portable/*.zip \ ; do