summaryrefslogtreecommitdiff
path: root/settings/accounts/ui/mastodon-update.qml
blob: 98dba8ca3f992dd3eb7d070e299fd0d5e7b77a2b (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
 * Copyright (C) 2013-2026 Jolla Ltd.
 */

import QtQuick 2.0
import Sailfish.Silica 1.0
import Sailfish.Accounts 1.0
import com.jolla.settings.accounts 1.0
import com.jolla.settings.accounts.mastodon 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 _extractAccountName(responseData) {
        if (!responseData) {
            return ""
        }

        var candidates = [
            "AccountUsername",
            "UserName",
            "user_name",
            "acct",
            "username",
            "preferred_username",
            "login",
            "ScreenName"
        ]
        for (var i = 0; i < candidates.length; ++i) {
            var value = responseData[candidates[i]]
            if (value) {
                var userName = value.toString().trim()
                if (userName.length > 0) {
                    return userName
                }
            }
        }

        return ""
    }

    function _formatMastodonAccountId(accountName, apiHost) {
        var value = accountName ? accountName.toString().trim() : ""
        if (value.length === 0) {
            return ""
        }

        value = value.replace(/^@+/, "")
        if (value.indexOf("@") !== -1) {
            return "@" + value
        }

        var host = apiHost.replace(/^https?:\/\//i, "")
        if (host.length === 0) {
            return ""
        }

        return "@" + value + "@" + host
    }

    function _extractAccessToken(responseData) {
        if (!responseData) {
            return ""
        }

        var token = responseData["AccessToken"]
        if (!token || token.toString().trim().length === 0) {
            token = responseData["access_token"]
        }
        return token ? token.toString().trim() : ""
    }

    function _isMastodonAccountId(value) {
        var text = value ? value.toString().trim() : ""
        return /^@[^@]+@[^@]+$/.test(text)
    }

    function _completeUpdate() {
        root.credentialsUpdated(root.accountId)
        root.goToEndDestination()
    }

    function _saveDescription(description) {
        if (description.length > 0) {
            account.setConfigurationValue("", "description", description)
            if (_isMastodonAccountId(description)) {
                account.setConfigurationValue("", "default_credentials_username", description)
            }
        }
        account.sync()
        _completeUpdate()
    }

    function _updateDescription(responseData) {
        var config = account.configurationValues("mastodon-microblog")
        var apiHost = normalizeApiHost(_valueFromServiceConfig(config, "api/Host"))
        var description = _formatMastodonAccountId(_extractAccountName(responseData), apiHost)
        if (description.length > 0) {
            _saveDescription(description)
            return
        }

        var accessToken = _extractAccessToken(responseData)
        if (accessToken.length === 0) {
            _completeUpdate()
            return
        }

        var xhr = new XMLHttpRequest()
        xhr.onreadystatechange = function() {
            if (xhr.readyState !== XMLHttpRequest.DONE) {
                return
            }

            var fetchedDescription = ""
            if (xhr.status >= 200 && xhr.status < 300) {
                try {
                    var response = JSON.parse(xhr.responseText)
                    fetchedDescription = _formatMastodonAccountId(_extractAccountName(response), apiHost)
                } catch (err) {
                }
            }

            if (fetchedDescription.length > 0) {
                _saveDescription(fetchedDescription)
            } else {
                _completeUpdate()
            }
        }

        xhr.open("GET", apiHost + "/api/v1/accounts/verify_credentials")
        xhr.setRequestHeader("Authorization", "Bearer " + accessToken)
        xhr.send()
    }

    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._updateDescription(responseData)
        }

        onAccountCredentialsUpdateError: {
            root.credentialsUpdateError(errorMessage)
        }

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