diff --git a/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoDetailsViewModel.kt b/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoDetailsViewModel.kt index 67a24d1613..640042faa2 100644 --- a/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoDetailsViewModel.kt +++ b/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoDetailsViewModel.kt @@ -27,7 +27,8 @@ enum class DetailFetch { * the whole design of this screen: the catalogue already carries the description, the summary, the * scope, the collaborators and the newest release *with its APK*, so the page can paint — and be * installed from — before any request is made, and a failed request costs the README rather than - * the page. + * the page. The exception is an entry the site has corrected since the mirror's snapshot: it names + * its version and carries no release, so that page installs once its record has arrived. */ data class RepoDetailsState( val module: OnlineModule? = null, diff --git a/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoModels.kt b/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoModels.kt index 059b2e5fc4..761d3c85a0 100644 --- a/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoModels.kt +++ b/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoModels.kt @@ -5,18 +5,21 @@ import com.google.gson.annotations.SerializedName /** * The module repository's JSON, as types. * - * These mirror what the server actually sends, measured against a live `modules.json` of 809 - * entries. Two things shape the file: + * These mirror what the server sends. Two things shape the file: * - * **Nullability is not decoration here.** `scope` is null on 506 of the 809 entries, `sourceUrl` on - * 369 and `summary` on 121. Gson constructs through `Unsafe` and runs neither Kotlin's - * default-argument logic nor its null checks, so a non-null type on a field the server omits yields - * a `null` that only explodes at the first dereference, far from the parse. Every field the payload - * does not guarantee is therefore declared optional. + * **Nullability is not decoration here.** Many entries leave `scope`, `sourceUrl` or `summary` + * null. Gson constructs through `Unsafe` and runs neither Kotlin's default-argument logic nor its + * null checks, so a non-null type on a field the server omits yields a `null` that only explodes at + * the first dereference, far from the parse. Every field the payload does not guarantee is + * therefore declared optional. * * **The list payload is nearly a detail payload.** Each list entry already carries exactly one * release — the newest — with its `.apk` asset and download URL. Installing the current version of * a module needs no second request, which is what lets the Store work on a bad connection. + * + * **Except where the list is the site's.** An entry from `module-list/.json` names its + * `latestRelease` and carries no release, scope or collaborators at all, so `releases` being empty + * means "ask for the detail record", not "nothing was ever published". */ data class OnlineModule( @SerializedName("name") val name: String, @@ -58,8 +61,8 @@ data class Collaborator( /** * Someone credited beyond the repository's collaborators. * - * An object, not a bare name, though the field reads like a list of names: the 49 entries that - * carry one hold `{type, name, link}`. Typed as a list of strings it makes Gson throw `Expected a + * An object, not a bare name, though the field reads like a list of names: an entry that carries + * one holds `{type, name, link}`. Typed as a list of strings it makes Gson throw `Expected a * string but was BEGIN_OBJECT`, which takes the whole catalogue parse — and with it the entire * Store — rather than the one module. */ @@ -88,11 +91,11 @@ data class Release( /** * A stable identity for a lazy list. * - * Release *names* are not unique in real data: `com.rww.wetypeswipe` currently publishes two - * releases both named `1.11.4`, under tags `43-` and `42-`. `LazyColumn` throws - * `IllegalArgumentException` on a duplicate key, so identity comes from `id`, which is unique - * by construction, with the tag as fallback and the index as last resort — a malformed payload - * then degrades into an odd-looking list rather than a crash. + * Release *names* are not unique in real data: a module can publish two releases under one name + * and different tags. `LazyColumn` throws `IllegalArgumentException` on a duplicate key, so + * identity comes from `id`, which is unique by construction, with the tag as fallback and the + * index as last resort — a malformed payload then degrades into an odd-looking list rather than + * a crash. */ fun key(index: Int): String = id ?: tagName ?: "release:$index" @@ -116,10 +119,8 @@ data class ReleaseAsset( /** * Whether this asset is an APK. * - * Judged on the declared content type *and* the filename: 923 of the 946 assets in the - * catalogue declare `application/vnd.android.package-archive`, but a few authors upload theirs - * as `application/octet-stream`, and trusting the type alone would hide the only download those - * modules have. + * Judged on the declared content type *and* the filename: an APK uploaded under a generic + * content type is still the only download its module has, and the type alone would hide it. */ val isApk: Boolean get() = diff --git a/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoScreen.kt b/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoScreen.kt index 431484882d..d76c19bd31 100644 --- a/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoScreen.kt +++ b/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoScreen.kt @@ -28,6 +28,7 @@ import androidx.compose.foundation.layout.size import androidx.compose.foundation.layout.width import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.items +import androidx.compose.foundation.lazy.rememberLazyListState import androidx.compose.material.icons.Icons import androidx.compose.material.icons.rounded.Check import androidx.compose.material.icons.rounded.CloudOff @@ -46,6 +47,7 @@ import androidx.compose.material3.Scaffold import androidx.compose.material3.Text import androidx.compose.material3.pulltorefresh.PullToRefreshBox import androidx.compose.runtime.Composable +import androidx.compose.runtime.SideEffect import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf @@ -124,8 +126,24 @@ fun RepoScreen( return@Column } + // A list holds its first visible row where it is when rows arrive above it, and rows do: + // the mirror's catalogue is shown before the site has corrected it, and what the site + // adds is the newest releases. A reader who has not scrolled is at the top to see what + // is newest, so the top is where they stay. + val listState = rememberLazyListState() + // Read out here on purpose: the rows are otherwise read only inside the list's own + // content, so a new first row would not recompose this scope and the effect, which is + // what has to run before the list is measured again, would not run at all. + val firstRow = entries.firstOrNull()?.module?.name + SideEffect { + if (firstRow != null && !listState.canScrollBackward) { + listState.requestScrollToItem(0) + } + } + PullToRefreshBox(isRefreshing = refreshing, onRefresh = viewModel::refresh) { LazyColumn( + state = listState, modifier = Modifier.fillMaxSize(), contentPadding = PaddingValues(bottom = 20.dp), ) { diff --git a/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoViewModel.kt b/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoViewModel.kt index 6f8f962534..33c0d710ad 100644 --- a/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoViewModel.kt +++ b/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoViewModel.kt @@ -37,9 +37,9 @@ enum class StoreSort { /** * Which releases count. * - * Two options, not three. Of the 809 modules in the catalogue 14 publish a beta and **none** - * publishes a snapshot, so a snapshot channel would have no data behind it at all — and a control - * that can never change anything is a control that lies. + * Two options, not three. The payload carries a beta release list and nothing a snapshot channel + * could select from, so such a channel would have no data behind it — and a control that can never + * change anything is a control that lies. */ enum class StoreChannel(val preference: String) { Stable("stable"), @@ -54,27 +54,28 @@ enum class StoreChannel(val preference: String) { /** * Every release the chosen channel admits, newest first. * - * **A beta is not a flagged element of `releases`; it is a different array.** In the live catalogue - * not one entry of any module's `releases` carries `isPrerelease`, and each of the 14 modules that - * publish a beta keeps it exclusively in `betaReleases`. Selecting the prerelease channel by - * filtering `releases` on `isPrerelease` therefore matches nothing at all, and the install bar - * takes the newest release with an APK straight off this list. + * **Where a beta lives depends on the payload.** In the catalogue it is not a flagged element of + * `releases` but a different array: not one list entry's `releases` carries `isPrerelease`, and + * each module that publishes a beta keeps it exclusively in `betaReleases`. In a detail record it + * is the reverse: there is no `betaReleases`, and the betas sit in `releases` with `isPrerelease` + * set. Both shapes are read here, because the install bar takes the newest release with an APK + * straight off this list whichever payload it came from. * - * Merged, the order has to come from the version code rather than from either array's position, - * because the two are sorted independently and a beta is not automatically newer: of today's 14, - * `com.luoshui.paycardeditor` publishes beta code 1 against stable code 8. + * On the prerelease channel the order has to come from the version code rather than from position + * in either shape. The two arrays are sorted independently, a detail record is sorted by date, and + * a beta is not automatically newer: a module's newest beta can carry a lower code than its newest + * stable release. [latestOn] names the highest code, so the first element here has to be that + * release. */ public fun OnlineModule.releasesOn(channel: StoreChannel): List { val published = releases.orEmpty().filter { it.isDraft != true } if (channel == StoreChannel.Prerelease) { val beta = betaReleases.orEmpty().filter { it.isDraft != true } - if (beta.isEmpty()) return published return (published + beta) .distinctBy { it.tagName ?: it.id ?: it.name } .sortedByDescending { it.version?.versionCode ?: Long.MIN_VALUE } } - // Defensive rather than load-bearing, and it stays because the mirror's shape is not ours to - // promise: a module that has only ever prereleased still has releases worth listing. + // A module that has only ever prereleased still has releases worth listing. val stable = published.filter { it.isPrerelease != true } return stable.ifEmpty { published } } @@ -83,8 +84,8 @@ public fun OnlineModule.releasesOn(channel: StoreChannel): List { * The version the channel says is current — which is what every "Update to …" label states. * * On the prerelease channel that is whichever of the two channels is genuinely newer, not the beta - * unconditionally. Advertising `latestBetaRelease` on its own offers `paycardeditor`'s readers a - * downgrade from code 8 to code 1 and calls it an update. + * unconditionally. Advertising `latestBetaRelease` on its own would offer a reader whose newest + * beta is older than their stable release a downgrade, and call it an update. */ public fun OnlineModule.latestOn(channel: StoreChannel): RepoVersion? { val stable = RepoVersion.parse(latestRelease) @@ -138,7 +139,7 @@ class RepoViewModel( * Every catalogue entry paired with what this device has, before any filtering. * * Shared rather than recomputed per consumer: both the list and the "n updates" count need it, - * and it walks 809 entries. + * and it walks the whole catalogue. */ private val allEntries: StateFlow> = combine( @@ -165,8 +166,8 @@ class RepoViewModel( entries.filter { it.matches(query) }.sortedWith(comparatorFor(view)) } // Off the main thread, for the reason ModulesViewModel records: stateIn on its own - // collects on Dispatchers.Main.immediate, which would put a filter and a sort over 809 - // entries on the UI thread on every keystroke. + // collects on Dispatchers.Main.immediate, which would put a filter and a sort over the + // whole catalogue on the UI thread on every keystroke. .flowOn(Dispatchers.Default) .stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), emptyList()) diff --git a/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/StoreApi.kt b/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/StoreApi.kt new file mode 100644 index 0000000000..d27e12c46b --- /dev/null +++ b/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/StoreApi.kt @@ -0,0 +1,341 @@ +package org.matrix.vector.ui.store + +import android.util.Log +import com.google.gson.Gson +import com.google.gson.JsonObject +import com.google.gson.JsonParser +import com.google.gson.stream.JsonReader +import java.net.HttpURLConnection +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.async +import kotlinx.coroutines.awaitAll +import kotlinx.coroutines.runBlocking +import okhttp3.CacheControl +import okhttp3.OkHttpClient +import okhttp3.Request +import okhttp3.Response + +/** + * The module repository's two hosts, and how one catalogue is made of them. + * + * **Neither host serves what the Store needs on its own.** + * - the mirror, `backup.modules.lsposed.org`, serves `modules.json`: every module in one cacheable + * response, each entry carrying its newest release with the APK. It is a periodic rebuild, so a + * release published since the last one is not in it. + * - the site, `modules.lsposed.org`, answers `modules.json` with a 403 and serves the index only as + * `module-list/.json`, a page of entries at a time, newest release first and current. Those + * entries state `latestRelease` but carry no release, scope or collaborators, and the pages + * answer a conditional request with the whole page rather than a 304, so walking all of them + * costs the whole index every time. + * + * So the mirror is the base and the site corrects it. The pages are ordered by release time and the + * mirror only ever trails, so everything it lacks sits at the front: the walk stops at the first + * page that agrees with the mirror entirely. An entry the site knows better loses its now-outdated + * releases rather than keep an APK that is no longer the one its version names; [StoreRepository] + * fetches the detail record for the installed ones. + * + * Either host can vanish. Without the site the mirror is shown as it is; without the mirror every + * page is read, and the catalogue is the site's alone. + */ +internal class StoreApi( + client: OkHttpClient, + private val log: StoreLog, + private val gson: Gson = Gson(), + private val site: String = "https://modules.lsposed.org/", + private val mirror: String = "https://backup.modules.lsposed.org/", +) { + + /** + * The shared client, with its cache made to hold what the replay against it needs. + * + * Two things otherwise empty it. OkHttp stores a 404 as readily as a 200, and under the same + * key, so a host that starts answering 404 — how a retired static site fails — would overwrite + * the last good catalogue with its own error page. And `must-revalidate` forbids exactly the + * stale read a cold start with no network is: a `FORCE_CACHE` request for such a response is + * refused although the bytes are on disk. + */ + private val client = + client + .newBuilder() + .addNetworkInterceptor { chain -> + val response = chain.proceed(chain.request()) + val stored = + response.isSuccessful || response.code == HttpURLConnection.HTTP_NOT_MODIFIED + when { + !stored -> response.cacheControl("no-store") + // Still never fresh, so the network is asked whenever it can be. + response.cacheControl.mustRevalidate -> response.cacheControl("max-age=0") + else -> response + } + } + .build() + + private fun Response.cacheControl(value: String): Response = + newBuilder().header("Cache-Control", value).build() + + /** + * The catalogue, or null when [cacheControl] could not produce a whole one. + * + * [everyPage] reads the site to its last page even where the mirror already agrees. That finds + * what does not sit at the front — a release republished under its old date, a beta with no + * stable release beside it — and costs the whole index, so it is for a reader who asked. + * + * [onBase] receives the mirror's list as soon as it is read, before the site has corrected it. + */ + fun catalog( + cacheControl: CacheControl, + everyPage: Boolean, + onBase: (StoreCatalog) -> Unit = {}, + ): StoreCatalog? { + val base = fetchSnapshot(cacheControl)?.also(onBase) + val modules = LinkedHashMap() + base?.modules?.associateByTo(modules) { it.name } + // With nothing to compare against, there is no page at which to stop. + val toTheEnd = everyPage || base == null + + var loadedAtMillis = base?.loadedAtMillis ?: 0L + var complete = false + var number = 1 + var pageCount = 1 + var source = cacheControl + walk@ while (true) { + // The first page goes alone, being what says how many there are. After it, every + // remaining page at once where all are wanted, and otherwise a few: the page that + // agrees with the base ends the walk, and whatever was read past it is thrown away. + val reach = if (toTheEnd) pageCount else number + READ_AHEAD - 1 + val asked = source + for (fetched in fetchPages(number..reach.coerceIn(number, pageCount), asked)) { + var page = fetched + if (page == null && base != null && !asked.onlyIfCached) { + // The base can be current straight off the disk while the site is out of + // reach, and the corrections it had last time are then still on disk too. The + // rest of the walk stays there: offline, every further attempt is a timeout + // to sit out. + source = CacheControl.FORCE_CACHE + page = fetchPage(number, source) + } + // Only a page read since that base, though, which a saved one need not be: an + // older page laid over a newer base would take a version back. + if (page == null || (base != null && page.receivedAtMillis < base.loadedAtMillis)) { + break@walk + } + if (loadedAtMillis == 0L) loadedAtMillis = page.receivedAtMillis + // Bounded, because this number decides how many requests go out together. + pageCount = page.pageCount.coerceIn(number, MAX_PAGES) + val corrected = page.modules.count { correct(modules, it) } + complete = number == pageCount || (!toTheEnd && corrected == 0) + if (complete) break@walk + number++ + } + } + + // A walk that broke off is only a loss when it was the sole source, and then only on the + // network: the replay against the cache is the last resort, and part of a list beats none. + if (base == null && !complete && (modules.isEmpty() || !cacheControl.onlyIfCached)) { + return null + } + if (number > 1 || complete) logI("store: site read to page $number of $pageCount") + return StoreCatalog(usable(modules.values), loaded = true, loadedAtMillis = loadedAtMillis) + } + + /** + * The full record for one module, from the site first because the mirror's lags. + * + * Read as strictly as a catalogue entry, and only accepted as the module that was asked for: + * the caller files it under that name. + */ + fun details(packageName: String, cacheControl: CacheControl): OnlineModule? = + sequenceOf(site, mirror).firstNotNullOfOrNull { host -> + fetch("${host}module/$packageName.json", cacheControl) { response -> + val root = JsonParser.parseReader(response.body.charStream()).asJsonObject + bind(root)?.takeIf { it.name == packageName } + } + } + + private fun fetchSnapshot(cacheControl: CacheControl): StoreCatalog? { + val url = mirror + "modules.json" + return fetch(url, cacheControl) { response -> + val parsed = usable(parseSnapshot(response)) + if (parsed.isEmpty()) return@fetch null + logI("store: ${parsed.size} modules from $url") + // `fromCache` is not `networkResponse == null`: a hit inside the freshness window + // is served from disk without touching the network, and calling that "the saved + // catalogue" would put an offline notice on a perfectly current list. Staleness is a + // property of the branch that produced the catalogue, so the caller sets the flag on + // the fallback path and only there. + StoreCatalog(parsed, loaded = true, loadedAtMillis = response.receivedResponseAtMillis) + } + } + + private class Page( + val modules: List, + val pageCount: Int, + val receivedAtMillis: Long, + ) + + /** + * [numbers] in order, each null where its page could not be read. + * + * Together rather than in turn, because a page costs a round trip whatever its size: in a row + * the whole index is a wait under a spinner, and at once, down the one HTTP/2 connection, it is + * about the cost of one. + */ + private fun fetchPages(numbers: IntRange, cacheControl: CacheControl): List = + if (numbers.first == numbers.last) { + listOf(fetchPage(numbers.first, cacheControl)) + } else { + runBlocking(Dispatchers.IO) { + numbers.map { async { fetchPage(it, cacheControl) } }.awaitAll() + } + } + + private fun fetchPage(number: Int, cacheControl: CacheControl): Page? = + fetch("${site}module-list/$number.json", cacheControl) { response -> + val root = JsonParser.parseReader(response.body.charStream()).asJsonObject + val modules = root.getAsJsonArray("modules").mapNotNull { bind(it.asJsonObject) } + // The walk counts for itself and only asks the page where the end is, so a page that + // misstates its own number cannot send it round again. + Page(modules, root.get("pageCount").asInt, response.receivedResponseAtMillis) + } + + /** + * One GET, with every way it can fail folded into null. + * + * [read] runs inside the same `try`, because a body that is not the JSON it was expected to be + * is one more way for a host to be down. + */ + private fun fetch(url: String, cacheControl: CacheControl, read: (Response) -> T?): T? = + try { + val request = Request.Builder().url(url).cacheControl(cacheControl).build() + // `use` covers the failure branch as well as the success one, and the failure branch is + // the one that runs whenever a host is down. + client.newCall(request).execute().use { response -> + if (response.isSuccessful) { + read(response) + } else { + // The FORCE_CACHE replay synthesises 504 without contacting the host, so only + // report a status the network actually produced. + if (response.networkResponse != null) { + logW("store: $url returned HTTP ${response.code}") + } + null + } + } + } catch (e: Exception) { + logW("store: $url unavailable", e) + null + } + + /** + * Reads the snapshot one entry at a time, and survives a bad one. + * + * Binding the whole array in a single `fromJson` call fails the entire Store on one unexpected + * field — `additionalAuthors` holds objects rather than the strings its name suggests, and + * `AdditionalAuthor` exists because of it. This is third-party data, so an entry the model does + * not expect must cost that entry and nothing else. + * + * Streamed off the response rather than through a `String`, which also keeps a body of this + * size from being materialised twice. + */ + private fun parseSnapshot(response: Response): List { + val modules = ArrayList(1536) + var rejected = 0 + JsonReader(response.body.charStream()).use { reader -> + reader.beginArray() + while (reader.hasNext()) { + // Parsing to a JsonElement first cannot fail on well-formed JSON, so a binding + // failure below leaves the reader cleanly positioned on the next entry. + val module = bind(JsonParser.parseReader(reader).asJsonObject) + if (module != null) modules.add(module) else rejected++ + } + reader.endArray() + } + if (rejected > 0) logW("store: skipped $rejected unreadable entries") + return modules + } + + private fun logI(message: String) = log.log(Log.INFO, message, null) + + private fun logW(message: String, cause: Throwable? = null) = log.log(Log.WARN, message, cause) + + /** Null for an entry the model cannot hold, including one with no name to key it by. */ + private fun bind(entry: JsonObject): OnlineModule? { + val module = runCatching { gson.fromJson(entry, OnlineModule::class.java) }.getOrNull() + // Gson constructs through `Unsafe`, so the non-null type on `name` is not a promise. + @Suppress("SENSELESS_COMPARISON") + return module?.takeIf { it.name != null } + } + + companion object { + /** Pages read together past the first, where the walk expects to stop early. */ + const val READ_AHEAD = 4 + + /** Well past what the index needs; beyond it a `pageCount` is a mistake, not a list. */ + const val MAX_PAGES = 200 + + /** + * Folds one page entry into [modules]; true when the page knew something they did not. + * + * The stable version and the time of the newest beta are the whole comparison. The two + * hosts word `summary` and `sourceUrl` differently for modules that have not changed, so + * comparing more would report a difference on every page and the walk would never stop + * early. + * + * **The later statement wins, not the site, and one channel at a time.** The mirror is only + * known to trail, never promised to, so a site that stopped being rebuilt would otherwise + * take every version the mirror has since learnt back to its own. The cost is a withdrawn + * release, which the site states by naming an older one: that waits for the mirror. + * + * A page is the later of the two on one channel and the earlier on the other often enough — + * a beta published since the snapshot, a stable release the page has yet to name — so each + * channel keeps what it holds unless this page states something newer on it. + * + * The beta is compared by its time because that is all a page says of it: there is no + * `latestBetaRelease` to read. A newer beta therefore leaves the entry naming none, and the + * detail record is what names it. + */ + fun correct(modules: MutableMap, entry: OnlineModule): Boolean { + val known = modules[entry.name] + // ISO-8601 in UTC on both hosts, so the text orders as the times do. + val newerBeta = + entry.latestBetaReleaseTime.orEmpty() > known?.latestBetaReleaseTime.orEmpty() + val newerRelease = + entry.latestRelease != known?.latestRelease && + entry.latestReleaseTime.orEmpty() >= known?.latestReleaseTime.orEmpty() + if (known != null && !newerRelease && !newerBeta) return false + modules[entry.name] = + known?.copy( + description = entry.description ?: known.description, + stargazerCount = entry.stargazerCount ?: known.stargazerCount, + updatedAt = entry.updatedAt ?: known.updatedAt, + latestRelease = if (newerRelease) entry.latestRelease else known.latestRelease, + latestReleaseTime = + if (newerRelease) entry.latestReleaseTime else known.latestReleaseTime, + latestBetaRelease = known.latestBetaRelease.takeUnless { newerBeta }, + latestBetaReleaseTime = + if (newerBeta) entry.latestBetaReleaseTime else known.latestBetaReleaseTime, + // Both lists are the base's, and one of its newest is no longer the newest: + // left in place, a one-tap update would install the old APK under the new + // version. The detail record carries both channels, so both go together. + releases = null, + betaReleases = null, + ) ?: entry + return true + } + + /** + * What is worth showing of a parsed catalogue. + * + * An entry is kept for having a release to install or, on a page entry, a version to name; + * one with neither has nothing to say. Keying by package name upstream is what stops a host + * serving the same package twice from crashing the Store's `LazyColumn`. + */ + fun usable(parsed: Collection): List = + parsed + .asSequence() + .filter { it.hide != true } + .filter { !it.releases.isNullOrEmpty() || !it.latestRelease.isNullOrBlank() } + .distinctBy { it.name } + .toList() + } +} diff --git a/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/StoreDataSource.kt b/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/StoreDataSource.kt index ff969b535f..a0a1412268 100644 --- a/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/StoreDataSource.kt +++ b/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/StoreDataSource.kt @@ -3,9 +3,10 @@ package org.matrix.vector.ui.store import kotlinx.coroutines.flow.StateFlow /** - * Everything the shared Store screen needs from its host to reach the catalogue and the device — the - * one place Vector's daemon+OkHttp and LSPatch's Shizuku+HttpURLConnection differ. Each app provides - * an implementation; the [RepoViewModel] and screen are otherwise identical. + * Everything the shared Store screen needs from its host to reach the catalogue and the device. + * [StoreRepository] implements it for every app that hosts the Store, since how the repository is + * read is a fact about the repository; what an app supplies is how the device is asked what is + * installed and where a log line goes. */ interface StoreDataSource { /** The online module catalogue, updated by [refresh]. */ diff --git a/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/StoreRepository.kt b/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/StoreRepository.kt new file mode 100644 index 0000000000..353ec64dab --- /dev/null +++ b/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/StoreRepository.kt @@ -0,0 +1,253 @@ +package org.matrix.vector.ui.store + +import android.util.Log +import java.util.concurrent.TimeUnit +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.flow.asStateFlow +import kotlinx.coroutines.flow.collectLatest +import kotlinx.coroutines.flow.combine +import kotlinx.coroutines.flow.distinctUntilChanged +import kotlinx.coroutines.flow.update +import kotlinx.coroutines.launch +import kotlinx.coroutines.sync.Mutex +import kotlinx.coroutines.withContext +import okhttp3.CacheControl +import okhttp3.OkHttpClient + +/** + * Where the Store's shared code reports, as a `Log` priority; each app routes it to its own log. + */ +fun interface StoreLog { + fun log(priority: Int, message: String, cause: Throwable?) +} + +/** + * The Store's data: the online catalogue, and what this device already has of it. + * + * One implementation for every app that hosts the Store. What differs between them is how the + * device is asked what is installed, which arrives as a function, and where a log line goes, which + * is [log]. Everything about the network is the same, and has to be: the hosts, their quirks and + * the ways they fail are facts about the repository, not about the app reading it. + * + * Where the catalogue comes from, and why from two hosts, is [StoreApi]'s business. What is decided + * here is when to ask and what to do when asking fails. + * + * **Caching is declared, not hoped for.** Every request states its own freshness, so the client's + * disk cache is actually used: the mirror's snapshot revalidates against the host's own `max-age` + * and its ETag, pull-to-refresh forces the network, and when the network yields nothing the same + * requests are replayed against the cache alone. That last step is why a cold start with no network + * renders the last known catalogue rather than an error, and it is also what gives the + * DNS-over-HTTPS setting an effect here, since the shared client is the one carrying the DoH + * resolver. + * + * **No snapshot file of its own.** The OkHttp cache already holds these exact bytes; a duplicate in + * the same cache directory would buy nothing but a second thing to keep in sync. + */ +open class StoreRepository( + client: OkHttpClient, + private val scope: CoroutineScope, + private val log: StoreLog, + /** What each installed package is at, or null when the device could not be asked. */ + private val readInstalledVersions: suspend () -> Map?, +) : StoreDataSource { + + private val api = StoreApi(client, log) + + private val _catalog = MutableStateFlow(StoreCatalog()) + override val catalog: StateFlow = _catalog.asStateFlow() + + private val _isRefreshing = MutableStateFlow(false) + override val isRefreshing: StateFlow = _isRefreshing.asStateFlow() + + private val _installed = MutableStateFlow>(emptyMap()) + + /** Detail records fetched for installed modules, by package name. See [completeInstalled]. */ + @Volatile private var records = emptyMap() + + /** What each package on this device is at, keyed by package name. */ + override val installedVersions: StateFlow> = _installed.asStateFlow() + + /** Held for the length of a refresh; `tryLock` leaves no window between checking and taking. */ + private val refreshing = Mutex() + + init { + scope.launch { completeInstalled() } + } + + /** + * Reloads the catalogue. + * + * [force] is pull-to-refresh: it bypasses the cache rather than revalidating against it, and + * reads the site to its last page, because a user who pulls is telling us they think what they + * are looking at is stale. + */ + override suspend fun refresh(force: Boolean) { + // A second caller during a refresh is a no-op rather than a queued duplicate of the whole + // download. + if (!refreshing.tryLock()) return + try { + if (!force && isCurrent(_catalog.value)) return + _isRefreshing.value = true + // Nothing in the block suspends, so a caller cancelled half way — a tab switch takes + // the Store's `viewModelScope` with it — still leaves a whole catalogue behind. + withContext(Dispatchers.IO) { + val freshness = + if (force) CacheControl.FORCE_NETWORK + else + CacheControl.Builder() + .maxAge(CATALOG_MAX_AGE_MINUTES, TimeUnit.MINUTES) + .build() + + // The mirror's list goes up as soon as it is read, before the site has corrected + // it, but only onto an empty screen: over a list already showing it would take + // the corrections away, and update badges with them, only to bring them back. + val blank = _catalog.value.isEmpty + val paint = { base: StoreCatalog -> if (blank) _catalog.value = complete(base) } + + // Before reporting nothing, ask the cache — the bytes from the last successful + // visit are usually still on disk, and a stale catalogue is far more use than an + // empty screen. + val fetched = + api.catalog(freshness, everyPage = force, paint) + ?: api.catalog(CacheControl.FORCE_CACHE, everyPage = false, paint) + ?.copy(fromCache = true) + + // Nothing on the network and nothing on disk. `loaded` still flips, so the screen + // can say the repository is unreachable instead of sitting forever on a spinner + // that means nothing. + _catalog.value = fetched?.let(::complete) ?: _catalog.value.copy(loaded = true) + } + } finally { + // In a `finally` rather than after the block: a cancelled `viewModelScope` — a + // rotation mid-refresh — would otherwise strand the flag at true, and with + // pull-to-refresh reading it that is a spinner that never stops. + _isRefreshing.value = false + refreshing.unlock() + } + } + + /** + * Whether an unforced refresh would only fetch again what is already here. + * + * The mirror's snapshot answers that for itself through its `max-age`, but the site's pages + * never answer 304, so without this every unforced refresh would download them again. + */ + private fun isCurrent(catalog: StoreCatalog): Boolean { + // Bounded below as well: a clock set back would otherwise hold a refresh off for as long + // as it was set back by. + val age = System.currentTimeMillis() - catalog.loadedAtMillis + val limit = TimeUnit.MINUTES.toMillis(CATALOG_MAX_AGE_MINUTES.toLong()) + return !catalog.isEmpty && !catalog.fromCache && age in 0 until limit + } + + /** + * The full record for one module: its README, and every release rather than only the newest. + * + * Returns null when no host answers. Callers are expected to fall back to the catalogue entry + * they already hold, which carries the description and, where the mirror supplied it, the + * scope, the collaborators and the newest release with its APK — a usable page, and much better + * than an error screen. + */ + override suspend fun details(packageName: String): OnlineModule? = + withContext(Dispatchers.IO) { + val freshness = + CacheControl.Builder().maxAge(DETAIL_MAX_AGE_MINUTES, TimeUnit.MINUTES).build() + api.details(packageName, freshness) + ?: api.details(packageName, CacheControl.FORCE_CACHE) + } + + /** + * Gives every installed module its releases, where the catalogue came without them. + * + * An entry the site corrected, or one read from the site alone, names a version and carries no + * APK. For most of the catalogue that costs nothing, since the detail page fetches its own + * record. For an installed module it would leave the app offering an update it then cannot + * install from where it offers it, so those few are fetched here. + * + * Driven by the two flows rather than called from [refresh], because the device's package list + * and the catalogue arrive in either order. A record that fails to arrive leaves the names as + * they were, and `distinctUntilChanged` then keeps this from asking in a loop. + */ + private suspend fun completeInstalled() { + combine(_catalog, _installed) { catalog, installed -> + catalog.modules + .filter { it.releases.isNullOrEmpty() && it.name in installed } + .map { it.name } + } + .distinctUntilChanged() + .collectLatest { names -> + val fetched = names.mapNotNull { details(it) } + if (fetched.isEmpty()) return@collectLatest + val message = + "store: ${fetched.size} of ${names.size} installed modules given releases" + log.log(Log.INFO, message, null) + // Without the README: these stay in memory for as long as the process does. + records = records + fetched.associate { it.name to it.copy(readmeHTML = null) } + _catalog.update(::complete) + } + } + + /** + * [catalog] with the [records] laid over the entries that lack releases. + * + * Applied to everything published and not only once after a fetch, because every reload brings + * the same bare entries back. + * + * Only a record of the very releases the entry names: the mirror answers when the site does + * not, and its record may be older. + */ + private fun complete(catalog: StoreCatalog): StoreCatalog { + val records = records + if (records.isEmpty()) return catalog + return catalog.copy( + modules = + catalog.modules.map { module -> + records[module.name]?.takeIf { + module.releases.isNullOrEmpty() && + it.latestRelease == module.latestRelease && + it.latestBetaReleaseTime == module.latestBetaReleaseTime + } ?: module + } + ) + } + + /** Re-reads installed versions; called on opening the Store and after an install lands. */ + override fun refreshInstalled() { + scope.launch { loadInstalled() } + } + + /** + * The same read, awaited and handed back, for a caller that has to act on what it finds. + * + * Which is how an install records what it produced: the note that suppresses a satisfied offer + * is compared against [installedVersions], so it has to be written from that same reading and + * not from one of the caller's own, which need not agree with it. + * + * Returns the last known map when the device cannot be asked, which is the safe direction: a + * note written from a stale version simply fails to match, and the offer stays. + */ + suspend fun readInstalled(): Map { + loadInstalled() + return _installed.value + } + + /** Keeps the last map when the device has no answer: what is installed did not stop being. */ + private suspend fun loadInstalled() { + readInstalledVersions()?.let { _installed.value = it } + } + + private companion object { + /** Matches the mirror's own `max-age`, so an unforced refresh revalidates. */ + const val CATALOG_MAX_AGE_MINUTES = 10 + + /** + * A ceiling, not an extension: a host states its own `max-age` for a record and OkHttp + * takes whichever of the two is shorter. This only bounds a record that arrives without + * one. + */ + const val DETAIL_MAX_AGE_MINUTES = 60 + } +} diff --git a/manager/src/main/kotlin/org/matrix/vector/manager/data/repository/RepoRepository.kt b/manager/src/main/kotlin/org/matrix/vector/manager/data/repository/RepoRepository.kt index b54183dcd0..78cd61eb9f 100644 --- a/manager/src/main/kotlin/org/matrix/vector/manager/data/repository/RepoRepository.kt +++ b/manager/src/main/kotlin/org/matrix/vector/manager/data/repository/RepoRepository.kt @@ -1,78 +1,27 @@ package org.matrix.vector.manager.data.repository -import com.google.gson.Gson -import com.google.gson.JsonParser -import com.google.gson.stream.JsonReader -import java.util.concurrent.TimeUnit +import android.util.Log import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.flow.MutableStateFlow -import kotlinx.coroutines.flow.StateFlow -import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.launch -import kotlinx.coroutines.sync.Mutex -import kotlinx.coroutines.withContext -import okhttp3.CacheControl import okhttp3.OkHttpClient -import okhttp3.Request -import okhttp3.Response -import org.matrix.vector.ui.store.StoreDataSource -import org.matrix.vector.ui.store.OnlineModule -import org.matrix.vector.ui.store.RepoVersion -import org.matrix.vector.ui.store.StoreCatalog import org.matrix.vector.manager.data.model.versionCodeCompat import org.matrix.vector.manager.di.ServiceLocator import org.matrix.vector.manager.ipc.DaemonClient import org.matrix.vector.manager.logI import org.matrix.vector.manager.logW +import org.matrix.vector.ui.store.RepoVersion +import org.matrix.vector.ui.store.StoreLog +import org.matrix.vector.ui.store.StoreRepository /** - * The Store's data: the online catalogue, and what this device already has of it. - * - * **The mirror list is two lists, and that is not an oversight.** The full `modules.json` is served - * by exactly one host today: `modules.lsposed.org` answers that path with a 403. Per-module - * `module/.json` *is* served by both hosts, so the public site is a real fallback there - * and only there. Merging these two lists back into one would quietly take the Store offline. + * The Store's data for Vector: [StoreRepository], with the daemon as what knows the device. * - * **Caching is declared, not hoped for.** Every request states its own freshness, so the 16 MB disk - * cache in `HttpClientFactory` is actually used: the catalogue revalidates against the server's own - * ten-minute `max-age` and its ETag, pull-to-refresh forces the network, and when every mirror - * fails the same request is replayed against the cache alone. That last step is why a cold start - * with no network renders the last known catalogue rather than an error, and it is also what gives - * the DNS-over-HTTPS setting an effect here, since the shared client is the one carrying the DoH - * resolver. - * - * There is deliberately **no snapshot file** of our own, unlike `GitHubRepository`. The OkHttp - * cache already holds these exact bytes; a 1.2 MB duplicate in the same cache directory would buy - * nothing but a second thing to keep in sync. + * Only the two things that are Vector's own live here. Installed versions come from the daemon and + * are re-read whenever its binder arrives, and log lines go through [logI] and [logW], which is + * what puts them in the Verbose tab and the exported zip. */ -class RepoRepository( - private val client: OkHttpClient, - private val daemon: DaemonClient, - private val scope: CoroutineScope, - private val gson: Gson = Gson(), -) : StoreDataSource { - - private val _catalog = MutableStateFlow(StoreCatalog()) - override val catalog: StateFlow = _catalog.asStateFlow() - - private val _isRefreshing = MutableStateFlow(false) - override val isRefreshing: StateFlow = _isRefreshing.asStateFlow() - - private val _installed = MutableStateFlow>(emptyMap()) - - /** - * What each package on this device is at, keyed by package name. - * - * One `getInstalledPackagesFromAllUsers` call and no `ModuleDetection`: the Store already knows - * that every name it asks about is a module, so it does not need the much more expensive - * discovery the Modules screen runs, which opens every APK to find out. - */ - override val installedVersions: StateFlow> = _installed.asStateFlow() - - - /** Held for the length of a refresh; `tryLock` leaves no window between checking and taking. */ - private val refreshing = Mutex() +class RepoRepository(client: OkHttpClient, daemon: DaemonClient, scope: CoroutineScope) : + StoreRepository(client, scope, VectorLog, { daemon.installedVersions() }) { init { scope.launch { @@ -80,232 +29,40 @@ class RepoRepository( // *not* cleared when the daemon goes away: which packages are installed is a fact // about the device, not about the framework, and dropping every "Installed" badge // because the daemon died would state something untrue. - ServiceLocator.service.collect { service -> if (service != null) loadInstalled() } - } - } - - /** - * Reloads the catalogue. - * - * [force] is pull-to-refresh: it bypasses the cache rather than revalidating against it, - * because a user who pulls is telling us they think what they are looking at is stale. - */ - override suspend fun refresh(force: Boolean) { - // A second caller during a refresh is a no-op rather than a queued duplicate of a 1.2 MB - // download. - if (!refreshing.tryLock()) return - try { - _isRefreshing.value = true - withContext(Dispatchers.IO) { - val freshness = - if (force) CacheControl.FORCE_NETWORK - else - CacheControl.Builder() - .maxAge(CATALOG_MAX_AGE_MINUTES, TimeUnit.MINUTES) - .build() - - val fetched = LIST_MIRRORS.firstNotNullOfOrNull { fetchCatalog(it, freshness) } - if (fetched != null) { - _catalog.value = fetched - return@withContext - } - - // Every mirror failed. Before reporting nothing, ask the cache — the bytes from - // the last successful visit are usually still on disk, and a stale catalogue is - // far more use than an empty screen. - val cached = - LIST_MIRRORS.firstNotNullOfOrNull { fetchCatalog(it, CacheControl.FORCE_CACHE) } - when { - cached != null -> _catalog.value = cached.copy(fromCache = true) - // Nothing on the network and nothing on disk. `loaded` still flips, so the - // screen can say the repository is unreachable instead of sitting forever on - // a spinner that means nothing. - else -> _catalog.value = _catalog.value.copy(loaded = true) - } - } - } finally { - // In a `finally` rather than after the block: a cancelled `viewModelScope` — a - // rotation mid-refresh — would otherwise strand the flag at true, and with - // pull-to-refresh reading it that is a spinner that never stops. - _isRefreshing.value = false - refreshing.unlock() - } - } - - /** - * The full record for one module: its README, and every release rather than only the newest. - * - * Returns null when no mirror answers. Callers are expected to fall back to the catalogue entry - * they already hold, which carries the description, the scope, the collaborators and the newest - * release with its APK — a usable page, and much better than an error screen. - */ - override suspend fun details(packageName: String): OnlineModule? = - withContext(Dispatchers.IO) { - val freshness = - CacheControl.Builder().maxAge(DETAIL_MAX_AGE_MINUTES, TimeUnit.MINUTES).build() - DETAIL_MIRRORS.firstNotNullOfOrNull { fetchDetails(it, packageName, freshness) } - ?: DETAIL_MIRRORS.firstNotNullOfOrNull { - fetchDetails(it, packageName, CacheControl.FORCE_CACHE) - } - } - - /** Re-reads installed versions; called on opening the Store and after an install lands. */ - override fun refreshInstalled() { - scope.launch { loadInstalled() } - } - - /** - * The same read, awaited and handed back, for a caller that has to act on what it finds. - * - * Which is how an install records what it produced: the note that suppresses a satisfied offer - * is compared against [installedVersions], so it has to be written from that same reading. A - * local `getPackageInfo` would answer for user 0 while this map answers with the highest version - * across every user, and on a device with a work profile the two differ — leaving a note that can - * never match and a row that nags for ever. - * - * Returns the last known map when the daemon cannot be reached, which is the safe direction: a - * note written from a stale version simply fails to match, and the offer stays. - */ - suspend fun readInstalled(): Map { - loadInstalled() - return _installed.value - } - - private suspend fun loadInstalled() { - val packages = - daemon - .getInstalledPackagesFromAllUsers(0, false) - .onFailure { e -> - logW("store: installed versions unavailable", e) - } - .getOrNull() ?: return - val versions = HashMap(packages.size) - for (info in packages) { - val version = RepoVersion(info.versionCodeCompat, info.versionName.orEmpty()) - // The daemon reports every user, so the same package arrives more than once. The - // highest version wins, because that is the one an update would have to beat. - val known = versions[info.packageName] - if (known == null || version.versionCode > known.versionCode) { - versions[info.packageName] = version - } - } - _installed.value = versions - } - - private fun fetchCatalog(baseUrl: String, cacheControl: CacheControl): StoreCatalog? { - val url = baseUrl + "modules.json" - return try { - // `use` covers the failure branch as well as the success one, and the failure branch is - // the one that runs whenever a mirror is down. - client.newCall(request(url, cacheControl)).execute().use { response -> - if (!response.isSuccessful) { - // The FORCE_CACHE replay synthesises 504 without contacting the mirror, so - // only report a status the network actually produced. - if (response.networkResponse != null) { - logW("store: $url returned HTTP ${response.code}") - } - return null - } - val parsed = parseCatalog(response) - if (parsed.isEmpty()) return null - logI("store: ${parsed.size} modules from $url") - // `fromCache` is deliberately *not* derived from `response.networkResponse`. A hit - // inside the ten-minute freshness window is served from disk without touching the - // network, and calling that "the saved catalogue" would put an offline notice on - // a perfectly current list. Staleness is a property of which branch produced this, - // so the caller sets the flag on the fallback path and only there. - StoreCatalog( - modules = usable(parsed), - loaded = true, - loadedAtMillis = response.receivedResponseAtMillis, - ) - } - } catch (e: Exception) { - logW("store: $url unavailable", e) - null - } - } - - private fun fetchDetails( - baseUrl: String, - packageName: String, - cacheControl: CacheControl, - ): OnlineModule? { - val url = "${baseUrl}module/$packageName.json" - return try { - client.newCall(request(url, cacheControl)).execute().use { response -> - if (!response.isSuccessful) return null - gson.fromJson(response.body.string(), OnlineModule::class.java) - } - } catch (e: Exception) { - logW("store: $url unavailable", e) - null + ServiceLocator.service.collect { service -> if (service != null) refreshInstalled() } } } +} - private fun request(url: String, cacheControl: CacheControl): Request = - Request.Builder().url(url).cacheControl(cacheControl).build() +private val VectorLog = StoreLog { priority, message, cause -> + if (priority >= Log.WARN) logW(message, cause) else logI(message, cause) +} - /** - * Reads the catalogue one entry at a time, and survives a bad one. - * - * Binding the whole array in a single `fromJson` call fails the entire Store on one unexpected - * field — `additionalAuthors` holds objects rather than the strings its name suggests, and - * `AdditionalAuthor` exists because of it. This is third-party data written by hundreds of - * authors, so an entry the model does not expect must cost that entry and nothing else. - * - * Streamed off the response rather than through a `String`, which also keeps the 1.2 MB body - * from being materialised twice. - */ - private fun parseCatalog(response: Response): List { - val modules = ArrayList(1024) - var rejected = 0 - JsonReader(response.body.charStream()).use { reader -> - reader.beginArray() - while (reader.hasNext()) { - // Parsing to a JsonElement first cannot fail on well-formed JSON, so a binding - // failure below leaves the reader cleanly positioned on the next entry. - val element = JsonParser.parseReader(reader) - val module = runCatching { gson.fromJson(element, OnlineModule::class.java) } - if (module.isSuccess) module.getOrNull()?.let(modules::add) else rejected++ - } - reader.endArray() +/** + * What each package is at, across every user, or null when the daemon cannot be reached. + * + * One `getInstalledPackagesFromAllUsers` call and no `ModuleDetection`: the Store already knows + * that every name it asks about is a module, so it does not need the much more expensive discovery + * the Modules screen runs, which opens every APK to find out. + * + * Every user rather than user 0, which matters to an install recording what it produced: on a + * device with a work profile a local `getPackageInfo` would answer differently from this map, and a + * note written from the one can never match the other. + */ +private suspend fun DaemonClient.installedVersions(): Map? { + val packages = + getInstalledPackagesFromAllUsers(0, false) + .onFailure { e -> logW("store: installed versions unavailable", e) } + .getOrNull() ?: return null + val versions = HashMap(packages.size) + for (info in packages) { + val version = RepoVersion(info.versionCodeCompat, info.versionName.orEmpty()) + // The daemon reports every user, so the same package arrives more than once. The highest + // version wins, because that is the one an update would have to beat. + val known = versions[info.packageName] + if (known == null || version.versionCode > known.versionCode) { + versions[info.packageName] = version } - if (rejected > 0) logW("store: skipped $rejected unreadable entries") - return modules - } - - /** - * What is worth showing of a parsed catalogue. - * - * `distinctBy` is not superstition about today's data — it is what stops a mirror serving the - * same package twice from crashing the Store's `LazyColumn`, which is keyed by package name. - * Entries with no release at all are dropped because there is nothing to install and nothing to - * say about them. - */ - private fun usable(parsed: List): List = - parsed - .asSequence() - .filter { it.hide != true } - .filter { !it.releases.isNullOrEmpty() } - .distinctBy { it.name } - .toList() - - private companion object { - /** - * The only host serving the full list. Probed rather than assumed: roughly 1.2 MB and 800 - * entries here, against a 403 from `modules.lsposed.org`. - */ - val LIST_MIRRORS = listOf("https://backup.modules.lsposed.org/") - - /** Detail is served by both hosts, so here the public site is a genuine fallback. */ - val DETAIL_MIRRORS = - listOf("https://backup.modules.lsposed.org/", "https://modules.lsposed.org/") - - /** Matches the server's own `cache-control: max-age=600`, so revalidation stays free. */ - const val CATALOG_MAX_AGE_MINUTES = 10 - - /** Longer: a module's release history changes far less often than the index does. */ - const val DETAIL_MAX_AGE_MINUTES = 60 } + return versions }