summaryrefslogtreecommitdiff
path: root/eventsview-plugins/eventsview-plugin-mastodon/mastodon-delegate.qml
diff options
context:
space:
mode:
authorAndrew Branson <andrew.branson@jolla.com>2026-02-18 21:37:05 +0100
committerAndrew Branson <andrew.branson@jolla.com>2026-02-18 21:37:05 +0100
commit60d8504a8b5b0d5e5fa40c4ab7df8e5f98c88e32 (patch)
tree79529799246d3affe713775abdf5577e1f08d907 /eventsview-plugins/eventsview-plugin-mastodon/mastodon-delegate.qml
parent1ce97352a0aa2cea0be39fb7eb3a8333aadd2487 (diff)
Refactor
- Move common pieces to eventsview-extensions e.g. interaction menu - Expose and add useful API to libsocialcache
Diffstat (limited to 'eventsview-plugins/eventsview-plugin-mastodon/mastodon-delegate.qml')
-rw-r--r--eventsview-plugins/eventsview-plugin-mastodon/mastodon-delegate.qml128
1 files changed, 20 insertions, 108 deletions
diff --git a/eventsview-plugins/eventsview-plugin-mastodon/mastodon-delegate.qml b/eventsview-plugins/eventsview-plugin-mastodon/mastodon-delegate.qml
index c8e8713..59006fb 100644
--- a/eventsview-plugins/eventsview-plugin-mastodon/mastodon-delegate.qml
+++ b/eventsview-plugins/eventsview-plugin-mastodon/mastodon-delegate.qml
@@ -10,6 +10,7 @@ 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
@@ -24,32 +25,23 @@ SocialMediaAccountDelegate {
socialNetwork: 9
dataType: SocialSync.Posts
providerName: "mastodon"
+ periodicSyncLoopEnabled: true
MastodonPostActions {
id: mastodonPostActions
}
- model: MastodonPostsModel {
- onCountChanged: {
- if (count > 0) {
- if (!updateTimer.running) {
- shortUpdateTimer.start()
- }
- } else {
- shortUpdateTimer.stop()
- }
- }
- }
+ model: MastodonPostsModel {}
delegate: MastodonFeedItem {
downloader: delegateItem.downloader
- imageList: delegateItem.variantRole(model, ["images", "mediaAttachments", "media"])
- avatarSource: delegateItem.convertUrl(delegateItem.stringRole(model, ["icon", "avatar", "avatarUrl"]))
- fallbackAvatarSource: delegateItem.stringRole(model, ["icon", "avatar", "avatarUrl"])
+ imageList: FeedUtils.variantRole(model, ["images", "mediaAttachments", "media"], undefined)
+ avatarSource: FeedUtils.normalizeAvatarUrl(FeedUtils.stringRole(model, ["icon", "avatar", "avatarUrl"], ""))
+ fallbackAvatarSource: FeedUtils.stringRole(model, ["icon", "avatar", "avatarUrl"], "")
resolvedStatusUrl: delegateItem.authorizeInteractionUrl(model)
- postId: delegateItem.stringRole(model, ["mastodonId", "statusId", "id", "twitterId"])
+ postId: FeedUtils.stringRole(model, ["mastodonId", "statusId", "id", "twitterId"], "")
postActions: mastodonPostActions
- accountId: delegateItem.firstAccountId(model)
+ accountId: FeedUtils.firstAccountId(model, -1)
onTriggered: Qt.openUrlExternally(resolvedStatusUrl)
@@ -69,94 +61,36 @@ SocialMediaAccountDelegate {
if (viewVisible) {
delegateItem.resetHasSyncableAccounts()
delegateItem.model.refresh()
- if (delegateItem.hasSyncableAccounts && !updateTimer.running) {
- shortUpdateTimer.start()
+ if (delegateItem.hasSyncableAccounts) {
+ delegateItem.startPeriodicSyncLoop()
}
} else {
- shortUpdateTimer.stop()
+ delegateItem.stopPeriodicSyncLoop()
}
}
onConnectedToNetworkChanged: {
if (viewVisible) {
- if (!updateTimer.running) {
- shortUpdateTimer.start()
- }
- }
- }
-
- // The Mastodon feed is updated 3 seconds after the feed view becomes visible,
- // unless it has been updated during last 60 seconds. After that it will be updated
- // periodically in every 60 seconds as long as the feed view is visible.
-
- Timer {
- id: shortUpdateTimer
-
- interval: 3000
- onTriggered: {
- delegateItem.sync()
- updateTimer.start()
+ delegateItem.startPeriodicSyncLoop()
}
}
- Timer {
- id: updateTimer
-
- interval: 60000
- repeat: true
- onTriggered: {
- if (delegateItem.viewVisible) {
- delegateItem.sync()
- } else {
- stop()
- }
- }
- }
-
- function variantRole(modelData, roleNames) {
- for (var i = 0; i < roleNames.length; ++i) {
- var value = modelData[roleNames[i]]
- if (typeof value !== "undefined" && value !== null) {
- return value
- }
- }
- return undefined
- }
-
- function stringRole(modelData, roleNames) {
- for (var i = 0; i < roleNames.length; ++i) {
- var value = modelData[roleNames[i]]
- if (typeof value === "undefined" || value === null) {
- continue
- }
- value = String(value)
- if (value.length > 0) {
- return value
- }
- }
- return ""
- }
-
function statusUrl(modelData) {
- var directUrl = stringRole(modelData, ["url", "link", "uri"])
+ var directUrl = FeedUtils.stringRole(modelData, ["url", "link", "uri"], "")
if (directUrl.length > 0) {
return directUrl
}
- var instanceUrl = stringRole(modelData, ["instanceUrl", "serverUrl", "baseUrl"])
+ var instanceUrl = FeedUtils.stringRole(modelData, ["instanceUrl", "serverUrl", "baseUrl"], "")
if (instanceUrl.length === 0) {
instanceUrl = "https://mastodon.social"
}
- while (instanceUrl.length > 0 && instanceUrl.charAt(instanceUrl.length - 1) === "/") {
- instanceUrl = instanceUrl.slice(0, instanceUrl.length - 1)
- }
+ instanceUrl = FeedUtils.stripTrailingSlashes(instanceUrl)
- var accountName = stringRole(modelData, ["accountName", "acct", "screenName", "username"])
- var statusId = stringRole(modelData, ["mastodonId", "statusId", "id", "twitterId"])
+ var accountName = FeedUtils.stringRole(modelData, ["accountName", "acct", "screenName", "username"], "")
+ var statusId = FeedUtils.stringRole(modelData, ["mastodonId", "statusId", "id", "twitterId"], "")
if (accountName.length > 0 && statusId.length > 0) {
- while (accountName.length > 0 && accountName.charAt(0) === "@") {
- accountName = accountName.substring(1)
- }
+ accountName = FeedUtils.trimLeadingCharacter(accountName, "@")
return instanceUrl + "/@" + accountName + "/" + statusId
}
@@ -169,13 +103,11 @@ SocialMediaAccountDelegate {
return targetUrl
}
- var instanceUrl = stringRole(modelData, ["instanceUrl", "serverUrl", "baseUrl"])
+ var instanceUrl = FeedUtils.stringRole(modelData, ["instanceUrl", "serverUrl", "baseUrl"], "")
if (instanceUrl.length === 0) {
return targetUrl
}
- while (instanceUrl.length > 0 && instanceUrl.charAt(instanceUrl.length - 1) === "/") {
- instanceUrl = instanceUrl.slice(0, instanceUrl.length - 1)
- }
+ instanceUrl = FeedUtils.stripTrailingSlashes(instanceUrl)
// Links on the user's own instance should open directly.
var sameServer = /^([a-z][a-z0-9+.-]*):\/\/([^\/?#]+)/i
@@ -191,24 +123,4 @@ SocialMediaAccountDelegate {
return instanceUrl + "/authorize_interaction?uri=" + encodeURIComponent(targetUrl)
}
-
- function convertUrl(source) {
- if (source.indexOf("_normal.") !== -1) {
- return source.replace("_normal.", "_bigger.")
- } else if (source.indexOf("_mini.") !== -1) {
- return source.replace("_mini.", "_bigger.")
- }
- return source
- }
-
- function firstAccountId(modelData) {
- var accounts = modelData.accounts
- if (accounts && accounts.length > 0) {
- var accountId = Number(accounts[0])
- if (!isNaN(accountId)) {
- return accountId
- }
- }
- return -1
- }
}