summaryrefslogtreecommitdiff
path: root/eventsview-plugins/eventsview-plugin-fediverse/fediversepostsmodel.cpp
blob: b6d0aa3c99fc8aa56163c4af26776a07f7a25365 (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
/*
 * Copyright (C) 2013-2026 Jolla Ltd.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This 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 library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "fediversepostsmodel.h"

#include <QtCore/QVariantList>

namespace {

QVariantList imageListForPost(const SocialPost::ConstPtr &post)
{
    QVariantList images;
    if (!post) {
        return images;
    }

    const QList<SocialPostImage::ConstPtr> postImages = post->images();
    for (const SocialPostImage::ConstPtr &image : postImages) {
        if (!image) {
            continue;
        }

        QVariantMap imageMap;
        imageMap.insert(QStringLiteral("url"), image->url());
        imageMap.insert(QStringLiteral("type"), image->type() == SocialPostImage::Video
                                             ? QStringLiteral("video")
                                             : QStringLiteral("photo"));
        images.append(imageMap);
    }

    return images;
}

QVariantList accountListForPost(const SocialPost::ConstPtr &post)
{
    QVariantList accounts;
    if (!post) {
        return accounts;
    }

    const QList<int> postAccounts = post->accounts();
    for (int accountId : postAccounts) {
        accounts.append(accountId);
    }
    return accounts;
}

void appendCommonPostFields(QMap<int, QVariant> *eventMap,
                            const SocialPost::ConstPtr &post,
                            int idRole,
                            int nameRole,
                            int bodyRole,
                            int timestampRole,
                            int iconRole,
                            int imagesRole,
                            int accountsRole)
{
    if (!eventMap || !post) {
        return;
    }

    eventMap->insert(idRole, post->identifier());
    eventMap->insert(nameRole, post->name());
    eventMap->insert(bodyRole, post->body());
    eventMap->insert(timestampRole, post->timestamp());
    eventMap->insert(iconRole, post->icon());
    eventMap->insert(imagesRole, imageListForPost(post));
    eventMap->insert(accountsRole, accountListForPost(post));
}

}

FediversePostsModel::FediversePostsModel(QObject *parent)
    : QAbstractListModel(parent)
{
    connect(&m_database, &AbstractSocialPostCacheDatabase::postsChanged,
            this, &FediversePostsModel::postsChanged);
    connect(&m_database, SIGNAL(accountIdFilterChanged()),
            this, SIGNAL(accountIdFilterChanged()));
}

int FediversePostsModel::rowCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent)
    return m_data.count();
}

QVariant FediversePostsModel::data(const QModelIndex &index, int role) const
{
    const int row = index.row();
    if (!index.isValid() || row < 0 || row >= m_data.count()) {
        return QVariant();
    }

    return m_data.at(row).value(role);
}

QHash<int, QByteArray> FediversePostsModel::roleNames() const
{
    QHash<int, QByteArray> roleNames;
    roleNames.insert(FediverseId, "fediverseId");
    roleNames.insert(Name, "name");
    roleNames.insert(AccountName, "accountName");
    roleNames.insert(Acct, "acct");
    roleNames.insert(Body, "body");
    roleNames.insert(Timestamp, "timestamp");
    roleNames.insert(Icon, "icon");
    roleNames.insert(Images, "images");
    roleNames.insert(Url, "url");
    roleNames.insert(Link, "link");
    roleNames.insert(BoostedBy, "boostedBy");
    roleNames.insert(RebloggedBy, "rebloggedBy");
    roleNames.insert(RepliesCount, "repliesCount");
    roleNames.insert(FavouritesCount, "favouritesCount");
    roleNames.insert(ReblogsCount, "reblogsCount");
    roleNames.insert(Favourited, "favourited");
    roleNames.insert(Reblogged, "reblogged");
    roleNames.insert(InstanceUrl, "instanceUrl");
    roleNames.insert(InstanceIconPath, "instanceIconPath");
    roleNames.insert(Accounts, "accounts");
    return roleNames;
}

QVariantList FediversePostsModel::accountIdFilter() const
{
    return m_database.accountIdFilter();
}

void FediversePostsModel::setAccountIdFilter(const QVariantList &accountIds)
{
    m_database.setAccountIdFilter(accountIds);
}

void FediversePostsModel::refresh()
{
    m_database.refresh();
}

void FediversePostsModel::postsChanged()
{
    QList<RowData> data;
    QList<SocialPost::ConstPtr> postsData = m_database.posts();
    Q_FOREACH (const SocialPost::ConstPtr &post, postsData) {
        RowData eventMap;
        const QString accountName = m_database.accountName(post);
        const QString postUrl = m_database.url(post);
        const QString boostedBy = m_database.boostedBy(post);
        const int repliesCount = m_database.repliesCount(post);
        const int favouritesCount = m_database.favouritesCount(post);
        const int reblogsCount = m_database.reblogsCount(post);
        const bool favourited = m_database.favourited(post);
        const bool reblogged = m_database.reblogged(post);

        appendCommonPostFields(&eventMap, post,
                               FediversePostsModel::FediverseId,
                               FediversePostsModel::Name,
                               FediversePostsModel::Body,
                               FediversePostsModel::Timestamp,
                               FediversePostsModel::Icon,
                               FediversePostsModel::Images,
                               FediversePostsModel::Accounts);
        eventMap.insert(FediversePostsModel::AccountName, accountName);
        eventMap.insert(FediversePostsModel::Acct, accountName);
        eventMap.insert(FediversePostsModel::Url, postUrl);
        eventMap.insert(FediversePostsModel::Link, postUrl);
        eventMap.insert(FediversePostsModel::BoostedBy, boostedBy);
        eventMap.insert(FediversePostsModel::RebloggedBy, boostedBy);
        eventMap.insert(FediversePostsModel::RepliesCount, repliesCount);
        eventMap.insert(FediversePostsModel::FavouritesCount, favouritesCount);
        eventMap.insert(FediversePostsModel::ReblogsCount, reblogsCount);
        eventMap.insert(FediversePostsModel::Favourited, favourited);
        eventMap.insert(FediversePostsModel::Reblogged, reblogged);
        eventMap.insert(FediversePostsModel::InstanceUrl, m_database.instanceUrl(post));
        eventMap.insert(FediversePostsModel::InstanceIconPath, m_database.instanceIconPath(post));
        data.append(eventMap);
    }

    const int oldCount = m_data.count();
    beginResetModel();
    m_data = data;
    endResetModel();
    if (oldCount != m_data.count()) {
        emit countChanged();
    }
}