1
0
mirror of https://github.com/AllanWang/Frost-for-Facebook.git synced 2024-11-08 20:12:39 +01:00

Prepare for messenger cookies

This commit is contained in:
Allan Wang 2021-01-10 20:28:07 -08:00
parent e5e1a0d4a5
commit 7ad9fdfc6f
3 changed files with 27 additions and 18 deletions

View File

@ -23,11 +23,12 @@ package com.pitchedapps.frost.facebook
const val FACEBOOK_COM = "facebook.com"
const val FBCDN_NET = "fbcdn.net"
const val WWW_FACEBOOK_COM = "www.$FACEBOOK_COM"
const val HTTPS_FACEBOOK_COM = "https://$WWW_FACEBOOK_COM"
const val HTTPS_MESSENGER_COM = "https://www.messenger.com/"
const val FACEBOOK_BASE_COM = "m.$FACEBOOK_COM"
const val FB_URL_BASE = "https://$FACEBOOK_BASE_COM/"
const val FACEBOOK_MBASIC_COM = "mbasic.$FACEBOOK_COM"
const val FB_URL_MBASIC_BASE = "https://$FACEBOOK_MBASIC_COM/"
const val MESSENGER_URL_BASE = "https://www.messenger.com/"
fun profilePictureUrl(id: Long) = "https://graph.facebook.com/$id/picture?type=large"
const val FB_LOGIN_URL = "${FB_URL_BASE}login"
const val FB_HOME_URL = "${FB_URL_BASE}home.php"

View File

@ -28,6 +28,7 @@ import com.pitchedapps.frost.prefs.Prefs
import com.pitchedapps.frost.utils.L
import com.pitchedapps.frost.utils.cookies
import com.pitchedapps.frost.utils.launchLogin
import kotlinx.coroutines.Dispatchers
import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine
import kotlinx.coroutines.NonCancellable
@ -44,7 +45,8 @@ import org.koin.dsl.module
class FbCookie(private val prefs: Prefs, private val cookieDao: CookieDao) {
companion object {
private const val COOKIE_DOMAIN = FB_URL_BASE
private const val FB_COOKIE_DOMAIN = HTTPS_FACEBOOK_COM
private const val MESSENGER_COOKIE_DOMAIN = HTTPS_MESSENGER_COM
fun module() = module {
single { FbCookie(get(), get()) }
@ -56,36 +58,37 @@ class FbCookie(private val prefs: Prefs, private val cookieDao: CookieDao) {
* Note that this is a synchronized call
*/
val webCookie: String?
get() = CookieManager.getInstance().getCookie(COOKIE_DOMAIN)
get() = CookieManager.getInstance().getCookie(FB_COOKIE_DOMAIN)
private suspend fun CookieManager.suspendSetWebCookie(cookie: String?): Boolean {
val messengerCookie: String?
get() = CookieManager.getInstance().getCookie(HTTPS_MESSENGER_COM)
private suspend fun CookieManager.suspendSetWebCookie(domain: String, cookie: String?): Boolean {
cookie ?: return true
return withContext(NonCancellable) {
removeAllCookies()
// Save all cookies regardless of result, then check if all succeeded
val result = cookie.split(";")
.map { async { setSingleWebCookie(it) } }
.map { async { setSingleWebCookie(domain, it) } }
.awaitAll().all { it }
flush()
L.d { "Cookies set" }
L._d { "Set $cookie\n\tResult $webCookie" }
result
}
}
private suspend fun CookieManager.setSingleWebCookie(domain: String, cookie: String): Boolean =
suspendCoroutine { cont ->
setCookie(domain, cookie.trim()) {
cont.resume(it)
}
}
private suspend fun CookieManager.removeAllCookies(): Boolean = suspendCoroutine { cont ->
removeAllCookies {
cont.resume(it)
}
}
private suspend fun CookieManager.setSingleWebCookie(cookie: String): Boolean =
suspendCoroutine { cont ->
setCookie(COOKIE_DOMAIN, cookie.trim()) {
cont.resume(it)
}
}
suspend fun save(id: Long) {
L.d { "New cookie found" }
prefs.userId = id
@ -108,14 +111,19 @@ class FbCookie(private val prefs: Prefs, private val cookieDao: CookieDao) {
}
suspend fun switchUser(cookie: CookieEntity?) {
if (cookie == null) {
if (cookie?.cookie == null) {
L.d { "Switching User; null cookie" }
return
}
withContext(NonCancellable) {
withContext(Dispatchers.IO + NonCancellable) {
L.d { "Switching User" }
prefs.userId = cookie.id
CookieManager.getInstance().suspendSetWebCookie(cookie.cookie)
CookieManager.getInstance().apply {
removeAllCookies()
suspendSetWebCookie(FB_COOKIE_DOMAIN, cookie.cookie)
// TODO set messenger cookie
flush()
}
}
}

View File

@ -48,7 +48,7 @@ enum class FbItem(
MARKETPLACE(R.string.marketplace, GoogleMaterial.Icon.gmd_store, "marketplace"),
MENU(R.string.menu, GoogleMaterial.Icon.gmd_menu, "settings"),
MESSAGES(R.string.messages, MaterialDesignIconic.Icon.gmi_comments, "messages"),
MESSENGER(R.string.messenger, CommunityMaterial.Icon2.cmd_facebook_messenger, "", prefix = MESSENGER_URL_BASE),
MESSENGER(R.string.messenger, CommunityMaterial.Icon2.cmd_facebook_messenger, "", prefix = HTTPS_MESSENGER_COM),
NOTES(R.string.notes, CommunityMaterial.Icon3.cmd_note, "notes"),
NOTIFICATIONS(R.string.notifications, MaterialDesignIconic.Icon.gmi_globe, "notifications"),
ON_THIS_DAY(R.string.on_this_day, GoogleMaterial.Icon.gmd_today, "onthisday"),