Merge branch 'master' of https://github.com/fourtf/chatterino2 into blacklist

This commit is contained in:
hemirt 2018-08-19 00:52:38 +02:00
commit ad711b4c15
164 changed files with 2948 additions and 2808 deletions

View file

@ -25,6 +25,9 @@ DerivePointerBinding: false
FixNamespaceComments: true FixNamespaceComments: true
IndentCaseLabels: true IndentCaseLabels: true
IndentWidth: 4 IndentWidth: 4
IndentWrappedFunctionNames: true
IndentPPDirectives: AfterHash
NamespaceIndentation: Inner
PointerBindsToType: false PointerBindsToType: false
SpacesBeforeTrailingComments: 2 SpacesBeforeTrailingComments: 2
Standard: Auto Standard: Auto

View file

@ -33,7 +33,7 @@ equals(QMAKE_CXX, "clang++")|equals(QMAKE_CXX, "g++") {
} }
# Icons # Icons
macx:ICON = resources/images/chatterino2.icns #macx:ICON = resources/images/chatterino2.icns
win32:RC_FILE = resources/windows.rc win32:RC_FILE = resources/windows.rc
@ -71,6 +71,8 @@ win32 {
# OSX include directory # OSX include directory
macx { macx {
INCLUDEPATH += /usr/local/include INCLUDEPATH += /usr/local/include
INCLUDEPATH += /usr/local/opt/openssl/include
LIBS += -L/usr/local/opt/openssl/lib
} }
# Optional dependency on Windows SDK 7 # Optional dependency on Windows SDK 7
@ -250,7 +252,8 @@ SOURCES += \
src/widgets/helper/EffectLabel.cpp \ src/widgets/helper/EffectLabel.cpp \
src/widgets/helper/Button.cpp \ src/widgets/helper/Button.cpp \
src/messages/MessageContainer.cpp \ src/messages/MessageContainer.cpp \
src/debug/Benchmark.cpp src/debug/Benchmark.cpp \
src/common/UsernameSet.cpp
HEADERS += \ HEADERS += \
src/Application.hpp \ src/Application.hpp \
@ -445,7 +448,8 @@ HEADERS += \
src/widgets/helper/EffectLabel.hpp \ src/widgets/helper/EffectLabel.hpp \
src/util/LayoutHelper.hpp \ src/util/LayoutHelper.hpp \
src/widgets/helper/Button.hpp \ src/widgets/helper/Button.hpp \
src/messages/MessageContainer.hpp src/messages/MessageContainer.hpp \
src/common/UsernameSet.hpp
RESOURCES += \ RESOURCES += \
resources/resources.qrc \ resources/resources.qrc \

View file

@ -6,11 +6,13 @@
#include "controllers/ignores/IgnoreController.hpp" #include "controllers/ignores/IgnoreController.hpp"
#include "controllers/moderationactions/ModerationActions.hpp" #include "controllers/moderationactions/ModerationActions.hpp"
#include "controllers/taggedusers/TaggedUsersController.hpp" #include "controllers/taggedusers/TaggedUsersController.hpp"
#include "debug/Log.hpp"
#include "messages/MessageBuilder.hpp" #include "messages/MessageBuilder.hpp"
#include "providers/bttv/BttvEmotes.hpp" #include "providers/bttv/BttvEmotes.hpp"
#include "providers/ffz/FfzEmotes.hpp" #include "providers/ffz/FfzEmotes.hpp"
#include "providers/twitch/PubsubClient.hpp" #include "providers/twitch/PubsubClient.hpp"
#include "providers/twitch/TwitchServer.hpp" #include "providers/twitch/TwitchServer.hpp"
#include "singletons/Emotes.hpp"
#include "singletons/Fonts.hpp" #include "singletons/Fonts.hpp"
#include "singletons/Logging.hpp" #include "singletons/Logging.hpp"
#include "singletons/NativeMessaging.hpp" #include "singletons/NativeMessaging.hpp"
@ -21,6 +23,7 @@
#include "singletons/WindowManager.hpp" #include "singletons/WindowManager.hpp"
#include "util/IsBigEndian.hpp" #include "util/IsBigEndian.hpp"
#include "util/PostToThread.hpp" #include "util/PostToThread.hpp"
#include "widgets/Window.hpp"
#include <atomic> #include <atomic>
@ -35,9 +38,7 @@ Application *Application::instance = nullptr;
// to each other // to each other
Application::Application(Settings &_settings, Paths &_paths) Application::Application(Settings &_settings, Paths &_paths)
: settings(&_settings) : resources(&this->emplace<Resources2>())
, paths(&_paths)
, resources(&this->emplace<Resources2>())
, themes(&this->emplace<Theme>()) , themes(&this->emplace<Theme>())
, fonts(&this->emplace<Fonts>()) , fonts(&this->emplace<Fonts>())
@ -98,15 +99,15 @@ void Application::save()
void Application::initNm() void Application::initNm()
{ {
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
#ifdef QT_DEBUG # ifdef QT_DEBUG
#ifdef C_DEBUG_NM # ifdef C_DEBUG_NM
this->nativeMessaging->registerHost(); this->nativeMessaging->registerHost();
this->nativeMessaging->openGuiMessageQueue(); this->nativeMessaging->openGuiMessageQueue();
#endif # endif
#else # else
this->nativeMessaging->registerHost(); this->nativeMessaging->registerHost();
this->nativeMessaging->openGuiMessageQueue(); this->nativeMessaging->openGuiMessageQueue();
#endif # endif
#endif #endif
} }

View file

@ -1,7 +1,6 @@
#pragma once #pragma once
#include "common/Singleton.hpp" #include "common/Singleton.hpp"
#include "singletons/Resources.hpp"
#include <QApplication> #include <QApplication>
#include <memory> #include <memory>
@ -26,7 +25,7 @@ class AccountManager;
class Emotes; class Emotes;
class Settings; class Settings;
class Fonts; class Fonts;
class Resources; class Resources2;
class Application class Application
{ {
@ -47,8 +46,6 @@ public:
friend void test(); friend void test();
Settings *const settings{};
Paths *const paths{};
Resources2 *const resources; Resources2 *const resources;
Theme *const themes{}; Theme *const themes{};

View file

@ -9,31 +9,31 @@
#include <memory> #include <memory>
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
#include <fcntl.h> # include <fcntl.h>
#include <io.h> # include <io.h>
#include <stdio.h> # include <stdio.h>
#endif #endif
namespace chatterino { namespace chatterino {
namespace { namespace {
void initFileMode() void initFileMode()
{ {
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
_setmode(_fileno(stdin), _O_BINARY); _setmode(_fileno(stdin), _O_BINARY);
_setmode(_fileno(stdout), _O_BINARY); _setmode(_fileno(stdout), _O_BINARY);
#endif #endif
} }
void runLoop(NativeMessagingClient &client) void runLoop(NativeMessagingClient &client)
{ {
while (true) { while (true) {
char size_c[4]; char size_c[4];
std::cin.read(size_c, 4); std::cin.read(size_c, 4);
if (std::cin.eof()) break; if (std::cin.eof()) break;
auto size = *reinterpret_cast<uint32_t *>(size_c); auto size = *reinterpret_cast<uint32_t *>(size_c);
#if 0 #if 0
bool bigEndian = isBigEndian(); bool bigEndian = isBigEndian();
@ -48,14 +48,14 @@ void runLoop(NativeMessagingClient &client)
} }
#endif #endif
std::unique_ptr<char[]> buffer(new char[size + 1]); std::unique_ptr<char[]> buffer(new char[size + 1]);
std::cin.read(buffer.get(), size); std::cin.read(buffer.get(), size);
*(buffer.get() + size) = '\0'; *(buffer.get() + size) = '\0';
client.sendMessage( client.sendMessage(QByteArray::fromRawData(
QByteArray::fromRawData(buffer.get(), static_cast<int32_t>(size))); buffer.get(), static_cast<int32_t>(size)));
}
} }
}
} // namespace } // namespace
bool shouldRunBrowserExtensionHost(const QStringList &args) bool shouldRunBrowserExtensionHost(const QStringList &args)

View file

@ -1,168 +1,168 @@
#ifdef __cplusplus #ifdef __cplusplus
#include <fmt/format.h> # include <fmt/format.h>
#include <irccommand.h> # include <irccommand.h>
#include <ircconnection.h> # include <ircconnection.h>
#include <rapidjson/document.h> # include <rapidjson/document.h>
#include <rapidjson/error/en.h> # include <rapidjson/error/en.h>
#include <rapidjson/error/error.h> # include <rapidjson/error/error.h>
#include <IrcMessage> # include <IrcMessage>
#include <QAbstractListModel> # include <QAbstractListModel>
#include <QAbstractNativeEventFilter> # include <QAbstractNativeEventFilter>
#include <QAction> # include <QAction>
#include <QApplication> # include <QApplication>
#include <QBrush> # include <QBrush>
#include <QBuffer> # include <QBuffer>
#include <QButtonGroup> # include <QButtonGroup>
#include <QByteArray> # include <QByteArray>
#include <QCheckBox> # include <QCheckBox>
#include <QClipboard> # include <QClipboard>
#include <QColor> # include <QColor>
#include <QComboBox> # include <QComboBox>
#include <QCompleter> # include <QCompleter>
#include <QCoreApplication> # include <QCoreApplication>
#include <QDateTime> # include <QDateTime>
#include <QDebug> # include <QDebug>
#include <QDesktopServices> # include <QDesktopServices>
#include <QDialog> # include <QDialog>
#include <QDialogButtonBox> # include <QDialogButtonBox>
#include <QDir> # include <QDir>
#include <QDockWidget> # include <QDockWidget>
#include <QDrag> # include <QDrag>
#include <QDragEnterEvent> # include <QDragEnterEvent>
#include <QElapsedTimer> # include <QElapsedTimer>
#include <QEventLoop> # include <QEventLoop>
#include <QFile> # include <QFile>
#include <QFileDialog> # include <QFileDialog>
#include <QFileInfo> # include <QFileInfo>
#include <QFlags> # include <QFlags>
#include <QFont> # include <QFont>
#include <QFontDatabase> # include <QFontDatabase>
#include <QFontDialog> # include <QFontDialog>
#include <QFontMetrics> # include <QFontMetrics>
#include <QFormLayout> # include <QFormLayout>
#include <QGraphicsBlurEffect> # include <QGraphicsBlurEffect>
#include <QGroupBox> # include <QGroupBox>
#include <QHBoxLayout> # include <QHBoxLayout>
#include <QHeaderView> # include <QHeaderView>
#include <QIcon> # include <QIcon>
#include <QImageReader> # include <QImageReader>
#include <QJsonArray> # include <QJsonArray>
#include <QJsonDocument> # include <QJsonDocument>
#include <QJsonObject> # include <QJsonObject>
#include <QJsonValue> # include <QJsonValue>
#include <QKeyEvent> # include <QKeyEvent>
#include <QLabel> # include <QLabel>
#include <QLayout> # include <QLayout>
#include <QLibrary> # include <QLibrary>
#include <QLineEdit> # include <QLineEdit>
#include <QList> # include <QList>
#include <QListView> # include <QListView>
#include <QListWidget> # include <QListWidget>
#include <QMap> # include <QMap>
#include <QMediaPlayer> # include <QMediaPlayer>
#include <QMenu> # include <QMenu>
#include <QMessageBox> # include <QMessageBox>
#include <QMimeData> # include <QMimeData>
#include <QMouseEvent> # include <QMouseEvent>
#include <QMutex> # include <QMutex>
#include <QMutexLocker> # include <QMutexLocker>
#include <QNetworkAccessManager> # include <QNetworkAccessManager>
#include <QNetworkReply> # include <QNetworkReply>
#include <QNetworkRequest> # include <QNetworkRequest>
#include <QObject> # include <QObject>
#include <QPaintEvent> # include <QPaintEvent>
#include <QPainter> # include <QPainter>
#include <QPainterPath> # include <QPainterPath>
#include <QPalette> # include <QPalette>
#include <QPixmap> # include <QPixmap>
#include <QPoint> # include <QPoint>
#include <QProcess> # include <QProcess>
#include <QPropertyAnimation> # include <QPropertyAnimation>
#include <QPushButton> # include <QPushButton>
#include <QRadialGradient> # include <QRadialGradient>
#include <QRect> # include <QRect>
#include <QRegularExpression> # include <QRegularExpression>
#include <QRunnable> # include <QRunnable>
#include <QScroller> # include <QScroller>
#include <QShortcut> # include <QShortcut>
#include <QSizePolicy> # include <QSizePolicy>
#include <QSlider> # include <QSlider>
#include <QStackedLayout> # include <QStackedLayout>
#include <QStandardPaths> # include <QStandardPaths>
#include <QString> # include <QString>
#include <QStyle> # include <QStyle>
#include <QStyleOption> # include <QStyleOption>
#include <QTabWidget> # include <QTabWidget>
#include <QTextEdit> # include <QTextEdit>
#include <QThread> # include <QThread>
#include <QThreadPool> # include <QThreadPool>
#include <QTime> # include <QTime>
#include <QTimer> # include <QTimer>
#include <QUrl> # include <QUrl>
#include <QUuid> # include <QUuid>
#include <QVBoxLayout> # include <QVBoxLayout>
#include <QVariant> # include <QVariant>
#include <QVector> # include <QVector>
#include <QWheelEvent> # include <QWheelEvent>
#include <QWidget> # include <QWidget>
#include <QtCore/QVariant> # include <QtCore/QVariant>
#include <QtGlobal> # include <QtGlobal>
#include <QtWidgets/QAction> # include <QtWidgets/QAction>
#include <QtWidgets/QApplication> # include <QtWidgets/QApplication>
#include <QtWidgets/QButtonGroup> # include <QtWidgets/QButtonGroup>
#include <QtWidgets/QDialog> # include <QtWidgets/QDialog>
#include <QtWidgets/QDialogButtonBox> # include <QtWidgets/QDialogButtonBox>
#include <QtWidgets/QFormLayout> # include <QtWidgets/QFormLayout>
#include <QtWidgets/QHBoxLayout> # include <QtWidgets/QHBoxLayout>
#include <QtWidgets/QHeaderView> # include <QtWidgets/QHeaderView>
#include <QtWidgets/QLabel> # include <QtWidgets/QLabel>
#include <QtWidgets/QLineEdit> # include <QtWidgets/QLineEdit>
#include <QtWidgets/QPushButton> # include <QtWidgets/QPushButton>
#include <QtWidgets/QTabWidget> # include <QtWidgets/QTabWidget>
#include <QtWidgets/QVBoxLayout> # include <QtWidgets/QVBoxLayout>
#include <algorithm> # include <algorithm>
#include <boost/current_function.hpp> # include <boost/current_function.hpp>
#include <boost/foreach.hpp> # include <boost/foreach.hpp>
#include <boost/noncopyable.hpp> # include <boost/noncopyable.hpp>
#include <boost/optional.hpp> # include <boost/optional.hpp>
#include <cassert> # include <cassert>
#include <chrono> # include <chrono>
#include <cinttypes> # include <cinttypes>
#include <climits> # include <climits>
#include <cmath> # include <cmath>
#include <cstdint> # include <cstdint>
#include <ctime> # include <ctime>
#include <functional> # include <functional>
#include <future> # include <future>
#include <list> # include <list>
#include <map> # include <map>
#include <memory> # include <memory>
#include <mutex> # include <mutex>
#include <pajlada/settings/serialize.hpp> # include <pajlada/settings/serialize.hpp>
#include <pajlada/settings/setting.hpp> # include <pajlada/settings/setting.hpp>
#include <pajlada/settings/settinglistener.hpp> # include <pajlada/settings/settinglistener.hpp>
#include <pajlada/signals/connection.hpp> # include <pajlada/signals/connection.hpp>
#include <pajlada/signals/signal.hpp> # include <pajlada/signals/signal.hpp>
#include <random> # include <random>
#include <set> # include <set>
#include <string> # include <string>
#include <thread> # include <thread>
#include <tuple> # include <tuple>
#include <type_traits> # include <type_traits>
#include <unordered_map> # include <unordered_map>
#include <unordered_set> # include <unordered_set>
#include <vector> # include <vector>
#ifndef UNUSED # ifndef UNUSED
#define UNUSED(x) (void)(x) # define UNUSED(x) (void)(x)
#endif # endif
#ifndef ATTR_UNUSED # ifndef ATTR_UNUSED
#ifdef Q_OS_WIN # ifdef Q_OS_WIN
#define ATTR_UNUSED # define ATTR_UNUSED
#else # else
#define ATTR_UNUSED __attribute__((unused)) # define ATTR_UNUSED __attribute__((unused))
#endif # endif
#endif # endif
#endif #endif

View file

@ -12,7 +12,7 @@
#include "widgets/dialogs/LastRunCrashDialog.hpp" #include "widgets/dialogs/LastRunCrashDialog.hpp"
#ifdef C_USE_BREAKPAD #ifdef C_USE_BREAKPAD
#include <QBreakpadHandler.h> # include <QBreakpadHandler.h>
#endif #endif
// void initQt(); // void initQt();
@ -23,80 +23,82 @@
namespace chatterino { namespace chatterino {
namespace { namespace {
void installCustomPalette() void installCustomPalette()
{ {
// borrowed from // borrowed from
// https://stackoverflow.com/questions/15035767/is-the-qt-5-dark-fusion-theme-available-for-windows // https://stackoverflow.com/questions/15035767/is-the-qt-5-dark-fusion-theme-available-for-windows
auto dark = qApp->palette(); auto dark = qApp->palette();
dark.setColor(QPalette::Window, QColor(22, 22, 22)); dark.setColor(QPalette::Window, QColor(22, 22, 22));
dark.setColor(QPalette::WindowText, Qt::white); dark.setColor(QPalette::WindowText, Qt::white);
dark.setColor(QPalette::Text, Qt::white); dark.setColor(QPalette::Text, Qt::white);
dark.setColor(QPalette::Disabled, QPalette::WindowText, dark.setColor(QPalette::Disabled, QPalette::WindowText,
QColor(127, 127, 127)); QColor(127, 127, 127));
dark.setColor(QPalette::Base, QColor("#333")); dark.setColor(QPalette::Base, QColor("#333"));
dark.setColor(QPalette::AlternateBase, QColor("#444")); dark.setColor(QPalette::AlternateBase, QColor("#444"));
dark.setColor(QPalette::ToolTipBase, Qt::white); dark.setColor(QPalette::ToolTipBase, Qt::white);
dark.setColor(QPalette::ToolTipText, Qt::white); dark.setColor(QPalette::ToolTipText, Qt::white);
dark.setColor(QPalette::Disabled, QPalette::Text, QColor(127, 127, 127)); dark.setColor(QPalette::Disabled, QPalette::Text,
dark.setColor(QPalette::Dark, QColor(35, 35, 35)); QColor(127, 127, 127));
dark.setColor(QPalette::Shadow, QColor(20, 20, 20)); dark.setColor(QPalette::Dark, QColor(35, 35, 35));
dark.setColor(QPalette::Button, QColor(70, 70, 70)); dark.setColor(QPalette::Shadow, QColor(20, 20, 20));
dark.setColor(QPalette::ButtonText, Qt::white); dark.setColor(QPalette::Button, QColor(70, 70, 70));
dark.setColor(QPalette::Disabled, QPalette::ButtonText, dark.setColor(QPalette::ButtonText, Qt::white);
QColor(127, 127, 127)); dark.setColor(QPalette::Disabled, QPalette::ButtonText,
dark.setColor(QPalette::BrightText, Qt::red); QColor(127, 127, 127));
dark.setColor(QPalette::Link, QColor(42, 130, 218)); dark.setColor(QPalette::BrightText, Qt::red);
dark.setColor(QPalette::Highlight, QColor(42, 130, 218)); dark.setColor(QPalette::Link, QColor(42, 130, 218));
dark.setColor(QPalette::Disabled, QPalette::Highlight, QColor(80, 80, 80)); dark.setColor(QPalette::Highlight, QColor(42, 130, 218));
dark.setColor(QPalette::HighlightedText, Qt::white); dark.setColor(QPalette::Disabled, QPalette::Highlight,
dark.setColor(QPalette::Disabled, QPalette::HighlightedText, QColor(80, 80, 80));
QColor(127, 127, 127)); dark.setColor(QPalette::HighlightedText, Qt::white);
dark.setColor(QPalette::Disabled, QPalette::HighlightedText,
QColor(127, 127, 127));
qApp->setPalette(dark); qApp->setPalette(dark);
}
void initQt()
{
// set up the QApplication flags
QApplication::setAttribute(Qt::AA_Use96Dpi, true);
#ifdef Q_OS_WIN32
QApplication::setAttribute(Qt::AA_DisableHighDpiScaling, true);
#endif
QApplication::setStyle(QStyleFactory::create("Fusion"));
installCustomPalette();
}
void showLastCrashDialog()
{
#ifndef C_DISABLE_CRASH_DIALOG
LastRunCrashDialog dialog;
switch (dialog.exec()) {
case QDialog::Accepted: {
}; break;
default: {
_exit(0);
}
} }
void initQt()
{
// set up the QApplication flags
QApplication::setAttribute(Qt::AA_Use96Dpi, true);
#ifdef Q_OS_WIN32
QApplication::setAttribute(Qt::AA_DisableHighDpiScaling, true);
#endif #endif
}
void createRunningFile(const QString &path) QApplication::setStyle(QStyleFactory::create("Fusion"));
{
QFile runningFile(path);
runningFile.open(QIODevice::WriteOnly | QIODevice::Truncate); installCustomPalette();
runningFile.flush(); }
runningFile.close();
}
void removeRunningFile(const QString &path) void showLastCrashDialog()
{ {
QFile::remove(path); #ifndef C_DISABLE_CRASH_DIALOG
} LastRunCrashDialog dialog;
switch (dialog.exec()) {
case QDialog::Accepted: {
}; break;
default: {
_exit(0);
}
}
#endif
}
void createRunningFile(const QString &path)
{
QFile runningFile(path);
runningFile.open(QIODevice::WriteOnly | QIODevice::Truncate);
runningFile.flush();
runningFile.close();
}
void removeRunningFile(const QString &path)
{
QFile::remove(path);
}
} // namespace } // namespace
void runGui(QApplication &a, Paths &paths, Settings &settings) void runGui(QApplication &a, Paths &paths, Settings &settings)
@ -107,7 +109,7 @@ void runGui(QApplication &a, Paths &paths, Settings &settings)
chatterino::Updates::getInstance().checkForUpdates(); chatterino::Updates::getInstance().checkForUpdates();
#ifdef C_USE_BREAKPAD #ifdef C_USE_BREAKPAD
QBreakpadInstance.setDumpPath(app->paths->settingsFolderPath + "/Crashes"); QBreakpadInstance.setDumpPath(getPaths()->settingsFolderPath + "/Crashes");
#endif #endif
// Running file // Running file

View file

@ -32,3 +32,5 @@ QStringAlias(UserName);
QStringAlias(UserId); QStringAlias(UserId);
QStringAlias(Url); QStringAlias(Url);
QStringAlias(Tooltip); QStringAlias(Tooltip);
QStringAlias(EmoteId);
QStringAlias(EmoteName);

View file

@ -22,15 +22,10 @@ namespace chatterino {
// Channel // Channel
// //
Channel::Channel(const QString &name, Type type) Channel::Channel(const QString &name, Type type)
: completionModel(name) : completionModel(*this)
, name_(name) , name_(name)
, type_(type) , type_(type)
{ {
QObject::connect(&this->clearCompletionModelTimer_, &QTimer::timeout,
[this]() {
this->completionModel.clearExpiredStrings(); //
});
this->clearCompletionModelTimer_.start(60 * 1000);
} }
Channel::~Channel() Channel::~Channel()
@ -165,13 +160,14 @@ void Channel::disableAllMessages()
LimitedQueueSnapshot<MessagePtr> snapshot = this->getMessageSnapshot(); LimitedQueueSnapshot<MessagePtr> snapshot = this->getMessageSnapshot();
int snapshotLength = snapshot.getLength(); int snapshotLength = snapshot.getLength();
for (int i = 0; i < snapshotLength; i++) { for (int i = 0; i < snapshotLength; i++) {
auto &s = snapshot[i]; auto &message = snapshot[i];
if (s->flags.hasAny({MessageFlag::System, MessageFlag::Timeout})) { if (message->flags.hasAny(
{MessageFlag::System, MessageFlag::Timeout})) {
continue; continue;
} }
// FOURTF: disabled for now // FOURTF: disabled for now
// s->flags.EnableFlag(MessageFlag::Disabled); const_cast<Message *>(message.get())->flags.set(MessageFlag::Disabled);
} }
} }

View file

@ -1,9 +1,7 @@
#pragma once #pragma once
#include "common/CompletionModel.hpp" #include "common/CompletionModel.hpp"
#include "messages/Image.hpp"
#include "messages/LimitedQueue.hpp" #include "messages/LimitedQueue.hpp"
#include "messages/Message.hpp"
#include <QString> #include <QString>
#include <QTimer> #include <QTimer>
@ -12,7 +10,9 @@
#include <memory> #include <memory>
namespace chatterino { namespace chatterino {
struct Message; struct Message;
using MessagePtr = std::shared_ptr<const Message>;
class Channel : public std::enable_shared_from_this<Channel> class Channel : public std::enable_shared_from_this<Channel>
{ {
@ -51,7 +51,6 @@ public:
void addOrReplaceTimeout(MessagePtr message); void addOrReplaceTimeout(MessagePtr message);
void disableAllMessages(); void disableAllMessages();
void replaceMessage(MessagePtr message, MessagePtr replacement); void replaceMessage(MessagePtr message, MessagePtr replacement);
virtual void addRecentChatter(const MessagePtr &message);
QStringList modList; QStringList modList;
@ -67,6 +66,7 @@ public:
protected: protected:
virtual void onConnected(); virtual void onConnected();
virtual void addRecentChatter(const MessagePtr &message);
private: private:
const QString name_; const QString name_;

View file

@ -3,7 +3,6 @@
#include "common/Aliases.hpp" #include "common/Aliases.hpp"
#include "common/Outcome.hpp" #include "common/Outcome.hpp"
#include "common/ProviderId.hpp" #include "common/ProviderId.hpp"
#include "debug/Log.hpp"
#include <QString> #include <QString>
#include <QWidget> #include <QWidget>
@ -39,4 +38,12 @@ std::weak_ptr<T> weakOf(T *element)
return element->shared_from_this(); return element->shared_from_this();
} }
struct Message;
using MessagePtr = std::shared_ptr<const Message>;
enum class CopyMode {
Everything,
OnlyTextAndEmotes,
};
} // namespace chatterino } // namespace chatterino

View file

@ -2,45 +2,30 @@
#include "Application.hpp" #include "Application.hpp"
#include "common/Common.hpp" #include "common/Common.hpp"
#include "common/UsernameSet.hpp"
#include "controllers/accounts/AccountController.hpp" #include "controllers/accounts/AccountController.hpp"
#include "controllers/commands/CommandController.hpp" #include "controllers/commands/CommandController.hpp"
#include "debug/Benchmark.hpp"
#include "debug/Log.hpp" #include "debug/Log.hpp"
#include "providers/twitch/TwitchChannel.hpp"
#include "providers/twitch/TwitchServer.hpp" #include "providers/twitch/TwitchServer.hpp"
#include "singletons/Emotes.hpp" #include "singletons/Emotes.hpp"
#include <QtAlgorithms> #include <QtAlgorithms>
#include <utility> #include <utility>
namespace chatterino { namespace chatterino {
// -- TaggedString //
// TaggedString
//
CompletionModel::TaggedString::TaggedString(const QString &_str, Type _type) CompletionModel::TaggedString::TaggedString(const QString &_string, Type _type)
: str(_str) : string(_string)
, type(_type) , type(_type)
, timeAdded(std::chrono::steady_clock::now())
{ {
} }
bool CompletionModel::TaggedString::isExpired(
const std::chrono::steady_clock::time_point &now) const
{
switch (this->type) {
case Type::Username: {
static std::chrono::minutes expirationTimer(10);
return (this->timeAdded + expirationTimer < now);
} break;
default: {
return false;
} break;
}
return false;
}
bool CompletionModel::TaggedString::isEmote() const bool CompletionModel::TaggedString::isEmote() const
{ {
return this->type > Type::EmoteStart && this->type < Type::EmoteEnd; return this->type > Type::EmoteStart && this->type < Type::EmoteEnd;
@ -48,35 +33,23 @@ bool CompletionModel::TaggedString::isEmote() const
bool CompletionModel::TaggedString::operator<(const TaggedString &that) const bool CompletionModel::TaggedString::operator<(const TaggedString &that) const
{ {
if (this->isEmote()) { if (this->isEmote() != that.isEmote()) {
if (that.isEmote()) { return this->isEmote();
int k = QString::compare(this->str, that.str, Qt::CaseInsensitive);
if (k == 0) {
return this->str > that.str;
}
return k < 0;
}
return true;
} }
if (that.isEmote()) { // try comparing insensitively, if they are the same then senstively
return false; // (fixes order of LuL and LUL)
} int k = QString::compare(this->string, that.string, Qt::CaseInsensitive);
if (k == 0) return this->string > that.string;
int k = QString::compare(this->str, that.str, Qt::CaseInsensitive);
if (k == 0) {
return false;
}
return k < 0; return k < 0;
} }
// -- CompletionModel //
// CompletionModel
CompletionModel::CompletionModel(const QString &channelName) //
: channelName_(channelName) CompletionModel::CompletionModel(Channel &channel)
: channel_(channel)
{ {
} }
@ -87,160 +60,90 @@ int CompletionModel::columnCount(const QModelIndex &) const
QVariant CompletionModel::data(const QModelIndex &index, int) const QVariant CompletionModel::data(const QModelIndex &index, int) const
{ {
std::lock_guard<std::mutex> lock(this->emotesMutex_); std::lock_guard<std::mutex> lock(this->itemsMutex_);
// TODO: Implement more safely auto it = this->items_.begin();
auto it = this->emotes_.begin();
std::advance(it, index.row()); std::advance(it, index.row());
return QVariant(it->str); return QVariant(it->string);
} }
int CompletionModel::rowCount(const QModelIndex &) const int CompletionModel::rowCount(const QModelIndex &) const
{ {
std::lock_guard<std::mutex> lock(this->emotesMutex_); std::lock_guard<std::mutex> lock(this->itemsMutex_);
return this->emotes_.size(); return this->items_.size();
} }
void CompletionModel::refresh() void CompletionModel::refresh(const QString &prefix)
{ {
log("[CompletionModel:{}] Refreshing...]", this->channelName_); std::lock_guard<std::mutex> guard(this->itemsMutex_);
this->items_.clear();
auto app = getApp(); if (prefix.length() < 2) return;
// User-specific: Twitch Emotes auto addString = [&](const QString &str, TaggedString::Type type) {
if (auto account = app->accounts->twitch.getCurrent()) { if (str.startsWith(prefix, Qt::CaseInsensitive))
for (const auto &emote : account->accessEmotes()->allEmoteNames) { this->items_.emplace(str + " ", type);
// XXX: No way to discern between a twitch global emote and sub
// emote right now
this->addString(emote.string,
TaggedString::Type::TwitchGlobalEmote);
}
}
// // Global: BTTV Global Emotes
// std::vector<QString> &bttvGlobalEmoteCodes =
// app->emotes->bttv.globalEmoteNames_; for (const auto &m :
// bttvGlobalEmoteCodes) {
// this->addString(m, TaggedString::Type::BTTVGlobalEmote);
// }
// // Global: FFZ Global Emotes
// std::vector<QString> &ffzGlobalEmoteCodes =
// app->emotes->ffz.globalEmoteCodes; for (const auto &m :
// ffzGlobalEmoteCodes) {
// this->addString(m, TaggedString::Type::FFZGlobalEmote);
// }
// Channel emotes
if (auto channel = dynamic_cast<TwitchChannel *>(
getApp()
->twitch2->getChannelOrEmptyByID(this->channelName_)
.get())) {
auto bttv = channel->bttvEmotes();
// auto it = bttv->begin();
// for (const auto &emote : *bttv) {
// }
// std::vector<QString> &bttvChannelEmoteCodes =
// app->emotes->bttv.channelEmoteName_[this->channelName_];
// for (const auto &m : bttvChannelEmoteCodes) {
// this->addString(m, TaggedString::Type::BTTVChannelEmote);
// }
// Channel-specific: FFZ Channel Emotes
for (const auto &emote : *channel->ffzEmotes()) {
this->addString(emote.second->name.string,
TaggedString::Type::FFZChannelEmote);
}
}
// Emojis
const auto &emojiShortCodes = app->emotes->emojis.shortCodes;
for (const auto &m : emojiShortCodes) {
this->addString(":" + m + ":", TaggedString::Type::Emoji);
}
// Commands
for (auto &command : app->commands->items.getVector()) {
this->addString(command.name, TaggedString::Command);
}
for (auto &command : app->commands->getDefaultTwitchCommandList()) {
this->addString(command, TaggedString::Command);
}
// Channel-specific: Usernames
// fourtf: only works with twitch chat
// auto c =
// ChannelManager::getInstance().getTwitchChannel(this->channelName);
// auto usernames = c->getUsernamesForCompletions();
// for (const auto &name : usernames) {
// assert(!name.displayName.isEmpty());
// this->addString(name.displayName);
// this->addString('@' + name.displayName);
// if (!name.localizedName.isEmpty()) {
// this->addString(name.localizedName);
// this->addString('@' + name.localizedName);
// }
// }
}
void CompletionModel::addString(const QString &str, TaggedString::Type type)
{
std::lock_guard<std::mutex> lock(this->emotesMutex_);
// Always add a space at the end of completions
this->emotes_.insert({str + " ", type});
}
void CompletionModel::addUser(const QString &username)
{
auto add = [this](const QString &str) {
auto ts = this->createUser(str + " ");
// Always add a space at the end of completions
std::pair<std::set<TaggedString>::iterator, bool> p =
this->emotes_.insert(ts);
if (!p.second) {
// No inseration was made, figure out if we need to replace the
// username.
if (p.first->str > ts.str) {
// Replace lowercase version of name with mixed-case version
this->emotes_.erase(p.first);
auto result2 = this->emotes_.insert(ts);
assert(result2.second);
} else {
p.first->timeAdded = std::chrono::steady_clock::now();
}
}
}; };
add(username); if (auto channel = dynamic_cast<TwitchChannel *>(&this->channel_)) {
add("@" + username); // account emotes
} if (auto account = getApp()->accounts->twitch.getCurrent()) {
for (const auto &emote : account->accessEmotes()->allEmoteNames) {
// XXX: No way to discern between a twitch global emote and sub
// emote right now
addString(emote.string, TaggedString::Type::TwitchGlobalEmote);
}
}
void CompletionModel::clearExpiredStrings() // Usernames
{ if (prefix.length() >= UsernameSet::PrefixLength) {
std::lock_guard<std::mutex> lock(this->emotesMutex_); auto usernames = channel->accessChatters();
auto now = std::chrono::steady_clock::now(); for (const auto &name : usernames->subrange(Prefix(prefix))) {
addString(name, TaggedString::Type::Username);
addString("@" + name, TaggedString::Type::Username);
}
}
for (auto it = this->emotes_.begin(); it != this->emotes_.end();) { // Bttv Global
const auto &taggedString = *it; for (auto &emote : *channel->globalBttv().emotes()) {
addString(emote.first.string, TaggedString::Type::BTTVChannelEmote);
}
if (taggedString.isExpired(now)) { // Ffz Global
// Log("String {} expired", taggedString.str); for (auto &emote : *channel->globalFfz().emotes()) {
it = this->emotes_.erase(it); addString(emote.first.string, TaggedString::Type::FFZChannelEmote);
} else { }
++it;
// Bttv Channel
for (auto &emote : *channel->bttvEmotes()) {
addString(emote.first.string, TaggedString::Type::BTTVGlobalEmote);
}
// Ffz Channel
for (auto &emote : *channel->ffzEmotes()) {
addString(emote.first.string, TaggedString::Type::BTTVGlobalEmote);
}
// Emojis
if (prefix.startsWith(":")) {
const auto &emojiShortCodes = getApp()->emotes->emojis.shortCodes;
for (auto &m : emojiShortCodes) {
addString(":" + m + ":", TaggedString::Type::Emoji);
}
}
// Commands
for (auto &command : getApp()->commands->items.getVector()) {
addString(command.name, TaggedString::Command);
}
for (auto &command :
getApp()->commands->getDefaultTwitchCommandList()) {
addString(command, TaggedString::Command);
} }
} }
} }
CompletionModel::TaggedString CompletionModel::createUser(const QString &str)
{
return TaggedString{str, TaggedString::Type::Username};
}
} // namespace chatterino } // namespace chatterino

View file

@ -1,7 +1,5 @@
#pragma once #pragma once
#include "common/Common.hpp"
#include <QAbstractListModel> #include <QAbstractListModel>
#include <chrono> #include <chrono>
@ -10,7 +8,7 @@
namespace chatterino { namespace chatterino {
class TwitchChannel; class Channel;
class CompletionModel : public QAbstractListModel class CompletionModel : public QAbstractListModel
{ {
@ -33,39 +31,30 @@ class CompletionModel : public QAbstractListModel
Command, Command,
}; };
TaggedString(const QString &_str, Type _type); TaggedString(const QString &string, Type type);
bool isExpired(const std::chrono::steady_clock::time_point &now) const;
bool isEmote() const; bool isEmote() const;
bool operator<(const TaggedString &that) const; bool operator<(const TaggedString &that) const;
QString str; QString string;
// Type will help decide the lifetime of the tagged strings
Type type; Type type;
mutable std::chrono::steady_clock::time_point timeAdded;
}; };
public: public:
CompletionModel(const QString &channelName); CompletionModel(Channel &channel);
virtual int columnCount(const QModelIndex &) const override; virtual int columnCount(const QModelIndex &) const override;
virtual QVariant data(const QModelIndex &index, int) const override; virtual QVariant data(const QModelIndex &index, int) const override;
virtual int rowCount(const QModelIndex &) const override; virtual int rowCount(const QModelIndex &) const override;
void refresh(); void refresh(const QString &prefix);
void addString(const QString &str, TaggedString::Type type);
void addUser(const QString &str);
void clearExpiredStrings();
private: private:
TaggedString createUser(const QString &str); TaggedString createUser(const QString &str);
mutable std::mutex emotesMutex_; std::set<TaggedString> items_;
std::set<TaggedString> emotes_; mutable std::mutex itemsMutex_;
Channel &channel_;
QString channelName_;
}; };
} // namespace chatterino } // namespace chatterino

View file

@ -2,12 +2,11 @@
#include <functional> #include <functional>
#include "Common.hpp"
class QNetworkReply; class QNetworkReply;
namespace chatterino { namespace chatterino {
class Outcome;
class NetworkResult; class NetworkResult;
using NetworkSuccessCallback = std::function<Outcome(NetworkResult)>; using NetworkSuccessCallback = std::function<Outcome(NetworkResult)>;

View file

@ -44,7 +44,7 @@ void NetworkData::writeToCache(const QByteArray &bytes)
if (this->useQuickLoadCache_) { if (this->useQuickLoadCache_) {
auto app = getApp(); auto app = getApp();
QFile cachedFile(app->paths->cacheDirectory + "/" + this->getHash()); QFile cachedFile(getPaths()->cacheDirectory + "/" + this->getHash());
if (cachedFile.open(QIODevice::WriteOnly)) { if (cachedFile.open(QIODevice::WriteOnly)) {
cachedFile.write(bytes); cachedFile.write(bytes);

View file

@ -1,16 +1,7 @@
#pragma once #pragma once
#include "common/NetworkRequester.hpp"
#include "common/NetworkWorker.hpp"
#include "debug/Log.hpp"
#include <QJsonDocument>
#include <QJsonObject>
#include <QNetworkAccessManager> #include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QThread> #include <QThread>
#include <QTimer>
#include <QUrl>
namespace chatterino { namespace chatterino {

View file

@ -1,7 +1,9 @@
#include "common/NetworkRequest.hpp" #include "common/NetworkRequest.hpp"
#include "Application.hpp" #include "Application.hpp"
#include "common/NetworkData.hpp"
#include "common/NetworkManager.hpp" #include "common/NetworkManager.hpp"
#include "common/Outcome.hpp"
#include "debug/Log.hpp" #include "debug/Log.hpp"
#include "providers/twitch/TwitchCommon.hpp" #include "providers/twitch/TwitchCommon.hpp"
#include "singletons/Paths.hpp" #include "singletons/Paths.hpp"
@ -145,7 +147,7 @@ Outcome NetworkRequest::tryLoadCachedFile()
{ {
auto app = getApp(); auto app = getApp();
QFile cachedFile(app->paths->cacheDirectory + "/" + this->data->getHash()); QFile cachedFile(getPaths()->cacheDirectory + "/" + this->data->getHash());
if (!cachedFile.exists()) { if (!cachedFile.exists()) {
// File didn't exist // File didn't exist

View file

@ -1,14 +1,13 @@
#pragma once #pragma once
#include "Application.hpp"
#include "common/NetworkCommon.hpp" #include "common/NetworkCommon.hpp"
#include "common/NetworkData.hpp"
#include "common/NetworkRequester.hpp" #include "common/NetworkRequester.hpp"
#include "common/NetworkResult.hpp" #include "common/NetworkResult.hpp"
#include "common/NetworkTimer.hpp" #include "common/NetworkTimer.hpp"
#include "common/NetworkWorker.hpp" #include "common/NetworkWorker.hpp"
namespace chatterino { namespace chatterino {
class NetworkData;
class NetworkRequest class NetworkRequest
{ {

112
src/common/UsernameSet.cpp Normal file
View file

@ -0,0 +1,112 @@
#include "UsernameSet.hpp"
#include <tuple>
namespace chatterino {
//
// UsernameSet
//
UsernameSet::ConstIterator UsernameSet::begin() const
{
return this->items.begin();
}
UsernameSet::ConstIterator UsernameSet::end() const
{
return this->items.end();
}
UsernameSet::Range UsernameSet::subrange(const Prefix &prefix) const
{
auto it = this->firstKeyForPrefix.find(prefix);
if (it != this->firstKeyForPrefix.end()) {
auto start = this->items.find(it->second);
auto end = start;
while (end != this->items.end() && prefix.isStartOf(*end)) {
end++;
}
return {start, end};
}
return {this->items.end(), this->items.end()};
}
std::set<QString>::size_type UsernameSet::size() const
{
return this->items.size();
}
std::pair<UsernameSet::Iterator, bool> UsernameSet::insert(const QString &value)
{
this->insertPrefix(value);
return this->items.insert(value);
}
std::pair<UsernameSet::Iterator, bool> UsernameSet::insert(QString &&value)
{
this->insertPrefix(value);
return this->items.insert(std::move(value));
}
void UsernameSet::insertPrefix(const QString &value)
{
auto &string = this->firstKeyForPrefix[Prefix(value)];
if (string.isNull() || value < string) string = value;
}
//
// Range
//
UsernameSet::Range::Range(ConstIterator start, ConstIterator end)
: start_(start)
, end_(end)
{
}
UsernameSet::ConstIterator UsernameSet::Range::begin()
{
return this->start_;
}
UsernameSet::ConstIterator UsernameSet::Range::end()
{
return this->end_;
}
//
// Prefix
//
Prefix::Prefix(const QString &string)
: first(string.size() >= 1 ? string[0].toLower() : '\0')
, second(string.size() >= 2 ? string[1].toLower() : '\0')
{
}
bool Prefix::operator==(const Prefix &other) const
{
return std::tie(this->first, this->second) ==
std::tie(other.first, other.second);
}
bool Prefix::isStartOf(const QString &string) const
{
if (string.size() == 0) {
return this->first == QChar('\0') && this->second == QChar('\0');
} else if (string.size() == 1) {
return this->first == string[0].toLower() &&
this->second == QChar('\0');
} else {
return this->first == string[0].toLower() &&
this->second == string[1].toLower();
}
}
} // namespace chatterino

View file

@ -0,0 +1,73 @@
#pragma once
#include <QString>
#include <functional>
#include <set>
#include <unordered_map>
namespace chatterino {
class Prefix
{
public:
Prefix(const QString &string);
bool operator==(const Prefix &other) const;
bool isStartOf(const QString &string) const;
private:
QChar first;
QChar second;
friend struct std::hash<Prefix>;
};
} // namespace chatterino
namespace std {
template <>
struct hash<chatterino::Prefix> {
size_t operator()(const chatterino::Prefix &prefix) const
{
return (size_t(prefix.first.unicode()) << 16) |
size_t(prefix.second.unicode());
}
};
} // namespace std
namespace chatterino {
class UsernameSet
{
public:
static constexpr int PrefixLength = 2;
using Iterator = std::set<QString>::iterator;
using ConstIterator = std::set<QString>::const_iterator;
class Range
{
public:
Range(ConstIterator start, ConstIterator end);
ConstIterator begin();
ConstIterator end();
private:
ConstIterator start_;
ConstIterator end_;
};
ConstIterator begin() const;
ConstIterator end() const;
Range subrange(const Prefix &prefix) const;
std::set<QString>::size_type size() const;
std::pair<Iterator, bool> insert(const QString &value);
std::pair<Iterator, bool> insert(QString &&value);
private:
void insertPrefix(const QString &string);
std::set<QString> items;
std::unordered_map<Prefix, QString> firstKeyForPrefix;
};
} // namespace chatterino

View file

@ -5,9 +5,9 @@
#define CHATTERINO_VERSION "2.0.4" #define CHATTERINO_VERSION "2.0.4"
#if defined(Q_OS_WIN) #if defined(Q_OS_WIN)
#define CHATTERINO_OS "win" # define CHATTERINO_OS "win"
#elif defined(Q_OS_MACOS) #elif defined(Q_OS_MACOS)
#define CHATTERINO_OS "macos" # define CHATTERINO_OS "macos"
#elif defined(Q_OS_LINUX) #elif defined(Q_OS_LINUX)
#define CHATTERINO_OS "linux" # define CHATTERINO_OS "linux"
#endif #endif

View file

@ -1,6 +1,8 @@
#include "AccountController.hpp" #include "AccountController.hpp"
#include "controllers/accounts/Account.hpp"
#include "controllers/accounts/AccountModel.hpp" #include "controllers/accounts/AccountModel.hpp"
#include "providers/twitch/TwitchAccount.hpp"
namespace chatterino { namespace chatterino {

View file

@ -1,16 +1,15 @@
#pragma once #pragma once
#include "common/Singleton.hpp"
#include <QObject> #include <QObject>
#include "common/SignalVector.hpp" #include "common/SignalVector.hpp"
#include "controllers/accounts/Account.hpp" #include "common/Singleton.hpp"
#include "providers/twitch/TwitchAccountManager.hpp" #include "providers/twitch/TwitchAccountManager.hpp"
#include "util/SharedPtrElementLess.hpp" #include "util/SharedPtrElementLess.hpp"
namespace chatterino { namespace chatterino {
class Account;
class Settings; class Settings;
class Paths; class Paths;

View file

@ -1,5 +1,6 @@
#include "AccountModel.hpp" #include "AccountModel.hpp"
#include "controllers/accounts/Account.hpp"
#include "util/StandardItemHelper.hpp" #include "util/StandardItemHelper.hpp"
namespace chatterino { namespace chatterino {

View file

@ -8,6 +8,7 @@
namespace chatterino { namespace chatterino {
class Account;
class AccountController; class AccountController;
class AccountModel : public SignalVectorModel<std::shared_ptr<Account>> class AccountModel : public SignalVectorModel<std::shared_ptr<Account>>

View file

@ -5,7 +5,10 @@
#include "controllers/accounts/AccountController.hpp" #include "controllers/accounts/AccountController.hpp"
#include "controllers/commands/Command.hpp" #include "controllers/commands/Command.hpp"
#include "controllers/commands/CommandModel.hpp" #include "controllers/commands/CommandModel.hpp"
#include "debug/Log.hpp"
#include "messages/Message.hpp"
#include "messages/MessageBuilder.hpp" #include "messages/MessageBuilder.hpp"
#include "messages/MessageElement.hpp"
#include "providers/twitch/TwitchApi.hpp" #include "providers/twitch/TwitchApi.hpp"
#include "providers/twitch/TwitchChannel.hpp" #include "providers/twitch/TwitchChannel.hpp"
#include "providers/twitch/TwitchServer.hpp" #include "providers/twitch/TwitchServer.hpp"

View file

@ -68,37 +68,39 @@ private:
namespace pajlada { namespace pajlada {
namespace Settings { namespace Settings {
template <> template <>
struct Serialize<chatterino::HighlightBlacklistUser> { struct Serialize<chatterino::HighlightBlacklistUser> {
static rapidjson::Value get(const chatterino::HighlightBlacklistUser &value, static rapidjson::Value get(
rapidjson::Document::AllocatorType &a) const chatterino::HighlightBlacklistUser &value,
{ rapidjson::Document::AllocatorType &a)
rapidjson::Value ret(rapidjson::kObjectType); {
rapidjson::Value ret(rapidjson::kObjectType);
AddMember(ret, "pattern", value.getPattern(), a); AddMember(ret, "pattern", value.getPattern(), a);
AddMember(ret, "regex", value.isRegex(), a); AddMember(ret, "regex", value.isRegex(), a);
return ret; return ret;
} }
}; };
template <> template <>
struct Deserialize<chatterino::HighlightBlacklistUser> { struct Deserialize<chatterino::HighlightBlacklistUser> {
static chatterino::HighlightBlacklistUser get(const rapidjson::Value &value) static chatterino::HighlightBlacklistUser get(
{ const rapidjson::Value &value)
QString pattern; {
bool isRegex = false; QString pattern;
bool isRegex = false;
if (!value.IsObject()) {
return chatterino::HighlightBlacklistUser(pattern, isRegex);
}
chatterino::rj::getSafe(value, "pattern", pattern);
chatterino::rj::getSafe(value, "regex", isRegex);
if (!value.IsObject()) {
return chatterino::HighlightBlacklistUser(pattern, isRegex); return chatterino::HighlightBlacklistUser(pattern, isRegex);
} }
};
chatterino::rj::getSafe(value, "pattern", pattern);
chatterino::rj::getSafe(value, "regex", isRegex);
return chatterino::HighlightBlacklistUser(pattern, isRegex);
}
};
} // namespace Settings } // namespace Settings
} // namespace pajlada } // namespace pajlada

View file

@ -1,15 +1,16 @@
#pragma once #pragma once
#include "common/Singleton.hpp" #include "common/ChatterinoSetting.hpp"
#include "common/SignalVector.hpp" #include "common/SignalVector.hpp"
#include "common/Singleton.hpp"
#include "controllers/highlights/HighlightBlacklistUser.hpp" #include "controllers/highlights/HighlightBlacklistUser.hpp"
#include "controllers/highlights/HighlightPhrase.hpp" #include "controllers/highlights/HighlightPhrase.hpp"
#include "messages/Message.hpp"
#include "singletons/Settings.hpp"
namespace chatterino { namespace chatterino {
struct Message;
using MessagePtr = std::shared_ptr<const Message>;
class Settings; class Settings;
class Paths; class Paths;

View file

@ -37,13 +37,13 @@ void HighlightModel::getRowFromItem(const HighlightPhrase &item,
void HighlightModel::afterInit() void HighlightModel::afterInit()
{ {
std::vector<QStandardItem *> row = this->createRow(); std::vector<QStandardItem *> row = this->createRow();
setBoolItem(row[0], getApp()->settings->enableHighlightsSelf.getValue(), setBoolItem(row[0], getSettings()->enableHighlightsSelf.getValue(), true,
true, false); false);
row[0]->setData("Your username (automatic)", Qt::DisplayRole); row[0]->setData("Your username (automatic)", Qt::DisplayRole);
setBoolItem(row[1], getApp()->settings->enableHighlightTaskbar.getValue(), setBoolItem(row[1], getSettings()->enableHighlightTaskbar.getValue(), true,
true, false); false);
setBoolItem(row[2], getApp()->settings->enableHighlightSound.getValue(), setBoolItem(row[2], getSettings()->enableHighlightSound.getValue(), true,
true, false); false);
row[3]->setFlags(0); row[3]->setFlags(0);
this->insertCustomRow(row, 0); this->insertCustomRow(row, 0);
} }
@ -55,20 +55,17 @@ void HighlightModel::customRowSetData(const std::vector<QStandardItem *> &row,
switch (column) { switch (column) {
case 0: { case 0: {
if (role == Qt::CheckStateRole) { if (role == Qt::CheckStateRole) {
getApp()->settings->enableHighlightsSelf.setValue( getSettings()->enableHighlightsSelf.setValue(value.toBool());
value.toBool());
} }
} break; } break;
case 1: { case 1: {
if (role == Qt::CheckStateRole) { if (role == Qt::CheckStateRole) {
getApp()->settings->enableHighlightTaskbar.setValue( getSettings()->enableHighlightTaskbar.setValue(value.toBool());
value.toBool());
} }
} break; } break;
case 2: { case 2: {
if (role == Qt::CheckStateRole) { if (role == Qt::CheckStateRole) {
getApp()->settings->enableHighlightSound.setValue( getSettings()->enableHighlightSound.setValue(value.toBool());
value.toBool());
} }
} break; } break;
case 3: { case 3: {

View file

@ -72,43 +72,45 @@ private:
namespace pajlada { namespace pajlada {
namespace Settings { namespace Settings {
template <> template <>
struct Serialize<chatterino::HighlightPhrase> { struct Serialize<chatterino::HighlightPhrase> {
static rapidjson::Value get(const chatterino::HighlightPhrase &value, static rapidjson::Value get(const chatterino::HighlightPhrase &value,
rapidjson::Document::AllocatorType &a) rapidjson::Document::AllocatorType &a)
{ {
rapidjson::Value ret(rapidjson::kObjectType); rapidjson::Value ret(rapidjson::kObjectType);
AddMember(ret, "pattern", value.getPattern(), a); AddMember(ret, "pattern", value.getPattern(), a);
AddMember(ret, "alert", value.getAlert(), a); AddMember(ret, "alert", value.getAlert(), a);
AddMember(ret, "sound", value.getSound(), a); AddMember(ret, "sound", value.getSound(), a);
AddMember(ret, "regex", value.isRegex(), a); AddMember(ret, "regex", value.isRegex(), a);
return ret; return ret;
}
};
template <>
struct Deserialize<chatterino::HighlightPhrase> {
static chatterino::HighlightPhrase get(const rapidjson::Value &value)
{
if (!value.IsObject()) {
return chatterino::HighlightPhrase(QString(), true, false, false);
} }
};
QString _pattern; template <>
bool _alert = true; struct Deserialize<chatterino::HighlightPhrase> {
bool _sound = false; static chatterino::HighlightPhrase get(const rapidjson::Value &value)
bool _isRegex = false; {
if (!value.IsObject()) {
return chatterino::HighlightPhrase(QString(), true, false,
false);
}
chatterino::rj::getSafe(value, "pattern", _pattern); QString _pattern;
chatterino::rj::getSafe(value, "alert", _alert); bool _alert = true;
chatterino::rj::getSafe(value, "sound", _sound); bool _sound = false;
chatterino::rj::getSafe(value, "regex", _isRegex); bool _isRegex = false;
return chatterino::HighlightPhrase(_pattern, _alert, _sound, _isRegex); chatterino::rj::getSafe(value, "pattern", _pattern);
} chatterino::rj::getSafe(value, "alert", _alert);
}; chatterino::rj::getSafe(value, "sound", _sound);
chatterino::rj::getSafe(value, "regex", _isRegex);
return chatterino::HighlightPhrase(_pattern, _alert, _sound,
_isRegex);
}
};
} // namespace Settings } // namespace Settings
} // namespace pajlada } // namespace pajlada

View file

@ -1,10 +1,9 @@
#pragma once #pragma once
#include "common/Singleton.hpp" #include "common/ChatterinoSetting.hpp"
#include "common/SignalVector.hpp" #include "common/SignalVector.hpp"
#include "common/Singleton.hpp"
#include "controllers/ignores/IgnorePhrase.hpp" #include "controllers/ignores/IgnorePhrase.hpp"
#include "singletons/Settings.hpp"
namespace chatterino { namespace chatterino {

View file

@ -150,22 +150,23 @@ struct Deserialize<chatterino::IgnorePhrase> {
QString(), false, false, QString(), false, false,
::chatterino::getSettings()->ignoredPhraseReplace.getValue(), true); ::chatterino::getSettings()->ignoredPhraseReplace.getValue(), true);
} }
};
QString _pattern; QString _pattern;
bool _isRegex = false; bool _isRegex = false;
bool _isBlock = false; bool _isBlock = false;
QString _replace; QString _replace;
bool _caseSens = true; bool _caseSens = true;
chatterino::rj::getSafe(value, "pattern", _pattern); chatterino::rj::getSafe(value, "pattern", _pattern);
chatterino::rj::getSafe(value, "regex", _isRegex); chatterino::rj::getSafe(value, "regex", _isRegex);
chatterino::rj::getSafe(value, "isBlock", _isBlock); chatterino::rj::getSafe(value, "isBlock", _isBlock);
chatterino::rj::getSafe(value, "replaceWith", _replace); chatterino::rj::getSafe(value, "replaceWith", _replace);
chatterino::rj::getSafe(value, "caseSensitive", _caseSens); chatterino::rj::getSafe(value, "caseSensitive", _caseSens);
return chatterino::IgnorePhrase(_pattern, _isRegex, _isBlock, _replace, _caseSens); return chatterino::IgnorePhrase(_pattern, _isRegex, _isBlock, _replace, _caseSens);
} }
}; }; // namespace Settings
} // namespace Settings } // namespace pajlada
} // namespace pajlada } // namespace pajlada

View file

@ -2,6 +2,7 @@
#include <QRegularExpression> #include <QRegularExpression>
#include "Application.hpp" #include "Application.hpp"
#include "messages/Image.hpp"
#include "singletons/Resources.hpp" #include "singletons/Resources.hpp"
namespace chatterino { namespace chatterino {

View file

@ -4,11 +4,13 @@
#include <boost/optional.hpp> #include <boost/optional.hpp>
#include <pajlada/settings/serialize.hpp> #include <pajlada/settings/serialize.hpp>
#include "messages/Image.hpp"
#include "util/RapidjsonHelpers.hpp" #include "util/RapidjsonHelpers.hpp"
namespace chatterino { namespace chatterino {
class Image;
using ImagePtr = std::shared_ptr<Image>;
class ModerationAction class ModerationAction
{ {
public: public:
@ -34,34 +36,34 @@ private:
namespace pajlada { namespace pajlada {
namespace Settings { namespace Settings {
template <> template <>
struct Serialize<chatterino::ModerationAction> { struct Serialize<chatterino::ModerationAction> {
static rapidjson::Value get(const chatterino::ModerationAction &value, static rapidjson::Value get(const chatterino::ModerationAction &value,
rapidjson::Document::AllocatorType &a) rapidjson::Document::AllocatorType &a)
{ {
rapidjson::Value ret(rapidjson::kObjectType); rapidjson::Value ret(rapidjson::kObjectType);
AddMember(ret, "pattern", value.getAction(), a); AddMember(ret, "pattern", value.getAction(), a);
return ret; return ret;
}
};
template <>
struct Deserialize<chatterino::ModerationAction> {
static chatterino::ModerationAction get(const rapidjson::Value &value)
{
if (!value.IsObject()) {
return chatterino::ModerationAction(QString());
} }
};
QString pattern; template <>
struct Deserialize<chatterino::ModerationAction> {
static chatterino::ModerationAction get(const rapidjson::Value &value)
{
if (!value.IsObject()) {
return chatterino::ModerationAction(QString());
}
chatterino::rj::getSafe(value, "pattern", pattern); QString pattern;
return chatterino::ModerationAction(pattern); chatterino::rj::getSafe(value, "pattern", pattern);
}
}; return chatterino::ModerationAction(pattern);
}
};
} // namespace Settings } // namespace Settings
} // namespace pajlada } // namespace pajlada

View file

@ -17,12 +17,16 @@ void ModerationActions::initialize(Settings &settings, Paths &paths)
assert(!this->initialized_); assert(!this->initialized_);
this->initialized_ = true; this->initialized_ = true;
for (auto &val : this->setting_.getValue()) { this->setting_ =
std::make_unique<ChatterinoSetting<std::vector<ModerationAction>>>(
"/moderation/actions");
for (auto &val : this->setting_->getValue()) {
this->items.insertItem(val); this->items.insertItem(val);
} }
this->items.delayedItemsChanged.connect([this] { // this->items.delayedItemsChanged.connect([this] { //
this->setting_.setValue(this->items.getVector()); this->setting_->setValue(this->items.getVector());
}); });
} }

View file

@ -25,8 +25,7 @@ public:
ModerationActionModel *createModel(QObject *parent); ModerationActionModel *createModel(QObject *parent);
private: private:
ChatterinoSetting<std::vector<ModerationAction>> setting_ = { std::unique_ptr<ChatterinoSetting<std::vector<ModerationAction>>> setting_;
"/moderation/actions"};
bool initialized_ = false; bool initialized_ = false;
}; };

View file

@ -29,12 +29,12 @@ void TaggedUsersModel::afterInit()
{ {
// std::vector<QStandardItem *> row = this->createRow(); // std::vector<QStandardItem *> row = this->createRow();
// setBoolItem(row[0], // setBoolItem(row[0],
// getApp()->settings->enableHighlightsSelf.getValue(), true, false); // getSettings()->enableHighlightsSelf.getValue(), true, false);
// row[0]->setData("Your username (automatic)", Qt::DisplayRole); // row[0]->setData("Your username (automatic)", Qt::DisplayRole);
// setBoolItem(row[1], // setBoolItem(row[1],
// getApp()->settings->enableHighlightTaskbar.getValue(), true, false); // getSettings()->enableHighlightTaskbar.getValue(), true, false);
// setBoolItem(row[2], // setBoolItem(row[2],
// getApp()->settings->enableHighlightSound.getValue(), true, false); // getSettings()->enableHighlightSound.getValue(), true, false);
// row[3]->setFlags(0); this->insertCustomRow(row, 0); // row[3]->setFlags(0); this->insertCustomRow(row, 0);
} }
@ -45,17 +45,17 @@ void TaggedUsersModel::afterInit()
// switch (column) { // switch (column) {
// case 0: { // case 0: {
// if (role == Qt::CheckStateRole) { // if (role == Qt::CheckStateRole) {
// getApp()->settings->enableHighlightsSelf.setValue(value.toBool()); // getSettings()->enableHighlightsSelf.setValue(value.toBool());
// } // }
// } break; // } break;
// case 1: { // case 1: {
// if (role == Qt::CheckStateRole) { // if (role == Qt::CheckStateRole) {
// getApp()->settings->enableHighlightTaskbar.setValue(value.toBool()); // getSettings()->enableHighlightTaskbar.setValue(value.toBool());
// } // }
// } break; // } break;
// case 2: { // case 2: {
// if (role == Qt::CheckStateRole) { // if (role == Qt::CheckStateRole) {
// getApp()->settings->enableHighlightSound.setValue(value.toBool()); // getSettings()->enableHighlightSound.setValue(value.toBool());
// } // }
// } break; // } break;
// case 3: { // case 3: {

View file

@ -7,9 +7,6 @@
#include <memory> #include <memory>
#include <unordered_map> #include <unordered_map>
QStringAlias(EmoteId);
QStringAlias(EmoteName);
namespace chatterino { namespace chatterino {
struct Emote { struct Emote {

View file

@ -1,93 +0,0 @@
#pragma once
#include <QString>
#include <boost/optional.hpp>
#include <unordered_map>
#include <util/QStringHash.hpp>
#include "common/UniqueAccess.hpp"
#include "messages/Emote.hpp"
namespace chatterino {
template <typename TKey>
class MapReplacement
{
public:
MapReplacement(std::unordered_map<TKey, EmotePtr> &items)
: oldItems_(items)
{
}
void add(const TKey &key, const Emote &data)
{
this->add(key, Emote(data));
}
void add(const TKey &key, Emote &&data)
{
auto it = this->oldItems_.find(key);
if (it != this->oldItems_.end() && *it->second == data) {
this->newItems_[key] = it->second;
} else {
this->newItems_[key] = std::make_shared<Emote>(std::move(data));
}
}
void apply()
{
this->oldItems_ = std::move(this->newItems_);
}
private:
std::unordered_map<TKey, EmotePtr> &oldItems_;
std::unordered_map<TKey, EmotePtr> newItems_;
};
template <typename TKey>
class EmoteCache
{
public:
using Iterator = typename std::unordered_map<TKey, EmotePtr>::iterator;
using ConstIterator = typename std::unordered_map<TKey, EmotePtr>::iterator;
Iterator begin()
{
return this->items_.begin();
}
ConstIterator begin() const
{
return this->items_.begin();
}
Iterator end()
{
return this->items_.end();
}
ConstIterator end() const
{
return this->items_.end();
}
boost::optional<EmotePtr> get(const TKey &key) const
{
auto it = this->items_.find(key);
if (it == this->items_.end())
return boost::none;
else
return it->second;
}
MapReplacement<TKey> makeReplacment()
{
return MapReplacement<TKey>(this->items_);
}
private:
std::unordered_map<TKey, EmotePtr> items_;
};
} // namespace chatterino

View file

@ -1,6 +1,7 @@
#include "messages/Image.hpp" #include "messages/Image.hpp"
#include "Application.hpp" #include "Application.hpp"
#include "common/Common.hpp"
#include "common/NetworkRequest.hpp" #include "common/NetworkRequest.hpp"
#include "debug/AssertInGuiThread.hpp" #include "debug/AssertInGuiThread.hpp"
#include "debug/Benchmark.hpp" #include "debug/Benchmark.hpp"
@ -21,157 +22,163 @@
namespace chatterino { namespace chatterino {
namespace { namespace {
// Frames // Frames
Frames::Frames() Frames::Frames()
{ {
DebugCount::increase("images"); DebugCount::increase("images");
}
Frames::Frames(const QVector<Frame<QPixmap>> &frames)
: items_(frames)
{
assertInGuiThread();
DebugCount::increase("images");
if (this->animated()) {
DebugCount::increase("animated images");
this->gifTimerConnection_ = getApp()->emotes->gifTimer.signal.connect(
[this] { this->advance(); });
}
}
Frames::~Frames()
{
assertInGuiThread();
DebugCount::decrease("images");
if (this->animated()) {
DebugCount::decrease("animated images");
} }
this->gifTimerConnection_.disconnect(); Frames::Frames(const QVector<Frame<QPixmap>> &frames)
} : items_(frames)
{
assertInGuiThread();
DebugCount::increase("images");
void Frames::advance() if (this->animated()) {
{ DebugCount::increase("animated images");
this->durationOffset_ += GIF_FRAME_LENGTH;
while (true) { this->gifTimerConnection_ =
this->index_ %= this->items_.size(); getApp()->emotes->gifTimer.signal.connect(
[this] { this->advance(); });
if (this->index_ >= this->items_.size()) {
this->index_ = this->index_;
}
if (this->durationOffset_ > this->items_[this->index_].duration) {
this->durationOffset_ -= this->items_[this->index_].duration;
this->index_ = (this->index_ + 1) % this->items_.size();
} else {
break;
} }
} }
}
bool Frames::animated() const Frames::~Frames()
{ {
return this->items_.size() > 1; assertInGuiThread();
} DebugCount::decrease("images");
boost::optional<QPixmap> Frames::current() const if (this->animated()) {
{ DebugCount::decrease("animated images");
if (this->items_.size() == 0) return boost::none; }
return this->items_[this->index_].image;
}
boost::optional<QPixmap> Frames::first() const this->gifTimerConnection_.disconnect();
{ }
if (this->items_.size() == 0) return boost::none;
return this->items_.front().image;
}
// functions void Frames::advance()
QVector<Frame<QImage>> readFrames(QImageReader &reader, const Url &url) {
{ this->durationOffset_ += GIF_FRAME_LENGTH;
QVector<Frame<QImage>> frames;
while (true) {
this->index_ %= this->items_.size();
if (this->index_ >= this->items_.size()) {
this->index_ = this->index_;
}
if (this->durationOffset_ > this->items_[this->index_].duration) {
this->durationOffset_ -= this->items_[this->index_].duration;
this->index_ = (this->index_ + 1) % this->items_.size();
} else {
break;
}
}
}
bool Frames::animated() const
{
return this->items_.size() > 1;
}
boost::optional<QPixmap> Frames::current() const
{
if (this->items_.size() == 0) return boost::none;
return this->items_[this->index_].image;
}
boost::optional<QPixmap> Frames::first() const
{
if (this->items_.size() == 0) return boost::none;
return this->items_.front().image;
}
// functions
QVector<Frame<QImage>> readFrames(QImageReader &reader, const Url &url)
{
QVector<Frame<QImage>> frames;
if (reader.imageCount() == 0) {
log("Error while reading image {}: '{}'", url.string,
reader.errorString());
return frames;
}
QImage image;
for (int index = 0; index < reader.imageCount(); ++index) {
if (reader.read(&image)) {
QPixmap::fromImage(image);
int duration = std::max(20, reader.nextImageDelay());
frames.push_back(Frame<QImage>{image, duration});
}
}
if (frames.size() == 0) {
log("Error while reading image {}: '{}'", url.string,
reader.errorString());
}
if (reader.imageCount() == 0) {
log("Error while reading image {}: '{}'", url.string,
reader.errorString());
return frames; return frames;
} }
QImage image; // parsed
for (int index = 0; index < reader.imageCount(); ++index) { template <typename Assign>
if (reader.read(&image)) { void assignDelayed(
QPixmap::fromImage(image); std::queue<std::pair<Assign, QVector<Frame<QPixmap>>>> &queued,
std::mutex &mutex, std::atomic_bool &loadedEventQueued)
int duration = std::max(20, reader.nextImageDelay()); {
frames.push_back(Frame<QImage>{image, duration});
}
}
if (frames.size() == 0) {
log("Error while reading image {}: '{}'", url.string,
reader.errorString());
}
return frames;
}
// parsed
template <typename Assign>
void assignDelayed(
std::queue<std::pair<Assign, QVector<Frame<QPixmap>>>> &queued,
std::mutex &mutex, std::atomic_bool &loadedEventQueued)
{
std::lock_guard<std::mutex> lock(mutex);
int i = 0;
while (!queued.empty()) {
queued.front().first(queued.front().second);
queued.pop();
if (++i > 50) {
QTimer::singleShot(
3, [&] { assignDelayed(queued, mutex, loadedEventQueued); });
return;
}
}
getApp()->windows->forceLayoutChannelViews();
loadedEventQueued = false;
}
template <typename Assign>
auto makeConvertCallback(const QVector<Frame<QImage>> &parsed, Assign assign)
{
return [parsed, assign] {
// convert to pixmap
auto frames = QVector<Frame<QPixmap>>();
std::transform(parsed.begin(), parsed.end(), std::back_inserter(frames),
[](auto &frame) {
return Frame<QPixmap>{
QPixmap::fromImage(frame.image), frame.duration};
});
// put into stack
static std::queue<std::pair<Assign, QVector<Frame<QPixmap>>>> queued;
static std::mutex mutex;
std::lock_guard<std::mutex> lock(mutex); std::lock_guard<std::mutex> lock(mutex);
queued.emplace(assign, frames); int i = 0;
static std::atomic_bool loadedEventQueued{false}; while (!queued.empty()) {
queued.front().first(queued.front().second);
queued.pop();
if (!loadedEventQueued) { if (++i > 50) {
loadedEventQueued = true; QTimer::singleShot(3, [&] {
assignDelayed(queued, mutex, loadedEventQueued);
QTimer::singleShot( });
100, [=] { assignDelayed(queued, mutex, loadedEventQueued); }); return;
}
} }
};
} getApp()->windows->forceLayoutChannelViews();
loadedEventQueued = false;
}
template <typename Assign>
auto makeConvertCallback(const QVector<Frame<QImage>> &parsed,
Assign assign)
{
return [parsed, assign] {
// convert to pixmap
auto frames = QVector<Frame<QPixmap>>();
std::transform(parsed.begin(), parsed.end(),
std::back_inserter(frames), [](auto &frame) {
return Frame<QPixmap>{
QPixmap::fromImage(frame.image),
frame.duration};
});
// put into stack
static std::queue<std::pair<Assign, QVector<Frame<QPixmap>>>>
queued;
static std::mutex mutex;
std::lock_guard<std::mutex> lock(mutex);
queued.emplace(assign, frames);
static std::atomic_bool loadedEventQueued{false};
if (!loadedEventQueued) {
loadedEventQueued = true;
QTimer::singleShot(100, [=] {
assignDelayed(queued, mutex, loadedEventQueued);
});
}
};
}
} // namespace } // namespace
// IMAGE2 // IMAGE2

View file

@ -1,45 +1,45 @@
#pragma once #pragma once
#include "common/Common.hpp"
#include <QPixmap> #include <QPixmap>
#include <QString> #include <QString>
#include <QThread> #include <QThread>
#include <QVector> #include <QVector>
#include <atomic> #include <atomic>
#include <boost/noncopyable.hpp> #include <boost/noncopyable.hpp>
#include <boost/optional.hpp>
#include <boost/variant.hpp> #include <boost/variant.hpp>
#include <memory> #include <memory>
#include <mutex> #include <mutex>
#include <pajlada/signals/signal.hpp> #include <pajlada/signals/signal.hpp>
#include "common/Aliases.hpp"
#include "common/NullablePtr.hpp" #include "common/NullablePtr.hpp"
namespace chatterino { namespace chatterino {
namespace { namespace {
template <typename Image> template <typename Image>
struct Frame { struct Frame {
Image image; Image image;
int duration; int duration;
}; };
class Frames : boost::noncopyable class Frames : boost::noncopyable
{ {
public: public:
Frames(); Frames();
Frames(const QVector<Frame<QPixmap>> &frames); Frames(const QVector<Frame<QPixmap>> &frames);
~Frames(); ~Frames();
bool animated() const; bool animated() const;
void advance(); void advance();
boost::optional<QPixmap> current() const; boost::optional<QPixmap> current() const;
boost::optional<QPixmap> first() const; boost::optional<QPixmap> first() const;
private: private:
QVector<Frame<QPixmap>> items_; QVector<Frame<QPixmap>> items_;
int index_{0}; int index_{0};
int durationOffset_{0}; int durationOffset_{0};
pajlada::Signals::Connection gifTimerConnection_; pajlada::Signals::Connection gifTimerConnection_;
}; };
} // namespace } // namespace
class Image; class Image;

View file

@ -76,7 +76,7 @@ const ImagePtr &ImageSet::getImage(float scale) const
} }
if (!this->imageX2_->isEmpty() && quality == 2) { if (!this->imageX2_->isEmpty() && quality == 2) {
return this->imageX3_; return this->imageX2_;
} }
return this->imageX1_; return this->imageX1_;

View file

@ -1,7 +1,6 @@
#pragma once #pragma once
#include <QString> #include <QString>
#include <common/Common.hpp>
namespace chatterino { namespace chatterino {

View file

@ -1,16 +1,16 @@
#pragma once #pragma once
#include "common/FlagsEnum.hpp" #include "common/FlagsEnum.hpp"
#include "messages/MessageElement.hpp"
#include "providers/twitch/PubsubActions.hpp"
#include "widgets/helper/ScrollbarHighlight.hpp" #include "widgets/helper/ScrollbarHighlight.hpp"
#include <QTime> #include <QTime>
#include <boost/noncopyable.hpp>
#include <cinttypes> #include <cinttypes>
#include <memory> #include <memory>
#include <vector> #include <vector>
namespace chatterino { namespace chatterino {
class MessageElement;
enum class MessageFlag : uint16_t { enum class MessageFlag : uint16_t {
None = 0, None = 0,

View file

@ -1,6 +1,9 @@
#include "MessageBuilder.hpp" #include "MessageBuilder.hpp"
#include "common/LinkParser.hpp" #include "common/LinkParser.hpp"
#include "messages/Message.hpp"
#include "messages/MessageElement.hpp"
#include "providers/twitch/PubsubActions.hpp"
#include "singletons/Emotes.hpp" #include "singletons/Emotes.hpp"
#include "singletons/Resources.hpp" #include "singletons/Resources.hpp"
#include "singletons/Theme.hpp" #include "singletons/Theme.hpp"
@ -17,7 +20,7 @@ MessagePtr makeSystemMessage(const QString &text)
} }
MessageBuilder::MessageBuilder() MessageBuilder::MessageBuilder()
: message_(std::make_unique<Message>()) : message_(std::make_shared<Message>())
{ {
} }
@ -165,7 +168,9 @@ Message &MessageBuilder::message()
MessagePtr MessageBuilder::release() MessagePtr MessageBuilder::release()
{ {
return MessagePtr(this->message_.release()); std::shared_ptr<Message> ptr;
this->message_.swap(ptr);
return ptr;
} }
void MessageBuilder::append(std::unique_ptr<MessageElement> element) void MessageBuilder::append(std::unique_ptr<MessageElement> element)

View file

@ -1,11 +1,15 @@
#pragma once #pragma once
#include "messages/Message.hpp" #include "messages/MessageElement.hpp"
#include <QRegularExpression> #include <QRegularExpression>
#include <ctime> #include <ctime>
namespace chatterino { namespace chatterino {
struct BanAction;
struct UnbanAction;
struct Message;
using MessagePtr = std::shared_ptr<const Message>;
struct SystemMessageTag { struct SystemMessageTag {
}; };
@ -56,7 +60,7 @@ public:
} }
private: private:
std::unique_ptr<Message> message_; std::shared_ptr<Message> message_;
}; };
} // namespace chatterino } // namespace chatterino

View file

@ -1,5 +1,7 @@
#include "MessageColor.hpp" #include "MessageColor.hpp"
#include "singletons/Theme.hpp"
namespace chatterino { namespace chatterino {
MessageColor::MessageColor(const QColor &color) MessageColor::MessageColor(const QColor &color)

View file

@ -1,10 +1,9 @@
#pragma once #pragma once
#include "singletons/Theme.hpp"
#include <QColor> #include <QColor>
namespace chatterino { namespace chatterino {
class Theme;
struct MessageColor { struct MessageColor {
enum Type { Custom, Text, Link, System }; enum Type { Custom, Text, Link, System };

View file

@ -3,9 +3,11 @@
#include "Application.hpp" #include "Application.hpp"
#include "controllers/moderationactions/ModerationActions.hpp" #include "controllers/moderationactions/ModerationActions.hpp"
#include "debug/Benchmark.hpp" #include "debug/Benchmark.hpp"
#include "messages/Emote.hpp"
#include "messages/layouts/MessageLayoutContainer.hpp" #include "messages/layouts/MessageLayoutContainer.hpp"
#include "messages/layouts/MessageLayoutElement.hpp" #include "messages/layouts/MessageLayoutElement.hpp"
#include "singletons/Settings.hpp" #include "singletons/Settings.hpp"
#include "singletons/Theme.hpp"
#include "util/DebugCount.hpp" #include "util/DebugCount.hpp"
namespace chatterino { namespace chatterino {
@ -223,8 +225,8 @@ void TimestampElement::addToContainer(MessageLayoutContainer &container,
{ {
if (flags.hasAny(this->getFlags())) { if (flags.hasAny(this->getFlags())) {
auto app = getApp(); auto app = getApp();
if (app->settings->timestampFormat != this->format_) { if (getSettings()->timestampFormat != this->format_) {
this->format_ = app->settings->timestampFormat.getValue(); this->format_ = getSettings()->timestampFormat.getValue();
this->element_.reset(this->formatTime(this->time_)); this->element_.reset(this->formatTime(this->time_));
} }
@ -236,7 +238,7 @@ TextElement *TimestampElement::formatTime(const QTime &time)
{ {
static QLocale locale("en_US"); static QLocale locale("en_US");
QString format = locale.toString(time, getApp()->settings->timestampFormat); QString format = locale.toString(time, getSettings()->timestampFormat);
return new TextElement(format, MessageElementFlag::Timestamp, return new TextElement(format, MessageElementFlag::Timestamp,
MessageColor::System, FontStyle::ChatMedium); MessageColor::System, FontStyle::ChatMedium);

View file

@ -1,8 +1,6 @@
#pragma once #pragma once
#include "common/FlagsEnum.hpp" #include "common/FlagsEnum.hpp"
#include "messages/Emote.hpp"
#include "messages/Image.hpp"
#include "messages/Link.hpp" #include "messages/Link.hpp"
#include "messages/MessageColor.hpp" #include "messages/MessageColor.hpp"
#include "singletons/Fonts.hpp" #include "singletons/Fonts.hpp"
@ -11,14 +9,20 @@
#include <QString> #include <QString>
#include <QTime> #include <QTime>
#include <boost/noncopyable.hpp> #include <boost/noncopyable.hpp>
#include <cstdint> #include <cstdint>
#include <memory> #include <memory>
#include <vector>
namespace chatterino { namespace chatterino {
class Channel; class Channel;
struct MessageLayoutContainer; struct MessageLayoutContainer;
class Image;
using ImagePtr = std::shared_ptr<Image>;
struct Emote;
using EmotePtr = std::shared_ptr<const Emote>;
enum class MessageElementFlag { enum class MessageElementFlag {
None = 0, None = 0,
Misc = (1 << 0), Misc = (1 << 0),

View file

@ -2,5 +2,4 @@
namespace chatterino { namespace chatterino {
} // namespace chatterino } // namespace chatterino

View file

@ -2,8 +2,12 @@
#include "Application.hpp" #include "Application.hpp"
#include "debug/Benchmark.hpp" #include "debug/Benchmark.hpp"
#include "messages/Message.hpp"
#include "messages/MessageElement.hpp"
#include "messages/layouts/MessageLayoutContainer.hpp"
#include "singletons/Emotes.hpp" #include "singletons/Emotes.hpp"
#include "singletons/Settings.hpp" #include "singletons/Settings.hpp"
#include "singletons/Theme.hpp"
#include "singletons/WindowManager.hpp" #include "singletons/WindowManager.hpp"
#include "util/DebugCount.hpp" #include "util/DebugCount.hpp"
@ -24,6 +28,7 @@ namespace chatterino {
MessageLayout::MessageLayout(MessagePtr message) MessageLayout::MessageLayout(MessagePtr message)
: message_(message) : message_(message)
, buffer_(nullptr) , buffer_(nullptr)
, container_(std::make_shared<MessageLayoutContainer>())
{ {
DebugCount::increase("message layout"); DebugCount::increase("message layout");
} }
@ -41,7 +46,7 @@ const Message *MessageLayout::getMessage()
// Height // Height
int MessageLayout::getHeight() const int MessageLayout::getHeight() const
{ {
return container_.getHeight(); return container_->getHeight();
} }
// Layout // Layout
@ -68,7 +73,7 @@ bool MessageLayout::layout(int width, float scale, MessageElementFlags flags)
// check if work mask changed // check if work mask changed
layoutRequired |= this->currentWordFlags_ != flags; layoutRequired |= this->currentWordFlags_ != flags;
this->currentWordFlags_ = flags; // app->settings->getWordTypeMask(); this->currentWordFlags_ = flags; // getSettings()->getWordTypeMask();
// check if layout was requested manually // check if layout was requested manually
layoutRequired |= this->flags.has(MessageLayoutFlag::RequiresLayout); layoutRequired |= this->flags.has(MessageLayoutFlag::RequiresLayout);
@ -82,9 +87,9 @@ bool MessageLayout::layout(int width, float scale, MessageElementFlags flags)
return false; return false;
} }
int oldHeight = this->container_.getHeight(); int oldHeight = this->container_->getHeight();
this->actuallyLayout(width, flags); this->actuallyLayout(width, flags);
if (widthChanged || this->container_.getHeight() != oldHeight) { if (widthChanged || this->container_->getHeight() != oldHeight) {
this->deleteBuffer(); this->deleteBuffer();
} }
this->invalidateBuffer(); this->invalidateBuffer();
@ -103,22 +108,22 @@ void MessageLayout::actuallyLayout(int width, MessageElementFlags _flags)
messageFlags.unset(MessageFlag::Collapsed); messageFlags.unset(MessageFlag::Collapsed);
} }
this->container_.begin(width, this->scale_, messageFlags); this->container_->begin(width, this->scale_, messageFlags);
for (const auto &element : this->message_->elements) { for (const auto &element : this->message_->elements) {
element->addToContainer(this->container_, _flags); element->addToContainer(*this->container_, _flags);
} }
if (this->height_ != this->container_.getHeight()) { if (this->height_ != this->container_->getHeight()) {
this->deleteBuffer(); this->deleteBuffer();
} }
this->container_.end(); this->container_->end();
this->height_ = this->container_.getHeight(); this->height_ = this->container_->getHeight();
// collapsed state // collapsed state
this->flags.unset(MessageLayoutFlag::Collapsed); this->flags.unset(MessageLayoutFlag::Collapsed);
if (this->container_.isCollapsed()) { if (this->container_->isCollapsed()) {
this->flags.set(MessageLayoutFlag::Collapsed); this->flags.set(MessageLayoutFlag::Collapsed);
} }
} }
@ -135,11 +140,12 @@ void MessageLayout::paint(QPainter &painter, int width, int y, int messageIndex,
if (!pixmap) { if (!pixmap) {
#ifdef Q_OS_MACOS #ifdef Q_OS_MACOS
pixmap = new QPixmap(int(width * painter.device()->devicePixelRatioF()), pixmap = new QPixmap(int(width * painter.device()->devicePixelRatioF()),
int(container_.getHeight() * int(container_->getHeight() *
painter.device()->devicePixelRatioF())); painter.device()->devicePixelRatioF()));
pixmap->setDevicePixelRatio(painter.device()->devicePixelRatioF()); pixmap->setDevicePixelRatio(painter.device()->devicePixelRatioF());
#else #else
pixmap = new QPixmap(width, std::max(16, this->container_.getHeight())); pixmap =
new QPixmap(width, std::max(16, this->container_->getHeight()));
#endif #endif
this->buffer_ = std::shared_ptr<QPixmap>(pixmap); this->buffer_ = std::shared_ptr<QPixmap>(pixmap);
@ -157,7 +163,7 @@ void MessageLayout::paint(QPainter &painter, int width, int y, int messageIndex,
// this->container.getHeight(), *pixmap); // this->container.getHeight(), *pixmap);
// draw gif emotes // draw gif emotes
this->container_.paintAnimatedElements(painter, y); this->container_->paintAnimatedElements(painter, y);
// draw disabled // draw disabled
if (this->message_->flags.has(MessageFlag::Disabled)) { if (this->message_->flags.has(MessageFlag::Disabled)) {
@ -167,12 +173,12 @@ void MessageLayout::paint(QPainter &painter, int width, int y, int messageIndex,
// draw selection // draw selection
if (!selection.isEmpty()) { if (!selection.isEmpty()) {
this->container_.paintSelection(painter, messageIndex, selection, y); this->container_->paintSelection(painter, messageIndex, selection, y);
} }
// draw message seperation line // draw message seperation line
if (app->settings->separateMessages.getValue()) { if (getSettings()->separateMessages.getValue()) {
painter.fillRect(0, y, this->container_.getWidth(), 1, painter.fillRect(0, y, this->container_->getWidth(), 1,
app->themes->splits.messageSeperator); app->themes->splits.messageSeperator);
} }
@ -184,9 +190,9 @@ void MessageLayout::paint(QPainter &painter, int width, int y, int messageIndex,
: app->themes->tabs.selected.backgrounds.unfocused.color(); : app->themes->tabs.selected.backgrounds.unfocused.color();
QBrush brush(color, static_cast<Qt::BrushStyle>( QBrush brush(color, static_cast<Qt::BrushStyle>(
app->settings->lastMessagePattern.getValue())); getSettings()->lastMessagePattern.getValue()));
painter.fillRect(0, y + this->container_.getHeight() - 1, painter.fillRect(0, y + this->container_->getHeight() - 1,
pixmap->width(), 1, brush); pixmap->width(), 1, brush);
} }
@ -208,7 +214,7 @@ void MessageLayout::updateBuffer(QPixmap *buffer, int /*messageIndex*/,
backgroundColor = app->themes->messages.backgrounds.highlighted; backgroundColor = app->themes->messages.backgrounds.highlighted;
} else if (this->message_->flags.has(MessageFlag::Subscription)) { } else if (this->message_->flags.has(MessageFlag::Subscription)) {
backgroundColor = app->themes->messages.backgrounds.subscription; backgroundColor = app->themes->messages.backgrounds.subscription;
} else if (app->settings->alternateMessageBackground.getValue() && } else if (getSettings()->alternateMessageBackground.getValue() &&
this->flags.has(MessageLayoutFlag::AlternateBackground)) { this->flags.has(MessageLayoutFlag::AlternateBackground)) {
backgroundColor = app->themes->messages.backgrounds.alternate; backgroundColor = app->themes->messages.backgrounds.alternate;
} else { } else {
@ -217,7 +223,7 @@ void MessageLayout::updateBuffer(QPixmap *buffer, int /*messageIndex*/,
painter.fillRect(buffer->rect(), backgroundColor); painter.fillRect(buffer->rect(), backgroundColor);
// draw message // draw message
this->container_.paintElements(painter); this->container_->paintElements(painter);
#ifdef FOURTF #ifdef FOURTF
// debug // debug
@ -252,7 +258,7 @@ void MessageLayout::deleteCache()
this->deleteBuffer(); this->deleteBuffer();
#ifdef XD #ifdef XD
this->container_.clear(); this->container_->clear();
#endif #endif
} }
@ -265,22 +271,23 @@ void MessageLayout::deleteCache()
const MessageLayoutElement *MessageLayout::getElementAt(QPoint point) const MessageLayoutElement *MessageLayout::getElementAt(QPoint point)
{ {
// go through all words and return the first one that contains the point. // go through all words and return the first one that contains the point.
return this->container_.getElementAt(point); return this->container_->getElementAt(point);
} }
int MessageLayout::getLastCharacterIndex() const int MessageLayout::getLastCharacterIndex() const
{ {
return this->container_.getLastCharacterIndex(); return this->container_->getLastCharacterIndex();
} }
int MessageLayout::getSelectionIndex(QPoint position) int MessageLayout::getSelectionIndex(QPoint position)
{ {
return this->container_.getSelectionIndex(position); return this->container_->getSelectionIndex(position);
} }
void MessageLayout::addSelectionText(QString &str, int from, int to) void MessageLayout::addSelectionText(QString &str, int from, int to,
CopyMode copymode)
{ {
this->container_.addSelectionText(str, from, to); this->container_->addSelectionText(str, from, to, copymode);
} }
} // namespace chatterino } // namespace chatterino

View file

@ -1,19 +1,25 @@
#pragma once #pragma once
#include "common/Common.hpp"
#include "common/FlagsEnum.hpp" #include "common/FlagsEnum.hpp"
#include "messages/Message.hpp"
#include "messages/Selection.hpp"
#include "messages/layouts/MessageLayoutContainer.hpp"
#include "messages/layouts/MessageLayoutElement.hpp"
#include <QPixmap> #include <QPixmap>
#include <boost/noncopyable.hpp> #include <boost/noncopyable.hpp>
#include <cinttypes> #include <cinttypes>
#include <memory> #include <memory>
namespace chatterino { namespace chatterino {
struct Message;
using MessagePtr = std::shared_ptr<const Message>;
struct Selection;
struct MessageLayoutContainer;
class MessageLayoutElement;
enum class MessageElementFlag;
using MessageElementFlags = FlagsEnum<MessageElementFlag>;
enum class MessageLayoutFlag : uint8_t { enum class MessageLayoutFlag : uint8_t {
RequiresBufferUpdate = 1 << 1, RequiresBufferUpdate = 1 << 1,
RequiresLayout = 1 << 2, RequiresLayout = 1 << 2,
@ -49,7 +55,8 @@ public:
const MessageLayoutElement *getElementAt(QPoint point); const MessageLayoutElement *getElementAt(QPoint point);
int getLastCharacterIndex() const; int getLastCharacterIndex() const;
int getSelectionIndex(QPoint position); int getSelectionIndex(QPoint position);
void addSelectionText(QString &str, int from = 0, int to = INT_MAX); void addSelectionText(QString &str, int from = 0, int to = INT_MAX,
CopyMode copymode = CopyMode::Everything);
// Misc // Misc
bool isDisabled() const; bool isDisabled() const;
@ -57,7 +64,7 @@ public:
private: private:
// variables // variables
MessagePtr message_; MessagePtr message_;
MessageLayoutContainer container_; std::shared_ptr<MessageLayoutContainer> container_;
std::shared_ptr<QPixmap> buffer_ = nullptr; std::shared_ptr<QPixmap> buffer_ = nullptr;
bool bufferValid_ = false; bool bufferValid_ = false;

View file

@ -1,16 +1,20 @@
#include "MessageLayoutContainer.hpp" #include "MessageLayoutContainer.hpp"
#include "Application.hpp" #include "Application.hpp"
#include "MessageLayoutElement.hpp" #include "messages/Message.hpp"
#include "messages/MessageElement.hpp"
#include "messages/Selection.hpp" #include "messages/Selection.hpp"
#include "messages/layouts/MessageLayoutElement.hpp"
#include "singletons/Fonts.hpp"
#include "singletons/Settings.hpp" #include "singletons/Settings.hpp"
#include "singletons/Theme.hpp"
#include <QDebug> #include <QDebug>
#include <QPainter> #include <QPainter>
#define COMPACT_EMOTES_OFFSET 6 #define COMPACT_EMOTES_OFFSET 6
#define MAX_UNCOLLAPSED_LINES \ #define MAX_UNCOLLAPSED_LINES \
(getApp()->settings->collpseMessagesMinLines.getValue()) (getSettings()->collpseMessagesMinLines.getValue())
namespace chatterino { namespace chatterino {
@ -231,7 +235,7 @@ void MessageLayoutContainer::end()
bool MessageLayoutContainer::canCollapse() bool MessageLayoutContainer::canCollapse()
{ {
return getApp()->settings->collpseMessagesMinLines.getValue() > 0 && return getSettings()->collpseMessagesMinLines.getValue() > 0 &&
this->flags_.has(MessageFlag::Collapsed); this->flags_.has(MessageFlag::Collapsed);
} }
@ -501,33 +505,41 @@ int MessageLayoutContainer::getLastCharacterIndex() const
return this->lines_.back().endCharIndex; return this->lines_.back().endCharIndex;
} }
void MessageLayoutContainer::addSelectionText(QString &str, int from, int to) void MessageLayoutContainer::addSelectionText(QString &str, int from, int to,
CopyMode copymode)
{ {
int index = 0; int index = 0;
bool first = true; bool first = true;
for (std::unique_ptr<MessageLayoutElement> &ele : this->elements_) { for (auto &element : this->elements_) {
int c = ele->getSelectionIndexCount(); if (copymode == CopyMode::OnlyTextAndEmotes) {
if (element->getCreator().getFlags().hasAny(
{MessageElementFlag::Timestamp,
MessageElementFlag::Username, MessageElementFlag::Badges}))
continue;
}
auto indexCount = element->getSelectionIndexCount();
if (first) { if (first) {
if (index + c > from) { if (index + indexCount > from) {
ele->addCopyTextToString(str, from - index, to - index); element->addCopyTextToString(str, from - index, to - index);
first = false; first = false;
if (index + c > to) { if (index + indexCount > to) {
break; break;
} }
} }
} else { } else {
if (index + c > to) { if (index + indexCount > to) {
ele->addCopyTextToString(str, 0, to - index); element->addCopyTextToString(str, 0, to - index);
break; break;
} else { } else {
ele->addCopyTextToString(str); element->addCopyTextToString(str);
} }
} }
index += c; index += indexCount;
} }
} }

View file

@ -1,18 +1,21 @@
#pragma once #pragma once
#include <memory>
#include <vector>
#include <QPoint> #include <QPoint>
#include <QRect> #include <QRect>
#include <memory>
#include <vector>
#include "messages/Message.hpp" #include "common/Common.hpp"
#include "common/FlagsEnum.hpp"
#include "messages/Selection.hpp" #include "messages/Selection.hpp"
#include "messages/layouts/MessageLayoutElement.hpp"
class QPainter; class QPainter;
namespace chatterino { namespace chatterino {
class MessageLayoutElement;
enum class MessageFlag : uint16_t;
using MessageFlags = FlagsEnum<MessageFlag>;
struct Margin { struct Margin {
int top; int top;
@ -72,7 +75,7 @@ struct MessageLayoutContainer {
// selection // selection
int getSelectionIndex(QPoint point); int getSelectionIndex(QPoint point);
int getLastCharacterIndex() const; int getLastCharacterIndex() const;
void addSelectionText(QString &str, int from, int to); void addSelectionText(QString &str, int from, int to, CopyMode copymode);
bool isCollapsed(); bool isCollapsed();
@ -92,7 +95,7 @@ private:
// variables // variables
float scale_ = 1.f; float scale_ = 1.f;
int width_ = 0; int width_ = 0;
MessageFlags flags_ = MessageFlag::None; MessageFlags flags_{};
int line_ = 0; int line_ = 0;
int height_ = 0; int height_ = 0;
int currentX_ = 0; int currentX_ = 0;

View file

@ -1,7 +1,9 @@
#include "messages/layouts/MessageLayoutElement.hpp" #include "messages/layouts/MessageLayoutElement.hpp"
#include "Application.hpp" #include "Application.hpp"
#include "messages/Image.hpp"
#include "messages/MessageElement.hpp" #include "messages/MessageElement.hpp"
#include "singletons/Theme.hpp"
#include "util/DebugCount.hpp" #include "util/DebugCount.hpp"
#include <QDebug> #include <QDebug>

View file

@ -3,19 +3,19 @@
#include <QPoint> #include <QPoint>
#include <QRect> #include <QRect>
#include <QString> #include <QString>
#include <boost/noncopyable.hpp> #include <boost/noncopyable.hpp>
#include <climits> #include <climits>
#include "messages/Image.hpp"
#include "messages/Link.hpp" #include "messages/Link.hpp"
#include "messages/MessageColor.hpp" #include "messages/MessageColor.hpp"
#include "singletons/Fonts.hpp"
class QPainter; class QPainter;
namespace chatterino { namespace chatterino {
class MessageElement; class MessageElement;
class Image;
using ImagePtr = std::shared_ptr<Image>;
enum class FontStyle : uint8_t;
class MessageLayoutElement : boost::noncopyable class MessageLayoutElement : boost::noncopyable
{ {

View file

@ -1,7 +1,9 @@
#include "providers/bttv/BttvEmotes.hpp" #include "providers/bttv/BttvEmotes.hpp"
#include "common/Common.hpp"
#include "common/NetworkRequest.hpp" #include "common/NetworkRequest.hpp"
#include "debug/Log.hpp" #include "debug/Log.hpp"
#include "messages/Emote.hpp"
#include "messages/Image.hpp" #include "messages/Image.hpp"
#include "messages/ImageSet.hpp" #include "messages/ImageSet.hpp"
#include "providers/twitch/TwitchChannel.hpp" #include "providers/twitch/TwitchChannel.hpp"
@ -11,73 +13,76 @@
namespace chatterino { namespace chatterino {
namespace { namespace {
Url getEmoteLink(QString urlTemplate, const EmoteId &id, Url getEmoteLink(QString urlTemplate, const EmoteId &id,
const QString &emoteScale) const QString &emoteScale)
{ {
urlTemplate.detach(); urlTemplate.detach();
return {urlTemplate.replace("{{id}}", id.string) return {urlTemplate.replace("{{id}}", id.string)
.replace("{{image}}", emoteScale)}; .replace("{{image}}", emoteScale)};
}
std::pair<Outcome, EmoteMap> parseGlobalEmotes(const QJsonObject &jsonRoot,
const EmoteMap &currentEmotes)
{
auto emotes = EmoteMap();
auto jsonEmotes = jsonRoot.value("emotes").toArray();
auto urlTemplate = qS("https:") + jsonRoot.value("urlTemplate").toString();
for (auto jsonEmote : jsonEmotes) {
auto id = EmoteId{jsonEmote.toObject().value("id").toString()};
auto name = EmoteName{jsonEmote.toObject().value("code").toString()};
auto emote = Emote(
{name,
ImageSet{
Image::fromUrl(getEmoteLink(urlTemplate, id, "1x"), 1),
Image::fromUrl(getEmoteLink(urlTemplate, id, "2x"), 0.5),
Image::fromUrl(getEmoteLink(urlTemplate, id, "3x"), 0.25)},
Tooltip{name.string + "<br />Global Bttv Emote"},
Url{"https://manage.betterttv.net/emotes/" + id.string}});
emotes[name] = cachedOrMakeEmotePtr(std::move(emote), currentEmotes);
} }
std::pair<Outcome, EmoteMap> parseGlobalEmotes(
const QJsonObject &jsonRoot, const EmoteMap &currentEmotes)
{
auto emotes = EmoteMap();
auto jsonEmotes = jsonRoot.value("emotes").toArray();
auto urlTemplate =
qS("https:") + jsonRoot.value("urlTemplate").toString();
return {Success, std::move(emotes)}; for (auto jsonEmote : jsonEmotes) {
} auto id = EmoteId{jsonEmote.toObject().value("id").toString()};
EmotePtr cachedOrMake(Emote &&emote, const EmoteId &id) auto name =
{ EmoteName{jsonEmote.toObject().value("code").toString()};
static std::unordered_map<EmoteId, std::weak_ptr<const Emote>> cache;
static std::mutex mutex;
return cachedOrMakeEmotePtr(std::move(emote), cache, mutex, id); auto emote = Emote(
} {name,
std::pair<Outcome, EmoteMap> parseChannelEmotes(const QJsonObject &jsonRoot) ImageSet{
{ Image::fromUrl(getEmoteLink(urlTemplate, id, "1x"), 1),
auto emotes = EmoteMap(); Image::fromUrl(getEmoteLink(urlTemplate, id, "2x"), 0.5),
auto jsonEmotes = jsonRoot.value("emotes").toArray(); Image::fromUrl(getEmoteLink(urlTemplate, id, "3x"), 0.25)},
auto urlTemplate = "https:" + jsonRoot.value("urlTemplate").toString(); Tooltip{name.string + "<br />Global Bttv Emote"},
Url{"https://manage.betterttv.net/emotes/" + id.string}});
for (auto jsonEmote_ : jsonEmotes) { emotes[name] =
auto jsonEmote = jsonEmote_.toObject(); cachedOrMakeEmotePtr(std::move(emote), currentEmotes);
}
auto id = EmoteId{jsonEmote.value("id").toString()}; return {Success, std::move(emotes)};
auto name = EmoteName{jsonEmote.value("code").toString()};
// emoteObject.value("imageType").toString();
auto emote = Emote(
{name,
ImageSet{
Image::fromUrl(getEmoteLink(urlTemplate, id, "1x"), 1),
Image::fromUrl(getEmoteLink(urlTemplate, id, "2x"), 0.5),
Image::fromUrl(getEmoteLink(urlTemplate, id, "3x"), 0.25)},
Tooltip{name.string + "<br />Channel Bttv Emote"},
Url{"https://manage.betterttv.net/emotes/" + id.string}});
emotes[name] = cachedOrMake(std::move(emote), id);
} }
EmotePtr cachedOrMake(Emote &&emote, const EmoteId &id)
{
static std::unordered_map<EmoteId, std::weak_ptr<const Emote>> cache;
static std::mutex mutex;
return {Success, std::move(emotes)}; return cachedOrMakeEmotePtr(std::move(emote), cache, mutex, id);
} }
std::pair<Outcome, EmoteMap> parseChannelEmotes(const QJsonObject &jsonRoot)
{
auto emotes = EmoteMap();
auto jsonEmotes = jsonRoot.value("emotes").toArray();
auto urlTemplate = "https:" + jsonRoot.value("urlTemplate").toString();
for (auto jsonEmote_ : jsonEmotes) {
auto jsonEmote = jsonEmote_.toObject();
auto id = EmoteId{jsonEmote.value("id").toString()};
auto name = EmoteName{jsonEmote.value("code").toString()};
// emoteObject.value("imageType").toString();
auto emote = Emote(
{name,
ImageSet{
Image::fromUrl(getEmoteLink(urlTemplate, id, "1x"), 1),
Image::fromUrl(getEmoteLink(urlTemplate, id, "2x"), 0.5),
Image::fromUrl(getEmoteLink(urlTemplate, id, "3x"), 0.25)},
Tooltip{name.string + "<br />Channel Bttv Emote"},
Url{"https://manage.betterttv.net/emotes/" + id.string}});
emotes[name] = cachedOrMake(std::move(emote), id);
}
return {Success, std::move(emotes)};
}
} // namespace } // namespace
// //
@ -88,12 +93,12 @@ BttvEmotes::BttvEmotes()
{ {
} }
std::shared_ptr<const EmoteMap> BttvEmotes::global() const std::shared_ptr<const EmoteMap> BttvEmotes::emotes() const
{ {
return this->global_.get(); return this->global_.get();
} }
boost::optional<EmotePtr> BttvEmotes::global(const EmoteName &name) const boost::optional<EmotePtr> BttvEmotes::emote(const EmoteName &name) const
{ {
auto emotes = this->global_.get(); auto emotes = this->global_.get();
auto it = emotes->find(name); auto it = emotes->find(name);
@ -102,7 +107,7 @@ boost::optional<EmotePtr> BttvEmotes::global(const EmoteName &name) const
return it->second; return it->second;
} }
void BttvEmotes::loadGlobal() void BttvEmotes::loadEmotes()
{ {
auto request = NetworkRequest(QString(globalEmoteApiUrl)); auto request = NetworkRequest(QString(globalEmoteApiUrl));

View file

@ -1,11 +1,16 @@
#pragma once #pragma once
#include <memory> #include <memory>
#include "boost/optional.hpp"
#include "common/Aliases.hpp"
#include "common/Atomic.hpp" #include "common/Atomic.hpp"
#include "messages/Emote.hpp"
namespace chatterino { namespace chatterino {
struct Emote;
using EmotePtr = std::shared_ptr<const Emote>;
class EmoteMap;
class BttvEmotes final class BttvEmotes final
{ {
static constexpr const char *globalEmoteApiUrl = static constexpr const char *globalEmoteApiUrl =
@ -16,9 +21,9 @@ class BttvEmotes final
public: public:
BttvEmotes(); BttvEmotes();
std::shared_ptr<const EmoteMap> global() const; std::shared_ptr<const EmoteMap> emotes() const;
boost::optional<EmotePtr> global(const EmoteName &name) const; boost::optional<EmotePtr> emote(const EmoteName &name) const;
void loadGlobal(); void loadEmotes();
static void loadChannel(const QString &channelName, static void loadChannel(const QString &channelName,
std::function<void(EmoteMap &&)> callback); std::function<void(EmoteMap &&)> callback);

View file

@ -14,39 +14,41 @@ ChatterinoBadges::ChatterinoBadges()
boost::optional<EmotePtr> ChatterinoBadges::getBadge(const UserName &username) boost::optional<EmotePtr> ChatterinoBadges::getBadge(const UserName &username)
{ {
return this->badges.access()->get(username); return boost::none;
// return this->badges.access()->get(username);
} }
void ChatterinoBadges::loadChatterinoBadges() void ChatterinoBadges::loadChatterinoBadges()
{ {
static QString url("https://fourtf.com/chatterino/badges.json"); // static QString url("https://fourtf.com/chatterino/badges.json");
NetworkRequest req(url); // NetworkRequest req(url);
req.setCaller(QThread::currentThread()); // req.setCaller(QThread::currentThread());
req.onSuccess([this](auto result) { // req.onSuccess([this](auto result) {
auto jsonRoot = result.parseJson(); // auto jsonRoot = result.parseJson();
auto badges = this->badges.access(); // auto badges = this->badges.access();
auto replacement = badges->makeReplacment(); // auto replacement = badges->makeReplacment();
for (auto jsonBadge_ : jsonRoot.value("badges").toArray()) { // for (auto jsonBadge_ : jsonRoot.value("badges").toArray()) {
auto jsonBadge = jsonBadge_.toObject(); // auto jsonBadge = jsonBadge_.toObject();
auto emote = Emote{ // auto emote = Emote{
EmoteName{}, ImageSet{Url{jsonBadge.value("image").toString()}}, // EmoteName{},
Tooltip{jsonBadge.value("tooltip").toString()}, Url{}}; // ImageSet{Url{jsonBadge.value("image").toString()}},
// Tooltip{jsonBadge.value("tooltip").toString()}, Url{}};
for (auto jsonUser : jsonBadge.value("users").toArray()) { // for (auto jsonUser : jsonBadge.value("users").toArray()) {
replacement.add(UserName{jsonUser.toString()}, // replacement.add(UserName{jsonUser.toString()},
std::move(emote)); // std::move(emote));
} // }
} // }
replacement.apply(); // replacement.apply();
return Success; // return Success;
}); //});
req.execute(); // req.execute();
} }
} // namespace chatterino } // namespace chatterino

View file

@ -1,14 +1,14 @@
#pragma once #pragma once
#include <boost/optional.hpp> #include <boost/optional.hpp>
#include <unordered_map>
#include "common/Common.hpp" #include "common/Aliases.hpp"
#include "common/UniqueAccess.hpp"
#include "messages/Emote.hpp"
#include "messages/EmoteCache.hpp"
namespace chatterino { namespace chatterino {
struct Emote;
using EmotePtr = std::shared_ptr<const Emote>;
class ChatterinoBadges class ChatterinoBadges
{ {
public: public:
@ -19,7 +19,7 @@ public:
private: private:
void loadChatterinoBadges(); void loadChatterinoBadges();
UniqueAccess<EmoteCache<UserName>> badges; // UniqueAccess<EmoteCache<UserName>> badges;
}; };
} // namespace chatterino } // namespace chatterino

View file

@ -2,6 +2,7 @@
#include "Application.hpp" #include "Application.hpp"
#include "debug/Log.hpp" #include "debug/Log.hpp"
#include "messages/Emote.hpp"
#include "singletons/Settings.hpp" #include "singletons/Settings.hpp"
#include <rapidjson/error/en.h> #include <rapidjson/error/en.h>
@ -12,82 +13,81 @@
#include <memory> #include <memory>
namespace chatterino { namespace chatterino {
namespace { namespace {
void parseEmoji(const std::shared_ptr<EmojiData> &emojiData,
const rapidjson::Value &unparsedEmoji,
QString shortCode = QString())
{
static uint unicodeBytes[4];
void parseEmoji(const std::shared_ptr<EmojiData> &emojiData, struct {
const rapidjson::Value &unparsedEmoji, bool apple;
QString shortCode = QString()) bool google;
{ bool twitter;
static uint unicodeBytes[4]; bool emojione;
bool facebook;
bool messenger;
} capabilities;
struct { if (!shortCode.isEmpty()) {
bool apple; emojiData->shortCodes.push_back(shortCode);
bool google; } else {
bool twitter; const auto &shortCodes = unparsedEmoji["short_names"];
bool emojione; for (const auto &shortCode : shortCodes.GetArray()) {
bool facebook; emojiData->shortCodes.emplace_back(shortCode.GetString());
bool messenger; }
} capabilities;
if (!shortCode.isEmpty()) {
emojiData->shortCodes.push_back(shortCode);
} else {
const auto &shortCodes = unparsedEmoji["short_names"];
for (const auto &shortCode : shortCodes.GetArray()) {
emojiData->shortCodes.emplace_back(shortCode.GetString());
} }
}
rj::getSafe(unparsedEmoji, "non_qualified", emojiData->nonQualifiedCode); rj::getSafe(unparsedEmoji, "non_qualified",
rj::getSafe(unparsedEmoji, "unified", emojiData->unifiedCode); emojiData->nonQualifiedCode);
rj::getSafe(unparsedEmoji, "unified", emojiData->unifiedCode);
rj::getSafe(unparsedEmoji, "has_img_apple", capabilities.apple); rj::getSafe(unparsedEmoji, "has_img_apple", capabilities.apple);
rj::getSafe(unparsedEmoji, "has_img_google", capabilities.google); rj::getSafe(unparsedEmoji, "has_img_google", capabilities.google);
rj::getSafe(unparsedEmoji, "has_img_twitter", capabilities.twitter); rj::getSafe(unparsedEmoji, "has_img_twitter", capabilities.twitter);
rj::getSafe(unparsedEmoji, "has_img_emojione", capabilities.emojione); rj::getSafe(unparsedEmoji, "has_img_emojione", capabilities.emojione);
rj::getSafe(unparsedEmoji, "has_img_facebook", capabilities.facebook); rj::getSafe(unparsedEmoji, "has_img_facebook", capabilities.facebook);
rj::getSafe(unparsedEmoji, "has_img_messenger", capabilities.messenger); rj::getSafe(unparsedEmoji, "has_img_messenger", capabilities.messenger);
if (capabilities.apple) { if (capabilities.apple) {
emojiData->capabilities.insert("Apple"); emojiData->capabilities.insert("Apple");
} }
if (capabilities.google) { if (capabilities.google) {
emojiData->capabilities.insert("Google"); emojiData->capabilities.insert("Google");
} }
if (capabilities.twitter) { if (capabilities.twitter) {
emojiData->capabilities.insert("Twitter"); emojiData->capabilities.insert("Twitter");
} }
if (capabilities.emojione) { if (capabilities.emojione) {
emojiData->capabilities.insert("EmojiOne 3"); emojiData->capabilities.insert("EmojiOne 3");
} }
if (capabilities.facebook) { if (capabilities.facebook) {
emojiData->capabilities.insert("Facebook"); emojiData->capabilities.insert("Facebook");
} }
if (capabilities.messenger) { if (capabilities.messenger) {
emojiData->capabilities.insert("Messenger"); emojiData->capabilities.insert("Messenger");
} }
QStringList unicodeCharacters; QStringList unicodeCharacters;
if (!emojiData->nonQualifiedCode.isEmpty()) { if (!emojiData->nonQualifiedCode.isEmpty()) {
unicodeCharacters = emojiData->nonQualifiedCode.toLower().split('-'); unicodeCharacters =
} else { emojiData->nonQualifiedCode.toLower().split('-');
unicodeCharacters = emojiData->unifiedCode.toLower().split('-'); } else {
unicodeCharacters = emojiData->unifiedCode.toLower().split('-');
}
if (unicodeCharacters.length() < 1) {
return;
}
int numUnicodeBytes = 0;
for (const QString &unicodeCharacter : unicodeCharacters) {
unicodeBytes[numUnicodeBytes++] =
QString(unicodeCharacter).toUInt(nullptr, 16);
}
emojiData->value = QString::fromUcs4(unicodeBytes, numUnicodeBytes);
} }
if (unicodeCharacters.length() < 1) {
return;
}
int numUnicodeBytes = 0;
for (const QString &unicodeCharacter : unicodeCharacters) {
unicodeBytes[numUnicodeBytes++] =
QString(unicodeCharacter).toUInt(nullptr, 16);
}
emojiData->value = QString::fromUcs4(unicodeBytes, numUnicodeBytes);
}
} // namespace } // namespace
void Emojis::load() void Emojis::load()
@ -103,12 +103,10 @@ void Emojis::load()
void Emojis::loadEmojis() void Emojis::loadEmojis()
{ {
std::map<std::string, QString> toneNames; auto toneNames = std::map<std::string, QString>{
toneNames["1F3FB"] = "tone1"; {"1F3FB", "tone1"}, {"1F3FC", "tone2"}, {"1F3FD", "tone3"},
toneNames["1F3FC"] = "tone2"; {"1F3FE", "tone4"}, {"1F3FF", "tone5"},
toneNames["1F3FD"] = "tone3"; };
toneNames["1F3FE"] = "tone4";
toneNames["1F3FF"] = "tone5";
QFile file(":/emoji.json"); QFile file(":/emoji.json");
file.open(QFile::ReadOnly); file.open(QFile::ReadOnly);
@ -218,7 +216,7 @@ void Emojis::loadEmojiSet()
{ {
auto app = getApp(); auto app = getApp();
app->settings->emojiSet.connect([=](const auto &emojiSet, auto) { getSettings()->emojiSet.connect([=](const auto &emojiSet, auto) {
log("Using emoji set {}", emojiSet); log("Using emoji set {}", emojiSet);
this->emojis.each([=](const auto &name, this->emojis.each([=](const auto &name,
std::shared_ptr<EmojiData> &emoji) { std::shared_ptr<EmojiData> &emoji) {
@ -233,27 +231,12 @@ void Emojis::loadEmojiSet()
// {"Google", "https://cdn.jsdelivr.net/npm/emoji-datasource-google@4.0.4/img/google/64/"}, // {"Google", "https://cdn.jsdelivr.net/npm/emoji-datasource-google@4.0.4/img/google/64/"},
// {"Messenger", "https://cdn.jsdelivr.net/npm/emoji-datasource-messenger@4.0.4/img/messenger/64/"}, // {"Messenger", "https://cdn.jsdelivr.net/npm/emoji-datasource-messenger@4.0.4/img/messenger/64/"},
// {"EmojiOne 2", "https://cdnjs.cloudflare.com/ajax/libs/emojione/2.2.6/assets/png/"},
// {"EmojiOne 3", "https://braize.pajlada.com/emoji/img/emojione/64/"},
// {"Twitter", "https://braize.pajlada.com/emoji/img/twitter/64/"},
// {"Facebook", "https://braize.pajlada.com/emoji/img/facebook/64/"},
// {"Apple", "https://braize.pajlada.com/emoji/img/apple/64/"},
// {"Google", "https://braize.pajlada.com/emoji/img/google/64/"},
// {"Messenger", "https://braize.pajlada.com/emoji/img/messenger/64/"},
{"EmojiOne 3", "https://pajbot.com/static/emoji/img/emojione/64/"}, {"EmojiOne 3", "https://pajbot.com/static/emoji/img/emojione/64/"},
{"Twitter", "https://pajbot.com/static/emoji/img/twitter/64/"}, {"Twitter", "https://pajbot.com/static/emoji/img/twitter/64/"},
{"Facebook", "https://pajbot.com/static/emoji/img/facebook/64/"}, {"Facebook", "https://pajbot.com/static/emoji/img/facebook/64/"},
{"Apple", "https://pajbot.com/static/emoji/img/apple/64/"}, {"Apple", "https://pajbot.com/static/emoji/img/apple/64/"},
{"Google", "https://pajbot.com/static/emoji/img/google/64/"}, {"Google", "https://pajbot.com/static/emoji/img/google/64/"},
{"Messenger", "https://pajbot.com/static/emoji/img/messenger/64/"}, {"Messenger", "https://pajbot.com/static/emoji/img/messenger/64/"},
// {"EmojiOne 3", "https://cdn.fourtf.com/emoji/emojione/64/"},
// {"Twitter", "https://cdn.fourtf.com/emoji/twitter/64/"},
// {"Facebook", "https://cdn.fourtf.com/emoji/facebook/64/"},
// {"Apple", "https://cdn.fourtf.com/emoji/apple/64/"},
// {"Google", "https://cdn.fourtf.com/emoji/google/64/"},
// {"Messenger", "https://cdn.fourtf.com/emoji/messenger/64/"},
}; };
// clang-format on // clang-format on

View file

@ -1,6 +1,5 @@
#pragma once #pragma once
#include "messages/Emote.hpp"
#include "util/ConcurrentMap.hpp" #include "util/ConcurrentMap.hpp"
#include <QMap> #include <QMap>
@ -12,6 +11,9 @@
namespace chatterino { namespace chatterino {
struct Emote;
using EmotePtr = std::shared_ptr<const Emote>;
struct EmojiData { struct EmojiData {
// actual byte-representation of the emoji (i.e. \154075\156150 which is // actual byte-representation of the emoji (i.e. \154075\156150 which is
// :male:) // :male:)

View file

@ -3,104 +3,107 @@
#include <QJsonArray> #include <QJsonArray>
#include "common/NetworkRequest.hpp" #include "common/NetworkRequest.hpp"
#include "common/Outcome.hpp"
#include "debug/Log.hpp" #include "debug/Log.hpp"
#include "messages/Emote.hpp"
#include "messages/Image.hpp" #include "messages/Image.hpp"
namespace chatterino { namespace chatterino {
namespace { namespace {
Url getEmoteLink(const QJsonObject &urls, const QString &emoteScale) Url getEmoteLink(const QJsonObject &urls, const QString &emoteScale)
{ {
auto emote = urls.value(emoteScale); auto emote = urls.value(emoteScale);
if (emote.isUndefined()) { if (emote.isUndefined()) {
return {""}; return {""};
}
assert(emote.isString());
return {"https:" + emote.toString()};
}
void fillInEmoteData(const QJsonObject &urls, const EmoteName &name,
const QString &tooltip, Emote &emoteData)
{
auto url1x = getEmoteLink(urls, "1");
auto url2x = getEmoteLink(urls, "2");
auto url3x = getEmoteLink(urls, "4");
//, code, tooltip
emoteData.name = name;
emoteData.images =
ImageSet{Image::fromUrl(url1x, 1), Image::fromUrl(url2x, 0.5),
Image::fromUrl(url3x, 0.25)};
emoteData.tooltip = {tooltip};
}
EmotePtr cachedOrMake(Emote &&emote, const EmoteId &id)
{
static std::unordered_map<EmoteId, std::weak_ptr<const Emote>> cache;
static std::mutex mutex;
return cachedOrMakeEmotePtr(std::move(emote), cache, mutex, id);
}
std::pair<Outcome, EmoteMap> parseGlobalEmotes(const QJsonObject &jsonRoot,
const EmoteMap &currentEmotes)
{
auto jsonSets = jsonRoot.value("sets").toObject();
auto emotes = EmoteMap();
for (auto jsonSet : jsonSets) {
auto jsonEmotes = jsonSet.toObject().value("emoticons").toArray();
for (auto jsonEmoteValue : jsonEmotes) {
auto jsonEmote = jsonEmoteValue.toObject();
auto name = EmoteName{jsonEmote.value("name").toString()};
auto id = EmoteId{jsonEmote.value("id").toString()};
auto urls = jsonEmote.value("urls").toObject();
auto emote = Emote();
fillInEmoteData(urls, name, name.string + "<br/>Global FFZ Emote",
emote);
emote.homePage =
Url{QString("https://www.frankerfacez.com/emoticon/%1-%2")
.arg(id.string)
.arg(name.string)};
emotes[name] =
cachedOrMakeEmotePtr(std::move(emote), currentEmotes);
} }
assert(emote.isString());
return {"https:" + emote.toString()};
} }
void fillInEmoteData(const QJsonObject &urls, const EmoteName &name,
const QString &tooltip, Emote &emoteData)
{
auto url1x = getEmoteLink(urls, "1");
auto url2x = getEmoteLink(urls, "2");
auto url3x = getEmoteLink(urls, "4");
return {Success, std::move(emotes)}; //, code, tooltip
} emoteData.name = name;
std::pair<Outcome, EmoteMap> parseChannelEmotes(const QJsonObject &jsonRoot) emoteData.images =
{ ImageSet{Image::fromUrl(url1x, 1), Image::fromUrl(url2x, 0.5),
auto jsonSets = jsonRoot.value("sets").toObject(); Image::fromUrl(url3x, 0.25)};
auto emotes = EmoteMap(); emoteData.tooltip = {tooltip};
}
EmotePtr cachedOrMake(Emote &&emote, const EmoteId &id)
{
static std::unordered_map<EmoteId, std::weak_ptr<const Emote>> cache;
static std::mutex mutex;
for (auto jsonSet : jsonSets) { return cachedOrMakeEmotePtr(std::move(emote), cache, mutex, id);
auto jsonEmotes = jsonSet.toObject().value("emoticons").toArray(); }
std::pair<Outcome, EmoteMap> parseGlobalEmotes(
const QJsonObject &jsonRoot, const EmoteMap &currentEmotes)
{
auto jsonSets = jsonRoot.value("sets").toObject();
auto emotes = EmoteMap();
for (auto _jsonEmote : jsonEmotes) { for (auto jsonSet : jsonSets) {
auto jsonEmote = _jsonEmote.toObject(); auto jsonEmotes = jsonSet.toObject().value("emoticons").toArray();
// margins for (auto jsonEmoteValue : jsonEmotes) {
auto id = EmoteId{QString::number(jsonEmote.value("id").toInt())}; auto jsonEmote = jsonEmoteValue.toObject();
auto name = EmoteName{jsonEmote.value("name").toString()};
auto urls = jsonEmote.value("urls").toObject();
Emote emote; auto name = EmoteName{jsonEmote.value("name").toString()};
fillInEmoteData(urls, name, name.string + "<br/>Channel FFZ Emote", auto id = EmoteId{jsonEmote.value("id").toString()};
emote); auto urls = jsonEmote.value("urls").toObject();
emote.homePage =
Url{QString("https://www.frankerfacez.com/emoticon/%1-%2")
.arg(id.string)
.arg(name.string)};
emotes[name] = cachedOrMake(std::move(emote), id); auto emote = Emote();
fillInEmoteData(urls, name,
name.string + "<br/>Global FFZ Emote", emote);
emote.homePage =
Url{QString("https://www.frankerfacez.com/emoticon/%1-%2")
.arg(id.string)
.arg(name.string)};
emotes[name] =
cachedOrMakeEmotePtr(std::move(emote), currentEmotes);
}
} }
}
return {Success, std::move(emotes)}; return {Success, std::move(emotes)};
} }
std::pair<Outcome, EmoteMap> parseChannelEmotes(const QJsonObject &jsonRoot)
{
auto jsonSets = jsonRoot.value("sets").toObject();
auto emotes = EmoteMap();
for (auto jsonSet : jsonSets) {
auto jsonEmotes = jsonSet.toObject().value("emoticons").toArray();
for (auto _jsonEmote : jsonEmotes) {
auto jsonEmote = _jsonEmote.toObject();
// margins
auto id =
EmoteId{QString::number(jsonEmote.value("id").toInt())};
auto name = EmoteName{jsonEmote.value("name").toString()};
auto urls = jsonEmote.value("urls").toObject();
Emote emote;
fillInEmoteData(urls, name,
name.string + "<br/>Channel FFZ Emote", emote);
emote.homePage =
Url{QString("https://www.frankerfacez.com/emoticon/%1-%2")
.arg(id.string)
.arg(name.string)};
emotes[name] = cachedOrMake(std::move(emote), id);
}
}
return {Success, std::move(emotes)};
}
} // namespace } // namespace
FfzEmotes::FfzEmotes() FfzEmotes::FfzEmotes()
@ -108,12 +111,12 @@ FfzEmotes::FfzEmotes()
{ {
} }
std::shared_ptr<const EmoteMap> FfzEmotes::global() const std::shared_ptr<const EmoteMap> FfzEmotes::emotes() const
{ {
return this->global_.get(); return this->global_.get();
} }
boost::optional<EmotePtr> FfzEmotes::global(const EmoteName &name) const boost::optional<EmotePtr> FfzEmotes::emote(const EmoteName &name) const
{ {
auto emotes = this->global_.get(); auto emotes = this->global_.get();
auto it = emotes->find(name); auto it = emotes->find(name);
@ -121,7 +124,7 @@ boost::optional<EmotePtr> FfzEmotes::global(const EmoteName &name) const
return boost::none; return boost::none;
} }
void FfzEmotes::loadGlobal() void FfzEmotes::loadEmotes()
{ {
QString url("https://api.frankerfacez.com/v1/set/global"); QString url("https://api.frankerfacez.com/v1/set/global");
@ -130,7 +133,7 @@ void FfzEmotes::loadGlobal()
request.setTimeout(30000); request.setTimeout(30000);
request.onSuccess([this](auto result) -> Outcome { request.onSuccess([this](auto result) -> Outcome {
auto emotes = this->global(); auto emotes = this->emotes();
auto pair = parseGlobalEmotes(result.parseJson(), *emotes); auto pair = parseGlobalEmotes(result.parseJson(), *emotes);
if (pair.first) if (pair.first)
this->global_.set( this->global_.set(

View file

@ -1,24 +1,27 @@
#pragma once #pragma once
#include <memory> #include <memory>
#include "boost/optional.hpp"
#include "common/Aliases.hpp"
#include "common/Atomic.hpp" #include "common/Atomic.hpp"
#include "messages/Emote.hpp"
namespace chatterino { namespace chatterino {
struct Emote;
using EmotePtr = std::shared_ptr<const Emote>;
class EmoteMap;
class FfzEmotes final class FfzEmotes final
{ {
static constexpr const char *globalEmoteApiUrl = static constexpr const char *globalEmoteApiUrl =
"https://api.frankerfacez.com/v1/set/global"; "https://api.frankerfacez.com/v1/set/global";
static constexpr const char *channelEmoteApiUrl =
"https://api.betterttv.net/2/channels/";
public: public:
FfzEmotes(); FfzEmotes();
std::shared_ptr<const EmoteMap> global() const; std::shared_ptr<const EmoteMap> emotes() const;
boost::optional<EmotePtr> global(const EmoteName &name) const; boost::optional<EmotePtr> emote(const EmoteName &name) const;
void loadGlobal(); void loadEmotes();
static void loadChannel(const QString &channelName, static void loadChannel(const QString &channelName,
std::function<void(EmoteMap &&)> callback); std::function<void(EmoteMap &&)> callback);

View file

@ -1,6 +1,8 @@
#include "AbstractIrcServer.hpp" #include "AbstractIrcServer.hpp"
#include "common/Channel.hpp"
#include "common/Common.hpp" #include "common/Common.hpp"
#include "debug/Log.hpp"
#include "messages/LimitedQueueSnapshot.hpp" #include "messages/LimitedQueueSnapshot.hpp"
#include "messages/Message.hpp" #include "messages/Message.hpp"
#include "messages/MessageBuilder.hpp" #include "messages/MessageBuilder.hpp"

View file

@ -1,6 +1,5 @@
#pragma once #pragma once
#include "common/Channel.hpp"
#include "providers/irc/IrcConnection2.hpp" #include "providers/irc/IrcConnection2.hpp"
#include <IrcMessage> #include <IrcMessage>
@ -11,6 +10,9 @@
namespace chatterino { namespace chatterino {
class Channel;
using ChannelPtr = std::shared_ptr<Channel>;
class AbstractIrcServer class AbstractIrcServer
{ {
public: public:

View file

@ -10,6 +10,7 @@
#include "providers/twitch/TwitchMessageBuilder.hpp" #include "providers/twitch/TwitchMessageBuilder.hpp"
#include "providers/twitch/TwitchServer.hpp" #include "providers/twitch/TwitchServer.hpp"
#include "singletons/Resources.hpp" #include "singletons/Resources.hpp"
#include "singletons/Settings.hpp"
#include "singletons/WindowManager.hpp" #include "singletons/WindowManager.hpp"
#include "util/IrcHelpers.hpp" #include "util/IrcHelpers.hpp"
@ -230,7 +231,7 @@ void IrcMessageHandler::handleWhisperMessage(Communi::IrcMessage *message)
c->addMessage(_message); c->addMessage(_message);
if (app->settings->inlineWhispers) { if (getSettings()->inlineWhispers) {
app->twitch.server->forEachChannel([_message](ChannelPtr channel) { app->twitch.server->forEachChannel([_message](ChannelPtr channel) {
channel->addMessage(_message); // channel->addMessage(_message); //
}); });

View file

@ -1,5 +1,6 @@
#include "providers/twitch/PartialTwitchUser.hpp" #include "providers/twitch/PartialTwitchUser.hpp"
#include "common/Common.hpp"
#include "common/NetworkRequest.hpp" #include "common/NetworkRequest.hpp"
#include "debug/Log.hpp" #include "debug/Log.hpp"
#include "providers/twitch/TwitchCommon.hpp" #include "providers/twitch/TwitchCommon.hpp"

View file

@ -24,157 +24,159 @@ static std::map<QString, std::string> sentMessages;
namespace detail { namespace detail {
PubSubClient::PubSubClient(WebsocketClient &websocketClient, PubSubClient::PubSubClient(WebsocketClient &websocketClient,
WebsocketHandle handle) WebsocketHandle handle)
: websocketClient_(websocketClient) : websocketClient_(websocketClient)
, handle_(handle) , handle_(handle)
{ {
}
void PubSubClient::start()
{
assert(!this->started_);
this->started_ = true;
this->ping();
}
void PubSubClient::stop()
{
assert(this->started_);
this->started_ = false;
}
bool PubSubClient::listen(rapidjson::Document &message)
{
int numRequestedListens = message["data"]["topics"].Size();
if (this->numListens_ + numRequestedListens > MAX_PUBSUB_LISTENS) {
// This PubSubClient is already at its peak listens
return false;
} }
this->numListens_ += numRequestedListens; void PubSubClient::start()
{
assert(!this->started_);
for (const auto &topic : message["data"]["topics"].GetArray()) { this->started_ = true;
this->listeners_.emplace_back(
Listener{topic.GetString(), false, false, false}); this->ping();
} }
auto uuid = CreateUUID(); void PubSubClient::stop()
{
assert(this->started_);
rj::set(message, "nonce", uuid); this->started_ = false;
}
std::string payload = rj::stringify(message); bool PubSubClient::listen(rapidjson::Document &message)
sentMessages[uuid] = payload; {
int numRequestedListens = message["data"]["topics"].Size();
this->send(payload.c_str()); if (this->numListens_ + numRequestedListens > MAX_PUBSUB_LISTENS) {
// This PubSubClient is already at its peak listens
return true; return false;
}
void PubSubClient::unlistenPrefix(const std::string &prefix)
{
std::vector<std::string> topics;
for (auto it = this->listeners_.begin(); it != this->listeners_.end();) {
const auto &listener = *it;
if (listener.topic.find(prefix) == 0) {
topics.push_back(listener.topic);
it = this->listeners_.erase(it);
} else {
++it;
} }
}
if (topics.empty()) { this->numListens_ += numRequestedListens;
return;
}
auto message = createUnlistenMessage(topics); for (const auto &topic : message["data"]["topics"].GetArray()) {
this->listeners_.emplace_back(
auto uuid = CreateUUID(); Listener{topic.GetString(), false, false, false});
rj::set(message, "nonce", CreateUUID());
std::string payload = rj::stringify(message);
sentMessages[uuid] = payload;
this->send(payload.c_str());
}
void PubSubClient::handlePong()
{
assert(this->awaitingPong_);
log("Got pong!");
this->awaitingPong_ = false;
}
bool PubSubClient::isListeningToTopic(const std::string &payload)
{
for (const auto &listener : this->listeners_) {
if (listener.topic == payload) {
return true;
} }
auto uuid = CreateUUID();
rj::set(message, "nonce", uuid);
std::string payload = rj::stringify(message);
sentMessages[uuid] = payload;
this->send(payload.c_str());
return true;
} }
return false; void PubSubClient::unlistenPrefix(const std::string &prefix)
} {
std::vector<std::string> topics;
void PubSubClient::ping() for (auto it = this->listeners_.begin();
{ it != this->listeners_.end();) {
assert(this->started_); const auto &listener = *it;
if (listener.topic.find(prefix) == 0) {
topics.push_back(listener.topic);
it = this->listeners_.erase(it);
} else {
++it;
}
}
if (!this->send(pingPayload)) { if (topics.empty()) {
return; return;
}
auto message = createUnlistenMessage(topics);
auto uuid = CreateUUID();
rj::set(message, "nonce", CreateUUID());
std::string payload = rj::stringify(message);
sentMessages[uuid] = payload;
this->send(payload.c_str());
} }
this->awaitingPong_ = true; void PubSubClient::handlePong()
{
assert(this->awaitingPong_);
auto self = this->shared_from_this(); log("Got pong!");
runAfter(this->websocketClient_.get_io_service(), std::chrono::seconds(15), this->awaitingPong_ = false;
[self](auto timer) { }
if (!self->started_) {
return;
}
if (self->awaitingPong_) { bool PubSubClient::isListeningToTopic(const std::string &payload)
log("No pong respnose, disconnect!"); {
// TODO(pajlada): Label this connection as "disconnect me" for (const auto &listener : this->listeners_) {
} if (listener.topic == payload) {
}); return true;
}
runAfter(this->websocketClient_.get_io_service(), std::chrono::minutes(5), }
[self](auto timer) {
if (!self->started_) {
return;
}
self->ping(); //
});
}
bool PubSubClient::send(const char *payload)
{
WebsocketErrorCode ec;
this->websocketClient_.send(this->handle_, payload,
websocketpp::frame::opcode::text, ec);
if (ec) {
log("Error sending message {}: {}", payload, ec.message());
// TODO(pajlada): Check which error code happened and maybe gracefully
// handle it
return false; return false;
} }
return true; void PubSubClient::ping()
} {
assert(this->started_);
if (!this->send(pingPayload)) {
return;
}
this->awaitingPong_ = true;
auto self = this->shared_from_this();
runAfter(this->websocketClient_.get_io_service(),
std::chrono::seconds(15), [self](auto timer) {
if (!self->started_) {
return;
}
if (self->awaitingPong_) {
log("No pong respnose, disconnect!");
// TODO(pajlada): Label this connection as "disconnect
// me"
}
});
runAfter(this->websocketClient_.get_io_service(),
std::chrono::minutes(5), [self](auto timer) {
if (!self->started_) {
return;
}
self->ping(); //
});
}
bool PubSubClient::send(const char *payload)
{
WebsocketErrorCode ec;
this->websocketClient_.send(this->handle_, payload,
websocketpp::frame::opcode::text, ec);
if (ec) {
log("Error sending message {}: {}", payload, ec.message());
// TODO(pajlada): Check which error code happened and maybe
// gracefully handle it
return false;
}
return true;
}
} // namespace detail } // namespace detail

View file

@ -32,41 +32,42 @@ using WebsocketErrorCode = websocketpp::lib::error_code;
namespace detail { namespace detail {
struct Listener { struct Listener {
std::string topic; std::string topic;
bool authed; bool authed;
bool persistent; bool persistent;
bool confirmed = false; bool confirmed = false;
}; };
class PubSubClient : public std::enable_shared_from_this<PubSubClient> class PubSubClient : public std::enable_shared_from_this<PubSubClient>
{ {
public: public:
PubSubClient(WebsocketClient &_websocketClient, WebsocketHandle _handle); PubSubClient(WebsocketClient &_websocketClient,
WebsocketHandle _handle);
void start(); void start();
void stop(); void stop();
bool listen(rapidjson::Document &message); bool listen(rapidjson::Document &message);
void unlistenPrefix(const std::string &prefix); void unlistenPrefix(const std::string &prefix);
void handlePong(); void handlePong();
bool isListeningToTopic(const std::string &topic); bool isListeningToTopic(const std::string &topic);
private: private:
void ping(); void ping();
bool send(const char *payload); bool send(const char *payload);
WebsocketClient &websocketClient_; WebsocketClient &websocketClient_;
WebsocketHandle handle_; WebsocketHandle handle_;
uint16_t numListens_ = 0; uint16_t numListens_ = 0;
std::vector<Listener> listeners_; std::vector<Listener> listeners_;
std::atomic<bool> awaitingPong_{false}; std::atomic<bool> awaitingPong_{false};
std::atomic<bool> started_{false}; std::atomic<bool> started_{false};
}; };
} // namespace detail } // namespace detail

View file

@ -1,6 +1,7 @@
#include "providers/twitch/PubsubHelpers.hpp" #include "providers/twitch/PubsubHelpers.hpp"
#include "providers/twitch/PubsubActions.hpp" #include "providers/twitch/PubsubActions.hpp"
#include "providers/twitch/TwitchAccount.hpp"
#include "util/RapidjsonHelpers.hpp" #include "util/RapidjsonHelpers.hpp"
namespace chatterino { namespace chatterino {

View file

@ -1,16 +1,14 @@
#pragma once #pragma once
#include "debug/Log.hpp"
#include "providers/twitch/TwitchAccount.hpp"
#include "util/RapidjsonHelpers.hpp"
#include <boost/asio.hpp> #include <boost/asio.hpp>
#include <boost/asio/steady_timer.hpp> #include <boost/asio/steady_timer.hpp>
#include <memory> #include <memory>
#include "debug/Log.hpp"
#include "util/RapidjsonHelpers.hpp"
namespace chatterino { namespace chatterino {
class TwitchAccount;
struct ActionUser; struct ActionUser;
const rapidjson::Value &getArgs(const rapidjson::Value &data); const rapidjson::Value &getArgs(const rapidjson::Value &data);

View file

@ -4,6 +4,7 @@
#include "Application.hpp" #include "Application.hpp"
#include "common/NetworkRequest.hpp" #include "common/NetworkRequest.hpp"
#include "common/Outcome.hpp"
#include "debug/Log.hpp" #include "debug/Log.hpp"
#include "providers/twitch/PartialTwitchUser.hpp" #include "providers/twitch/PartialTwitchUser.hpp"
#include "providers/twitch/TwitchCommon.hpp" #include "providers/twitch/TwitchCommon.hpp"
@ -14,32 +15,32 @@ namespace chatterino {
namespace { namespace {
EmoteName cleanUpCode(const EmoteName &dirtyEmoteCode) EmoteName cleanUpCode(const EmoteName &dirtyEmoteCode)
{ {
auto cleanCode = dirtyEmoteCode.string; auto cleanCode = dirtyEmoteCode.string;
cleanCode.detach(); cleanCode.detach();
static QMap<QString, QString> emoteNameReplacements{ static QMap<QString, QString> emoteNameReplacements{
{"[oO](_|\\.)[oO]", "O_o"}, {"\\&gt\\;\\(", "&gt;("}, {"[oO](_|\\.)[oO]", "O_o"}, {"\\&gt\\;\\(", "&gt;("},
{"\\&lt\\;3", "&lt;3"}, {"\\:-?(o|O)", ":O"}, {"\\&lt\\;3", "&lt;3"}, {"\\:-?(o|O)", ":O"},
{"\\:-?(p|P)", ":P"}, {"\\:-?[\\\\/]", ":/"}, {"\\:-?(p|P)", ":P"}, {"\\:-?[\\\\/]", ":/"},
{"\\:-?[z|Z|\\|]", ":Z"}, {"\\:-?\\(", ":("}, {"\\:-?[z|Z|\\|]", ":Z"}, {"\\:-?\\(", ":("},
{"\\:-?\\)", ":)"}, {"\\:-?D", ":D"}, {"\\:-?\\)", ":)"}, {"\\:-?D", ":D"},
{"\\;-?(p|P)", ";P"}, {"\\;-?\\)", ";)"}, {"\\;-?(p|P)", ";P"}, {"\\;-?\\)", ";)"},
{"R-?\\)", "R)"}, {"B-?\\)", "B)"}, {"R-?\\)", "R)"}, {"B-?\\)", "B)"},
}; };
auto it = emoteNameReplacements.find(dirtyEmoteCode.string); auto it = emoteNameReplacements.find(dirtyEmoteCode.string);
if (it != emoteNameReplacements.end()) { if (it != emoteNameReplacements.end()) {
cleanCode = it.value(); cleanCode = it.value();
}
cleanCode.replace("&lt;", "<");
cleanCode.replace("&gt;", ">");
return {cleanCode};
} }
cleanCode.replace("&lt;", "<");
cleanCode.replace("&gt;", ">");
return {cleanCode};
}
} // namespace } // namespace
TwitchAccount::TwitchAccount(const QString &username, const QString &oauthToken, TwitchAccount::TwitchAccount(const QString &username, const QString &oauthToken,
@ -416,7 +417,7 @@ void TwitchAccount::loadEmotes()
} }
AccessGuard<const TwitchAccount::TwitchAccountEmoteData> AccessGuard<const TwitchAccount::TwitchAccountEmoteData>
TwitchAccount::accessEmotes() const TwitchAccount::accessEmotes() const
{ {
return this->emotes_.accessConst(); return this->emotes_.accessConst();
} }

View file

@ -1,5 +1,6 @@
#pragma once #pragma once
#include "common/Aliases.hpp"
#include "common/Atomic.hpp" #include "common/Atomic.hpp"
#include "common/UniqueAccess.hpp" #include "common/UniqueAccess.hpp"
#include "controllers/accounts/Account.hpp" #include "controllers/accounts/Account.hpp"

View file

@ -2,6 +2,7 @@
#include "common/Common.hpp" #include "common/Common.hpp"
#include "debug/Log.hpp" #include "debug/Log.hpp"
#include "providers/twitch/TwitchAccount.hpp"
#include "providers/twitch/TwitchCommon.hpp" #include "providers/twitch/TwitchCommon.hpp"
namespace chatterino { namespace chatterino {

View file

@ -17,6 +17,7 @@
namespace chatterino { namespace chatterino {
class TwitchAccount;
class AccountController; class AccountController;
class TwitchAccountManager class TwitchAccountManager

View file

@ -1,5 +1,6 @@
#include "providers/twitch/TwitchApi.hpp" #include "providers/twitch/TwitchApi.hpp"
#include "common/Common.hpp"
#include "common/NetworkRequest.hpp" #include "common/NetworkRequest.hpp"
#include "debug/Log.hpp" #include "debug/Log.hpp"
#include "providers/twitch/TwitchCommon.hpp" #include "providers/twitch/TwitchCommon.hpp"

View file

@ -1,6 +1,7 @@
#pragma once #pragma once
#include <QString> #include <QString>
#include <functional>
namespace chatterino { namespace chatterino {

View file

@ -4,19 +4,14 @@
#include <QJsonObject> #include <QJsonObject>
#include <QJsonValue> #include <QJsonValue>
#include <QThread> #include <QThread>
#include "common/NetworkRequest.hpp" #include "common/NetworkRequest.hpp"
#include "common/Outcome.hpp"
#include "debug/Log.hpp"
#include "messages/Emote.hpp"
namespace chatterino { namespace chatterino {
TwitchBadges::TwitchBadges()
{
}
void TwitchBadges::initialize(Settings &settings, Paths &paths)
{
this->loadTwitchBadges();
}
void TwitchBadges::loadTwitchBadges() void TwitchBadges::loadTwitchBadges()
{ {
static QString url( static QString url(
@ -26,32 +21,34 @@ void TwitchBadges::loadTwitchBadges()
req.setCaller(QThread::currentThread()); req.setCaller(QThread::currentThread());
req.onSuccess([this](auto result) -> Outcome { req.onSuccess([this](auto result) -> Outcome {
auto root = result.parseJson(); auto root = result.parseJson();
QJsonObject sets = root.value("badge_sets").toObject(); auto badgeSets = this->badgeSets_.access();
for (QJsonObject::iterator it = sets.begin(); it != sets.end(); ++it) { auto jsonSets = root.value("badge_sets").toObject();
QJsonObject versions = for (auto sIt = jsonSets.begin(); sIt != jsonSets.end(); ++sIt) {
it.value().toObject().value("versions").toObject(); auto key = sIt.key();
auto versions = sIt.value().toObject().value("versions").toObject();
for (auto vIt = versions.begin(); vIt != versions.end(); ++vIt) {
auto versionObj = vIt.value().toObject();
for (auto versionIt = std::begin(versions);
versionIt != std::end(versions); ++versionIt) {
auto emote = Emote{ auto emote = Emote{
{""}, {""},
ImageSet{ ImageSet{
Image::fromUrl({root.value("image_url_1x").toString()}, Image::fromUrl(
1), {versionObj.value("image_url_1x").toString()}, 1),
Image::fromUrl({root.value("image_url_2x").toString()}, Image::fromUrl(
0.5), {versionObj.value("image_url_2x").toString()}, .5),
Image::fromUrl({root.value("image_url_4x").toString()}, Image::fromUrl(
0.25), {versionObj.value("image_url_4x").toString()}, .25),
}, },
Tooltip{root.value("description").toString()}, Tooltip{root.value("description").toString()},
Url{root.value("clickURL").toString()}}; Url{root.value("clickURL").toString()}};
// "title" // "title"
// "clickAction" // "clickAction"
QJsonObject versionObj = versionIt.value().toObject(); log("{} {}", key, vIt.key());
this->badges.emplace(versionIt.key(),
std::make_shared<Emote>(emote)); (*badgeSets)[key][vIt.key()] = std::make_shared<Emote>(emote);
} }
} }
@ -61,4 +58,18 @@ void TwitchBadges::loadTwitchBadges()
req.execute(); req.execute();
} }
boost::optional<EmotePtr> TwitchBadges::badge(const QString &set,
const QString &version) const
{
auto badgeSets = this->badgeSets_.access();
auto it = badgeSets->find(set);
if (it != badgeSets->end()) {
auto it2 = it->second.find(version);
if (it2 != it->second.end()) {
return it2->second;
}
}
return boost::none;
}
} // namespace chatterino } // namespace chatterino

View file

@ -1,26 +1,32 @@
#pragma once #pragma once
#include <QString> #include <QString>
#include <messages/Emote.hpp> #include <boost/optional.hpp>
#include <unordered_map> #include <unordered_map>
#include "common/UniqueAccess.hpp"
#include "util/QStringHash.hpp" #include "util/QStringHash.hpp"
namespace chatterino { namespace chatterino {
struct Emote;
using EmotePtr = std::shared_ptr<const Emote>;
class Settings; class Settings;
class Paths; class Paths;
class TwitchBadges class TwitchBadges
{ {
public: public:
TwitchBadges();
void initialize(Settings &settings, Paths &paths);
private:
void loadTwitchBadges(); void loadTwitchBadges();
std::unordered_map<QString, EmotePtr> badges; boost::optional<EmotePtr> badge(const QString &set,
const QString &version) const;
private:
UniqueAccess<
std::unordered_map<QString, std::unordered_map<QString, EmotePtr>>>
badgeSets_; // "bits": { "100": ... "500": ...
}; };
} // namespace chatterino } // namespace chatterino

View file

@ -1,5 +1,6 @@
#include "providers/twitch/TwitchChannel.hpp" #include "providers/twitch/TwitchChannel.hpp"
#include "Application.hpp"
#include "common/Common.hpp" #include "common/Common.hpp"
#include "common/NetworkRequest.hpp" #include "common/NetworkRequest.hpp"
#include "controllers/accounts/AccountController.hpp" #include "controllers/accounts/AccountController.hpp"
@ -24,66 +25,87 @@
namespace chatterino { namespace chatterino {
namespace { namespace {
auto parseRecentMessages(const QJsonObject &jsonRoot, TwitchChannel &channel) auto parseRecentMessages(const QJsonObject &jsonRoot,
{ TwitchChannel &channel)
QJsonArray jsonMessages = jsonRoot.value("messages").toArray(); {
std::vector<MessagePtr> messages; QJsonArray jsonMessages = jsonRoot.value("messages").toArray();
std::vector<MessagePtr> messages;
if (jsonMessages.empty()) return messages; if (jsonMessages.empty()) return messages;
for (const auto jsonMessage : jsonMessages) { for (const auto jsonMessage : jsonMessages) {
auto content = jsonMessage.toString().toUtf8(); auto content = jsonMessage.toString().toUtf8();
// passing nullptr as the channel makes the message invalid but we don't // passing nullptr as the channel makes the message invalid but we
// check for that anyways // don't check for that anyways
auto message = Communi::IrcMessage::fromData(content, nullptr); auto message = Communi::IrcMessage::fromData(content, nullptr);
auto privMsg = dynamic_cast<Communi::IrcPrivateMessage *>(message); auto privMsg = dynamic_cast<Communi::IrcPrivateMessage *>(message);
assert(privMsg); assert(privMsg);
MessageParseArgs args; MessageParseArgs args;
TwitchMessageBuilder builder(&channel, privMsg, args); TwitchMessageBuilder builder(&channel, privMsg, args);
if (!builder.isIgnored()) { if (!builder.isIgnored()) {
messages.push_back(builder.build()); messages.push_back(builder.build());
}
} }
}
return messages; return messages;
} }
std::pair<Outcome, UsernameSet> parseChatters(const QJsonObject &jsonRoot)
{
static QStringList categories = {"moderators", "staff", "admins",
"global_mods", "viewers"};
auto usernames = UsernameSet();
// parse json
QJsonObject jsonCategories = jsonRoot.value("chatters").toObject();
for (const auto &category : categories) {
for (auto jsonCategory : jsonCategories.value(category).toArray()) {
usernames.insert(jsonCategory.toString());
}
}
return {Success, std::move(usernames)};
}
} // namespace } // namespace
TwitchChannel::TwitchChannel(const QString &name) TwitchChannel::TwitchChannel(const QString &name,
TwitchBadges &globalTwitchBadges, BttvEmotes &bttv,
FfzEmotes &ffz)
: Channel(name, Channel::Type::Twitch) : Channel(name, Channel::Type::Twitch)
, bttvEmotes_(std::make_shared<EmoteMap>())
, ffzEmotes_(std::make_shared<EmoteMap>())
, subscriptionUrl_("https://www.twitch.tv/subs/" + name) , subscriptionUrl_("https://www.twitch.tv/subs/" + name)
, channelUrl_("https://twitch.tv/" + name) , channelUrl_("https://twitch.tv/" + name)
, popoutPlayerUrl_("https://player.twitch.tv/?channel=" + name) , popoutPlayerUrl_("https://player.twitch.tv/?channel=" + name)
, globalTwitchBadges_(globalTwitchBadges)
, globalBttv_(bttv)
, globalFfz_(ffz)
, bttvEmotes_(std::make_shared<EmoteMap>())
, ffzEmotes_(std::make_shared<EmoteMap>())
, mod_(false) , mod_(false)
{ {
log("[TwitchChannel:{}] Opened", name); log("[TwitchChannel:{}] Opened", name);
// this->refreshChannelEmotes();
// this->refreshViewerList();
this->managedConnect(getApp()->accounts->twitch.currentUserChanged, this->managedConnect(getApp()->accounts->twitch.currentUserChanged,
[=] { this->setMod(false); }); [=] { this->setMod(false); });
// pubsub // pubsub
this->userStateChanged.connect([=] { this->refreshPubsub(); });
this->managedConnect(getApp()->accounts->twitch.currentUserChanged, this->managedConnect(getApp()->accounts->twitch.currentUserChanged,
[=] { this->refreshPubsub(); }); [=] { this->refreshPubsub(); });
this->refreshPubsub(); this->refreshPubsub();
this->userStateChanged.connect([this] { this->refreshPubsub(); });
// room id loaded -> refresh live status // room id loaded -> refresh live status
this->roomIdChanged.connect([this]() { this->roomIdChanged.connect([this]() {
this->refreshPubsub(); this->refreshPubsub();
this->refreshLiveStatus(); this->refreshLiveStatus();
this->loadBadges(); this->refreshBadges();
this->loadCheerEmotes(); this->refreshCheerEmotes();
}); });
// timers // timers
QObject::connect(&this->chattersListTimer_, &QTimer::timeout, QObject::connect(&this->chattersListTimer_, &QTimer::timeout,
[=] { this->refreshViewerList(); }); [=] { this->refreshChatters(); });
this->chattersListTimer_.start(5 * 60 * 1000); this->chattersListTimer_.start(5 * 60 * 1000);
QObject::connect(&this->liveStatusTimer_, &QTimer::timeout, QObject::connect(&this->liveStatusTimer_, &QTimer::timeout,
@ -97,11 +119,17 @@ TwitchChannel::TwitchChannel(const QString &name)
// debugging // debugging
#if 0 #if 0
for (int i = 0; i < 1000; i++) { for (int i = 0; i < 1000; i++) {
this->addMessage(makeSystemMessage("asdf")); this->addMessage(makeSystemMessage("asef"));
} }
#endif #endif
} }
void TwitchChannel::initialize()
{
this->refreshChatters();
this->refreshChannelEmotes();
}
bool TwitchChannel::isEmpty() const bool TwitchChannel::isEmpty() const
{ {
return this->getName().isEmpty(); return this->getName().isEmpty();
@ -192,9 +220,7 @@ bool TwitchChannel::isBroadcaster() const
void TwitchChannel::addRecentChatter(const MessagePtr &message) void TwitchChannel::addRecentChatter(const MessagePtr &message)
{ {
assert(!message->loginName.isEmpty()); this->chatters_.access()->insert(message->displayName);
this->completionModel.addUser(message->displayName);
} }
void TwitchChannel::addJoinedUser(const QString &user) void TwitchChannel::addJoinedUser(const QString &user)
@ -284,11 +310,31 @@ bool TwitchChannel::isLive() const
} }
AccessGuard<const TwitchChannel::StreamStatus> AccessGuard<const TwitchChannel::StreamStatus>
TwitchChannel::accessStreamStatus() const TwitchChannel::accessStreamStatus() const
{ {
return this->streamStatus_.accessConst(); return this->streamStatus_.accessConst();
} }
AccessGuard<const UsernameSet> TwitchChannel::accessChatters() const
{
return this->chatters_.accessConst();
}
const TwitchBadges &TwitchChannel::globalTwitchBadges() const
{
return this->globalTwitchBadges_;
}
const BttvEmotes &TwitchChannel::globalBttv() const
{
return this->globalBttv_;
}
const FfzEmotes &TwitchChannel::globalFfz() const
{
return this->globalFfz_;
}
boost::optional<EmotePtr> TwitchChannel::bttvEmote(const EmoteName &name) const boost::optional<EmotePtr> TwitchChannel::bttvEmote(const EmoteName &name) const
{ {
auto emotes = this->bttvEmotes_.get(); auto emotes = this->bttvEmotes_.get();
@ -300,7 +346,7 @@ boost::optional<EmotePtr> TwitchChannel::bttvEmote(const EmoteName &name) const
boost::optional<EmotePtr> TwitchChannel::ffzEmote(const EmoteName &name) const boost::optional<EmotePtr> TwitchChannel::ffzEmote(const EmoteName &name) const
{ {
auto emotes = this->bttvEmotes_.get(); auto emotes = this->ffzEmotes_.get();
auto it = emotes->find(name); auto it = emotes->find(name);
if (it == emotes->end()) return boost::none; if (it == emotes->end()) return boost::none;
@ -371,7 +417,7 @@ void TwitchChannel::refreshLiveStatus()
//>>>>>>> 9bfbdefd2f0972a738230d5b95a009f73b1dd933 //>>>>>>> 9bfbdefd2f0972a738230d5b95a009f73b1dd933
request.onSuccess( request.onSuccess(
[this, weak = this->weak_from_this()](auto result) -> Outcome { [this, weak = weakOf<Channel>(this)](auto result) -> Outcome {
ChannelPtr shared = weak.lock(); ChannelPtr shared = weak.lock();
if (!shared) return Failure; if (!shared) return Failure;
@ -494,7 +540,7 @@ void TwitchChannel::refreshPubsub()
account); account);
} }
void TwitchChannel::refreshViewerList() void TwitchChannel::refreshChatters()
{ {
// setting? // setting?
const auto streamStatus = this->accessStreamStatus(); const auto streamStatus = this->accessStreamStatus();
@ -512,36 +558,23 @@ void TwitchChannel::refreshViewerList()
request.setCaller(QThread::currentThread()); request.setCaller(QThread::currentThread());
request.onSuccess( request.onSuccess(
[this, weak = this->weak_from_this()](auto result) -> Outcome { [this, weak = weakOf<Channel>(this)](auto result) -> Outcome {
// channel still exists? // channel still exists?
auto shared = weak.lock(); auto shared = weak.lock();
if (!shared) return Failure; if (!shared) return Failure;
return this->parseViewerList(result.parseJson()); auto pair = parseChatters(result.parseJson());
if (pair.first) {
*this->chatters_.access() = std::move(pair.second);
}
return pair.first;
}); });
request.execute(); request.execute();
} }
Outcome TwitchChannel::parseViewerList(const QJsonObject &jsonRoot) void TwitchChannel::refreshBadges()
{
static QStringList categories = {"moderators", "staff", "admins",
"global_mods", "viewers"};
// parse json
QJsonObject jsonCategories = jsonRoot.value("chatters").toObject();
for (const auto &category : categories) {
for (const auto jsonCategory :
jsonCategories.value(category).toArray()) {
this->completionModel.addUser(jsonCategory.toString());
}
}
return Success;
}
void TwitchChannel::loadBadges()
{ {
auto url = Url{"https://badges.twitch.tv/v1/badges/channels/" + auto url = Url{"https://badges.twitch.tv/v1/badges/channels/" +
this->roomId() + "/display?language=en"}; this->roomId() + "/display?language=en"};
@ -586,7 +619,7 @@ void TwitchChannel::loadBadges()
req.execute(); req.execute();
} }
void TwitchChannel::loadCheerEmotes() void TwitchChannel::refreshCheerEmotes()
{ {
/*auto url = Url{"https://api.twitch.tv/kraken/bits/actions?channel_id=" + /*auto url = Url{"https://api.twitch.tv/kraken/bits/actions?channel_id=" +
this->getRoomId()}; this->getRoomId()};
@ -650,7 +683,7 @@ void TwitchChannel::loadCheerEmotes()
*/ */
} }
boost::optional<EmotePtr> TwitchChannel::getTwitchBadge( boost::optional<EmotePtr> TwitchChannel::twitchBadge(
const QString &set, const QString &version) const const QString &set, const QString &version) const
{ {
auto badgeSets = this->badgeSets_.access(); auto badgeSets = this->badgeSets_.access();

View file

@ -1,22 +1,32 @@
#pragma once #pragma once
#include <IrcConnection> #include "common/Aliases.hpp"
#include "common/Atomic.hpp" #include "common/Atomic.hpp"
#include "common/Channel.hpp" #include "common/Channel.hpp"
#include "common/Common.hpp" #include "common/Outcome.hpp"
#include "common/UniqueAccess.hpp" #include "common/UniqueAccess.hpp"
#include "messages/Emote.hpp" #include "common/UsernameSet.hpp"
#include "singletons/Emotes.hpp" #include "providers/twitch/TwitchEmotes.hpp"
#include "util/ConcurrentMap.hpp"
#include <pajlada/signals/signalholder.hpp>
#include <rapidjson/document.h>
#include <IrcConnection>
#include <QColor>
#include <QRegularExpression>
#include <boost/optional.hpp>
#include <mutex> #include <mutex>
#include <pajlada/signals/signalholder.hpp>
#include <unordered_map> #include <unordered_map>
namespace chatterino { namespace chatterino {
struct Emote;
using EmotePtr = std::shared_ptr<const Emote>;
class EmoteMap;
class TwitchBadges;
class FfzEmotes;
class BttvEmotes;
class TwitchServer; class TwitchServer;
class TwitchChannel final : public Channel, pajlada::Signals::SignalHolder class TwitchChannel final : public Channel, pajlada::Signals::SignalHolder
@ -32,11 +42,6 @@ public:
QString streamType; QString streamType;
}; };
struct UserState {
bool mod;
bool broadcaster;
};
struct RoomModes { struct RoomModes {
bool submode = false; bool submode = false;
bool r9k = false; bool r9k = false;
@ -46,92 +51,94 @@ public:
QString broadcasterLang; QString broadcasterLang;
}; };
void refreshChannelEmotes(); void initialize();
// Channel methods // Channel methods
virtual bool isEmpty() const override; virtual bool isEmpty() const override;
virtual bool canSendMessage() const override; virtual bool canSendMessage() const override;
virtual void sendMessage(const QString &message) override; virtual void sendMessage(const QString &message) override;
// Auto completion
void addRecentChatter(const MessagePtr &message) final;
void addJoinedUser(const QString &user);
void addPartedUser(const QString &user);
// Twitch data
bool isLive() const;
virtual bool isMod() const override; virtual bool isMod() const override;
void setMod(bool value);
virtual bool isBroadcaster() const override; virtual bool isBroadcaster() const override;
// Data
const QString &subscriptionUrl();
const QString &channelUrl();
const QString &popoutPlayerUrl();
bool isLive() const;
QString roomId() const; QString roomId() const;
void setRoomId(const QString &id);
AccessGuard<const RoomModes> accessRoomModes() const; AccessGuard<const RoomModes> accessRoomModes() const;
void setRoomModes(const RoomModes &roomModes_);
AccessGuard<const StreamStatus> accessStreamStatus() const; AccessGuard<const StreamStatus> accessStreamStatus() const;
AccessGuard<const UsernameSet> accessChatters() const;
// Emotes
const TwitchBadges &globalTwitchBadges() const;
const BttvEmotes &globalBttv() const;
const FfzEmotes &globalFfz() const;
boost::optional<EmotePtr> bttvEmote(const EmoteName &name) const; boost::optional<EmotePtr> bttvEmote(const EmoteName &name) const;
boost::optional<EmotePtr> ffzEmote(const EmoteName &name) const; boost::optional<EmotePtr> ffzEmote(const EmoteName &name) const;
std::shared_ptr<const EmoteMap> bttvEmotes() const; std::shared_ptr<const EmoteMap> bttvEmotes() const;
std::shared_ptr<const EmoteMap> ffzEmotes() const; std::shared_ptr<const EmoteMap> ffzEmotes() const;
const QString &subscriptionUrl();
const QString &channelUrl();
const QString &popoutPlayerUrl();
boost::optional<EmotePtr> getTwitchBadge(const QString &set, void refreshChannelEmotes();
const QString &version) const;
// Badges
boost::optional<EmotePtr> twitchBadge(const QString &set,
const QString &version) const;
// Signals // Signals
pajlada::Signals::NoArgSignal roomIdChanged; pajlada::Signals::NoArgSignal roomIdChanged;
pajlada::Signals::NoArgSignal liveStatusChanged;
pajlada::Signals::NoArgSignal userStateChanged; pajlada::Signals::NoArgSignal userStateChanged;
pajlada::Signals::NoArgSignal liveStatusChanged;
pajlada::Signals::NoArgSignal roomModesChanged; pajlada::Signals::NoArgSignal roomModesChanged;
protected:
void addRecentChatter(const MessagePtr &message) override;
private: private:
struct NameOptions { struct NameOptions {
QString displayName; QString displayName;
QString localizedName; QString localizedName;
}; };
struct CheerEmote { explicit TwitchChannel(const QString &channelName,
// a Cheermote indicates one tier TwitchBadges &globalTwitchBadges,
QColor color; BttvEmotes &globalBttv, FfzEmotes &globalFfz);
int minBits;
EmotePtr animatedEmote;
EmotePtr staticEmote;
};
struct CheerEmoteSet {
QRegularExpression regex;
std::vector<CheerEmote> cheerEmotes;
};
explicit TwitchChannel(const QString &channelName);
// Methods // Methods
void refreshLiveStatus(); void refreshLiveStatus();
Outcome parseLiveStatus(const rapidjson::Document &document); Outcome parseLiveStatus(const rapidjson::Document &document);
void refreshPubsub(); void refreshPubsub();
void refreshViewerList(); void refreshChatters();
Outcome parseViewerList(const QJsonObject &jsonRoot); void refreshBadges();
void refreshCheerEmotes();
void loadRecentMessages(); void loadRecentMessages();
void addJoinedUser(const QString &user);
void addPartedUser(const QString &user);
void setLive(bool newLiveStatus); void setLive(bool newLiveStatus);
void setMod(bool value);
void setRoomId(const QString &id);
void setRoomModes(const RoomModes &roomModes_);
void loadBadges(); // Data
void loadCheerEmotes();
// Twitch data
UniqueAccess<StreamStatus> streamStatus_;
UniqueAccess<UserState> userState_;
UniqueAccess<RoomModes> roomModes_;
Atomic<std::shared_ptr<const EmoteMap>> bttvEmotes_;
Atomic<std::shared_ptr<const EmoteMap>> ffzEmotes_;
const QString subscriptionUrl_; const QString subscriptionUrl_;
const QString channelUrl_; const QString channelUrl_;
const QString popoutPlayerUrl_; const QString popoutPlayerUrl_;
UniqueAccess<StreamStatus> streamStatus_;
UniqueAccess<RoomModes> roomModes_;
UniqueAccess<UsernameSet> chatters_; // maps 2 char prefix to set of names
// Emotes
TwitchBadges &globalTwitchBadges_;
BttvEmotes &globalBttv_;
FfzEmotes &globalFfz_;
Atomic<std::shared_ptr<const EmoteMap>> bttvEmotes_;
Atomic<std::shared_ptr<const EmoteMap>> ffzEmotes_;
// Badges
UniqueAccess<std::map<QString, std::map<QString, EmotePtr>>>
badgeSets_; // "subscribers": { "0": ... "3": ... "6": ...
UniqueAccess<std::vector<CheerEmoteSet>> cheerEmoteSets_;
bool mod_ = false; bool mod_ = false;
UniqueAccess<QString> roomID_; UniqueAccess<QString> roomID_;
@ -141,10 +148,6 @@ private:
UniqueAccess<QStringList> partedUsers_; UniqueAccess<QStringList> partedUsers_;
bool partedUsersMergeQueued_ = false; bool partedUsersMergeQueued_ = false;
// "subscribers": { "0": ... "3": ... "6": ...
UniqueAccess<std::map<QString, std::map<QString, EmotePtr>>> badgeSets_;
UniqueAccess<std::vector<CheerEmoteSet>> cheerEmoteSets_;
// -- // --
QByteArray messageSuffix_; QByteArray messageSuffix_;
QString lastSentMessage_; QString lastSentMessage_;
@ -153,6 +156,8 @@ private:
QTimer chattersListTimer_; QTimer chattersListTimer_;
friend class TwitchServer; friend class TwitchServer;
friend class TwitchMessageBuilder;
friend class IrcMessageHandler;
}; };
} // namespace chatterino } // namespace chatterino

View file

@ -3,6 +3,7 @@
#include "common/NetworkRequest.hpp" #include "common/NetworkRequest.hpp"
#include "debug/Benchmark.hpp" #include "debug/Benchmark.hpp"
#include "debug/Log.hpp" #include "debug/Log.hpp"
#include "messages/Emote.hpp"
#include "messages/Image.hpp" #include "messages/Image.hpp"
#include "util/RapidjsonHelpers.hpp" #include "util/RapidjsonHelpers.hpp"

View file

@ -1,19 +1,33 @@
#pragma once #pragma once
#include <QColor>
#include <QRegularExpression>
#include <QString> #include <QString>
#include <unordered_map> #include <unordered_map>
#include "common/Aliases.hpp"
#include "common/UniqueAccess.hpp" #include "common/UniqueAccess.hpp"
#include "messages/Emote.hpp"
#include "providers/twitch/EmoteValue.hpp"
#include "providers/twitch/TwitchAccount.hpp"
#include "providers/twitch/TwitchEmotes.hpp" #include "providers/twitch/TwitchEmotes.hpp"
#include "util/ConcurrentMap.hpp"
#define TWITCH_EMOTE_TEMPLATE \ #define TWITCH_EMOTE_TEMPLATE \
"https://static-cdn.jtvnw.net/emoticons/v1/{id}/{scale}" "https://static-cdn.jtvnw.net/emoticons/v1/{id}/{scale}"
namespace chatterino { namespace chatterino {
struct Emote;
using EmotePtr = std::shared_ptr<const Emote>;
struct CheerEmote {
QColor color;
int minBits;
EmotePtr animatedEmote;
EmotePtr staticEmote;
};
struct CheerEmoteSet {
QRegularExpression regex;
std::vector<CheerEmote> cheerEmotes;
};
class TwitchEmotes class TwitchEmotes
{ {

View file

@ -5,6 +5,8 @@
#include "controllers/highlights/HighlightController.hpp" #include "controllers/highlights/HighlightController.hpp"
#include "controllers/ignores/IgnoreController.hpp" #include "controllers/ignores/IgnoreController.hpp"
#include "debug/Log.hpp" #include "debug/Log.hpp"
#include "messages/Message.hpp"
#include "providers/twitch/TwitchBadges.hpp"
#include "providers/twitch/TwitchChannel.hpp" #include "providers/twitch/TwitchChannel.hpp"
#include "singletons/Emotes.hpp" #include "singletons/Emotes.hpp"
#include "singletons/Resources.hpp" #include "singletons/Resources.hpp"
@ -12,6 +14,7 @@
#include "singletons/Theme.hpp" #include "singletons/Theme.hpp"
#include "singletons/WindowManager.hpp" #include "singletons/WindowManager.hpp"
#include "util/IrcHelpers.hpp" #include "util/IrcHelpers.hpp"
#include "widgets/Window.hpp"
#include <QApplication> #include <QApplication>
#include <QDebug> #include <QDebug>
@ -61,7 +64,7 @@ bool TwitchMessageBuilder::isIgnored() const
} }
} }
if (app->settings->enableTwitchIgnoredUsers && this->tags.contains("user-id")) { if (getSettings()->enableTwitchIgnoredUsers && this->tags.contains("user-id")) {
auto sourceUserID = this->tags.value("user-id").toString(); auto sourceUserID = this->tags.value("user-id").toString();
for (const auto &user : app->accounts->twitch.getCurrent()->getIgnores()) { for (const auto &user : app->accounts->twitch.getCurrent()->getIgnores()) {
@ -77,8 +80,6 @@ bool TwitchMessageBuilder::isIgnored() const
MessagePtr TwitchMessageBuilder::build() MessagePtr TwitchMessageBuilder::build()
{ {
auto app = getApp();
// PARSING // PARSING
this->parseUsername(); this->parseUsername();
@ -86,13 +87,6 @@ MessagePtr TwitchMessageBuilder::build()
this->senderIsBroadcaster = true; this->senderIsBroadcaster = true;
} }
//#ifdef XD
// if (this->originalMessage.length() > 100) {
// this->message->flags.has(MessageFlag::Collapsed);
// this->emplace<EmoteElement>(getApp()->resources->badgeCollapsed,
// MessageElementFlag::Collapsed);
// }
//#endif
this->message().flags.has(MessageFlag::Collapsed); this->message().flags.has(MessageFlag::Collapsed);
// PARSING // PARSING
@ -329,11 +323,7 @@ void TwitchMessageBuilder::addWords(
// split words // split words
for (auto &variant : getApp()->emotes->emojis.parse(word)) { for (auto &variant : getApp()->emotes->emojis.parse(word)) {
boost::apply_visitor(/*overloaded{[&](EmotePtr arg) { boost::apply_visitor([&](auto &&arg) { this->addTextOrEmoji(arg); }, variant);
this->addTextOrEmoji(arg); },
[&](const QString &arg) {
this->addTextOrEmoji(arg); }}*/
[&](auto &&arg) { this->addTextOrEmoji(arg); }, variant);
} }
for (int j = 0; j < word.size(); j++) { for (int j = 0; j < word.size(); j++) {
@ -410,7 +400,7 @@ void TwitchMessageBuilder::addTextOrEmoji(const QString &string_)
} }
// if (!linkString.isEmpty()) { // if (!linkString.isEmpty()) {
// if (getApp()->settings->lowercaseLink) { // if (getSettings()->lowercaseLink) {
// QRegularExpression httpRegex("\\bhttps?://", // QRegularExpression httpRegex("\\bhttps?://",
// QRegularExpression::CaseInsensitiveOption); QRegularExpression // QRegularExpression::CaseInsensitiveOption); QRegularExpression
// ftpRegex("\\bftps?://", // ftpRegex("\\bftps?://",
@ -565,14 +555,14 @@ void TwitchMessageBuilder::appendUsername()
// IrcManager::getInstance().getUser().getUserName(); // IrcManager::getInstance().getUser().getUserName();
} else if (this->args.isReceivedWhisper) { } else if (this->args.isReceivedWhisper) {
// Sender username // Sender username
this->emplace<TextElement>(usernameText, MessageElementFlag::Text, this->usernameColor_, this->emplace<TextElement>(usernameText, MessageElementFlag::Username, this->usernameColor_,
FontStyle::ChatMediumBold) FontStyle::ChatMediumBold)
->setLink({Link::UserInfo, this->userName}); ->setLink({Link::UserInfo, this->userName});
auto currentUser = app->accounts->twitch.getCurrent(); auto currentUser = app->accounts->twitch.getCurrent();
// Separator // Separator
this->emplace<TextElement>("->", MessageElementFlag::Text, this->emplace<TextElement>("->", MessageElementFlag::Username,
app->themes->messages.textColors.system, FontStyle::ChatMedium); app->themes->messages.textColors.system, FontStyle::ChatMedium);
QColor selfColor = currentUser->color(); QColor selfColor = currentUser->color();
@ -581,14 +571,14 @@ void TwitchMessageBuilder::appendUsername()
} }
// Your own username // Your own username
this->emplace<TextElement>(currentUser->getUserName() + ":", MessageElementFlag::Text, this->emplace<TextElement>(currentUser->getUserName() + ":", MessageElementFlag::Username,
selfColor, FontStyle::ChatMediumBold); selfColor, FontStyle::ChatMediumBold);
} else { } else {
if (!this->action_) { if (!this->action_) {
usernameText += ":"; usernameText += ":";
} }
this->emplace<TextElement>(usernameText, MessageElementFlag::Text, this->usernameColor_, this->emplace<TextElement>(usernameText, MessageElementFlag::Username, this->usernameColor_,
FontStyle::ChatMediumBold) FontStyle::ChatMediumBold)
->setLink({Link::UserInfo, this->userName}); ->setLink({Link::UserInfo, this->userName});
} }
@ -613,8 +603,8 @@ void TwitchMessageBuilder::parseHighlights(bool isPastMsg)
// update the media player url if necessary // update the media player url if necessary
QUrl highlightSoundUrl; QUrl highlightSoundUrl;
if (app->settings->customHighlightSound) { if (getSettings()->customHighlightSound) {
highlightSoundUrl = QUrl::fromLocalFile(app->settings->pathHighlightSound.getValue()); highlightSoundUrl = QUrl::fromLocalFile(getSettings()->pathHighlightSound.getValue());
} else { } else {
highlightSoundUrl = QUrl("qrc:/sounds/ping2.wav"); highlightSoundUrl = QUrl("qrc:/sounds/ping2.wav");
} }
@ -630,9 +620,9 @@ void TwitchMessageBuilder::parseHighlights(bool isPastMsg)
std::vector<HighlightPhrase> activeHighlights = app->highlights->phrases.getVector(); std::vector<HighlightPhrase> activeHighlights = app->highlights->phrases.getVector();
std::vector<HighlightPhrase> userHighlights = app->highlights->highlightedUsers.getVector(); std::vector<HighlightPhrase> userHighlights = app->highlights->highlightedUsers.getVector();
if (app->settings->enableHighlightsSelf && currentUsername.size() > 0) { if (getSettings()->enableHighlightsSelf && currentUsername.size() > 0) {
HighlightPhrase selfHighlight(currentUsername, app->settings->enableHighlightTaskbar, HighlightPhrase selfHighlight(currentUsername, getSettings()->enableHighlightTaskbar,
app->settings->enableHighlightSound, false); getSettings()->enableHighlightSound, false);
activeHighlights.emplace_back(std::move(selfHighlight)); activeHighlights.emplace_back(std::move(selfHighlight));
} }
@ -689,7 +679,7 @@ void TwitchMessageBuilder::parseHighlights(bool isPastMsg)
this->message().flags.set(MessageFlag::Highlighted, doHighlight); this->message().flags.set(MessageFlag::Highlighted, doHighlight);
if (!isPastMsg) { if (!isPastMsg) {
if (playSound && (!hasFocus || app->settings->highlightAlwaysPlaySound)) { if (playSound && (!hasFocus || getSettings()->highlightAlwaysPlaySound)) {
player->play(); player->play();
} }
@ -748,13 +738,13 @@ Outcome TwitchMessageBuilder::tryAppendEmote(const EmoteName &name)
auto flags = MessageElementFlags(); auto flags = MessageElementFlags();
auto emote = boost::optional<EmotePtr>{}; auto emote = boost::optional<EmotePtr>{};
if ((emote = getApp()->emotes->bttv.global(name))) { if ((emote = this->twitchChannel->globalBttv().emote(name))) {
flags = MessageElementFlag::BttvEmote; flags = MessageElementFlag::BttvEmote;
} else if (twitchChannel && (emote = this->twitchChannel->bttvEmote(name))) { } else if ((emote = this->twitchChannel->bttvEmote(name))) {
flags = MessageElementFlag::BttvEmote; flags = MessageElementFlag::BttvEmote;
} else if ((emote = getApp()->emotes->ffz.global(name))) { } else if ((emote = this->twitchChannel->globalFfz().emote(name))) {
flags = MessageElementFlag::FfzEmote; flags = MessageElementFlag::FfzEmote;
} else if (twitchChannel && (emote = this->twitchChannel->ffzEmote(name))) { } else if ((emote = this->twitchChannel->ffzEmote(name))) {
flags = MessageElementFlag::FfzEmote; flags = MessageElementFlag::FfzEmote;
} }
@ -767,40 +757,23 @@ Outcome TwitchMessageBuilder::tryAppendEmote(const EmoteName &name)
} }
// fourtf: this is ugly // fourtf: this is ugly
// maybe put the individual badges into a map instead of this
// mess
void TwitchMessageBuilder::appendTwitchBadges() void TwitchMessageBuilder::appendTwitchBadges()
{ {
auto app = getApp(); auto app = getApp();
auto iterator = this->tags.find("badges"); auto iterator = this->tags.find("badges");
if (iterator == this->tags.end())
if (iterator == this->tags.end()) {
// No badges in this message
return; return;
}
QStringList badges = iterator.value().toString().split(',');
for (QString badge : badges) {
if (badge.isEmpty()) {
continue;
}
for (QString badge : iterator.value().toString().split(',')) {
if (badge.startsWith("bits/")) { if (badge.startsWith("bits/")) {
// if (!app->resources->dynamicBadgesLoaded) {
// // Do nothing
// continue;
// }
QString cheerAmount = badge.mid(5); QString cheerAmount = badge.mid(5);
QString tooltip = QString("Twitch cheer ") + cheerAmount; QString tooltip = QString("Twitch cheer ") + cheerAmount;
// Try to fetch channel-specific bit badge // Try to fetch channel-specific bit badge
try { try {
if (twitchChannel) if (twitchChannel)
if (const auto &badge = if (const auto &badge = this->twitchChannel->twitchBadge("bits", cheerAmount)) {
this->twitchChannel->getTwitchBadge("bits", cheerAmount)) {
this->emplace<EmoteElement>(badge.get(), MessageElementFlag::BadgeVanity) this->emplace<EmoteElement>(badge.get(), MessageElementFlag::BadgeVanity)
->setTooltip(tooltip); ->setTooltip(tooltip);
continue; continue;
@ -810,16 +783,10 @@ void TwitchMessageBuilder::appendTwitchBadges()
} }
// Use default bit badge // Use default bit badge
// try { if (auto badge = this->twitchChannel->globalTwitchBadges().badge("bits", cheerAmount)) {
// const auto &badge = this->emplace<EmoteElement>(badge.get(), MessageElementFlag::BadgeVanity)
// app->resources->badgeSets.at("bits").versions.at(cheerAmount); ->setTooltip(tooltip);
// this->emplace<ImageElement>(badge.badgeImage1x, }
// MessageElementFlag::BadgeVanity)
// ->setTooltip(tooltip);
//} catch (const std::out_of_range &) {
// Log("No default bit badge for version {} found", cheerAmount);
// continue;
//}
} else if (badge == "staff/1") { } else if (badge == "staff/1") {
this->emplace<ImageElement>(Image::fromPixmap(app->resources->twitch.staff), this->emplace<ImageElement>(Image::fromPixmap(app->resources->twitch.staff),
MessageElementFlag::BadgeGlobalAuthority) MessageElementFlag::BadgeGlobalAuthority)
@ -865,80 +832,26 @@ void TwitchMessageBuilder::appendTwitchBadges()
} break; } break;
} }
} else if (badge.startsWith("subscriber/")) { } else if (badge.startsWith("subscriber/")) {
// if (channelResources.loaded == false) { if (auto badgeEmote = this->twitchChannel->twitchBadge("subscriber", badge.mid(11))) {
// // qDebug() << "Channel resources are not loaded, this->emplace<EmoteElement>(badgeEmote.get(), MessageElementFlag::BadgeSubscription)
// can't add the subscriber ->setTooltip((*badgeEmote)->tooltip.string);
// // badge"; continue;
// continue; }
// }
// auto badgeSetIt = channelResources.badgeSets.find("subscriber"); // use default subscriber badge if custom one not found
// if (badgeSetIt == channelResources.badgeSets.end()) { this->emplace<ImageElement>(Image::fromPixmap(app->resources->twitch.subscriber, 0.25),
// // Fall back to default badge MessageElementFlag::BadgeSubscription)
// this->emplace<ImageElement>(app->resources->badgeSubscriber, ->setTooltip("Twitch Subscriber");
// MessageElementFlag::BadgeSubscription)
// ->setTooltip("Twitch Subscriber");
// continue;
//}
// const auto &badgeSet = badgeSetIt->second;
// std::string versionKey = badge.mid(11).toStdString();
// auto badgeVersionIt = badgeSet.versions.find(versionKey);
// if (badgeVersionIt == badgeSet.versions.end()) {
// // Fall back to default badge
// this->emplace<ImageElement>(app->resources->badgeSubscriber,
// MessageElementFlag::BadgeSubscription)
// ->setTooltip("Twitch Subscriber");
// continue;
//}
// auto &badgeVersion = badgeVersionIt->second;
// this->emplace<ImageElement>(badgeVersion.badgeImage1x,
// MessageElementFlag::BadgeSubscription)
// ->setTooltip("Twitch " +
// QString::fromStdString(badgeVersion.title));
} else { } else {
// if (!app->resources->dynamicBadgesLoaded) { auto splits = badge.split('/');
// // Do nothing if (splits.size() != 2)
// continue; continue;
//}
// QStringList parts = badge.split('/'); if (auto badgeEmote = this->twitchChannel->twitchBadge(splits[0], splits[1])) {
this->emplace<EmoteElement>(badgeEmote.get(), MessageElementFlag::BadgeVanity)
// if (parts.length() != 2) { ->setTooltip((*badgeEmote)->tooltip.string);
// qDebug() << "Bad number of parts: " << parts.length() << " in continue;
// " << parts; continue; }
//}
// MessageElementFlags badgeType =
// MessageElementFlag::BadgeVanity;
// std::string badgeSetKey = parts[0].toStdString();
// std::string versionKey = parts[1].toStdString();
// try {
// auto &badgeSet = app->resources->badgeSets.at(badgeSetKey);
// try {
// auto &badgeVersion = badgeSet.versions.at(versionKey);
// this->emplace<ImageElement>(badgeVersion.badgeImage1x,
// badgeType)
// ->setTooltip("Twitch " +
// QString::fromStdString(badgeVersion.title));
// } catch (const std::exception &e) {
// qDebug() << "Exception caught:" << e.what()
// << "when trying to fetch badge version " <<
// versionKey.c_str();
// }
//} catch (const std::exception &e) {
// qDebug() << "No badge set with key" << badgeSetKey.c_str()
// << ". Exception: " << e.what();
//}
} }
} }
} }

View file

@ -1,15 +1,18 @@
#pragma once #pragma once
#include "common/Aliases.hpp"
#include "common/Outcome.hpp"
#include "messages/MessageBuilder.hpp" #include "messages/MessageBuilder.hpp"
#include "singletons/Emotes.hpp"
#include <IrcMessage> #include <IrcMessage>
#include <QString> #include <QString>
#include <QVariant> #include <QVariant>
namespace chatterino { namespace chatterino {
struct Emote;
using EmotePtr = std::shared_ptr<const Emote>;
class Channel; class Channel;
class TwitchChannel; class TwitchChannel;

View file

@ -8,235 +8,235 @@ namespace chatterino {
namespace { namespace {
template <typename Type> template <typename Type>
inline bool ReadValue(const rapidjson::Value &object, const char *key, inline bool ReadValue(const rapidjson::Value &object, const char *key,
Type &out) Type &out)
{ {
if (!object.HasMember(key)) { if (!object.HasMember(key)) {
return false;
}
const auto &value = object[key];
if (!value.Is<Type>()) {
return false;
}
out = value.Get<Type>();
return true;
}
template <>
inline bool ReadValue<QString>(const rapidjson::Value &object, const char *key,
QString &out)
{
if (!object.HasMember(key)) {
return false;
}
const auto &value = object[key];
if (!value.IsString()) {
return false;
}
out = value.GetString();
return true;
}
template <>
inline bool ReadValue<std::vector<QString>>(const rapidjson::Value &object,
const char *key,
std::vector<QString> &out)
{
if (!object.HasMember(key)) {
return false;
}
const auto &value = object[key];
if (!value.IsArray()) {
return false;
}
for (const rapidjson::Value &innerValue : value.GetArray()) {
if (!innerValue.IsString()) {
return false; return false;
} }
out.emplace_back(innerValue.GetString()); const auto &value = object[key];
}
return true; if (!value.Is<Type>()) {
}
// Parse a single cheermote set (or "action") from the twitch api
inline bool ParseSingleCheermoteSet(JSONCheermoteSet &set,
const rapidjson::Value &action)
{
if (!action.IsObject()) {
return false;
}
if (!ReadValue(action, "prefix", set.prefix)) {
return false;
}
if (!ReadValue(action, "scales", set.scales)) {
return false;
}
if (!ReadValue(action, "backgrounds", set.backgrounds)) {
return false;
}
if (!ReadValue(action, "states", set.states)) {
return false;
}
if (!ReadValue(action, "type", set.type)) {
return false;
}
if (!ReadValue(action, "updated_at", set.updatedAt)) {
return false;
}
if (!ReadValue(action, "priority", set.priority)) {
return false;
}
// Tiers
if (!action.HasMember("tiers")) {
return false;
}
const auto &tiersValue = action["tiers"];
if (!tiersValue.IsArray()) {
return false;
}
for (const rapidjson::Value &tierValue : tiersValue.GetArray()) {
JSONCheermoteSet::CheermoteTier tier;
if (!tierValue.IsObject()) {
return false; return false;
} }
if (!ReadValue(tierValue, "min_bits", tier.minBits)) { out = value.Get<Type>();
return true;
}
template <>
inline bool ReadValue<QString>(const rapidjson::Value &object,
const char *key, QString &out)
{
if (!object.HasMember(key)) {
return false; return false;
} }
if (!ReadValue(tierValue, "id", tier.id)) { const auto &value = object[key];
if (!value.IsString()) {
return false; return false;
} }
if (!ReadValue(tierValue, "color", tier.color)) { out = value.GetString();
return true;
}
template <>
inline bool ReadValue<std::vector<QString>>(const rapidjson::Value &object,
const char *key,
std::vector<QString> &out)
{
if (!object.HasMember(key)) {
return false; return false;
} }
// Images const auto &value = object[key];
if (!tierValue.HasMember("images")) {
if (!value.IsArray()) {
return false; return false;
} }
const auto &imagesValue = tierValue["images"]; for (const rapidjson::Value &innerValue : value.GetArray()) {
if (!innerValue.IsString()) {
if (!imagesValue.IsObject()) { return false;
return false;
}
// Read images object
for (const auto &imageBackgroundValue : imagesValue.GetObject()) {
QString background = imageBackgroundValue.name.GetString();
bool backgroundExists = false;
for (const auto &bg : set.backgrounds) {
if (background == bg) {
backgroundExists = true;
break;
}
} }
if (!backgroundExists) { out.emplace_back(innerValue.GetString());
continue; }
return true;
}
// Parse a single cheermote set (or "action") from the twitch api
inline bool ParseSingleCheermoteSet(JSONCheermoteSet &set,
const rapidjson::Value &action)
{
if (!action.IsObject()) {
return false;
}
if (!ReadValue(action, "prefix", set.prefix)) {
return false;
}
if (!ReadValue(action, "scales", set.scales)) {
return false;
}
if (!ReadValue(action, "backgrounds", set.backgrounds)) {
return false;
}
if (!ReadValue(action, "states", set.states)) {
return false;
}
if (!ReadValue(action, "type", set.type)) {
return false;
}
if (!ReadValue(action, "updated_at", set.updatedAt)) {
return false;
}
if (!ReadValue(action, "priority", set.priority)) {
return false;
}
// Tiers
if (!action.HasMember("tiers")) {
return false;
}
const auto &tiersValue = action["tiers"];
if (!tiersValue.IsArray()) {
return false;
}
for (const rapidjson::Value &tierValue : tiersValue.GetArray()) {
JSONCheermoteSet::CheermoteTier tier;
if (!tierValue.IsObject()) {
return false;
} }
const rapidjson::Value &imageBackgroundStates = if (!ReadValue(tierValue, "min_bits", tier.minBits)) {
imageBackgroundValue.value; return false;
if (!imageBackgroundStates.IsObject()) {
continue;
} }
// Read each key which represents a background if (!ReadValue(tierValue, "id", tier.id)) {
for (const auto &imageBackgroundState : return false;
imageBackgroundStates.GetObject()) { }
QString state = imageBackgroundState.name.GetString();
bool stateExists = false; if (!ReadValue(tierValue, "color", tier.color)) {
for (const auto &_state : set.states) { return false;
if (state == _state) { }
stateExists = true;
// Images
if (!tierValue.HasMember("images")) {
return false;
}
const auto &imagesValue = tierValue["images"];
if (!imagesValue.IsObject()) {
return false;
}
// Read images object
for (const auto &imageBackgroundValue : imagesValue.GetObject()) {
QString background = imageBackgroundValue.name.GetString();
bool backgroundExists = false;
for (const auto &bg : set.backgrounds) {
if (background == bg) {
backgroundExists = true;
break; break;
} }
} }
if (!stateExists) { if (!backgroundExists) {
continue; continue;
} }
const rapidjson::Value &imageScalesValue = const rapidjson::Value &imageBackgroundStates =
imageBackgroundState.value; imageBackgroundValue.value;
if (!imageScalesValue.IsObject()) { if (!imageBackgroundStates.IsObject()) {
continue; continue;
} }
// Read each key which represents a scale // Read each key which represents a background
for (const auto &imageScaleValue : for (const auto &imageBackgroundState :
imageScalesValue.GetObject()) { imageBackgroundStates.GetObject()) {
QString scale = imageScaleValue.name.GetString(); QString state = imageBackgroundState.name.GetString();
bool scaleExists = false; bool stateExists = false;
for (const auto &_scale : set.scales) { for (const auto &_state : set.states) {
if (scale == _scale) { if (state == _state) {
scaleExists = true; stateExists = true;
break; break;
} }
} }
if (!scaleExists) { if (!stateExists) {
continue; continue;
} }
const rapidjson::Value &imageScaleURLValue = const rapidjson::Value &imageScalesValue =
imageScaleValue.value; imageBackgroundState.value;
if (!imageScaleURLValue.IsString()) { if (!imageScalesValue.IsObject()) {
continue; continue;
} }
QString url = imageScaleURLValue.GetString(); // Read each key which represents a scale
for (const auto &imageScaleValue :
imageScalesValue.GetObject()) {
QString scale = imageScaleValue.name.GetString();
bool scaleExists = false;
for (const auto &_scale : set.scales) {
if (scale == _scale) {
scaleExists = true;
break;
}
}
bool ok = false; if (!scaleExists) {
qreal scaleNumber = scale.toFloat(&ok); continue;
if (!ok) { }
continue;
const rapidjson::Value &imageScaleURLValue =
imageScaleValue.value;
if (!imageScaleURLValue.IsString()) {
continue;
}
QString url = imageScaleURLValue.GetString();
bool ok = false;
qreal scaleNumber = scale.toFloat(&ok);
if (!ok) {
continue;
}
qreal chatterinoScale = 1 / scaleNumber;
auto image = Image::fromUrl({url}, chatterinoScale);
// TODO(pajlada): Fill in name and tooltip
tier.images[background][state][scale] = image;
} }
qreal chatterinoScale = 1 / scaleNumber;
auto image = Image::fromUrl({url}, chatterinoScale);
// TODO(pajlada): Fill in name and tooltip
tier.images[background][state][scale] = image;
} }
} }
set.tiers.emplace_back(tier);
} }
set.tiers.emplace_back(tier); return true;
} }
return true;
}
} // namespace } // namespace
// Look through the results of // Look through the results of

View file

@ -7,12 +7,12 @@
#include "providers/twitch/IrcMessageHandler.hpp" #include "providers/twitch/IrcMessageHandler.hpp"
#include "providers/twitch/PubsubClient.hpp" #include "providers/twitch/PubsubClient.hpp"
#include "providers/twitch/TwitchAccount.hpp" #include "providers/twitch/TwitchAccount.hpp"
#include "providers/twitch/TwitchChannel.hpp"
#include "providers/twitch/TwitchHelpers.hpp" #include "providers/twitch/TwitchHelpers.hpp"
#include "providers/twitch/TwitchMessageBuilder.hpp" #include "providers/twitch/TwitchMessageBuilder.hpp"
#include "util/PostToThread.hpp" #include "util/PostToThread.hpp"
#include <IrcCommand> #include <IrcCommand>
#include <cassert> #include <cassert>
// using namespace Communi; // using namespace Communi;
@ -39,6 +39,10 @@ void TwitchServer::initialize(Settings &settings, Paths &paths)
{ {
getApp()->accounts->twitch.currentUserChanged.connect( getApp()->accounts->twitch.currentUserChanged.connect(
[this]() { postToThread([this] { this->connect(); }); }); [this]() { postToThread([this] { this->connect(); }); });
this->twitchBadges.loadTwitchBadges();
this->bttv.loadEmotes();
this->ffz.loadEmotes();
} }
void TwitchServer::initializeConnection(IrcConnection *connection, bool isRead, void TwitchServer::initializeConnection(IrcConnection *connection, bool isRead,
@ -79,9 +83,9 @@ void TwitchServer::initializeConnection(IrcConnection *connection, bool isRead,
std::shared_ptr<Channel> TwitchServer::createChannel(const QString &channelName) std::shared_ptr<Channel> TwitchServer::createChannel(const QString &channelName)
{ {
auto channel = auto channel = std::shared_ptr<TwitchChannel>(new TwitchChannel(
std::shared_ptr<TwitchChannel>(new TwitchChannel(channelName)); channelName, this->twitchBadges, this->bttv, this->ffz));
channel->refreshChannelEmotes(); channel->initialize();
channel->sendMessageSignal.connect( channel->sendMessageSignal.connect(
[this, channel = channel.get()](auto &chan, auto &msg, bool &sent) { [this, channel = channel.get()](auto &chan, auto &msg, bool &sent) {

View file

@ -1,10 +1,13 @@
#pragma once #pragma once
#include "common/Atomic.hpp" #include "common/Atomic.hpp"
#include "common/Channel.hpp"
#include "common/Singleton.hpp" #include "common/Singleton.hpp"
#include "pajlada/signals/signalholder.hpp"
#include "providers/bttv/BttvEmotes.hpp"
#include "providers/ffz/FfzEmotes.hpp"
#include "providers/irc/AbstractIrcServer.hpp" #include "providers/irc/AbstractIrcServer.hpp"
#include "providers/twitch/TwitchAccount.hpp" #include "providers/twitch/TwitchBadges.hpp"
#include "providers/twitch/TwitchChannel.hpp"
#include <chrono> #include <chrono>
#include <memory> #include <memory>
@ -14,8 +17,8 @@ namespace chatterino {
class Settings; class Settings;
class Paths; class Paths;
class PubSub; class PubSub;
class TwitchChannel;
class TwitchServer final : public AbstractIrcServer, public Singleton class TwitchServer final : public AbstractIrcServer, public Singleton
{ {
@ -66,6 +69,9 @@ private:
std::chrono::steady_clock::time_point lastErrorTimeAmount_; std::chrono::steady_clock::time_point lastErrorTimeAmount_;
bool singleConnection_ = false; bool singleConnection_ = false;
TwitchBadges twitchBadges;
BttvEmotes bttv;
FfzEmotes ffz;
pajlada::Signals::SignalHolder signalHolder_; pajlada::Signals::SignalHolder signalHolder_;
}; };

View file

@ -34,43 +34,43 @@ struct TwitchUser {
namespace pajlada { namespace pajlada {
namespace Settings { namespace Settings {
template <> template <>
struct Deserialize<chatterino::TwitchUser> { struct Deserialize<chatterino::TwitchUser> {
static chatterino::TwitchUser get(const rapidjson::Value &value, static chatterino::TwitchUser get(const rapidjson::Value &value,
bool *error = nullptr) bool *error = nullptr)
{ {
using namespace chatterino; using namespace chatterino;
TwitchUser user; TwitchUser user;
if (!value.IsObject()) {
PAJLADA_REPORT_ERROR(error)
PAJLADA_THROW_EXCEPTION(
"Deserialized rapidjson::Value is wrong type");
return user;
}
if (!rj::getSafe(value, "_id", user.id)) {
PAJLADA_REPORT_ERROR(error)
PAJLADA_THROW_EXCEPTION("Missing ID key");
return user;
}
if (!rj::getSafe(value, "name", user.name)) {
PAJLADA_REPORT_ERROR(error)
PAJLADA_THROW_EXCEPTION("Missing name key");
return user;
}
if (!rj::getSafe(value, "display_name", user.displayName)) {
PAJLADA_REPORT_ERROR(error)
PAJLADA_THROW_EXCEPTION("Missing display name key");
return user;
}
if (!value.IsObject()) {
PAJLADA_REPORT_ERROR(error)
PAJLADA_THROW_EXCEPTION(
"Deserialized rapidjson::Value is wrong type");
return user; return user;
} }
};
if (!rj::getSafe(value, "_id", user.id)) {
PAJLADA_REPORT_ERROR(error)
PAJLADA_THROW_EXCEPTION("Missing ID key");
return user;
}
if (!rj::getSafe(value, "name", user.name)) {
PAJLADA_REPORT_ERROR(error)
PAJLADA_THROW_EXCEPTION("Missing name key");
return user;
}
if (!rj::getSafe(value, "display_name", user.displayName)) {
PAJLADA_REPORT_ERROR(error)
PAJLADA_THROW_EXCEPTION("Missing display name key");
return user;
}
return user;
}
};
} // namespace Settings } // namespace Settings
} // namespace pajlada } // namespace pajlada

View file

@ -15,8 +15,6 @@ void Emotes::initialize(Settings &settings, Paths &paths)
[] { getApp()->accounts->twitch.getCurrent()->loadEmotes(); }); [] { getApp()->accounts->twitch.getCurrent()->loadEmotes(); });
this->emojis.load(); this->emojis.load();
this->bttv.loadGlobal();
this->ffz.loadGlobal();
this->gifTimer.initialize(); this->gifTimer.initialize();
} }

View file

@ -25,8 +25,6 @@ public:
bool isIgnoredEmote(const QString &emote); bool isIgnoredEmote(const QString &emote);
TwitchEmotes twitch; TwitchEmotes twitch;
BttvEmotes bttv;
FfzEmotes ffz;
Emojis emojis; Emojis emojis;
GIFTimer gifTimer; GIFTimer gifTimer;

View file

@ -2,22 +2,23 @@
#include "Application.hpp" #include "Application.hpp"
#include "debug/AssertInGuiThread.hpp" #include "debug/AssertInGuiThread.hpp"
#include "singletons/Settings.hpp"
#include "singletons/WindowManager.hpp" #include "singletons/WindowManager.hpp"
#include <QDebug> #include <QDebug>
#include <QtGlobal> #include <QtGlobal>
#ifdef Q_OS_WIN32 #ifdef Q_OS_WIN32
#define DEFAULT_FONT_FAMILY "Segoe UI" # define DEFAULT_FONT_FAMILY "Segoe UI"
#define DEFAULT_FONT_SIZE 10 # define DEFAULT_FONT_SIZE 10
#else #else
#ifdef Q_OS_MACOS # ifdef Q_OS_MACOS
#define DEFAULT_FONT_FAMILY "Helvetica Neue" # define DEFAULT_FONT_FAMILY "Helvetica Neue"
#define DEFAULT_FONT_SIZE 12 # define DEFAULT_FONT_SIZE 12
#else # else
#define DEFAULT_FONT_FAMILY "Arial" # define DEFAULT_FONT_FAMILY "Arial"
#define DEFAULT_FONT_SIZE 11 # define DEFAULT_FONT_SIZE 11
#endif # endif
#endif #endif
namespace chatterino { namespace chatterino {
@ -26,7 +27,7 @@ Fonts::Fonts()
: chatFontFamily("/appearance/currentFontFamily", DEFAULT_FONT_FAMILY) : chatFontFamily("/appearance/currentFontFamily", DEFAULT_FONT_FAMILY)
, chatFontSize("/appearance/currentFontSize", DEFAULT_FONT_SIZE) , chatFontSize("/appearance/currentFontSize", DEFAULT_FONT_SIZE)
{ {
this->fontsByType_.resize(size_t(EndType)); this->fontsByType_.resize(size_t(FontStyle::EndType));
} }
void Fonts::initialize(Settings &, Paths &) void Fonts::initialize(Settings &, Paths &)
@ -61,21 +62,21 @@ void Fonts::initialize(Settings &, Paths &)
}); });
} }
QFont Fonts::getFont(Fonts::Type type, float scale) QFont Fonts::getFont(FontStyle type, float scale)
{ {
return this->getOrCreateFontData(type, scale).font; return this->getOrCreateFontData(type, scale).font;
} }
QFontMetrics Fonts::getFontMetrics(Fonts::Type type, float scale) QFontMetrics Fonts::getFontMetrics(FontStyle type, float scale)
{ {
return this->getOrCreateFontData(type, scale).metrics; return this->getOrCreateFontData(type, scale).metrics;
} }
Fonts::FontData &Fonts::getOrCreateFontData(Type type, float scale) Fonts::FontData &Fonts::getOrCreateFontData(FontStyle type, float scale)
{ {
assertInGuiThread(); assertInGuiThread();
assert(type >= 0 && type < EndType); assert(type < FontStyle::EndType);
auto &map = this->fontsByType_[size_t(type)]; auto &map = this->fontsByType_[size_t(type)];
@ -94,23 +95,22 @@ Fonts::FontData &Fonts::getOrCreateFontData(Type type, float scale)
return result.first->second; return result.first->second;
} }
Fonts::FontData Fonts::createFontData(Type type, float scale) Fonts::FontData Fonts::createFontData(FontStyle type, float scale)
{ {
// check if it's a chat (scale the setting) // check if it's a chat (scale the setting)
if (type >= ChatStart && type <= ChatEnd) { if (type >= FontStyle::ChatStart && type <= FontStyle::ChatEnd) {
static std::unordered_map<Type, ChatFontData> sizeScale{ static std::unordered_map<FontStyle, ChatFontData> sizeScale{
{ChatSmall, {0.6f, false, QFont::Normal}}, {FontStyle::ChatSmall, {0.6f, false, QFont::Normal}},
{ChatMediumSmall, {0.8f, false, QFont::Normal}}, {FontStyle::ChatMediumSmall, {0.8f, false, QFont::Normal}},
{ChatMedium, {1, false, QFont::Normal}}, {FontStyle::ChatMedium, {1, false, QFont::Normal}},
{ChatMediumBold, {FontStyle::ChatMediumBold,
{1, false, {1, false, QFont::Weight(getSettings()->boldScale.getValue())}},
QFont::Weight(getApp()->settings->boldScale.getValue())}}, {FontStyle::ChatMediumItalic, {1, true, QFont::Normal}},
{ChatMediumItalic, {1, true, QFont::Normal}}, {FontStyle::ChatLarge, {1.2f, false, QFont::Normal}},
{ChatLarge, {1.2f, false, QFont::Normal}}, {FontStyle::ChatVeryLarge, {1.4f, false, QFont::Normal}},
{ChatVeryLarge, {1.4f, false, QFont::Normal}},
}; };
sizeScale[ChatMediumBold] = { sizeScale[FontStyle::ChatMediumBold] = {
1, false, QFont::Weight(getApp()->settings->boldScale.getValue())}; 1, false, QFont::Weight(getSettings()->boldScale.getValue())};
auto data = sizeScale[type]; auto data = sizeScale[type];
return FontData( return FontData(
QFont(QString::fromStdString(this->chatFontFamily.getValue()), QFont(QString::fromStdString(this->chatFontFamily.getValue()),
@ -126,11 +126,11 @@ Fonts::FontData Fonts::createFontData(Type type, float scale)
constexpr float multiplier = 1.f; constexpr float multiplier = 1.f;
#endif #endif
static std::unordered_map<Type, UiFontData> defaultSize{ static std::unordered_map<FontStyle, UiFontData> defaultSize{
{Tiny, {8, "Monospace", false, QFont::Normal}}, {FontStyle::Tiny, {8, "Monospace", false, QFont::Normal}},
{UiMedium, {FontStyle::UiMedium,
{int(9 * multiplier), DEFAULT_FONT_FAMILY, false, QFont::Normal}}, {int(9 * multiplier), DEFAULT_FONT_FAMILY, false, QFont::Normal}},
{UiTabs, {FontStyle::UiTabs,
{int(9 * multiplier), DEFAULT_FONT_FAMILY, false, QFont::Normal}}, {int(9 * multiplier), DEFAULT_FONT_FAMILY, false, QFont::Normal}},
}; };

View file

@ -16,6 +16,27 @@ namespace chatterino {
class Settings; class Settings;
class Paths; class Paths;
enum class FontStyle : uint8_t {
Tiny,
ChatSmall,
ChatMediumSmall,
ChatMedium,
ChatMediumBold,
ChatMediumItalic,
ChatLarge,
ChatVeryLarge,
UiMedium,
UiTabs,
// don't remove this value
EndType,
// make sure to update these values accordingly!
ChatStart = ChatSmall,
ChatEnd = ChatVeryLarge,
};
class Fonts final : public Singleton class Fonts final : public Singleton
{ {
public: public:
@ -24,29 +45,9 @@ public:
virtual void initialize(Settings &settings, Paths &paths) override; virtual void initialize(Settings &settings, Paths &paths) override;
// font data gets set in createFontData(...) // font data gets set in createFontData(...)
enum Type : uint8_t {
Tiny,
ChatSmall,
ChatMediumSmall,
ChatMedium,
ChatMediumBold,
ChatMediumItalic,
ChatLarge,
ChatVeryLarge,
UiMedium, QFont getFont(FontStyle type, float scale);
UiTabs, QFontMetrics getFontMetrics(FontStyle type, float scale);
// don't remove this value
EndType,
// make sure to update these values accordingly!
ChatStart = ChatSmall,
ChatEnd = ChatVeryLarge,
};
QFont getFont(Type type, float scale);
QFontMetrics getFontMetrics(Type type, float scale);
pajlada::Settings::Setting<std::string> chatFontFamily; pajlada::Settings::Setting<std::string> chatFontFamily;
pajlada::Settings::Setting<int> chatFontSize; pajlada::Settings::Setting<int> chatFontSize;
@ -78,12 +79,10 @@ private:
QFont::Weight weight; QFont::Weight weight;
}; };
FontData &getOrCreateFontData(Type type, float scale); FontData &getOrCreateFontData(FontStyle type, float scale);
FontData createFontData(Type type, float scale); FontData createFontData(FontStyle type, float scale);
std::vector<std::unordered_map<float, FontData>> fontsByType_; std::vector<std::unordered_map<float, FontData>> fontsByType_;
}; };
using FontStyle = Fonts::Type;
} // namespace chatterino } // namespace chatterino

View file

@ -17,11 +17,11 @@
namespace ipc = boost::interprocess; namespace ipc = boost::interprocess;
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
#include <QProcess> # include <QProcess>
#include <Windows.h> # include <Windows.h>
#include "singletons/WindowManager.hpp" # include "singletons/WindowManager.hpp"
#include "widgets/AttachedWindow.hpp" # include "widgets/AttachedWindow.hpp"
#endif #endif
#include <iostream> #include <iostream>

View file

@ -43,6 +43,6 @@ private:
boost::optional<bool> portable_; boost::optional<bool> portable_;
}; };
[[deprecated]] Paths *getPaths(); Paths *getPaths();
} // namespace chatterino } // namespace chatterino

View file

@ -5,7 +5,6 @@
#include "common/ChatterinoSetting.hpp" #include "common/ChatterinoSetting.hpp"
#include "controllers/highlights/HighlightPhrase.hpp" #include "controllers/highlights/HighlightPhrase.hpp"
#include "controllers/moderationactions/ModerationAction.hpp" #include "controllers/moderationactions/ModerationAction.hpp"
#include "messages/MessageElement.hpp"
#include <pajlada/settings/setting.hpp> #include <pajlada/settings/setting.hpp>
#include <pajlada/settings/settinglistener.hpp> #include <pajlada/settings/settinglistener.hpp>
@ -26,6 +25,8 @@ public:
/// Appearance /// Appearance
BoolSetting showTimestamps = {"/appearance/messages/showTimestamps", true}; BoolSetting showTimestamps = {"/appearance/messages/showTimestamps", true};
BoolSetting enableAnimationsWhenFocused = {
"/appearance/enableAnimationsWhenFocused", false};
QStringSetting timestampFormat = {"/appearance/messages/timestampFormat", QStringSetting timestampFormat = {"/appearance/messages/timestampFormat",
"h:mm"}; "h:mm"};
BoolSetting showBadges = {"/appearance/messages/showBadges", true}; BoolSetting showBadges = {"/appearance/messages/showBadges", true};

View file

@ -10,21 +10,21 @@ namespace chatterino {
namespace detail { namespace detail {
double getMultiplierByTheme(const QString &themeName) double getMultiplierByTheme(const QString &themeName)
{ {
if (themeName == "Light") { if (themeName == "Light") {
return 0.8; return 0.8;
} else if (themeName == "White") { } else if (themeName == "White") {
return 1.0; return 1.0;
} else if (themeName == "Black") { } else if (themeName == "Black") {
return -1.0; return -1.0;
} else if (themeName == "Dark") { } else if (themeName == "Dark") {
return -0.8;
}
return -0.8; return -0.8;
} }
return -0.8;
}
} // namespace detail } // namespace detail
Theme::Theme() Theme::Theme()

View file

@ -1,6 +1,7 @@
#include "Updates.hpp" #include "Updates.hpp"
#include "common/NetworkRequest.hpp" #include "common/NetworkRequest.hpp"
#include "common/Outcome.hpp"
#include "common/Version.hpp" #include "common/Version.hpp"
#include "singletons/Paths.hpp" #include "singletons/Paths.hpp"
#include "util/CombinePath.hpp" #include "util/CombinePath.hpp"

Some files were not shown because too many files have changed in this diff Show more