aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbCookie.kt
blob: e5f0b8fe40f973ea2977e549b72ce3e22397a855 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package com.pitchedapps.frost.facebook

import android.app.Activity
import android.content.Context
import android.webkit.CookieManager
import com.pitchedapps.frost.dbflow.CookieModel
import com.pitchedapps.frost.dbflow.loadFbCookie
import com.pitchedapps.frost.dbflow.removeCookie
import com.pitchedapps.frost.dbflow.saveFbCookie
import com.pitchedapps.frost.utils.L
import com.pitchedapps.frost.utils.Prefs
import com.pitchedapps.frost.utils.cookies
import com.pitchedapps.frost.utils.launchLogin
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.subjects.SingleSubject

/**
 * Created by Allan Wang on 2017-05-30.
 */
object FbCookie {

    inline val webCookie: String?
        get() = CookieManager.getInstance().getCookie(FB_URL_BASE)

    private fun setWebCookie(cookie: String?, callback: (() -> Unit)?) {
        with(CookieManager.getInstance()) {
            removeAllCookies {
                if (cookie == null) {
                    callback?.invoke()
                    return@removeAllCookies
                }
                L.d { "Setting cookie" }
                val cookies = cookie.split(";").map { Pair(it, SingleSubject.create<Boolean>()) }
                cookies.forEach { (cookie, callback) -> setCookie(FB_URL_BASE, cookie, { callback.onSuccess(it) }) }
                Observable.zip<Boolean, Unit>(cookies.map { (_, callback) -> callback.toObservable() }, {})
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe {
                            callback?.invoke()
                            L.d { "Cookies set" }
                            L._d { cookie }
                            flush()
                        }
            }
        }
    }

    operator fun invoke() {
        L.d { "FbCookie Invoke User" }
        with(CookieManager.getInstance()) {
            setAcceptCookie(true)
        }
        val dbCookie = loadFbCookie(Prefs.userId)?.cookie
        if (dbCookie != null && webCookie == null) {
            L.d { "DbCookie found & WebCookie is null; setting webcookie" }
            setWebCookie(dbCookie, null)
        }
    }

    fun save(id: Long) {
        L.d { "New cookie found" }
        Prefs.userId = id
        CookieManager.getInstance().flush()
        val cookie = CookieModel(Prefs.userId, "", webCookie)
        saveFbCookie(cookie)
    }

    fun reset(callback: () -> Unit) {
        Prefs.userId = -1L
        with(CookieManager.getInstance()) {
            removeAllCookies {
                flush()
                callback()
            }
        }
    }

    fun switchUser(id: Long, callback: () -> Unit) = switchUser(loadFbCookie(id), callback)

    fun switchUser(name: String, callback: () -> Unit) = switchUser(loadFbCookie(name), callback)

    fun switchUser(cookie: CookieModel?, callback: () -> Unit) {
        if (cookie == null) {
            L.d { "Switching User; null cookie" }
            callback()
            return
        }
        L.d { "Switching User" }
        Prefs.userId = cookie.id
        setWebCookie(cookie.cookie, callback)
    }

    /**
     * Helper function to remove the current cookies
     * and launch the proper login page
     */
    fun logout(context: Context) {
        val cookies = arrayListOf<CookieModel>()
        if (context is Activity)
            cookies.addAll(context.cookies().filter { it.id != Prefs.userId })
        logout(Prefs.userId) {
            context.launchLogin(cookies, true)
        }
    }

    /**
     * Clear the cookies of the given id
     */
    fun logout(id: Long, callback: () -> Unit) {
        L.d { "Logging out user" }
        removeCookie(id)
        reset(callback)
    }

    /**
     * Notifications may come from different accounts, and we need to switch the cookies to load them
     * When coming back to the main app, switch back to our original account before continuing
     */
    fun switchBackUser(callback: () -> Unit) {
        if (Prefs.prevId == -1L) return callback()
        val prevId = Prefs.prevId
        Prefs.prevId = -1L
        if (prevId != Prefs.userId) {
            switchUser(prevId) {
                L.d { "Switch back user" }
                L._d { "${Prefs.userId} to $prevId" }
                callback()
            }
        } else callback()
    }
}