diff options
| author | Andrew Branson <andrew.branson@jolla.com> | 2026-02-10 23:52:48 +0100 |
|---|---|---|
| committer | Andrew Branson <andrew.branson@jolla.com> | 2026-02-11 00:12:00 +0100 |
| commit | b42a78104f6d38ab3aa578e8d2201ac8c685a28c (patch) | |
| tree | 0a3cbf5483f0272f1a20678907a240ab8bfb0b11 /settings/accounts/ui/mastodon-update.qml | |
| parent | 69628390815254297bbd8c95436f6780fa846fae (diff) | |
Show account handle as description
Diffstat (limited to 'settings/accounts/ui/mastodon-update.qml')
| -rw-r--r-- | settings/accounts/ui/mastodon-update.qml | 125 |
1 files changed, 123 insertions, 2 deletions
diff --git a/settings/accounts/ui/mastodon-update.qml b/settings/accounts/ui/mastodon-update.qml index d577b05..fa99a82 100644 --- a/settings/accounts/ui/mastodon-update.qml +++ b/settings/accounts/ui/mastodon-update.qml @@ -34,6 +34,128 @@ AccountCredentialsAgent { 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 @@ -83,8 +205,7 @@ AccountCredentialsAgent { } onAccountCredentialsUpdated: { - root.credentialsUpdated(root.accountId) - root.goToEndDestination() + root._updateDescription(responseData) } onAccountCredentialsUpdateError: { |
