Move ImageUploader into a directory, refactor out UploadedImage

and all its template implementations
This commit is contained in:
Mm2PL 2023-12-02 00:43:04 +01:00
parent d7a888d651
commit 6e8af5e3b3
No known key found for this signature in database
GPG key ID: 94AC9B80EFA15ED9
6 changed files with 130 additions and 9 deletions

View file

@ -12,7 +12,7 @@
#include "controllers/notifications/NotificationController.hpp"
#include "controllers/sound/ISoundController.hpp"
#include "providers/seventv/SeventvAPI.hpp"
#include "singletons/ImageUploader.hpp"
#include "singletons/imageuploader/ImageUploader.hpp"
#ifdef CHATTERINO_HAVE_PLUGINS
# include "controllers/plugins/PluginController.hpp"
#endif

View file

@ -429,8 +429,9 @@ set(SOURCE_FILES
singletons/Emotes.hpp
singletons/Fonts.cpp
singletons/Fonts.hpp
singletons/ImageUploader.cpp
singletons/ImageUploader.hpp
singletons/imageuploader/ImageUploader.cpp
singletons/imageuploader/ImageUploader.hpp
singletons/imageuploader/UploadedImage.hpp
singletons/Logging.cpp
singletons/Logging.hpp
singletons/NativeMessaging.cpp

View file

@ -1,4 +1,4 @@
#include "singletons/ImageUploader.hpp"
#include "singletons/imageuploader/ImageUploader.hpp"
#include "common/Env.hpp"
#include "common/NetworkRequest.hpp"
@ -6,6 +6,7 @@
#include "common/QLogging.hpp"
#include "messages/MessageBuilder.hpp"
#include "providers/twitch/TwitchMessageBuilder.hpp"
#include "singletons/imageuploader/UploadedImageModel.hpp"
#include "singletons/Paths.hpp"
#include "singletons/Settings.hpp"
#include "util/CombinePath.hpp"
@ -16,9 +17,10 @@
#include <QHttpMultiPart>
#include <QJsonArray>
#include <QJsonDocument>
#include <qloggingcategory.h>
#include <QLoggingCategory>
#include <QMimeDatabase>
#include <QMutex>
#include <QObject>
#include <QPointer>
#include <QSaveFile>
#include <rapidjson/document.h>
@ -53,7 +55,7 @@ void ImageUploader::logToFile(const QString &originalFilePath,
const QString &imageLink,
const QString &deletionLink, ChannelPtr channel)
{
this->imageLogSetting_->push_back(UploadedImage{
this->images_.append(UploadedImage{
.channelName = channel->getName(),
.deletionLink = deletionLink,
.imageLink = imageLink,
@ -91,9 +93,17 @@ QString getLinkFromResponse(NetworkResult response, QString pattern)
void ImageUploader::save()
{
this->uploadedImagesSetting_->setValue(this->images_.raw());
this->sm_->save();
}
UploadedImageModel *ImageUploader::createModel(QObject *parent)
{
auto *model = new UploadedImageModel(parent);
model->initialize(&this->images_);
return model;
}
void ImageUploader::initialize(Settings &settings, Paths &paths)
{
auto logPath = (getSettings()->logPath.getValue().isEmpty()
@ -108,10 +118,19 @@ void ImageUploader::initialize(Settings &settings, Paths &paths)
this->sm_->setBackupEnabled(true);
this->sm_->setBackupSlots(9);
this->imageLogSetting_ = std::make_unique<
this->uploadedImagesSetting_ = std::make_unique<
pajlada::Settings::Setting<std::vector<UploadedImage>>>(
"/uploadedImages", this->sm_);
for (const auto &item : this->uploadedImagesSetting_->getValue())
{
this->images_.append(item);
}
this->signals_.addConnection(
this->images_.delayedItemsChanged.connect([this]() {
this->uploadedImagesSetting_->setValue(this->images_.raw());
}));
// try to read old log
QFile oldLogFile(oldLogName);
bool isOldLogFileOkay =
@ -139,7 +158,10 @@ void ImageUploader::initialize(Settings &settings, Paths &paths)
<< "Unable to parse ImageUploader.json: getSafe failed";
return;
}
this->imageLogSetting_->setValue(temporary);
for (const auto &t : temporary)
{
this->images_.append(t);
}
oldLogFile.close();
oldLogFile.rename(combinePath(logPath, "ImageUploader.old.json"));
this->sm_->save();

View file

@ -0,0 +1,98 @@
#pragma once
#include "util/RapidjsonHelpers.hpp"
#include <QString>
#include <Qt>
#include <cstdint>
namespace chatterino {
struct UploadedImage {
QString channelName;
QString deletionLink;
QString imageLink;
QString localPath;
int64_t timestamp{};
bool deleted = false;
};
} // namespace chatterino
namespace pajlada {
template <>
struct Serialize<chatterino::UploadedImage> {
static rapidjson::Value get(const chatterino::UploadedImage &value,
rapidjson::Document::AllocatorType &a)
{
rapidjson::Value ret(rapidjson::kObjectType);
chatterino::rj::set(ret, "channelName", value.channelName, a);
chatterino::rj::set(ret, "imageLink", value.imageLink, a);
chatterino::rj::set(ret, "timestamp", value.timestamp, a);
chatterino::rj::set(ret, "localPath", value.localPath, a);
chatterino::rj::set(ret, "deletionLink", value.deletionLink, a);
if (value.deleted)
{
chatterino::rj::set(ret, "deleted", value.deleted, a);
}
return ret;
}
};
template <>
struct Deserialize<chatterino::UploadedImage> {
static chatterino::UploadedImage get(const rapidjson::Value &value,
bool *error = nullptr)
{
chatterino::UploadedImage img;
if (!value.IsObject())
{
PAJLADA_REPORT_ERROR(error);
return img;
}
if (value["localPath"].IsNull())
{
img.localPath = QString();
}
else if (!chatterino::rj::getSafe(value, "localPath", img.localPath))
{
PAJLADA_REPORT_ERROR(error);
return img;
}
if (!chatterino::rj::getSafe(value, "imageLink", img.imageLink))
{
PAJLADA_REPORT_ERROR(error);
return img;
}
if (value["deletionLink"].IsNull())
{
img.deletionLink = QString();
}
else if (!chatterino::rj::getSafe(value, "deletionLink",
img.deletionLink))
{
PAJLADA_REPORT_ERROR(error);
return img;
}
if (!chatterino::rj::getSafe(value, "channelName", img.channelName))
{
PAJLADA_REPORT_ERROR(error);
return img;
}
if (!chatterino::rj::getSafe(value, "timestamp", img.timestamp))
{
PAJLADA_REPORT_ERROR(error);
return img;
}
if (!chatterino::rj::getSafe(value, "deleted", img.deleted))
{
img.deleted = false;
return img;
}
return img;
}
};
} // namespace pajlada

View file

@ -16,7 +16,7 @@
#include "providers/twitch/TwitchIrcServer.hpp"
#include "providers/twitch/TwitchMessageBuilder.hpp"
#include "singletons/Fonts.hpp"
#include "singletons/ImageUploader.hpp"
#include "singletons/imageuploader/ImageUploader.hpp"
#include "singletons/Settings.hpp"
#include "singletons/Theme.hpp"
#include "singletons/WindowManager.hpp"