|
| 1 | +# Nostr Sync |
| 2 | + |
| 3 | +## What This PR Adds |
| 4 | + |
| 5 | +This PR adds a new **Nostr Sync** feature area and end-to-end private sync for: |
| 6 | + |
| 7 | +1. Watch history (including per-video resume position) |
| 8 | +2. Subscriptions (including subscription group membership) |
| 9 | + |
| 10 | +The sync transport is Nostr app data events: |
| 11 | + |
| 12 | +1. **Kind**: `30078` |
| 13 | +2. **Storage convention**: `NIP-78` |
| 14 | +3. **Payload encryption**: `NIP-44` |
| 15 | +4. **External signer integration**: `NIP-55` (Amber-compatible) |
| 16 | + |
| 17 | +## User-Facing Changes |
| 18 | + |
| 19 | +### Navigation and screen |
| 20 | + |
| 21 | +1. Added a new **Nostr Sync** section in the left panel. |
| 22 | +2. Nostr Sync screen contains: |
| 23 | + - `Sync watch history` toggle |
| 24 | + - `Sync subscriptions` toggle |
| 25 | + - `Sign In` (when no identity) |
| 26 | + - `Show Identity` + `Clear Identity` (when identity exists) |
| 27 | + - Relay checklist with: |
| 28 | + - add relay |
| 29 | + - remove relay |
| 30 | + - reset relays to defaults |
| 31 | + |
| 32 | +### Identity flows |
| 33 | + |
| 34 | +When identity is not set: |
| 35 | + |
| 36 | +1. `Sign In` opens dialog. |
| 37 | +2. If NIP-55 signer app exists: |
| 38 | + - Prompt to use signer app. |
| 39 | +3. If signer app is missing: |
| 40 | + - Prompt to install Amber. |
| 41 | +4. `Advanced` section supports: |
| 42 | + - Generate local keypair |
| 43 | + - Scan `nsec` from QR |
| 44 | + - Paste `nsec` in input field and import via dialog `Done` |
| 45 | + |
| 46 | +When identity is set: |
| 47 | + |
| 48 | +1. `Show Identity` dialog shows: |
| 49 | + - `npub` + copy + QR |
| 50 | + - `nsec` (if local) masked by default + eye toggle + copy + QR |
| 51 | + - profile image if available |
| 52 | +2. For signer-managed identities (no local `nsec`): |
| 53 | + - `nsec` label remains visible |
| 54 | + - explanatory message shown under label |
| 55 | +3. `Clear Identity` shows destructive confirmation. |
| 56 | + |
| 57 | +### Pull-to-sync |
| 58 | + |
| 59 | +1. Added pull-to-refresh-triggered sync in: |
| 60 | + - History view |
| 61 | + - Subscriptions view |
| 62 | +2. Refresh indicator remains visible while sync is running and stops when it completes/fails. |
| 63 | + |
| 64 | +## Relay Configuration |
| 65 | + |
| 66 | +Configured relay list: |
| 67 | + |
| 68 | +- `wss://relay.primal.net` |
| 69 | +- `wss://relay.damus.io` |
| 70 | +- `wss://relay.snort.social` |
| 71 | +- `wss://nostr.oxtr.dev` |
| 72 | +- `wss://nos.lol` |
| 73 | +- `wss://nostr.bitcoiner.social` |
| 74 | +- `wss://nostr.semisol.dev` |
| 75 | +- `wss://shu01.shugur.net` |
| 76 | +- `wss://shu02.shugur.net` |
| 77 | +- `wss://shu03.shugur.net` |
| 78 | +- `wss://shu04.shugur.net` |
| 79 | +- `wss://shu05.shugur.net` |
| 80 | + |
| 81 | +Default enabled relays for fresh config: |
| 82 | + |
| 83 | +1. `wss://relay.primal.net` |
| 84 | +2. `wss://relay.damus.io` |
| 85 | +3. `wss://relay.snort.social` |
| 86 | +4. `wss://nostr.oxtr.dev` |
| 87 | +5. `wss://nos.lol` |
| 88 | +6. `wss://nostr.bitcoiner.social` |
| 89 | +7. `wss://nostr.semisol.dev` |
| 90 | + |
| 91 | +All `shu*.shugur.net` relays are unchecked by default. |
| 92 | + |
| 93 | +Relay list is user-configurable in-app: |
| 94 | + |
| 95 | +1. Add custom relay URL |
| 96 | +2. Remove any relay entry |
| 97 | +3. Reset list and checked state back to defaults |
| 98 | + |
| 99 | +## Sync Protocol and Event Shape |
| 100 | + |
| 101 | +### Event kind and filtering |
| 102 | + |
| 103 | +1. Sync reads/writes Nostr events with kind `30078`. |
| 104 | +2. Relay query filter includes: |
| 105 | + - `kinds: [30078]` |
| 106 | + - author pubkey |
| 107 | + |
| 108 | +### Payload envelope |
| 109 | + |
| 110 | +Published event content wraps encrypted payload in JSON: |
| 111 | + |
| 112 | +- `data.enc = "nip44"` |
| 113 | +- `data.ciphertext = <nip44 ciphertext>` |
| 114 | + |
| 115 | +The encrypted plaintext payload includes: |
| 116 | + |
| 117 | +1. `v` (payload version) |
| 118 | +2. `category` (`watch_history` or `subscriptions`) |
| 119 | +3. `updated_at` |
| 120 | +4. `data` object (actual merged records) |
| 121 | + |
| 122 | +Event tags include: |
| 123 | + |
| 124 | +1. `d` tag (device/category-scoped) |
| 125 | +2. `p` tag (self pubkey) |
| 126 | +3. `client` tag (`newpipe-sync`) |
| 127 | + |
| 128 | +### Sign/encrypt modes |
| 129 | + |
| 130 | +Two modes are supported: |
| 131 | + |
| 132 | +1. **Local key mode** (`nsec` present): |
| 133 | + - NIP-44 encrypt/decrypt locally |
| 134 | + - event signing locally |
| 135 | +2. **Signer-only mode** (no local `nsec`, NIP-55 identity): |
| 136 | + - NIP-44 encrypt/decrypt via signer app |
| 137 | + - event signing via signer app |
| 138 | + |
| 139 | +## Merge Strategy (CRDT-style) |
| 140 | + |
| 141 | +This implementation uses deterministic per-record merge rules (CRDT-style behavior), so data from multiple devices converges. |
| 142 | + |
| 143 | +### Watch history merge |
| 144 | + |
| 145 | +Keyed by `serviceId + url`. |
| 146 | + |
| 147 | +For each record: |
| 148 | + |
| 149 | +1. `repeatCount = max(local, remote)` |
| 150 | +2. Newer `accessTs` wins for time-sensitive fields (title/type/duration/uploader/thumbnail) |
| 151 | +3. Missing fields are filled from the other side when possible |
| 152 | +4. `progressMillis` (resume position) follows newer `accessTs`; if winner has no value, |
| 153 | + keep the available non-negative value from the other side; on equal `accessTs`, max progress wins |
| 154 | + |
| 155 | +DB apply rule: |
| 156 | + |
| 157 | +1. Upsert stream row |
| 158 | +2. Compare with latest history entry |
| 159 | +3. Store merged `accessDate = max(local, remote)` and merged `repeatCount = max(local, remote)` |
| 160 | +4. Upsert `stream_state.progress_time` when merged record includes `progressMillis` |
| 161 | + |
| 162 | +### Subscription merge |
| 163 | + |
| 164 | +Keyed by `serviceId + url`. |
| 165 | + |
| 166 | +For each record: |
| 167 | + |
| 168 | +1. Text fields kept if non-empty (prefer existing non-empty, fill blanks) |
| 169 | +2. `subscriberCount` uses max positive value when available |
| 170 | +3. Group memberships merged as union by group name |
| 171 | +4. Group icon conflict rule prefers non-default icon over default `ALL` |
| 172 | + |
| 173 | +DB apply rule: |
| 174 | + |
| 175 | +1. Insert missing subscriptions |
| 176 | +2. Update existing only if merged values changed |
| 177 | +3. Reconcile feed groups by name (create/update) |
| 178 | +4. Rewrite membership for each group from merged membership set |
| 179 | + |
| 180 | +## Robustness and UX Fixes Included |
| 181 | + |
| 182 | +1. Crash fixes around Nostr Sync screen initialization. |
| 183 | +2. Icon updates (`sync`, QR icon variant). |
| 184 | +3. Portrait-locked QR scanning capture activity. |
| 185 | +4. NIP-55 handling fixes for signer result parsing and package resolution. |
| 186 | +5. Improved identity dialog layout/visibility behavior across screen sizes. |
| 187 | + |
| 188 | +## Known Scope Boundaries |
| 189 | + |
| 190 | +1. Watch later playlist sync is intentionally not implemented in the active flow. |
| 191 | +2. Public playlist publication is not implemented in this PR. |
| 192 | + |
| 193 | +## Testing Notes |
| 194 | + |
| 195 | +Manual flows covered during development: |
| 196 | + |
| 197 | +1. Local `nsec` identity generation/import/clear/show |
| 198 | +2. Signer-managed identity via NIP-55 |
| 199 | +3. History sync across devices with same identity and relays |
| 200 | +4. Resume-position sync (`stream_state.progress_time`) across devices |
| 201 | +5. Subscription sync including grouped subscriptions |
| 202 | +6. Pull-to-sync behavior in History and Subscriptions |
| 203 | +7. Relay enable/disable behavior |
| 204 | + |
| 205 | +## Files of Interest |
| 206 | + |
| 207 | +Core: |
| 208 | + |
| 209 | +1. `app/src/main/java/org/schabi/newpipe/local/nostr/NostrSyncManager.java` |
| 210 | +2. `app/src/main/java/org/schabi/newpipe/local/nostr/NostrSyncFragment.java` |
| 211 | +3. `app/src/main/java/org/schabi/newpipe/local/nostr/Nip55SignerClient.java` |
| 212 | +4. `app/src/main/java/org/schabi/newpipe/local/nostr/NostrKeyUtils.java` |
| 213 | + |
| 214 | +DAO: |
| 215 | + |
| 216 | +1. `app/src/main/java/org/schabi/newpipe/database/history/dao/StreamHistoryDAO.kt` |
| 217 | +2. `app/src/main/java/org/schabi/newpipe/database/stream/dao/StreamStateDAO.kt` |
| 218 | +3. `app/src/main/java/org/schabi/newpipe/database/subscription/SubscriptionDAO.kt` |
| 219 | +4. `app/src/main/java/org/schabi/newpipe/database/feed/dao/FeedGroupDAO.kt` |
| 220 | + |
| 221 | +UI: |
| 222 | + |
| 223 | +1. `app/src/main/res/layout/fragment_nostr_sync.xml` |
| 224 | +2. `app/src/main/res/layout/dialog_nostr_sign_in.xml` |
| 225 | +3. `app/src/main/res/layout/dialog_nostr_identity.xml` |
| 226 | +4. `app/src/main/res/layout/fragment_history_playlist.xml` |
| 227 | +5. `app/src/main/res/layout/fragment_subscription.xml` |
0 commit comments