Skip to content

Commit

Permalink
Refactored the Image Uploader feature. (#4971)
Browse files Browse the repository at this point in the history
Co-authored-by: Rasmus Karlsson <rasmus.karlsson@pajlada.com>
  • Loading branch information
Mm2PL and pajlada authored Nov 19, 2023
1 parent 7898b97 commit fbc8aac
Show file tree
Hide file tree
Showing 13 changed files with 264 additions and 137 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
- Bugfix: Fixed a freeze from a bad regex in _Ignores_. (#4965)
- Bugfix: Fixed some emotes not appearing when using _Ignores_. (#4965)
- Bugfix: Fixed lookahead/-behind not working in _Ignores_. (#4965)
- Bugfix: Fixed Image Uploader accidentally deleting images with some hosts when link resolver was enabled. (#4971)
- Bugfix: Fixed rare crash with Image Uploader when closing a split right after starting an upload. (#4971)
- Dev: Change clang-format from v14 to v16. (#4929)
- Dev: Fixed UTF16 encoding of `modes` file for the installer. (#4791)
- Dev: Temporarily disable High DPI scaling on Qt6 builds on Windows. (#4767)
Expand Down Expand Up @@ -66,6 +68,7 @@
- Dev: `Details` file properties tab is now populated on Windows. (#4912)
- Dev: Removed `Outcome` from network requests. (#4959)
- Dev: Added Tests for Windows and MacOS in CI. (#4970)
- Dev: Refactored the Image Uploader feature. (#4971)

## 2.4.6

Expand Down
5 changes: 5 additions & 0 deletions mocks/include/mocks/EmptyApplication.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ class EmptyApplication : public IApplication
{
return nullptr;
}

ImageUploader *getImageUploader() override
{
return nullptr;
}
};

} // namespace chatterino::mock
2 changes: 2 additions & 0 deletions src/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "controllers/hotkeys/HotkeyController.hpp"
#include "controllers/ignores/IgnoreController.hpp"
#include "controllers/notifications/NotificationController.hpp"
#include "singletons/ImageUploader.hpp"
#ifdef CHATTERINO_HAVE_PLUGINS
# include "controllers/plugins/PluginController.hpp"
#endif
Expand Down Expand Up @@ -79,6 +80,7 @@ Application::Application(Settings &_settings, Paths &_paths)
, hotkeys(&this->emplace<HotkeyController>())
, windows(&this->emplace<WindowManager>())
, toasts(&this->emplace<Toasts>())
, imageUploader(&this->emplace<ImageUploader>())

, commands(&this->emplace<CommandController>())
, notifications(&this->emplace<NotificationController>())
Expand Down
7 changes: 7 additions & 0 deletions src/Application.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class Toasts;
class ChatterinoBadges;
class FfzBadges;
class SeventvBadges;
class ImageUploader;

class IApplication
{
Expand All @@ -66,6 +67,7 @@ class IApplication
virtual SeventvBadges *getSeventvBadges() = 0;
virtual IUserDataController *getUserData() = 0;
virtual ITwitchLiveController *getTwitchLiveController() = 0;
virtual ImageUploader *getImageUploader() = 0;
};

class Application : public IApplication
Expand Down Expand Up @@ -94,6 +96,7 @@ class Application : public IApplication
HotkeyController *const hotkeys{};
WindowManager *const windows{};
Toasts *const toasts{};
ImageUploader *const imageUploader{};

CommandController *const commands{};
NotificationController *const notifications{};
Expand Down Expand Up @@ -167,6 +170,10 @@ class Application : public IApplication
}
IUserDataController *getUserData() override;
ITwitchLiveController *getTwitchLiveController() override;
ImageUploader *getImageUploader() override
{
return this->imageUploader;
}

pajlada::Signals::NoArgSignal streamerModeChanged;

Expand Down
4 changes: 2 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,8 @@ set(SOURCE_FILES
singletons/Emotes.hpp
singletons/Fonts.cpp
singletons/Fonts.hpp
singletons/ImageUploader.cpp
singletons/ImageUploader.hpp
singletons/Logging.cpp
singletons/Logging.hpp
singletons/NativeMessaging.cpp
Expand Down Expand Up @@ -475,8 +477,6 @@ set(SOURCE_FILES
util/IpcQueue.hpp
util/LayoutHelper.cpp
util/LayoutHelper.hpp
util/NuulsUploader.cpp
util/NuulsUploader.hpp
util/RapidjsonHelpers.cpp
util/RapidjsonHelpers.hpp
util/RatelimitBucket.cpp
Expand Down
2 changes: 1 addition & 1 deletion src/common/QLogging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Q_LOGGING_CATEGORY(chatterinoNativeMessage, "chatterino.nativemessage",
Q_LOGGING_CATEGORY(chatterinoNetwork, "chatterino.network", logThreshold);
Q_LOGGING_CATEGORY(chatterinoNotification, "chatterino.notification",
logThreshold);
Q_LOGGING_CATEGORY(chatterinoNuulsuploader, "chatterino.nuulsuploader",
Q_LOGGING_CATEGORY(chatterinoImageuploader, "chatterino.imageuploader",
logThreshold);
Q_LOGGING_CATEGORY(chatterinoPubSub, "chatterino.pubsub", logThreshold);
Q_LOGGING_CATEGORY(chatterinoRecentMessages, "chatterino.recentmessages",
Expand Down
2 changes: 1 addition & 1 deletion src/common/QLogging.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Q_DECLARE_LOGGING_CATEGORY(chatterinoMessage);
Q_DECLARE_LOGGING_CATEGORY(chatterinoNativeMessage);
Q_DECLARE_LOGGING_CATEGORY(chatterinoNetwork);
Q_DECLARE_LOGGING_CATEGORY(chatterinoNotification);
Q_DECLARE_LOGGING_CATEGORY(chatterinoNuulsuploader);
Q_DECLARE_LOGGING_CATEGORY(chatterinoImageuploader);
Q_DECLARE_LOGGING_CATEGORY(chatterinoPubSub);
Q_DECLARE_LOGGING_CATEGORY(chatterinoRecentMessages);
Q_DECLARE_LOGGING_CATEGORY(chatterinoSettings);
Expand Down
53 changes: 53 additions & 0 deletions src/messages/MessageBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "controllers/accounts/AccountController.hpp"
#include "messages/Image.hpp"
#include "messages/Message.hpp"
#include "messages/MessageColor.hpp"
#include "messages/MessageElement.hpp"
#include "providers/LinkResolver.hpp"
#include "providers/twitch/PubSubActions.hpp"
Expand Down Expand Up @@ -660,6 +661,58 @@ MessageBuilder::MessageBuilder(LiveUpdatesUpdateEmoteSetMessageTag /*unused*/,
this->message().flags.set(MessageFlag::DoNotTriggerNotification);
}

MessageBuilder::MessageBuilder(ImageUploaderResultTag /*unused*/,
const QString &imageLink,
const QString &deletionLink,
size_t imagesStillQueued, size_t secondsLeft)
: MessageBuilder()
{
this->message().flags.set(MessageFlag::System);
this->message().flags.set(MessageFlag::DoNotTriggerNotification);

this->emplace<TimestampElement>();

using MEF = MessageElementFlag;
auto addText = [this](QString text, MessageElementFlags mefs = MEF::Text,
MessageColor color =
MessageColor::System) -> TextElement * {
this->message().searchText += text;
this->message().messageText += text;
return this->emplace<TextElement>(text, mefs, color);
};

addText("Your image has been uploaded to");

// ASSUMPTION: the user gave this uploader configuration to the program
// therefore they trust that the host is not wrong/malicious. This doesn't obey getSettings()->lowercaseDomains.
// This also ensures that the LinkResolver doesn't get these links.
addText(imageLink, {MEF::OriginalLink, MEF::LowercaseLink},
MessageColor::Link)
->setLink({Link::Url, imageLink})
->setTrailingSpace(false);

if (!deletionLink.isEmpty())
{
addText("(Deletion link:");
addText(deletionLink, {MEF::OriginalLink, MEF::LowercaseLink},
MessageColor::Link)
->setLink({Link::Url, deletionLink})
->setTrailingSpace(false);
addText(")")->setTrailingSpace(false);
}
addText(".");

if (imagesStillQueued == 0)
{
return;
}

addText(QString("%1 left. Please wait until all of them are uploaded. "
"About %2 seconds left.")
.arg(imagesStillQueued)
.arg(secondsLeft));
}

Message *MessageBuilder::operator->()
{
return this->message_.get();
Expand Down
17 changes: 17 additions & 0 deletions src/messages/MessageBuilder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,20 @@ struct LiveUpdatesAddEmoteMessageTag {
};
struct LiveUpdatesUpdateEmoteSetMessageTag {
};
struct ImageUploaderResultTag {
};

const SystemMessageTag systemMessage{};
const TimeoutMessageTag timeoutMessage{};
const LiveUpdatesUpdateEmoteMessageTag liveUpdatesUpdateEmoteMessage{};
const LiveUpdatesRemoveEmoteMessageTag liveUpdatesRemoveEmoteMessage{};
const LiveUpdatesAddEmoteMessageTag liveUpdatesAddEmoteMessage{};
const LiveUpdatesUpdateEmoteSetMessageTag liveUpdatesUpdateEmoteSetMessage{};

// This signifies that you want to construct a message containing the result of
// a successful image upload.
const ImageUploaderResultTag imageUploaderResultMessage{};

MessagePtr makeSystemMessage(const QString &text);
MessagePtr makeSystemMessage(const QString &text, const QTime &time);
std::pair<MessagePtr, MessagePtr> makeAutomodMessage(
Expand Down Expand Up @@ -88,6 +95,16 @@ class MessageBuilder
MessageBuilder(LiveUpdatesUpdateEmoteSetMessageTag, const QString &platform,
const QString &actor, const QString &emoteSetName);

/**
* "Your image has been uploaded to %1[ (Deletion link: %2)]."
* or "Your image has been uploaded to %1 %2. %3 left. "
* "Please wait until all of them are uploaded. "
* "About %4 seconds left."
*/
MessageBuilder(ImageUploaderResultTag, const QString &imageLink,
const QString &deletionLink, size_t imagesStillQueued = 0,
size_t secondsLeft = 0);

virtual ~MessageBuilder() = default;

Message *operator->();
Expand Down
Loading

0 comments on commit fbc8aac

Please sign in to comment.