summaryrefslogtreecommitdiff
path: root/eventsview-plugins/eventsview-plugin-fediverse/fediverse-delegate.qml
blob: 334be86a10ad2b8a5ceaed290df435100ce04668 (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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
 * SPDX-FileCopyrightText: 2013 - 2026 Jolla Ltd.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

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

SocialMediaAccountDelegate {
    id: delegateItem
    property string instanceHomeUrl: ""
    property string instanceIconPath: ""

    //: Fediverse posts
    //% "Posts"
    headerText: qsTrId("lipstick-jolla-home-la-fediverse_posts")
    headerIcon: delegateItem.instanceIconPath.length > 0 ? delegateItem.instanceIconPath : "image://theme/icon-l-fediverse"
    showRemainingCount: false

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

    FediversePostActions {
        id: fediversePostActions
    }

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

    delegate: FediverseFeedItem {
        downloader: delegateItem.downloader
        imageList: model.images
        avatarSource: model.icon
        fallbackAvatarSource: model.icon
        resolvedStatusUrl: delegateItem.authorizeInteractionUrl(model)
        postId: model.fediverseId
        postActions: fediversePostActions
        accountId: delegateItem.firstAccountId(model, -1)

        onTriggered: {
            if (resolvedStatusUrl.length > 0) {
                Qt.openUrlExternally(resolvedStatusUrl)
            }
        }

        Component.onCompleted: {
            delegateItem.instanceHomeUrl = statusUrl({instanceUrl: model.instanceUrl})
            if (model.instanceIconPath && model.instanceIconPath.length > 0) {
                delegateItem.instanceIconPath = model.instanceIconPath
            }
            refreshTimeCount = Qt.binding(function() { return delegateItem.refreshTimeCount })
            connectedToNetwork = Qt.binding(function() { return delegateItem.connectedToNetwork })
            eventsColumnMaxWidth = Qt.binding(function() { return delegateItem.eventsColumnMaxWidth })
        }
    }
    //% "Show more in Fediverse"
    expandedLabel: qsTrId("lipstick-jolla-home-la-show-more-in-fediverse")

    onHeaderClicked: {
        if (delegateItem.instanceHomeUrl.length > 0) {
            Qt.openUrlExternally(delegateItem.instanceHomeUrl)
        }
    }
    onExpandedClicked: {
        if (delegateItem.instanceHomeUrl.length > 0) {
            Qt.openUrlExternally(delegateItem.instanceHomeUrl)
        }
    }

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

    Connections {
        target: delegateItem.model

        onCountChanged: {
            if (target.count === 0) {
                delegateItem.instanceHomeUrl = ""
                delegateItem.instanceIconPath = ""
            }
        }
    }

    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 statusUrl(modelData) {
        var directUrl = modelData && modelData.url ? modelData.url.toString() : ""
        if (directUrl.length > 0) {
            return directUrl
        }

        var instanceUrl = modelData && modelData.instanceUrl ? modelData.instanceUrl.toString() : ""
        instanceUrl = stripTrailingSlashes(instanceUrl)
        if (instanceUrl.length === 0) {
            return ""
        }

        var accountName = modelData && modelData.accountName ? modelData.accountName.toString() : ""
        var statusId = modelData && modelData.fediverseId ? modelData.fediverseId.toString() : ""
        if (accountName.length > 0 && statusId.length > 0) {
            accountName = trimLeadingCharacter(accountName, "@")
            return instanceUrl + "/@" + accountName + "/" + statusId
        }

        return instanceUrl + "/explore"
    }

    function authorizeInteractionUrl(modelData) {
        var targetUrl = statusUrl(modelData)
        if (targetUrl.length === 0) {
            return targetUrl
        }

        var instanceUrl = modelData && modelData.instanceUrl ? modelData.instanceUrl.toString() : ""
        if (instanceUrl.length === 0) {
            return targetUrl
        }
        instanceUrl = stripTrailingSlashes(instanceUrl)

        // Links on the user's own instance should open directly.
        var sameServer = /^([a-z][a-z0-9+.-]*):\/\/([^\/?#]+)/i
        var targetMatch = targetUrl.match(sameServer)
        var instanceMatch = instanceUrl.match(sameServer)
        if (targetMatch && instanceMatch
                && targetMatch.length > 2
                && instanceMatch.length > 2
                && targetMatch[1].toLowerCase() === instanceMatch[1].toLowerCase()
                && targetMatch[2].toLowerCase() === instanceMatch[2].toLowerCase()) {
            return targetUrl
        }

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