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 | |
| parent | 2100e260e58e12cef347ecf17e3aad9422d22e86 (diff) | |
Simplify FeedItem and delegate
| -rw-r--r-- | eventsview-plugins/eventsview-plugin-mastodon/MastodonFeedItem.qml | 23 | ||||
| -rw-r--r-- | eventsview-plugins/eventsview-plugin-mastodon/mastodon-delegate.qml | 92 |
2 files changed, 84 insertions, 31 deletions
diff --git a/eventsview-plugins/eventsview-plugin-mastodon/MastodonFeedItem.qml b/eventsview-plugins/eventsview-plugin-mastodon/MastodonFeedItem.qml index 9d04dfe..8db1811 100644 --- a/eventsview-plugins/eventsview-plugin-mastodon/MastodonFeedItem.qml +++ b/eventsview-plugins/eventsview-plugin-mastodon/MastodonFeedItem.qml @@ -9,20 +9,19 @@ import Sailfish.Silica 1.0 import Sailfish.TextLinking 1.0 import org.nemomobile.lipstick 0.1 import "shared" -import "shared/SocialFeedUtils.js" as FeedUtils SocialMediaFeedItem { id: item property variant imageList - property string resolvedStatusUrl: FeedUtils.stringRole(model, ["url", "link", "uri"], "") + property string resolvedStatusUrl: model && model.url ? model.url.toString() : "" property string postId property QtObject postActions - property int likeCount: FeedUtils.intRole(model, ["favouritesCount", "likeCount", "favoriteCount"], 0) - property int commentCount: FeedUtils.intRole(model, ["repliesCount", "commentCount"], 0) - property int boostCount: FeedUtils.intRole(model, ["reblogsCount", "boostCount", "repostsCount"], 0) - property bool favourited: FeedUtils.boolRole(model, ["favourited"], false) - property bool reblogged: FeedUtils.boolRole(model, ["reblogged"], false) + property int likeCount: model && model.favouritesCount ? model.favouritesCount : 0 + property int commentCount: model && model.repliesCount ? model.repliesCount : 0 + property int boostCount: model && model.reblogsCount ? model.reblogsCount : 0 + property bool favourited: model ? !!model.favourited : false + property bool reblogged: model ? !!model.reblogged : false property int _likeCountOverride: -1 property int _boostCountOverride: -1 property int _favouritedOverride: -1 @@ -36,10 +35,10 @@ SocialMediaFeedItem { property var _actionMenu property real _contextMenuHeight: (_contextMenuOpen && _actionMenu) ? _actionMenu.height : 0 - property string _booster: FeedUtils.stringRole(model, ["boostedBy", "rebloggedBy", "retweeter"], "") - property string _displayName: FeedUtils.stringRole(model, ["name", "displayName", "display_name"], "") - property string _accountName: FeedUtils.stringRole(model, ["accountName", "acct", "screenName", "username"], "") - property string _bodyText: FeedUtils.stringRole(model, ["body", "content", "text"], "") + property string _booster: model && model.boostedBy ? model.boostedBy.toString() : "" + property string _displayName: model && model.name ? model.name.toString() : "" + property string _accountName: model && model.accountName ? model.accountName.toString() : "" + property string _bodyText: model && model.body ? model.body.toString() : "" //: Action label shown in Mastodon interaction menu. //% "Favourite" readonly property string _favouriteActionText: qsTrId("lipstick-jolla-home-la-mastodon_favourite") @@ -187,7 +186,7 @@ SocialMediaFeedItem { if (item.postId.length > 0) { return item.postId } - return FeedUtils.stringRole(model, ["mastodonId", "statusId", "id", "twitterId"], "") + return model && model.mastodonId ? model.mastodonId.toString() : "" } function actionAccountId() { 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 + } } |
