aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAllan Wang <me@allanwang.ca>2021-01-10 20:28:07 -0800
committerAllan Wang <me@allanwang.ca>2021-01-10 20:28:07 -0800
commit7ad9fdfc6ff5faebb9588335013d1f414d3d0bad (patch)
tree89a892e43e15c25207b3a0778baf4400692780a6
parente5e1a0d4a521eb3c668e04c2123e0ecdd71230e5 (diff)
downloadfrost-7ad9fdfc6ff5faebb9588335013d1f414d3d0bad.tar.gz
frost-7ad9fdfc6ff5faebb9588335013d1f414d3d0bad.tar.bz2
frost-7ad9fdfc6ff5faebb9588335013d1f414d3d0bad.zip
Prepare for messenger cookies
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/facebook/FbConst.kt3
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/facebook/FbCookie.kt40
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/facebook/FbItem.kt2
3 files changed, 27 insertions, 18 deletions
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbConst.kt b/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbConst.kt
index 55321ed2..9edcc496 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbConst.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbConst.kt
@@ -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"
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbCookie.kt b/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbCookie.kt
index e66eaf27..78d51aef 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbCookie.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbCookie.kt
@@ -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()
+ }
}
}
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbItem.kt b/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbItem.kt
index 3ad79f4a..9b08c775 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbItem.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbItem.kt
@@ -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"),