summaryrefslogtreecommitdiff
path: root/eventsview-plugins/eventsview-plugin-mastodon/mastodon-delegate.qml
blob: aefaa96258bb8593f212c5b3d80f52b707f6e196 (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
/****************************************************************************
 **
 ** Copyright (C) 2013-2026 Jolla Ltd.
 **
 ****************************************************************************/

import QtQuick 2.0
import Sailfish.Silica 1.0
import org.nemomobile.socialcache 1.0
import com.jolla.eventsview.mastodon 1.0
import QtQml.Models 2.1
import "shared"

SocialMediaAccountDelegate {
    id: delegateItem

    //: Mastodon posts
    //% "Posts"
    headerText: qsTrId("lipstick-jolla-home-la-mastodon_posts")
    headerIcon: "image://theme/icon-l-mastodon"
    showRemainingCount: false

    services: ["Posts"]
    socialNetwork: 9
    dataType: SocialSync.Posts
    providerName: "mastodon"

    MastodonPostActions {
        id: mastodonPostActions
    }

    model: MastodonPostsModel {
        onCountChanged: {
            if (count > 0) {
                if (!updateTimer.running) {
                    shortUpdateTimer.start()
                }
            } else {
                shortUpdateTimer.stop()
            }
        }
    }

    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"])
        resolvedStatusUrl: delegateItem.statusUrl(model)
        postId: delegateItem.stringRole(model, ["mastodonId", "statusId", "id", "twitterId"])
        postActions: mastodonPostActions
        accountId: delegateItem.firstAccountId(model)

        onTriggered: Qt.openUrlExternally(resolvedStatusUrl)

        Component.onCompleted: {
            refreshTimeCount = Qt.binding(function() { return delegateItem.refreshTimeCount })
            connectedToNetwork = Qt.binding(function() { return delegateItem.connectedToNetwork })
            eventsColumnMaxWidth = Qt.binding(function() { return delegateItem.eventsColumnMaxWidth })
        }
    }
    //% "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")

    onViewVisibleChanged: {
        if (viewVisible) {
            delegateItem.resetHasSyncableAccounts()
            delegateItem.model.refresh()
            if (delegateItem.hasSyncableAccounts && !updateTimer.running) {
                shortUpdateTimer.start()
            }
        } else {
            shortUpdateTimer.stop()
        }
    }

    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()
        }
    }

    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"])
        if (directUrl.length > 0) {
            return directUrl
        }

        var instanceUrl = 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)
        }

        var accountName = stringRole(modelData, ["accountName", "acct", "screenName", "username"])
        var statusId = stringRole(modelData, ["mastodonId", "statusId", "id", "twitterId"])
        if (accountName.length > 0 && statusId.length > 0) {
            while (accountName.length > 0 && accountName.charAt(0) === "@") {
                accountName = accountName.substring(1)
            }
            return instanceUrl + "/@" + accountName + "/" + statusId
        }

        return instanceUrl + "/explore"
    }

    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
    }
}