Skip to content
This repository has been archived by the owner on Sep 29, 2024. It is now read-only.

Commit

Permalink
Add "Clear cookies" button to ban info dialog (only works on 4chan fo…
Browse files Browse the repository at this point in the history
…r now). Fix `get thread page` spam when the app is launched with no internet.
  • Loading branch information
K1rakishou committed Mar 10, 2024
1 parent 4742980 commit 08097ac
Show file tree
Hide file tree
Showing 14 changed files with 269 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import com.github.k1rakishou.chan.core.site.parser.search.SimpleCommentParser
import com.github.k1rakishou.chan.core.site.sites.lynxchan.engine.LynxchanGetBoardsUseCase
import com.github.k1rakishou.chan.core.usecase.BookmarkFilterWatchableThreadsUseCase
import com.github.k1rakishou.chan.core.usecase.CatalogDataPreloader
import com.github.k1rakishou.chan.core.usecase.ClearPostingCookies
import com.github.k1rakishou.chan.core.usecase.DownloadThemeJsonFilesUseCase
import com.github.k1rakishou.chan.core.usecase.ExportBackupFileUseCase
import com.github.k1rakishou.chan.core.usecase.ExportDownloadedThreadAsHtmlUseCase
Expand Down Expand Up @@ -531,4 +532,10 @@ class UseCaseModule {
return LoadBoardFlagsUseCase(proxiedOkHttpClient)
}

@Provides
@Singleton
fun provideClearSiteCookies(siteManager: SiteManager): ClearPostingCookies {
return ClearPostingCookies(siteManager)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class DialogFactory(
get() = _themeEngine.get()

private val visibleDialogs = mutableMapOf<String, AlertDialogHandle>()
private val visibleComposeDialogs = mutableMapOf<String, KurobaComposeDialogController.KurobaComposeDialogHandle>()
private val visibleComposeDialogs = mutableMapOf<String, KurobaComposeDialogController.DialogHandle>()

lateinit var containerController: Controller

Expand All @@ -63,20 +63,20 @@ class DialogFactory(
checkAppVisibility: Boolean = true,
onAppearListener: (() -> Unit)? = null,
onDismissListener: (() -> Unit)? = null,
): KurobaComposeDialogController.KurobaComposeDialogHandle? {
): KurobaComposeDialogController.DialogHandle? {
if (checkAppVisibility && !applicationVisibilityManager.isAppInForeground()) {
return null
}

val kurobaComposeDialogHandle = KurobaComposeDialogController.KurobaComposeDialogHandle()
val dialogHandle = KurobaComposeDialogController.DialogHandle()

// TODO: Clickable links.
containerController.presentController(
KurobaComposeDialogController(
context = context,
canDismissByClickingOutside = cancelable,
params = params,
kurobaComposeDialogHandle = kurobaComposeDialogHandle,
dialogHandle = dialogHandle,
onAppeared = onAppearListener,
onDismissed = {
if (dialogId != null) {
Expand All @@ -89,10 +89,10 @@ class DialogFactory(
)

if (dialogId != null) {
visibleComposeDialogs[dialogId] = kurobaComposeDialogHandle
visibleComposeDialogs[dialogId] = dialogHandle
}

return kurobaComposeDialogHandle
return dialogHandle
}

@JvmOverloads
Expand All @@ -101,13 +101,13 @@ class DialogFactory(
titleText: String,
dialogId: String? = null,
descriptionText: CharSequence? = null,
cancelable: Boolean = true,
checkAppVisibility: Boolean = true,
customLinkMovementMethod: LinkMovementMethod? = null,
onPositiveButtonClickListener: (() -> Unit) = { },
positiveButtonTextId: Int = R.string.ok,
onAppearListener: (() -> Unit)? = null,
onDismissListener: (() -> Unit)? = null,
cancelable: Boolean = true,
checkAppVisibility: Boolean = true,
customLinkMovementMethod: LinkMovementMethod? = null
onDismissListener: (() -> Unit)? = null
): KurobaAlertDialog.AlertDialogHandle? {
if (checkAppVisibility && !applicationVisibilityManager.isAppInForeground()) {
return null
Expand Down Expand Up @@ -160,6 +160,7 @@ class DialogFactory(
descriptionText: CharSequence? = null,
customView: View? = null,
cancelable: Boolean = true,
customLinkMovementMethod: LinkMovementMethod? = null,
onPositiveButtonClickListener: ((DialogInterface) -> Unit) = { },
positiveButtonText: String = getString(R.string.ok),
onNeutralButtonClickListener: ((DialogInterface) -> Unit) = { },
Expand Down Expand Up @@ -199,6 +200,7 @@ class DialogFactory(
onNegativeButtonClickListener.invoke(dialog)
}
.setCancelable(cancelable)
.setCustomLinkMovementMethod(customLinkMovementMethod)
.create(viewGroup, callbacks, alertDialogHandle)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,13 @@ import java.util.*
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.ConcurrentMap
import java.util.concurrent.TimeUnit
import kotlin.collections.HashSet
import kotlin.coroutines.CoroutineContext

class PageRequestManager(
private val siteManager: SiteManager,
private val boardManager: BoardManager
) : CoroutineScope {
private val requestedBoards = Collections.synchronizedSet(HashSet<BoardDescriptor>())
private val savedBoards = Collections.synchronizedSet(HashSet<BoardDescriptor>())
private val boardPagesMap: ConcurrentMap<BoardDescriptor, BoardPages> = ConcurrentHashMap()
private val boardTimeMap: ConcurrentMap<BoardDescriptor, Long> = ConcurrentHashMap()
private val notifyIntervals = ConcurrentHashMap<ChanDescriptor.ThreadDescriptor, Long>()
Expand All @@ -58,14 +56,14 @@ class PageRequestManager(
override val coroutineContext: CoroutineContext
get() = Dispatchers.Default + SupervisorJob() + CoroutineName("PageRequestManager")

fun getBoardPages(boardDescriptor: BoardDescriptor, requestPagesIfNotCached: Boolean = true): BoardPages? {
fun getBoardPages(boardDescriptor: BoardDescriptor, requestPagesIfNotCached: Boolean = false): BoardPages? {
return getPages(
boardDescriptor = boardDescriptor,
requestPagesIfNotCached = requestPagesIfNotCached
)
}

fun getPage(originalPostDescriptor: PostDescriptor?, requestPagesIfNotCached: Boolean = true): BoardPage? {
fun getPage(originalPostDescriptor: PostDescriptor?, requestPagesIfNotCached: Boolean = false): BoardPage? {
if (originalPostDescriptor == null) {
return null
}
Expand All @@ -81,7 +79,7 @@ class PageRequestManager(
)
}

fun getPage(threadDescriptor: ChanDescriptor.ThreadDescriptor?, requestPagesIfNotCached: Boolean = true): BoardPage? {
fun getPage(threadDescriptor: ChanDescriptor.ThreadDescriptor?, requestPagesIfNotCached: Boolean = false): BoardPage? {
if (threadDescriptor == null || threadDescriptor.threadNo < 0) {
return null
}
Expand Down Expand Up @@ -178,31 +176,14 @@ class PageRequestManager(
return null
}

if (savedBoards.contains(boardDescriptor)) {
if (requestPagesIfNotCached) {
// If we have it stored already, return the pages for it
// also issue a new request if 3 minutes have passed
shouldUpdate(boardDescriptor)
}

return boardPagesMap[boardDescriptor]
if (requestPagesIfNotCached) {
// If we have it stored already, return the pages for it
// also issue a new request if 3 minutes have passed
shouldUpdate(boardDescriptor)
}

if (!requestPagesIfNotCached) {
return null
}

val alreadyRequested = synchronized(this) {
requestedBoards.contains(boardDescriptor)
}

if (alreadyRequested) {
return null
}

launch {
// Otherwise, get the site for the board and request the pages for it
requestBoardInternal(boardDescriptor)
if (boardPagesMap.contains(boardDescriptor)) {
return boardPagesMap[boardDescriptor]
}

return null
Expand Down Expand Up @@ -304,7 +285,6 @@ class PageRequestManager(
) {
Logger.d(TAG, "Got pages for ${boardDescriptor.siteName()}/${boardDescriptor.boardCode}/")

savedBoards.add(boardDescriptor)
boardTimeMap[boardDescriptor] = System.currentTimeMillis()
boardPagesMap[boardDescriptor] = pages

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,10 @@ class ThreadPresenter @Inject constructor(
}

if (chanDescriptor is ChanDescriptor.CatalogDescriptor && !ChanSettings.neverShowPages.get()) {
pageRequestManager.getBoardPages(chanDescriptor.boardDescriptor())
pageRequestManager.getBoardPages(
boardDescriptor = chanDescriptor.boardDescriptor(),
requestPagesIfNotCached = true
)
}

chanThreadManager.bindChanDescriptor(chanDescriptor)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ interface SiteActions {
replyPostDescriptor: PostDescriptor
): ModularResult<Boolean> = ModularResult.value(true)

fun clearPostingCookies() = Unit

enum class LoginType {
Passcode,
TokenAndPass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,14 @@ open class Chan4 : SiteBase() {
).execute()
}

override fun clearPostingCookies() {
chan4CaptchaCookie.setSync("")
cloudFlareClearanceCookieMap.clear()
chan4CaptchaSettings.update(sync = false) { chan4CaptchaSetting ->
chan4CaptchaSetting.copy(captchaTicket = null)
}
}

private fun HttpUrl.Builder.addBoardCodeParameter(boardCode: String?): HttpUrl.Builder {
if (boardCode.isNullOrEmpty()) {
return this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.supervisorScope
import kotlin.time.ExperimentalTime
import kotlin.time.measureTime

class CatalogDataPreloader(
Expand All @@ -24,7 +23,6 @@ class CatalogDataPreloader(
private val seenPostsManager: Lazy<SeenPostsManager>
) {

@OptIn(ExperimentalTime::class)
suspend fun preloadCatalogInfo(catalogDescriptor: ChanDescriptor.CatalogDescriptor) {
BackgroundUtils.ensureMainThread()
Logger.d(TAG, "preloadCatalogInfo($catalogDescriptor) begin")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.github.k1rakishou.chan.core.usecase

import com.github.k1rakishou.chan.core.manager.SiteManager
import com.github.k1rakishou.core_logger.Logger
import com.github.k1rakishou.model.data.descriptor.SiteDescriptor

class ClearPostingCookies(
private val siteManager: SiteManager
) {

fun perform(siteDescriptor: SiteDescriptor) {
Logger.debug(TAG) { "perform(${siteDescriptor})" }

siteManager.bySiteDescriptor(siteDescriptor)
?.actions()
?.clearPostingCookies()
}

companion object {
private const val TAG = "ClearPostingCookies"
}

}
Loading

0 comments on commit 08097ac

Please sign in to comment.