From e0f289862bb76c36c01db9b092cafeb4cf8f6ebc Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Sun, 24 Jan 2021 16:55:33 -0800 Subject: Update db to include messenger cookie --- .../main/kotlin/com/pitchedapps/frost/db/CookiesDb.kt | 16 +++++++++++++++- app/src/main/kotlin/com/pitchedapps/frost/db/Database.kt | 4 ++-- .../kotlin/com/pitchedapps/frost/facebook/FbCookie.kt | 7 +++++-- 3 files changed, 22 insertions(+), 5 deletions(-) (limited to 'app/src/main/kotlin/com/pitchedapps') diff --git a/app/src/main/kotlin/com/pitchedapps/frost/db/CookiesDb.kt b/app/src/main/kotlin/com/pitchedapps/frost/db/CookiesDb.kt index 388edfe6..163d151b 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/db/CookiesDb.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/db/CookiesDb.kt @@ -23,6 +23,8 @@ import androidx.room.Entity import androidx.room.Insert import androidx.room.OnConflictStrategy import androidx.room.Query +import androidx.room.migration.Migration +import androidx.sqlite.db.SupportSQLiteDatabase import com.pitchedapps.frost.prefs.Prefs import kotlinx.android.parcel.Parcelize @@ -37,7 +39,8 @@ data class CookieEntity( @ColumnInfo(name = "cookie_id") val id: Long, val name: String?, - val cookie: String? + val cookie: String?, + val cookieMessenger: String? = null // Version 2 ) : Parcelable { override fun toString(): String = "CookieEntity(${hashCode()})" @@ -61,6 +64,9 @@ interface CookieDao { @Query("DELETE FROM cookies WHERE cookie_id = :id") fun _deleteById(id: Long) + + @Query("UPDATE cookies SET cookieMessenger = :cookie WHERE cookie_id = :id") + fun _updateMessengerCookie(id: Long, cookie: String?) } suspend fun CookieDao.selectAll() = dao { _selectAll() } @@ -69,3 +75,11 @@ suspend fun CookieDao.save(cookie: CookieEntity) = dao { _save(cookie) } suspend fun CookieDao.save(cookies: List) = dao { _save(cookies) } suspend fun CookieDao.deleteById(id: Long) = dao { _deleteById(id) } suspend fun CookieDao.currentCookie(prefs: Prefs) = selectById(prefs.userId) +suspend fun CookieDao.updateMessengerCookie(id: Long, cookie: String?) = + dao { _updateMessengerCookie(id, cookie) } + +val COOKIES_MIGRATION_1_2 = object : Migration(1, 2) { + override fun migrate(database: SupportSQLiteDatabase) { + database.execSQL("ALTER TABLE cookies ADD COLUMN cookieMessenger TEXT") + } +} diff --git a/app/src/main/kotlin/com/pitchedapps/frost/db/Database.kt b/app/src/main/kotlin/com/pitchedapps/frost/db/Database.kt index 21a2f1dc..bd0b4ee0 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/db/Database.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/db/Database.kt @@ -31,7 +31,7 @@ interface FrostPrivateDao { @Database( entities = [CookieEntity::class, NotificationEntity::class, CacheEntity::class], - version = 1, + version = 2, exportSchema = true ) abstract class FrostPrivateDatabase : RoomDatabase(), FrostPrivateDao { @@ -84,7 +84,7 @@ class FrostDatabase( val privateDb = Room.databaseBuilder( context, FrostPrivateDatabase::class.java, FrostPrivateDatabase.DATABASE_NAME - ).frostBuild() + ).addMigrations(COOKIES_MIGRATION_1_2).frostBuild() val publicDb = Room.databaseBuilder( context, FrostPublicDatabase::class.java, FrostPublicDatabase.DATABASE_NAME 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 efaa03ab..db40495b 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbCookie.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbCookie.kt @@ -24,6 +24,7 @@ import com.pitchedapps.frost.db.CookieEntity import com.pitchedapps.frost.db.deleteById import com.pitchedapps.frost.db.save import com.pitchedapps.frost.db.selectById +import com.pitchedapps.frost.db.updateMessengerCookie import com.pitchedapps.frost.prefs.Prefs import com.pitchedapps.frost.utils.L import com.pitchedapps.frost.utils.cookies @@ -115,14 +116,16 @@ class FbCookie(private val prefs: Prefs, private val cookieDao: CookieDao) { L.d { "Switching User; null cookie" } return } + val currentId = prefs.userId withContext(Dispatchers.IO + NonCancellable) { L.d { "Switching User" } - // TODO save old messenger cookie + // Save current messenger cookie state. + cookieDao.updateMessengerCookie(currentId, messengerCookie) prefs.userId = cookie.id CookieManager.getInstance().apply { removeAllCookies() suspendSetWebCookie(FB_COOKIE_DOMAIN, cookie.cookie) - // TODO set messenger cookie + suspendSetWebCookie(MESSENGER_COOKIE_DOMAIN, cookie.cookieMessenger) flush() } } -- cgit v1.2.3