Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Streamline calls to PageSummary and MediaList. #5239

Merged
merged 2 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 15 additions & 17 deletions app/src/main/java/org/wikipedia/dataclient/RestService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,25 @@ interface RestService {

@Headers("x-analytics: preview=1", "Accept: $ACCEPT_HEADER_SUMMARY")
@GET("page/summary/{title}")
suspend fun getSummaryResponse(
suspend fun getPageSummary(
@Path("title") title: String,
@Header("Referer") referrerUrl: String? = null,
@Header("Cache-Control") cacheControl: String? = null,
@Header(OfflineCacheInterceptor.SAVE_HEADER) saveHeader: String? = null,
@Header(OfflineCacheInterceptor.LANG_HEADER) langHeader: String? = null,
@Header(OfflineCacheInterceptor.TITLE_HEADER) titleHeader: String? = null
): Response<PageSummary>
): PageSummary

@Headers("x-analytics: preview=1", "Accept: $ACCEPT_HEADER_SUMMARY")
@GET("page/summary/{title}")
suspend fun getPageSummary(
@Header("Referer") referrerUrl: String?,
@Path("title") title: String
): PageSummary
suspend fun getSummaryResponse(
@Path("title") title: String,
@Header("Referer") referrerUrl: String? = null,
@Header("Cache-Control") cacheControl: String? = null,
@Header(OfflineCacheInterceptor.SAVE_HEADER) saveHeader: String? = null,
@Header(OfflineCacheInterceptor.LANG_HEADER) langHeader: String? = null,
@Header(OfflineCacheInterceptor.TITLE_HEADER) titleHeader: String? = null
): Response<PageSummary>

@Headers("Accept: $ACCEPT_HEADER_DEFINITION")
@GET("page/definition/{title}")
Expand All @@ -58,7 +62,11 @@ interface RestService {

@GET("page/media-list/{title}")
suspend fun getMediaList(
@Path("title") title: String
@Path("title") title: String,
@Header("Cache-Control") cacheControl: String? = null,
@Header(OfflineCacheInterceptor.SAVE_HEADER) saveHeader: String? = null,
@Header(OfflineCacheInterceptor.LANG_HEADER) langHeader: String? = null,
@Header(OfflineCacheInterceptor.TITLE_HEADER) titleHeader: String? = null
): MediaList

@GET("page/media-list/{title}/{revision}")
Expand All @@ -67,16 +75,6 @@ interface RestService {
@Path("revision") revision: Long
): MediaList

@GET("page/media-list/{title}/{revision}")
suspend fun getMediaListResponse(
@Path("title") title: String?,
@Path("revision") revision: Long,
@Header("Cache-Control") cacheControl: String?,
@Header(OfflineCacheInterceptor.SAVE_HEADER) saveHeader: String?,
@Header(OfflineCacheInterceptor.LANG_HEADER) langHeader: String?,
@Header(OfflineCacheInterceptor.TITLE_HEADER) titleHeader: String?
): Response<MediaList>

@GET("feed/onthisday/events/{mm}/{dd}")
suspend fun getOnThisDay(@Path("mm") month: Int,
@Path("dd") day: Int): OnThisDay
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class DescriptionEditViewModel(savedStateHandle: SavedStateHandle) : ViewModel()
}) {
_loadPageSummaryState.value = Resource.Loading()
editingAllowed = false
val summaryResponse = async { ServiceFactory.getRest(pageTitle.wikiSite).getPageSummary(null, pageTitle.prefixedText) }
val summaryResponse = async { ServiceFactory.getRest(pageTitle.wikiSite).getPageSummary(pageTitle.prefixedText) }
val infoResponse = async { ServiceFactory.get(pageTitle.wikiSite).getWikiTextForSectionWithInfo(pageTitle.prefixedText, 0) }

val editError = infoResponse.await().query?.firstPage()?.getErrorForAction("edit")
Expand Down Expand Up @@ -145,8 +145,8 @@ class DescriptionEditViewModel(savedStateHandle: SavedStateHandle) : ViewModel()
var revision = -1L
while (revision < newRevision && retry < 10) {
delay(2000)
val pageSummaryResponse = ServiceFactory.getRest(pageTitle.wikiSite).getSummaryResponse(pageTitle.prefixedText, cacheControl = OkHttpConnectionFactory.CACHE_CONTROL_FORCE_NETWORK.toString())
revision = pageSummaryResponse.body()?.revision ?: -1L
val pageSummary = ServiceFactory.getRest(pageTitle.wikiSite).getPageSummary(pageTitle.prefixedText, cacheControl = OkHttpConnectionFactory.CACHE_CONTROL_FORCE_NETWORK.toString())
revision = pageSummary.revision
retry++
}
_waitForRevisionState.value = Resource.Success(true)
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/org/wikipedia/edit/EditSectionViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ class EditSectionViewModel(savedStateHandle: SavedStateHandle) : ViewModel() {
while (revision < newRevision && retry < 10) {
delay(2000)
val pageSummaryResponse = ServiceFactory.getRest(pageTitle.wikiSite)
.getSummaryResponse(pageTitle.prefixedText, cacheControl = OkHttpConnectionFactory.CACHE_CONTROL_FORCE_NETWORK.toString())
revision = pageSummaryResponse.body()?.revision ?: -1L
.getPageSummary(pageTitle.prefixedText, cacheControl = OkHttpConnectionFactory.CACHE_CONTROL_FORCE_NETWORK.toString())
revision = pageSummaryResponse.revision
retry++
}
_waitForRevisionState.value = Resource.Success(revision)
Expand Down
9 changes: 5 additions & 4 deletions app/src/main/java/org/wikipedia/page/PageFragmentLoadState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,9 @@ class PageFragmentLoadState(private var model: PageViewModel,
}

val pageSummaryRequest = async {
ServiceFactory.getRest(title.wikiSite).getSummaryResponse(title.prefixedText, null, model.cacheControl.toString(),
if (model.isInReadingList) OfflineCacheInterceptor.SAVE_HEADER_SAVE else null, title.wikiSite.languageCode, UriUtil.encodeURL(title.prefixedText))
ServiceFactory.getRest(title.wikiSite).getSummaryResponse(title.prefixedText, cacheControl = model.cacheControl.toString(),
saveHeader = if (model.isInReadingList) OfflineCacheInterceptor.SAVE_HEADER_SAVE else null,
langHeader = title.wikiSite.languageCode, titleHeader = UriUtil.encodeURL(title.prefixedText))
}
val watchedRequest = async {
if (WikipediaApp.instance.isOnline && AccountUtil.isLoggedIn) {
Expand All @@ -155,8 +156,8 @@ class PageFragmentLoadState(private var model: PageViewModel,

val pageSummaryResponse = pageSummaryRequest.await()
val watchedResponse = watchedRequest.await()
val isWatched = watchedResponse.query?.firstPage()?.watched ?: false
val hasWatchlistExpiry = watchedResponse.query?.firstPage()?.hasWatchlistExpiry() ?: false
val isWatched = watchedResponse.query?.firstPage()?.watched == true
val hasWatchlistExpiry = watchedResponse.query?.firstPage()?.hasWatchlistExpiry() == true
if (pageSummaryResponse.body() == null) {
throw RuntimeException("Summary response was invalid.")
}
Expand Down
52 changes: 21 additions & 31 deletions app/src/main/java/org/wikipedia/savedpages/SavedPageSyncService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import androidx.work.OneTimeWorkRequestBuilder
import androidx.work.WorkManager
import androidx.work.WorkerParameters
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.withContext
import okhttp3.Request
import okio.Buffer
Expand All @@ -23,9 +24,7 @@ import org.wikipedia.dataclient.WikiSite
import org.wikipedia.dataclient.okhttp.HttpStatusException
import org.wikipedia.dataclient.okhttp.OfflineCacheInterceptor
import org.wikipedia.dataclient.okhttp.OkHttpConnectionFactory
import org.wikipedia.dataclient.page.PageSummary
import org.wikipedia.events.PageDownloadEvent
import org.wikipedia.gallery.MediaList
import org.wikipedia.page.PageTitle
import org.wikipedia.pageimages.db.PageImage
import org.wikipedia.readinglist.database.ReadingListPage
Expand All @@ -35,7 +34,6 @@ import org.wikipedia.settings.Prefs
import org.wikipedia.util.*
import org.wikipedia.util.log.L
import org.wikipedia.views.CircularProgressBar
import retrofit2.Response
import java.io.IOException
import java.util.concurrent.TimeUnit

Expand Down Expand Up @@ -158,11 +156,21 @@ class SavedPageSyncService(context: Context, params: WorkerParameters) : Corouti
return withContext(Dispatchers.IO) {
val pageTitle = ReadingListPage.toPageTitle(page)

// API calls
val summaryResponse = reqPageSummary(pageTitle)
val revision = summaryResponse.body()?.revision ?: 0
val mediaListResponse = reqMediaList(pageTitle, revision)
val mobileHTMLResponse = reqMobileHTML(pageTitle)
val summaryCall = async { ServiceFactory.getRest(pageTitle.wikiSite).getPageSummary(pageTitle.prefixedText,
cacheControl = OkHttpConnectionFactory.CACHE_CONTROL_FORCE_NETWORK.toString(),
saveHeader = OfflineCacheInterceptor.SAVE_HEADER_SAVE, langHeader = pageTitle.wikiSite.languageCode,
titleHeader = UriUtil.encodeURL(pageTitle.prefixedText)) }

val mediaListCall = async { ServiceFactory.getRest(pageTitle.wikiSite).getMediaList(pageTitle.prefixedText,
cacheControl = OkHttpConnectionFactory.CACHE_CONTROL_FORCE_NETWORK.toString(),
saveHeader = OfflineCacheInterceptor.SAVE_HEADER_SAVE, langHeader = pageTitle.wikiSite.languageCode,
titleHeader = UriUtil.encodeURL(pageTitle.prefixedText)) }

val mobileHTMLCall = async { reqMobileHTML(pageTitle) }

val summaryResponse = summaryCall.await()
val mediaListResponse = mediaListCall.await()
val mobileHTMLResponse = mobileHTMLCall.await()

page.downloadProgress = MEDIA_LIST_PROGRESS
FlowEventBus.post(PageDownloadEvent(page))
Expand All @@ -175,22 +183,22 @@ class SavedPageSyncService(context: Context, params: WorkerParameters) : Corouti
}
if (Prefs.isImageDownloadEnabled) {
// download thumbnail and lead image
if (!summaryResponse.body()?.thumbnailUrl.isNullOrEmpty()) {
page.thumbUrl = UriUtil.resolveProtocolRelativeUrl(pageTitle.wikiSite, summaryResponse.body()?.thumbnailUrl.orEmpty())
if (!summaryResponse.thumbnailUrl.isNullOrEmpty()) {
page.thumbUrl = UriUtil.resolveProtocolRelativeUrl(pageTitle.wikiSite, summaryResponse.thumbnailUrl.orEmpty())
AppDatabase.instance.pageImagesDao().insertPageImageSync(PageImage(pageTitle, page.thumbUrl.orEmpty()))
fileUrls.add(UriUtil.resolveProtocolRelativeUrl(
ImageUrlUtil.getUrlForPreferredSize(page.thumbUrl.orEmpty(), DimenUtil.calculateLeadImageWidth())))
}

// download article images
for (item in mediaListResponse.body()?.getItems("image").orEmpty()) {
for (item in mediaListResponse.getItems("image")) {
item.srcSets.let {
fileUrls.add(item.getImageUrl(DimenUtil.densityScalar))
}
}
}
page.displayTitle = summaryResponse.body()?.displayTitle.orEmpty()
page.description = summaryResponse.body()?.description.orEmpty()
page.displayTitle = summaryResponse.displayTitle.orEmpty()
page.description = summaryResponse.description.orEmpty()

reqSaveFiles(page, pageTitle, fileUrls)

Expand All @@ -202,24 +210,6 @@ class SavedPageSyncService(context: Context, params: WorkerParameters) : Corouti
}
}

private suspend fun reqPageSummary(pageTitle: PageTitle): Response<PageSummary> {
return withContext(Dispatchers.IO) {
ServiceFactory.getRest(pageTitle.wikiSite).getSummaryResponse(pageTitle.prefixedText,
null, OkHttpConnectionFactory.CACHE_CONTROL_FORCE_NETWORK.toString(),
OfflineCacheInterceptor.SAVE_HEADER_SAVE, pageTitle.wikiSite.languageCode,
UriUtil.encodeURL(pageTitle.prefixedText))
}
}

private suspend fun reqMediaList(pageTitle: PageTitle, revision: Long): Response<MediaList> {
return withContext(Dispatchers.IO) {
ServiceFactory.getRest(pageTitle.wikiSite).getMediaListResponse(pageTitle.prefixedText,
revision, OkHttpConnectionFactory.CACHE_CONTROL_FORCE_NETWORK.toString(),
OfflineCacheInterceptor.SAVE_HEADER_SAVE, pageTitle.wikiSite.languageCode,
UriUtil.encodeURL(pageTitle.prefixedText))
}
}

private suspend fun reqMobileHTML(pageTitle: PageTitle): okhttp3.Response {
val request = makeUrlRequest(pageTitle.wikiSite,
ServiceFactory.getRestBasePath(pageTitle.wikiSite) +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class SuggestedEditsImageRecsFragmentViewModel(savedStateHandle: SavedStateHandl

recommendation = page?.growthimagesuggestiondata?.first()!!
val wikiSite = WikiSite.forLanguageCode(langCode)
summary = ServiceFactory.getRest(wikiSite).getPageSummary(null, page.title)
summary = ServiceFactory.getRest(wikiSite).getPageSummary(page.title)
pageTitle = summary.getPageTitle(wikiSite)

val thumbUrl = UriUtil.resolveProtocolRelativeUrl(ImageUrlUtil.getUrlForPreferredSize(recommendation.images[0].metadata!!.thumbUrl, Constants.PREFERRED_CARD_THUMBNAIL_SIZE))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ object EditingSuggestionsProvider {
}
}

pageSummary = ServiceFactory.getRest(wiki).getPageSummary(null, title)
pageSummary = ServiceFactory.getRest(wiki).getPageSummary(title)
} finally {
mutex.release()
}
Expand Down Expand Up @@ -154,14 +154,14 @@ object EditingSuggestionsProvider {

titles?.let {
val sourcePageSummary = async {
ServiceFactory.getRest(it.first.wikiSite).getPageSummary(null, it.first.prefixedText).apply {
ServiceFactory.getRest(it.first.wikiSite).getPageSummary(it.first.prefixedText).apply {
if (description.isNullOrEmpty()) {
description = it.first.description
}
}
}
val targetPageSummary = async {
ServiceFactory.getRest(it.second.wikiSite).getPageSummary(null, it.second.prefixedText)
ServiceFactory.getRest(it.second.wikiSite).getPageSummary(it.second.prefixedText)
}
pair = sourcePageSummary.await() to targetPageSummary.await()
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/org/wikipedia/util/L10nUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ object L10nUtil {
if (shouldUpdateExtracts) {
list.map { pageSummary ->
async {
ServiceFactory.getRest(wikiSite).getPageSummary(null, pageSummary.apiTitle)
ServiceFactory.getRest(wikiSite).getPageSummary(pageSummary.apiTitle)
}
}.awaitAll().forEachIndexed { index, pageSummary ->
list[index].extract = pageSummary.extract
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ class WidgetFeaturedPageWorker(
val summary = if (result.tfa != null) {
val hasParentLanguageCode = !WikipediaApp.instance.languageState.getDefaultLanguageCode(WikipediaApp.instance.wikiSite.languageCode).isNullOrEmpty()
if (hasParentLanguageCode) {
ServiceFactory.getRest(WikipediaApp.instance.wikiSite).getPageSummary(null, result.tfa.apiTitle)
ServiceFactory.getRest(WikipediaApp.instance.wikiSite).getPageSummary(result.tfa.apiTitle)
} else {
result.tfa
}
} else {
val response = ServiceFactory.get(mainPageTitle.wikiSite).parseTextForMainPage(mainPageTitle.prefixedText)
ServiceFactory.getRest(WikipediaApp.instance.wikiSite).getPageSummary(null, findFeaturedArticleTitle(response.text))
ServiceFactory.getRest(WikipediaApp.instance.wikiSite).getPageSummary(findFeaturedArticleTitle(response.text))
}

val pageTitle = summary.getPageTitle(app.wikiSite)
Expand Down
Loading