summaryrefslogtreecommitdiff
path: root/settings/accounts/ui/mastodon-update.qml
blob: 3a6c25cede7537bf21652badc0e949ba2e49af04 (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
import QtQuick 2.0
import Sailfish.Silica 1.0
import Sailfish.Accounts 1.0
import com.jolla.settings.accounts 1.0

AccountCredentialsAgent {
    id: root

    property bool _started

    readonly property string callbackUri: "http://ipv4.jolla.com/online/status.html"

    function normalizeApiHost(rawHost) {
        var host = rawHost ? rawHost.trim() : ""
        if (host.length === 0) {
            host = "https://mastodon.social"
        }

        host = host.replace(/^https?:\/\//i, "")
        var pathSeparator = host.indexOf("/")
        if (pathSeparator !== -1) {
            host = host.substring(0, pathSeparator)
        }
        host = host.replace(/\/+$/, "")

        if (host.length === 0) {
            host = "mastodon.social"
        }
        return "https://" + host.toLowerCase()
    }

    function _valueFromServiceConfig(config, key) {
        return config && config[key] ? config[key].toString() : ""
    }

    function _startUpdate() {
        if (_started || initialPage.status !== PageStatus.Active || account.status !== Account.Initialized) {
            return
        }

        var config = account.configurationValues("mastodon-microblog")
        var apiHost = normalizeApiHost(_valueFromServiceConfig(config, "api/Host"))
        var oauthHost = _valueFromServiceConfig(config, "auth/oauth2/web_server/Host")
        if (oauthHost.length === 0) {
            oauthHost = apiHost.replace(/^https?:\/\//i, "")
        }

        var clientId = _valueFromServiceConfig(config, "auth/oauth2/web_server/ClientId")
        var clientSecret = _valueFromServiceConfig(config, "auth/oauth2/web_server/ClientSecret")
        if (clientId.length === 0 || clientSecret.length === 0) {
            credentialsUpdateError("Missing Mastodon OAuth client credentials")
            return
        }

        _started = true

        var sessionData = {
            "ClientId": clientId,
            "ClientSecret": clientSecret,
            "Host": oauthHost,
            "AuthPath": "oauth/authorize",
            "TokenPath": "oauth/token",
            "ResponseType": "code",
            "Scope": ["read", "write"],
            "RedirectUri": callbackUri
        }
        initialPage.prepareAccountCredentialsUpdate(account, root.accountProvider, "mastodon-microblog", sessionData)
    }

    Account {
        id: account
        identifier: root.accountId

        onStatusChanged: {
            root._startUpdate()
        }
    }

    initialPage: OAuthAccountSetupPage {
        onStatusChanged: {
            root._startUpdate()
        }

        onAccountCredentialsUpdated: {
            root.credentialsUpdated(root.accountId)
            root.goToEndDestination()
        }

        onAccountCredentialsUpdateError: {
            root.credentialsUpdateError(errorMessage)
        }

        onPageContainerChanged: {
            if (pageContainer == null) {
                cancelSignIn()
            }
        }
    }
}