summaryrefslogtreecommitdiff
path: root/buteo-plugins/buteo-sync-plugin-mastodon-posts/mastodonpostssyncadaptor.cpp
blob: deddb0a4637ad60553cfb4597bf31b63afa2a588 (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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/****************************************************************************
 **
 ** Copyright (C) 2013-2026 Jolla Ltd.
 **
 ** This program/library is free software; you can redistribute it and/or
 ** modify it under the terms of the GNU Lesser General Public License
 ** version 2.1 as published by the Free Software Foundation.
 **
 ** This program/library is distributed in the hope that it will be useful,
 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 ** Lesser General Public License for more details.
 **
 ** You should have received a copy of the GNU Lesser General Public
 ** License along with this program/library; if not, write to the Free
 ** Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 ** 02110-1301 USA
 **
 ****************************************************************************/

#include "mastodonpostssyncadaptor.h"
#include "trace.h"

#include <QtCore/QJsonArray>
#include <QtCore/QJsonObject>
#include <QtCore/QJsonValue>
#include <QtCore/QRegularExpression>
#include <QtCore/QUrl>
#include <QtCore/QUrlQuery>
#include <QtNetwork/QNetworkRequest>

namespace {
    QString decodeHtmlEntities(QString text)
    {
        text.replace(QStringLiteral("&quot;"), QStringLiteral("\""));
        text.replace(QStringLiteral("&apos;"), QStringLiteral("'"));
        text.replace(QStringLiteral("&lt;"), QStringLiteral("<"));
        text.replace(QStringLiteral("&gt;"), QStringLiteral(">"));
        text.replace(QStringLiteral("&amp;"), QStringLiteral("&"));
        text.replace(QStringLiteral("&nbsp;"), QStringLiteral(" "));

        static const QRegularExpression decimalEntity(QStringLiteral("&#(\\d+);"));
        QRegularExpressionMatch match;
        int index = 0;
        while ((index = text.indexOf(decimalEntity, index, &match)) != -1) {
            const uint value = match.captured(1).toUInt();
            const QString replacement = value > 0 ? QString(QChar(value)) : QString();
            text.replace(index, match.capturedLength(0), replacement);
            index += replacement.size();
        }

        static const QRegularExpression hexEntity(QStringLiteral("&#x([0-9a-fA-F]+);"));
        index = 0;
        while ((index = text.indexOf(hexEntity, index, &match)) != -1) {
            bool ok = false;
            const uint value = match.captured(1).toUInt(&ok, 16);
            const QString replacement = ok && value > 0 ? QString(QChar(value)) : QString();
            text.replace(index, match.capturedLength(0), replacement);
            index += replacement.size();
        }

        return text;
    }

    QString displayNameForAccount(const QJsonObject &account)
    {
        const QString displayName = account.value(QStringLiteral("display_name")).toString().trimmed();
        if (!displayName.isEmpty()) {
            return displayName;
        }

        const QString username = account.value(QStringLiteral("username")).toString().trimmed();
        if (!username.isEmpty()) {
            return username;
        }

        return account.value(QStringLiteral("acct")).toString().trimmed();
    }
}

MastodonPostsSyncAdaptor::MastodonPostsSyncAdaptor(QObject *parent)
    : MastodonDataTypeSyncAdaptor(SocialNetworkSyncAdaptor::Posts, parent)
{
    setInitialActive(m_db.isValid());
}

MastodonPostsSyncAdaptor::~MastodonPostsSyncAdaptor()
{
}

QString MastodonPostsSyncAdaptor::syncServiceName() const
{
    return QStringLiteral("mastodon-microblog");
}

void MastodonPostsSyncAdaptor::purgeDataForOldAccount(int oldId, SocialNetworkSyncAdaptor::PurgeMode)
{
    m_db.removePosts(oldId);
    m_db.commit();
    m_db.wait();
    m_db.refresh();
    m_db.wait();

    purgeCachedImages(&m_imageCacheDb, oldId);
}

void MastodonPostsSyncAdaptor::beginSync(int accountId, const QString &accessToken)
{
    requestPosts(accountId, accessToken);
}

void MastodonPostsSyncAdaptor::finalize(int accountId)
{
    if (syncAborted()) {
        qCInfo(lcSocialPlugin) << "sync aborted, won't commit database changes";
    } else {
        m_db.commit();
        m_db.wait();
        m_db.refresh();
        m_db.wait();
        purgeExpiredImages(&m_imageCacheDb, accountId);
    }
}

QString MastodonPostsSyncAdaptor::sanitizeContent(const QString &content)
{
    QString plain = content;
    plain.replace(QRegularExpression(QStringLiteral("<\\s*br\\s*/?\\s*>"), QRegularExpression::CaseInsensitiveOption), QStringLiteral("\n"));
    plain.replace(QRegularExpression(QStringLiteral("<\\s*/\\s*p\\s*>"), QRegularExpression::CaseInsensitiveOption), QStringLiteral("\n"));
    plain.remove(QRegularExpression(QStringLiteral("<[^>]+>"), QRegularExpression::CaseInsensitiveOption));

    return decodeHtmlEntities(plain).trimmed();
}

QDateTime MastodonPostsSyncAdaptor::parseTimestamp(const QString &timestampString)
{
    QDateTime timestamp;

#if QT_VERSION >= QT_VERSION_CHECK(5, 8, 0)
    timestamp = QDateTime::fromString(timestampString, Qt::ISODateWithMs);
    if (timestamp.isValid()) {
        return timestamp;
    }
#endif

    timestamp = QDateTime::fromString(timestampString, Qt::ISODate);
    if (timestamp.isValid()) {
        return timestamp;
    }

    // Qt 5.6 cannot parse ISO-8601 timestamps with fractional seconds.
    const int timeSeparator = timestampString.indexOf(QLatin1Char('T'));
    const int fractionSeparator = timestampString.indexOf(QLatin1Char('.'), timeSeparator + 1);
    if (timeSeparator > -1 && fractionSeparator > -1) {
        int timezoneSeparator = timestampString.indexOf(QLatin1Char('Z'), fractionSeparator + 1);
        if (timezoneSeparator == -1) {
            timezoneSeparator = timestampString.indexOf(QLatin1Char('+'), fractionSeparator + 1);
        }
        if (timezoneSeparator == -1) {
            timezoneSeparator = timestampString.indexOf(QLatin1Char('-'), fractionSeparator + 1);
        }

        QString stripped = timestampString;
        if (timezoneSeparator > -1) {
            stripped.remove(fractionSeparator, timezoneSeparator - fractionSeparator);
        } else {
            stripped.truncate(fractionSeparator);
        }

        timestamp = QDateTime::fromString(stripped, Qt::ISODate);
    }

    return timestamp;
}

void MastodonPostsSyncAdaptor::requestPosts(int accountId, const QString &accessToken)
{
    QUrl url(apiHost(accountId) + QStringLiteral("/api/v1/timelines/home"));

    QUrlQuery query(url);
    query.addQueryItem(QStringLiteral("limit"), QStringLiteral("20"));
    url.setQuery(query);

    QNetworkRequest request(url);
    request.setRawHeader("Authorization", QStringLiteral("Bearer %1").arg(accessToken).toUtf8());

    QNetworkReply *reply = m_networkAccessManager->get(request);
    if (reply) {
        reply->setProperty("accountId", accountId);
        connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(errorHandler(QNetworkReply::NetworkError)));
        connect(reply, SIGNAL(sslErrors(QList<QSslError>)), this, SLOT(sslErrorsHandler(QList<QSslError>)));
        connect(reply, SIGNAL(finished()), this, SLOT(finishedPostsHandler()));

        incrementSemaphore(accountId);
        setupReplyTimeout(accountId, reply);
    } else {
        qCWarning(lcSocialPlugin) << "unable to request home timeline posts from Mastodon account with id" << accountId;
    }
}

void MastodonPostsSyncAdaptor::finishedPostsHandler()
{
    QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());
    if (!reply) {
        return;
    }

    const bool isError = reply->property("isError").toBool();
    const int accountId = reply->property("accountId").toInt();
    QByteArray replyData = reply->readAll();

    disconnect(reply);
    reply->deleteLater();
    removeReplyTimeout(accountId, reply);

    bool ok = false;
    QJsonArray statuses = parseJsonArrayReplyData(replyData, &ok);
    if (!isError && ok) {
        if (!statuses.size()) {
            qCDebug(lcSocialPlugin) << "no feed posts received for account" << accountId;
            decrementSemaphore(accountId);
            return;
        }

        m_db.removePosts(accountId);

        const int sinceSpan = m_accountSyncProfile
                ? m_accountSyncProfile->key(Buteo::KEY_SYNC_SINCE_DAYS_PAST, QStringLiteral("7")).toInt()
                : 7;

        foreach (const QJsonValue &statusValue, statuses) {
            const QJsonObject statusObject = statusValue.toObject();
            if (statusObject.isEmpty()) {
                continue;
            }

            QJsonObject postObject = statusObject;
            QString boostedBy;
            if (statusObject.contains(QStringLiteral("reblog"))
                    && statusObject.value(QStringLiteral("reblog")).isObject()
                    && !statusObject.value(QStringLiteral("reblog")).isNull()) {
                boostedBy = displayNameForAccount(statusObject.value(QStringLiteral("account")).toObject());
                postObject = statusObject.value(QStringLiteral("reblog")).toObject();
            }

            QDateTime eventTimestamp = parseTimestamp(statusObject.value(QStringLiteral("created_at")).toString());
            if (!eventTimestamp.isValid()) {
                eventTimestamp = parseTimestamp(postObject.value(QStringLiteral("created_at")).toString());
            }
            if (!eventTimestamp.isValid()) {
                continue;
            }

            if (eventTimestamp.daysTo(QDateTime::currentDateTime()) > sinceSpan) {
                continue;
            }

            const QJsonObject account = postObject.value(QStringLiteral("account")).toObject();
            const QString displayName = displayNameForAccount(account);
            const QString accountName = account.value(QStringLiteral("acct")).toString();
            QString icon = account.value(QStringLiteral("avatar_static")).toString();
            if (icon.isEmpty()) {
                icon = account.value(QStringLiteral("avatar")).toString();
            }

            QString identifier = postObject.value(QStringLiteral("id")).toVariant().toString();
            if (identifier.isEmpty()) {
                continue;
            }

            QString url = postObject.value(QStringLiteral("url")).toString();
            if (url.isEmpty() && !accountName.isEmpty()) {
                url = QStringLiteral("%1/@%2/%3").arg(apiHost(accountId), accountName, identifier);
            }

            const QString body = sanitizeContent(postObject.value(QStringLiteral("content")).toString());
            const int repliesCount = postObject.value(QStringLiteral("replies_count")).toInt();
            const int favouritesCount = postObject.value(QStringLiteral("favourites_count")).toInt();
            const int reblogsCount = postObject.value(QStringLiteral("reblogs_count")).toInt();
            const bool favourited = postObject.value(QStringLiteral("favourited")).toBool();
            const bool reblogged = postObject.value(QStringLiteral("reblogged")).toBool();

            QList<QPair<QString, SocialPostImage::ImageType> > imageList;
            const QJsonArray mediaAttachments = postObject.value(QStringLiteral("media_attachments")).toArray();
            foreach (const QJsonValue &attachmentValue, mediaAttachments) {
                const QJsonObject attachment = attachmentValue.toObject();
                const QString mediaType = attachment.value(QStringLiteral("type")).toString();

                QString mediaUrl;
                SocialPostImage::ImageType imageType = SocialPostImage::Invalid;
                if (mediaType == QLatin1String("image")) {
                    mediaUrl = attachment.value(QStringLiteral("url")).toString();
                    imageType = SocialPostImage::Photo;
                } else if (mediaType == QLatin1String("video") || mediaType == QLatin1String("gifv")) {
                    mediaUrl = attachment.value(QStringLiteral("preview_url")).toString();
                    if (mediaUrl.isEmpty()) {
                        mediaUrl = attachment.value(QStringLiteral("url")).toString();
                    }
                    imageType = SocialPostImage::Video;
                }

                if (!mediaUrl.isEmpty() && imageType != SocialPostImage::Invalid) {
                    imageList.append(qMakePair(mediaUrl, imageType));
                }
            }

            m_db.addMastodonPost(identifier,
                                displayName,
                                accountName,
                                body,
                                eventTimestamp,
                                icon,
                                imageList,
                                url,
                                boostedBy,
                                repliesCount,
                                favouritesCount,
                                reblogsCount,
                                favourited,
                                reblogged,
                                apiHost(accountId),
                                accountId);
        }
    } else {
        qCWarning(lcSocialPlugin) << "unable to parse event feed data from request with account" << accountId
                                  << ", got:" << QString::fromUtf8(replyData);
    }

    decrementSemaphore(accountId);
}