File indexing completed on 2024-05-19 16:01:04

0001 // SPDX-FileCopyrightText: 2021 kaniini <https://git.pleroma.social/kaniini>
0002 // SPDX-FileCopyrightText: 2021 Carl Schwan <carl@carlschwan.eu>
0003 // SPDX-License-Identifier: GPL-3.0-only
0004 
0005 #include "attachmenteditormodel.h"
0006 #include "account/abstractaccount.h"
0007 #include "account/account.h"
0008 #include <QTimer>
0009 
0010 AttachmentEditorModel::AttachmentEditorModel(QObject *parent, AbstractAccount *account)
0011     : QAbstractListModel(parent)
0012     , m_account(account)
0013 {
0014 }
0015 
0016 int AttachmentEditorModel::rowCount(const QModelIndex &parent) const
0017 {
0018     Q_UNUSED(parent);
0019     return m_attachments.size();
0020 }
0021 
0022 int AttachmentEditorModel::count() const
0023 {
0024     return rowCount({});
0025 }
0026 
0027 QVariant AttachmentEditorModel::data(const QModelIndex &index, int role) const
0028 {
0029     if (!index.isValid()) {
0030         return {};
0031     }
0032     const int row = index.row();
0033     const auto &attachment = m_attachments[row];
0034 
0035     switch (role) {
0036     case PreviewRole:
0037         return attachment->m_preview_url;
0038     case DescriptionRole:
0039         return attachment->description();
0040     }
0041 
0042     return {};
0043 }
0044 
0045 QHash<int, QByteArray> AttachmentEditorModel::roleNames() const
0046 {
0047     return {
0048         {PreviewRole, QByteArrayLiteral("preview")},
0049         {DescriptionRole, QByteArrayLiteral("description")},
0050     };
0051 }
0052 
0053 QNetworkReply *AttachmentEditorModel::append(const QUrl &filename)
0054 {
0055     if (rowCount({}) >= 4) {
0056         return nullptr;
0057     }
0058     return m_account->upload(filename, [=](QNetworkReply *reply) {
0059         const auto doc = QJsonDocument::fromJson(reply->readAll());
0060 
0061         if (!doc.isObject()) {
0062             return;
0063         }
0064 
0065         beginInsertRows({}, m_attachments.count(), m_attachments.count());
0066         m_attachments.append(new Attachment{doc.object(), this});
0067         endInsertRows();
0068         Q_EMIT countChanged();
0069     });
0070 }
0071 
0072 void AttachmentEditorModel::appendExisting(Attachment *attachment)
0073 {
0074     beginInsertRows({}, m_attachments.count(), m_attachments.count());
0075     m_attachments.append(attachment);
0076     endInsertRows();
0077     Q_EMIT countChanged();
0078 }
0079 
0080 void AttachmentEditorModel::removeAttachment(int row)
0081 {
0082     beginRemoveRows({}, row, row);
0083     m_attachments.removeAt(row);
0084     endRemoveRows();
0085     Q_EMIT countChanged();
0086 }
0087 
0088 void AttachmentEditorModel::setDescription(int row, const QString &description)
0089 {
0090     auto &attachment = m_attachments[row];
0091     const auto id = attachment->id();
0092     attachment->setDescription(description);
0093 
0094     const auto attachementUrl = m_account->apiUrl(QStringLiteral("/api/v1/media/%1").arg(id));
0095     const QJsonObject obj{
0096         {"description", description},
0097     };
0098     const QJsonDocument doc(obj);
0099     m_account->put(attachementUrl, doc, true, this, nullptr);
0100     Q_EMIT dataChanged(index(row, 0), index(row, 0), {DescriptionRole});
0101 }
0102 
0103 const QVector<Attachment *> &AttachmentEditorModel::attachments() const
0104 {
0105     return m_attachments;
0106 }