diff options
| author | Andrew Branson <andrew.branson@jolla.com> | 2026-02-20 14:26:11 +0100 |
|---|---|---|
| committer | Andrew Branson <andrew.branson@jolla.com> | 2026-02-21 12:56:46 +0100 |
| commit | 97c06240cd8ba05c5348efa57ce0bed9690ff545 (patch) | |
| tree | aef9139a2bc4f888ff8952258db017f60d78229e /eventsview-plugins/eventsview-plugin-mastodon/mastodon-delegate.qml | |
| parent | 2100e260e58e12cef347ecf17e3aad9422d22e86 (diff) | |
Simplify FeedItem and delegate
Diffstat (limited to 'eventsview-plugins/eventsview-plugin-mastodon/mastodon-delegate.qml')
| -rw-r--r-- | eventsview-plugins/eventsview-plugin-mastodon/mastodon-delegate.qml | 92 |
1 files changed, 73 insertions, 19 deletions
diff --git a/eventsview-plugins/eventsview-plugin-mastodon/mastodon-delegate.qml b/eventsview-plugins/eventsview-plugin-mastodon/mastodon-delegate.qml index 4049d56..aa1bcb0 100644 --- a/eventsview-plugins/eventsview-plugin-mastodon/mastodon-delegate.qml +++ b/eventsview-plugins/eventsview-plugin-mastodon/mastodon-delegate.qml @@ -10,10 +10,10 @@ import org.nemomobile.socialcache 1.0 import com.jolla.eventsview.mastodon 1.0 import QtQml.Models 2.1 import "shared" -import "shared/SocialFeedUtils.js" as FeedUtils SocialMediaAccountDelegate { id: delegateItem + property string instanceHomeUrl: "" //: Mastodon posts //% "Posts" @@ -22,7 +22,7 @@ SocialMediaAccountDelegate { showRemainingCount: false services: ["Posts"] - socialNetwork: 9 + socialNetwork: SocialSync.Mastodon dataType: SocialSync.Posts providerName: "mastodon" periodicSyncLoopEnabled: true @@ -35,17 +35,22 @@ SocialMediaAccountDelegate { delegate: MastodonFeedItem { downloader: delegateItem.downloader - imageList: FeedUtils.variantRole(model, ["images", "mediaAttachments", "media"], undefined) - avatarSource: FeedUtils.stringRole(model, ["icon", "avatar", "avatarUrl"], "") - fallbackAvatarSource: FeedUtils.stringRole(model, ["icon", "avatar", "avatarUrl"], "") + imageList: model.images + avatarSource: model.icon + fallbackAvatarSource: model.icon resolvedStatusUrl: delegateItem.authorizeInteractionUrl(model) - postId: FeedUtils.stringRole(model, ["mastodonId", "statusId", "id", "twitterId"], "") + postId: model.mastodonId postActions: mastodonPostActions - accountId: FeedUtils.firstAccountId(model, -1) + accountId: delegateItem.firstAccountId(model, -1) - onTriggered: Qt.openUrlExternally(resolvedStatusUrl) + onTriggered: { + if (resolvedStatusUrl.length > 0) { + Qt.openUrlExternally(resolvedStatusUrl) + } + } Component.onCompleted: { + delegateItem.instanceHomeUrl = statusUrl({instanceUrl: model.instanceUrl}) refreshTimeCount = Qt.binding(function() { return delegateItem.refreshTimeCount }) connectedToNetwork = Qt.binding(function() { return delegateItem.connectedToNetwork }) eventsColumnMaxWidth = Qt.binding(function() { return delegateItem.eventsColumnMaxWidth }) @@ -54,8 +59,16 @@ SocialMediaAccountDelegate { //% "Show more in Mastodon" expandedLabel: qsTrId("lipstick-jolla-home-la-show-more-in-mastodon") - onHeaderClicked: Qt.openUrlExternally("https://mastodon.social/explore") - onExpandedClicked: Qt.openUrlExternally("https://mastodon.social/explore") + onHeaderClicked: { + if (delegateItem.instanceHomeUrl.length > 0) { + Qt.openUrlExternally(delegateItem.instanceHomeUrl) + } + } + onExpandedClicked: { + if (delegateItem.instanceHomeUrl.length > 0) { + Qt.openUrlExternally(delegateItem.instanceHomeUrl) + } + } onViewVisibleChanged: { if (viewVisible) { @@ -75,22 +88,32 @@ SocialMediaAccountDelegate { } } + Connections { + target: delegateItem.model + + onCountChanged: { + if (target.count === 0) { + delegateItem.instanceHomeUrl = "" + } + } + } + function statusUrl(modelData) { - var directUrl = FeedUtils.stringRole(modelData, ["url", "link", "uri"], "") + var directUrl = modelData && modelData.url ? modelData.url.toString() : "" if (directUrl.length > 0) { return directUrl } - var instanceUrl = FeedUtils.stringRole(modelData, ["instanceUrl", "serverUrl", "baseUrl"], "") + var instanceUrl = modelData && modelData.instanceUrl ? modelData.instanceUrl.toString() : "" + instanceUrl = stripTrailingSlashes(instanceUrl) if (instanceUrl.length === 0) { - instanceUrl = "https://mastodon.social" + return "" } - instanceUrl = FeedUtils.stripTrailingSlashes(instanceUrl) - var accountName = FeedUtils.stringRole(modelData, ["accountName", "acct", "screenName", "username"], "") - var statusId = FeedUtils.stringRole(modelData, ["mastodonId", "statusId", "id", "twitterId"], "") + var accountName = modelData && modelData.accountName ? modelData.accountName.toString() : "" + var statusId = modelData && modelData.mastodonId ? modelData.mastodonId.toString() : "" if (accountName.length > 0 && statusId.length > 0) { - accountName = FeedUtils.trimLeadingCharacter(accountName, "@") + accountName = trimLeadingCharacter(accountName, "@") return instanceUrl + "/@" + accountName + "/" + statusId } @@ -103,11 +126,11 @@ SocialMediaAccountDelegate { return targetUrl } - var instanceUrl = FeedUtils.stringRole(modelData, ["instanceUrl", "serverUrl", "baseUrl"], "") + var instanceUrl = modelData && modelData.instanceUrl ? modelData.instanceUrl.toString() : "" if (instanceUrl.length === 0) { return targetUrl } - instanceUrl = FeedUtils.stripTrailingSlashes(instanceUrl) + instanceUrl = stripTrailingSlashes(instanceUrl) // Links on the user's own instance should open directly. var sameServer = /^([a-z][a-z0-9+.-]*):\/\/([^\/?#]+)/i @@ -123,4 +146,35 @@ SocialMediaAccountDelegate { return instanceUrl + "/authorize_interaction?uri=" + encodeURIComponent(targetUrl) } + + function firstAccountId(modelData, defaultValue) { + var fallback = typeof defaultValue === "undefined" ? -1 : Number(defaultValue) + var accounts = modelData ? modelData.accounts : undefined + if (!accounts || accounts.length <= 0) { + return fallback + } + + var accountId = Number(accounts[0]) + return isNaN(accountId) ? fallback : accountId + } + + function stripTrailingSlashes(value) { + value = String(value || "") + while (value.length > 0 && value.charAt(value.length - 1) === "/") { + value = value.slice(0, value.length - 1) + } + return value + } + + function trimLeadingCharacter(value, character) { + value = String(value || "") + if (!character || character.length === 0) { + return value + } + + while (value.length > 0 && value.charAt(0) === character) { + value = value.substring(1) + } + return value + } } |
