mirror-chatterino2/src/providers/twitch/TwitchMessageBuilder.cpp

837 lines
27 KiB
C++
Raw Normal View History

2018-06-26 14:09:39 +02:00
#include "providers/twitch/TwitchMessageBuilder.hpp"
2018-06-26 14:09:39 +02:00
#include "Application.hpp"
#include "controllers/accounts/AccountController.hpp"
#include "controllers/highlights/HighlightController.hpp"
#include "controllers/ignores/IgnoreController.hpp"
#include "debug/Log.hpp"
#include "messages/Message.hpp"
2018-08-14 17:45:17 +02:00
#include "providers/twitch/TwitchBadges.hpp"
2018-06-26 14:09:39 +02:00
#include "providers/twitch/TwitchChannel.hpp"
2018-06-28 19:46:45 +02:00
#include "singletons/Emotes.hpp"
#include "singletons/Resources.hpp"
#include "singletons/Settings.hpp"
2018-06-28 20:03:04 +02:00
#include "singletons/Theme.hpp"
2018-06-26 14:09:39 +02:00
#include "singletons/WindowManager.hpp"
#include "util/IrcHelpers.hpp"
#include "widgets/Window.hpp"
2017-04-12 17:46:44 +02:00
#include <QApplication>
#include <QDebug>
#include <QMediaPlayer>
2018-08-02 14:23:27 +02:00
#include <boost/variant.hpp>
2017-04-14 17:52:22 +02:00
namespace chatterino {
2018-08-06 21:17:03 +02:00
TwitchMessageBuilder::TwitchMessageBuilder(
Channel *_channel, const Communi::IrcPrivateMessage *_ircMessage,
const MessageParseArgs &_args)
2017-07-02 18:12:11 +02:00
: channel(_channel)
2017-12-31 00:50:07 +01:00
, twitchChannel(dynamic_cast<TwitchChannel *>(_channel))
2017-07-02 18:12:11 +02:00
, ircMessage(_ircMessage)
, args(_args)
, tags(this->ircMessage->tags())
2018-07-06 19:23:47 +02:00
, originalMessage_(_ircMessage->content())
, action_(_ircMessage->isAction())
{
this->usernameColor_ = getApp()->themes->messages.textColors.system;
}
2018-08-06 21:17:03 +02:00
TwitchMessageBuilder::TwitchMessageBuilder(
Channel *_channel, const Communi::IrcMessage *_ircMessage,
const MessageParseArgs &_args, QString content, bool isAction)
: channel(_channel)
, twitchChannel(dynamic_cast<TwitchChannel *>(_channel))
, ircMessage(_ircMessage)
, args(_args)
, tags(this->ircMessage->tags())
2018-07-06 19:23:47 +02:00
, originalMessage_(content)
, action_(isAction)
2017-04-12 17:46:44 +02:00
{
this->usernameColor_ = getApp()->themes->messages.textColors.system;
2017-04-12 17:46:44 +02:00
}
2018-01-23 21:33:49 +01:00
bool TwitchMessageBuilder::isIgnored() const
2017-04-12 17:46:44 +02:00
{
auto app = getApp();
2018-05-13 19:24:32 +02:00
// TODO(pajlada): Do we need to check if the phrase is valid first?
for (const auto &phrase : app->ignores->phrases.getVector()) {
2018-07-06 19:23:47 +02:00
if (phrase.isMatch(this->originalMessage_)) {
2018-08-11 14:20:53 +02:00
log("Blocking message because it contains ignored phrase {}",
2018-08-06 21:17:03 +02:00
phrase.getPattern());
2018-01-23 21:33:49 +01:00
return true;
}
}
if (getSettings()->enableTwitchIgnoredUsers &&
2018-08-06 21:17:03 +02:00
this->tags.contains("user-id")) {
auto sourceUserID = this->tags.value("user-id").toString();
2018-08-06 21:17:03 +02:00
for (const auto &user :
app->accounts->twitch.getCurrent()->getIgnores()) {
if (sourceUserID == user.id) {
2018-08-11 14:20:53 +02:00
log("Blocking message because it's from blocked user {}",
2018-08-06 21:17:03 +02:00
user.name);
return true;
}
}
}
2018-01-23 21:33:49 +01:00
return false;
2017-04-12 17:46:44 +02:00
}
2018-01-23 23:28:06 +01:00
MessagePtr TwitchMessageBuilder::build()
2017-04-12 17:46:44 +02:00
{
auto app = getApp();
// PARSING
this->parseUsername();
2018-08-02 14:23:27 +02:00
if (this->userName == this->channel->getName()) {
this->senderIsBroadcaster = true;
}
2018-08-07 07:55:31 +02:00
this->message().flags.has(MessageFlag::Collapsed);
2018-01-23 23:28:06 +01:00
// PARSING
this->parseMessageID();
2018-01-23 23:28:06 +01:00
this->parseRoomID();
2018-01-23 23:28:06 +01:00
this->appendChannelName();
2018-01-23 23:28:06 +01:00
// timestamp
bool isPastMsg = this->tags.contains("historical");
2017-12-28 17:56:00 +01:00
if (isPastMsg) {
// This may be architecture dependent(datatype)
qint64 ts = this->tags.value("tmi-sent-ts").toLongLong();
QDateTime dateTime = QDateTime::fromMSecsSinceEpoch(ts);
this->emplace<TimestampElement>(dateTime.time());
} else {
this->emplace<TimestampElement>();
}
2017-04-12 17:46:44 +02:00
bool addModerationElement = true;
if (this->senderIsBroadcaster) {
addModerationElement = false;
} else {
bool hasUserType = this->tags.contains("user-type");
if (hasUserType) {
QString userType = this->tags.value("user-type").toString();
if (userType == "mod") {
if (!args.isStaffOrBroadcaster) {
addModerationElement = false;
}
}
}
}
if (addModerationElement) {
this->emplace<TwitchModerationElement>();
}
2017-04-12 17:46:44 +02:00
2018-01-23 23:28:06 +01:00
this->appendTwitchBadges();
2017-04-12 17:46:44 +02:00
2018-01-23 23:28:06 +01:00
this->appendChatterinoBadges();
2017-04-12 17:46:44 +02:00
this->appendUsername();
2017-04-12 17:46:44 +02:00
// highlights
this->parseHighlights(isPastMsg);
2017-04-12 17:46:44 +02:00
2018-08-02 14:23:27 +02:00
// QString bits;
2017-07-02 18:12:11 +02:00
auto iterator = this->tags.find("bits");
if (iterator != this->tags.end()) {
2018-08-02 14:23:27 +02:00
this->hasBits_ = true;
// bits = iterator.value().toString();
2017-04-12 17:46:44 +02:00
}
// twitch emotes
2018-08-02 14:23:27 +02:00
std::vector<std::pair<int, EmotePtr>> twitchEmotes;
2017-04-12 17:46:44 +02:00
2017-07-02 18:12:11 +02:00
iterator = this->tags.find("emotes");
if (iterator != this->tags.end()) {
QStringList emoteString = iterator.value().toString().split('/');
2017-04-12 17:46:44 +02:00
for (QString emote : emoteString) {
2017-12-17 02:18:13 +01:00
this->appendTwitchEmote(ircMessage, emote, twitchEmotes);
2017-04-12 17:46:44 +02:00
}
2018-08-06 21:17:03 +02:00
std::sort(
twitchEmotes.begin(), twitchEmotes.end(),
[](const auto &a, const auto &b) { return a.first < b.first; });
2017-04-12 17:46:44 +02:00
}
// words
2018-07-06 19:23:47 +02:00
QStringList splits = this->originalMessage_.split(' ');
2017-04-12 17:46:44 +02:00
2018-08-02 14:23:27 +02:00
this->addWords(splits, twitchEmotes);
2017-04-12 17:46:44 +02:00
2018-08-07 01:35:24 +02:00
this->message().searchText = this->userName + ": " + this->originalMessage_;
2018-08-02 14:23:27 +02:00
2018-08-07 01:35:24 +02:00
return this->release();
2018-08-02 14:23:27 +02:00
}
2017-08-12 12:07:53 +02:00
2018-08-06 21:17:03 +02:00
void TwitchMessageBuilder::addWords(
const QStringList &words,
const std::vector<std::pair<int, EmotePtr>> &twitchEmotes)
2018-08-02 14:23:27 +02:00
{
auto i = int();
auto currentTwitchEmote = twitchEmotes.begin();
for (const auto &word : words) {
// check if it's a twitch emote twitch emote
2018-08-06 21:17:03 +02:00
if (currentTwitchEmote != twitchEmotes.end() &&
currentTwitchEmote->first == i) {
auto emoteImage = currentTwitchEmote->second;
2018-08-06 21:17:03 +02:00
this->emplace<EmoteElement>(emoteImage,
2018-08-07 07:55:31 +02:00
MessageElementFlag::TwitchEmote);
2017-04-12 17:46:44 +02:00
2018-08-02 14:23:27 +02:00
i += word.length() + 1;
currentTwitchEmote++;
2017-04-12 17:46:44 +02:00
continue;
}
// split words
2018-08-02 14:23:27 +02:00
for (auto &variant : getApp()->emotes->emojis.parse(word)) {
2018-08-14 17:45:17 +02:00
boost::apply_visitor([&](auto &&arg) { this->addTextOrEmoji(arg); },
2018-08-06 21:17:03 +02:00
variant);
2017-04-12 17:46:44 +02:00
}
2018-08-02 14:23:27 +02:00
for (int j = 0; j < word.size(); j++) {
2017-08-17 22:46:16 +02:00
i++;
2017-04-12 17:46:44 +02:00
2018-08-02 14:23:27 +02:00
if (word.at(j).isHighSurrogate()) {
2017-08-17 22:46:16 +02:00
j++;
}
}
2017-04-12 17:46:44 +02:00
2017-08-17 22:46:16 +02:00
i++;
2017-04-12 17:46:44 +02:00
}
2018-08-02 14:23:27 +02:00
}
2017-04-12 17:46:44 +02:00
2018-08-02 14:23:27 +02:00
void TwitchMessageBuilder::addTextOrEmoji(EmotePtr emote)
{
2018-08-07 07:55:31 +02:00
this->emplace<EmoteElement>(emote, MessageElementFlag::EmojiAll);
2018-08-02 14:23:27 +02:00
}
2017-04-12 17:46:44 +02:00
2018-08-02 14:23:27 +02:00
void TwitchMessageBuilder::addTextOrEmoji(const QString &string_)
{
auto string = QString(string_);
2018-07-04 12:22:01 +02:00
2018-08-02 14:23:27 +02:00
if (this->hasBits_ && this->tryParseCheermote(string)) {
// This string was parsed as a cheermote
return;
}
2017-08-12 12:09:26 +02:00
2018-08-02 14:23:27 +02:00
// TODO: Implement ignored emotes
// Format of ignored emotes:
// Emote name: "forsenPuke" - if string in ignoredEmotes
// Will match emote regardless of source (i.e. bttv, ffz)
// Emote source + name: "bttv:nyanPls"
if (this->tryAppendEmote({string})) {
// Successfully appended an emote
return;
}
2017-08-05 18:44:14 +02:00
2018-08-02 14:23:27 +02:00
// Actually just text
auto linkString = this->matchLink(string);
auto link = Link();
2018-08-06 21:17:03 +02:00
auto textColor = this->action_ ? MessageColor(this->usernameColor_)
: MessageColor(MessageColor::Text);
2018-08-02 14:23:27 +02:00
2018-08-06 18:41:30 +02:00
if (linkString.isEmpty()) {
if (string.startsWith('@')) {
2018-08-07 07:55:31 +02:00
this->emplace<TextElement>(string, MessageElementFlag::BoldUsername,
2018-08-06 21:17:03 +02:00
textColor, FontStyle::ChatMediumBold);
2018-08-07 07:55:31 +02:00
this->emplace<TextElement>(
string, MessageElementFlag::NonBoldUsername, textColor);
2018-08-06 18:41:30 +02:00
} else {
2018-08-07 07:55:31 +02:00
this->emplace<TextElement>(string, MessageElementFlag::Text,
textColor);
2017-04-12 17:46:44 +02:00
}
2018-08-06 18:41:30 +02:00
} else {
static QRegularExpression domainRegex(
R"(^(?:(?:ftp|http)s?:\/\/)?([^\/:]+)(?:\/.*)?$)",
QRegularExpression::CaseInsensitiveOption);
QString lowercaseLinkString;
auto match = domainRegex.match(string);
if (match.isValid()) {
lowercaseLinkString = string.mid(0, match.capturedStart(1)) +
2018-08-06 21:17:03 +02:00
match.captured(1).toLower() +
string.mid(match.capturedEnd(1));
2018-08-06 18:41:30 +02:00
} else {
lowercaseLinkString = string;
2017-08-17 22:46:16 +02:00
}
2018-08-02 14:23:27 +02:00
link = Link(Link::Url, linkString);
2017-08-17 22:46:16 +02:00
2018-08-02 14:23:27 +02:00
textColor = MessageColor(MessageColor::Link);
2018-08-06 21:17:03 +02:00
this->emplace<TextElement>(lowercaseLinkString,
2018-08-07 07:55:31 +02:00
MessageElementFlag::LowercaseLink, textColor)
2018-08-06 21:17:03 +02:00
->setLink(link);
2018-08-07 07:55:31 +02:00
this->emplace<TextElement>(string, MessageElementFlag::OriginalLink,
textColor)
2018-08-02 14:23:27 +02:00
->setLink(link);
2017-04-12 17:46:44 +02:00
}
2018-08-06 18:41:30 +02:00
// if (!linkString.isEmpty()) {
// if (getSettings()->lowercaseLink) {
2018-08-06 18:41:30 +02:00
// QRegularExpression httpRegex("\\bhttps?://",
2018-08-06 21:17:03 +02:00
// QRegularExpression::CaseInsensitiveOption); QRegularExpression
// ftpRegex("\\bftps?://",
2018-08-06 18:41:30 +02:00
// QRegularExpression::CaseInsensitiveOption); QRegularExpression
// getDomain("\\/\\/([^\\/]*)"); QString tempString = string;
// if (!string.contains(httpRegex)) {
// if (!string.contains(ftpRegex)) {
// tempString.insert(0, "http://");
// }
// }
// QString domain = getDomain.match(tempString).captured(1);
// string.replace(domain, domain.toLower());
// }
// link = Link(Link::Url, linkString);
// textColor = MessageColor(MessageColor::Link);
//}
// if (string.startsWith('@')) {
2018-08-07 07:55:31 +02:00
// this->emplace<TextElement>(string, MessageElementFlag::BoldUsername,
2018-08-06 21:17:03 +02:00
// textColor,
2018-08-06 18:41:30 +02:00
// FontStyle::ChatMediumBold) //
// ->setLink(link);
2018-08-07 07:55:31 +02:00
// this->emplace<TextElement>(string,
// MessageElementFlag::NonBoldUsername,
2018-08-06 18:41:30 +02:00
// textColor) //
// ->setLink(link);
//} else {
2018-08-07 07:55:31 +02:00
// this->emplace<TextElement>(string, MessageElementFlag::Text,
// textColor) //
2018-08-06 18:41:30 +02:00
// ->setLink(link);
//}
2017-07-02 18:12:11 +02:00
}
void TwitchMessageBuilder::parseMessageID()
{
auto iterator = this->tags.find("id");
if (iterator != this->tags.end()) {
this->messageID = iterator.value().toString();
}
2017-04-12 17:46:44 +02:00
}
2017-07-02 18:12:11 +02:00
void TwitchMessageBuilder::parseRoomID()
{
2017-12-31 00:50:07 +01:00
if (this->twitchChannel == nullptr) {
return;
}
2017-07-02 18:12:11 +02:00
auto iterator = this->tags.find("room-id");
2017-07-02 18:12:11 +02:00
if (iterator != std::end(this->tags)) {
2018-07-06 19:23:47 +02:00
this->roomID_ = iterator.value().toString();
2017-07-02 18:12:11 +02:00
2018-08-11 17:15:17 +02:00
if (this->twitchChannel->roomId().isEmpty()) {
2018-07-15 20:28:54 +02:00
this->twitchChannel->setRoomId(this->roomID_);
2017-07-02 18:12:11 +02:00
}
}
}
2018-01-23 23:28:06 +01:00
void TwitchMessageBuilder::appendChannelName()
2017-07-02 18:12:11 +02:00
{
2018-08-02 14:23:27 +02:00
QString channelName("#" + this->channel->getName());
Link link(Link::Url, this->channel->getName() + "\n" + this->messageID);
2018-08-07 07:55:31 +02:00
this->emplace<TextElement>(channelName, MessageElementFlag::ChannelName,
2018-08-06 21:17:03 +02:00
MessageColor::System) //
->setLink(link);
2017-07-02 18:12:11 +02:00
}
void TwitchMessageBuilder::parseUsername()
{
auto iterator = this->tags.find("color");
if (iterator != this->tags.end()) {
2018-07-06 19:23:47 +02:00
this->usernameColor_ = QColor(iterator.value().toString());
2017-07-02 18:12:11 +02:00
}
// username
this->userName = this->ircMessage->nick();
2017-07-02 18:12:11 +02:00
if (this->userName.isEmpty() || this->args.trimSubscriberUsername) {
2017-07-02 18:12:11 +02:00
this->userName = this->tags.value(QLatin1String("login")).toString();
}
2017-12-17 17:48:46 +01:00
2018-06-04 12:23:23 +02:00
// display name
// auto displayNameVariant = this->tags.value("display-name");
// if (displayNameVariant.isValid()) {
2018-08-06 21:17:03 +02:00
// this->userName = displayNameVariant.toString() + " (" +
// this->userName + ")";
2018-06-04 12:23:23 +02:00
// }
2018-08-07 01:35:24 +02:00
this->message().loginName = this->userName;
}
2017-07-02 18:12:11 +02:00
void TwitchMessageBuilder::appendUsername()
{
auto app = getApp();
QString username = this->userName;
2018-08-07 01:35:24 +02:00
this->message().loginName = username;
QString localizedName;
2017-07-02 18:12:11 +02:00
auto iterator = this->tags.find("display-name");
if (iterator != this->tags.end()) {
2018-08-06 21:17:03 +02:00
QString displayName =
parseTagString(iterator.value().toString()).trimmed();
2018-08-06 21:17:03 +02:00
if (QString::compare(displayName, this->userName,
Qt::CaseInsensitive) == 0) {
username = displayName;
2018-08-07 01:35:24 +02:00
this->message().displayName = displayName;
} else {
localizedName = displayName;
2018-08-07 01:35:24 +02:00
this->message().displayName = username;
this->message().localizedName = displayName;
}
2017-07-02 18:12:11 +02:00
}
bool hasLocalizedName = !localizedName.isEmpty();
// The full string that will be rendered in the chat widget
QString usernameText;
pajlada::Settings::Setting<int> usernameDisplayMode(
2018-08-06 21:17:03 +02:00
"/appearance/messages/usernameDisplayMode",
UsernameDisplayMode::UsernameAndLocalizedName);
switch (usernameDisplayMode.getValue()) {
case UsernameDisplayMode::Username: {
usernameText = username;
} break;
case UsernameDisplayMode::LocalizedName: {
if (hasLocalizedName) {
usernameText = localizedName;
} else {
usernameText = username;
}
} break;
default:
case UsernameDisplayMode::UsernameAndLocalizedName: {
if (hasLocalizedName) {
usernameText = username + "(" + localizedName + ")";
} else {
usernameText = username;
}
} break;
}
2017-07-02 18:12:11 +02:00
if (this->args.isSentWhisper) {
// TODO(pajlada): Re-implement
2018-08-06 21:17:03 +02:00
// userDisplayString +=
// IrcManager::getInstance().getUser().getUserName();
} else if (this->args.isReceivedWhisper) {
// Sender username
2018-08-07 07:55:31 +02:00
this->emplace<TextElement>(usernameText, MessageElementFlag::Text,
2018-08-06 21:17:03 +02:00
this->usernameColor_,
2018-05-23 04:22:17 +02:00
FontStyle::ChatMediumBold)
->setLink({Link::UserInfo, this->userName});
2018-05-26 20:26:25 +02:00
auto currentUser = app->accounts->twitch.getCurrent();
// Separator
2018-08-07 07:55:31 +02:00
this->emplace<TextElement>("->", MessageElementFlag::Text,
2018-08-06 21:17:03 +02:00
app->themes->messages.textColors.system,
FontStyle::ChatMedium);
QColor selfColor = currentUser->color();
if (!selfColor.isValid()) {
selfColor = app->themes->messages.textColors.system;
}
2017-07-02 18:12:11 +02:00
// Your own username
2018-08-06 21:17:03 +02:00
this->emplace<TextElement>(currentUser->getUserName() + ":",
2018-08-07 07:55:31 +02:00
MessageElementFlag::Text, selfColor,
2018-08-06 21:17:03 +02:00
FontStyle::ChatMediumBold);
} else {
2018-07-06 19:23:47 +02:00
if (!this->action_) {
usernameText += ":";
}
2017-07-02 18:12:11 +02:00
2018-08-07 07:55:31 +02:00
this->emplace<TextElement>(usernameText, MessageElementFlag::Text,
2018-08-06 21:17:03 +02:00
this->usernameColor_,
2018-05-23 04:22:17 +02:00
FontStyle::ChatMediumBold)
->setLink({Link::UserInfo, this->userName});
2017-07-02 18:12:11 +02:00
}
}
void TwitchMessageBuilder::parseHighlights(bool isPastMsg)
{
static auto player = new QMediaPlayer;
static QUrl currentPlayerUrl;
auto app = getApp();
2018-05-26 20:26:25 +02:00
auto currentUser = app->accounts->twitch.getCurrent();
QString currentUsername = currentUser->getUserName();
if (this->ircMessage->nick() == currentUsername) {
currentUser->setColor(this->usernameColor_);
// Do nothing. Highlights cannot be triggered by yourself
return;
}
// update the media player url if necessary
QUrl highlightSoundUrl;
if (getSettings()->customHighlightSound) {
2018-08-06 21:17:03 +02:00
highlightSoundUrl =
QUrl::fromLocalFile(getSettings()->pathHighlightSound.getValue());
} else {
highlightSoundUrl = QUrl("qrc:/sounds/ping2.wav");
}
if (currentPlayerUrl != highlightSoundUrl) {
player->setMedia(highlightSoundUrl);
currentPlayerUrl = highlightSoundUrl;
}
// TODO: This vector should only be rebuilt upon highlights being changed
// fourtf: should be implemented in the HighlightsController
2018-08-06 21:17:03 +02:00
std::vector<HighlightPhrase> activeHighlights =
app->highlights->phrases.getVector();
std::vector<HighlightPhrase> userHighlights =
app->highlights->highlightedUsers.getVector();
if (getSettings()->enableHighlightsSelf && currentUsername.size() > 0) {
2018-08-06 21:17:03 +02:00
HighlightPhrase selfHighlight(
currentUsername, getSettings()->enableHighlightTaskbar,
getSettings()->enableHighlightSound, false);
activeHighlights.emplace_back(std::move(selfHighlight));
}
bool doHighlight = false;
bool playSound = false;
bool doAlert = false;
bool hasFocus = (QApplication::focusWidget() != nullptr);
if (!app->highlights->blacklistContains(this->ircMessage->nick())) {
2018-06-26 17:06:17 +02:00
for (const HighlightPhrase &highlight : activeHighlights) {
2018-07-06 19:23:47 +02:00
if (highlight.isMatch(this->originalMessage_)) {
2018-08-11 14:20:53 +02:00
log("Highlight because {} matches {}", this->originalMessage_,
2018-06-26 17:06:17 +02:00
highlight.getPattern());
doHighlight = true;
2018-05-06 12:52:47 +02:00
if (highlight.getAlert()) {
doAlert = true;
}
2018-05-06 12:52:47 +02:00
if (highlight.getSound()) {
playSound = true;
}
if (playSound && doAlert) {
2018-08-06 21:17:03 +02:00
// Break if no further action can be taken from other
// highlights This might change if highlights can have
// custom colors/sounds/actions
break;
}
}
}
for (const HighlightPhrase &userHighlight : userHighlights) {
if (userHighlight.isMatch(this->ircMessage->nick())) {
2018-08-11 14:20:53 +02:00
log("Highlight because user {} sent a message",
2018-08-06 21:17:03 +02:00
this->ircMessage->nick());
doHighlight = true;
if (userHighlight.getAlert()) {
doAlert = true;
}
if (userHighlight.getSound()) {
playSound = true;
}
if (playSound && doAlert) {
2018-08-06 21:17:03 +02:00
// Break if no further action can be taken from other
// usernames Mostly used for regex stuff
break;
}
}
}
2018-08-07 07:55:31 +02:00
this->message().flags.set(MessageFlag::Highlighted, doHighlight);
if (!isPastMsg) {
2018-08-06 21:17:03 +02:00
if (playSound &&
(!hasFocus || getSettings()->highlightAlwaysPlaySound)) {
player->play();
}
if (doAlert) {
2018-08-06 21:17:03 +02:00
QApplication::alert(getApp()->windows->getMainWindow().window(),
2500);
}
}
}
}
2018-08-06 21:17:03 +02:00
void TwitchMessageBuilder::appendTwitchEmote(
const Communi::IrcMessage *ircMessage, const QString &emote,
std::vector<std::pair<int, EmotePtr>> &vec)
{
auto app = getApp();
if (!emote.contains(':')) {
return;
}
2018-08-02 14:23:27 +02:00
auto parameters = emote.split(':');
if (parameters.length() < 2) {
return;
}
2018-08-02 14:23:27 +02:00
auto id = EmoteId{parameters.at(0)};
2018-08-02 14:23:27 +02:00
auto occurences = parameters.at(1).split(',');
for (QString occurence : occurences) {
2018-08-02 14:23:27 +02:00
auto coords = occurence.split('-');
if (coords.length() < 2) {
return;
}
2018-08-02 14:23:27 +02:00
auto start = coords.at(0).toInt();
auto end = coords.at(1).toInt();
2018-08-06 21:17:03 +02:00
if (start >= end || start < 0 ||
end > this->originalMessage_.length()) {
return;
}
2018-08-06 21:17:03 +02:00
auto name =
EmoteName{this->originalMessage_.mid(start, end - start + 1)};
2018-08-06 21:17:03 +02:00
vec.push_back(std::make_pair(
start, app->emotes->twitch.getOrCreateEmote(id, name)));
}
}
2018-08-02 14:23:27 +02:00
Outcome TwitchMessageBuilder::tryAppendEmote(const EmoteName &name)
2017-07-23 09:53:50 +02:00
{
2018-08-07 07:55:31 +02:00
auto flags = MessageElementFlags();
2018-08-02 14:23:27 +02:00
auto emote = boost::optional<EmotePtr>{};
if ((emote = this->twitchChannel->globalBttv().emote(name))) {
2018-08-07 07:55:31 +02:00
flags = MessageElementFlag::BttvEmote;
} else if ((emote = this->twitchChannel->bttvEmote(name))) {
2018-08-07 07:55:31 +02:00
flags = MessageElementFlag::BttvEmote;
} else if ((emote = this->twitchChannel->globalFfz().emote(name))) {
2018-08-07 07:55:31 +02:00
flags = MessageElementFlag::FfzEmote;
2018-08-13 13:54:39 +02:00
} else if ((emote = this->twitchChannel->ffzEmote(name))) {
2018-08-07 07:55:31 +02:00
flags = MessageElementFlag::FfzEmote;
2017-07-23 09:53:50 +02:00
}
2018-08-02 14:23:27 +02:00
if (emote) {
this->emplace<EmoteElement>(emote.get(), flags);
return Success;
}
return Failure;
2017-07-23 09:53:50 +02:00
}
// fourtf: this is ugly
2018-01-23 23:28:06 +01:00
void TwitchMessageBuilder::appendTwitchBadges()
{
auto app = getApp();
2017-07-02 18:12:11 +02:00
auto iterator = this->tags.find("badges");
2018-08-14 17:45:17 +02:00
if (iterator == this->tags.end()) return;
2017-07-02 18:12:11 +02:00
2018-08-14 17:45:17 +02:00
for (QString badge : iterator.value().toString().split(',')) {
if (badge.startsWith("bits/")) {
2018-08-02 14:23:27 +02:00
QString cheerAmount = badge.mid(5);
QString tooltip = QString("Twitch cheer ") + cheerAmount;
2017-06-15 23:13:01 +02:00
2018-01-12 22:38:00 +01:00
// Try to fetch channel-specific bit badge
2017-06-16 08:03:13 +02:00
try {
2018-08-02 14:23:27 +02:00
if (twitchChannel)
2018-08-13 13:54:39 +02:00
if (const auto &badge = this->twitchChannel->twitchBadge(
2018-08-06 21:17:03 +02:00
"bits", cheerAmount)) {
2018-08-07 07:55:31 +02:00
this->emplace<EmoteElement>(
badge.get(), MessageElementFlag::BadgeVanity)
2018-08-02 14:23:27 +02:00
->setTooltip(tooltip);
continue;
}
2018-01-12 22:38:00 +01:00
} catch (const std::out_of_range &) {
// Channel does not contain a special bit badge for this version
}
2017-06-16 08:03:13 +02:00
2018-01-12 22:38:00 +01:00
// Use default bit badge
2018-08-14 17:45:17 +02:00
if (auto badge = this->twitchChannel->globalTwitchBadges().badge(
"bits", cheerAmount)) {
this->emplace<EmoteElement>(badge.get(),
MessageElementFlag::BadgeVanity)
->setTooltip(tooltip);
}
} else if (badge == "staff/1") {
2018-08-06 21:17:03 +02:00
this->emplace<ImageElement>(
2018-08-10 18:56:17 +02:00
Image::fromPixmap(app->resources->twitch.staff),
2018-08-07 07:55:31 +02:00
MessageElementFlag::BadgeGlobalAuthority)
->setTooltip("Twitch Staff");
} else if (badge == "admin/1") {
2018-08-02 14:23:27 +02:00
this->emplace<ImageElement>(
2018-08-10 18:56:17 +02:00
Image::fromPixmap(app->resources->twitch.admin),
2018-08-07 07:55:31 +02:00
MessageElementFlag::BadgeGlobalAuthority)
2018-08-06 21:17:03 +02:00
->setTooltip("Twitch Admin");
} else if (badge == "global_mod/1") {
2018-08-07 07:55:31 +02:00
this->emplace<ImageElement>(
2018-08-10 18:56:17 +02:00
Image::fromPixmap(app->resources->twitch.globalmod),
2018-08-07 07:55:31 +02:00
MessageElementFlag::BadgeGlobalAuthority)
->setTooltip("Twitch Global Moderator");
} else if (badge == "moderator/1") {
// TODO: Implement custom FFZ moderator badge
2018-08-07 07:55:31 +02:00
this->emplace<ImageElement>(
2018-08-10 18:56:17 +02:00
Image::fromPixmap(app->resources->twitch.moderator),
2018-08-07 07:55:31 +02:00
MessageElementFlag::BadgeChannelAuthority)
->setTooltip("Twitch Channel Moderator");
} else if (badge == "turbo/1") {
2018-08-06 21:17:03 +02:00
this->emplace<ImageElement>(
2018-08-10 18:56:17 +02:00
Image::fromPixmap(app->resources->twitch.turbo),
2018-08-07 07:55:31 +02:00
MessageElementFlag::BadgeGlobalAuthority)
->setTooltip("Twitch Turbo Subscriber");
} else if (badge == "broadcaster/1") {
2018-08-02 14:23:27 +02:00
this->emplace<ImageElement>(
2018-08-10 18:56:17 +02:00
Image::fromPixmap(app->resources->twitch.broadcaster),
2018-08-07 07:55:31 +02:00
MessageElementFlag::BadgeChannelAuthority)
->setTooltip("Twitch Broadcaster");
} else if (badge == "premium/1") {
2018-08-06 21:17:03 +02:00
this->emplace<ImageElement>(
2018-08-10 18:56:17 +02:00
Image::fromPixmap(app->resources->twitch.prime),
2018-08-07 07:55:31 +02:00
MessageElementFlag::BadgeVanity)
->setTooltip("Twitch Prime Subscriber");
} else if (badge.startsWith("partner/")) {
int index = badge.midRef(8).toInt();
switch (index) {
case 1: {
2018-08-02 14:23:27 +02:00
this->emplace<ImageElement>(
2018-08-10 18:56:17 +02:00
Image::fromPixmap(app->resources->twitch.verified,
0.25),
2018-08-07 07:55:31 +02:00
MessageElementFlag::BadgeVanity)
->setTooltip("Twitch Verified");
} break;
default: {
2018-08-06 21:17:03 +02:00
printf("[TwitchMessageBuilder] Unhandled partner badge "
"index: %d\n",
index);
} break;
}
} else if (badge.startsWith("subscriber/")) {
2018-08-14 17:45:17 +02:00
if (auto badgeEmote = this->twitchChannel->twitchBadge(
"subscriber", badge.mid(11))) {
this->emplace<EmoteElement>(
badgeEmote.get(), MessageElementFlag::BadgeSubscription)
->setTooltip((*badgeEmote)->tooltip.string);
continue;
}
// use default subscriber badge if custom one not found
this->emplace<ImageElement>(
Image::fromPixmap(app->resources->twitch.subscriber, 0.25),
MessageElementFlag::BadgeSubscription)
->setTooltip("Twitch Subscriber");
} else {
2018-08-14 17:45:17 +02:00
auto splits = badge.split('/');
if (splits.size() != 2) continue;
if (auto badgeEmote =
this->twitchChannel->twitchBadge(splits[0], splits[1])) {
this->emplace<EmoteElement>(badgeEmote.get(),
MessageElementFlag::BadgeVanity)
->setTooltip((*badgeEmote)->tooltip.string);
continue;
}
}
}
}
2018-01-23 23:28:06 +01:00
void TwitchMessageBuilder::appendChatterinoBadges()
2017-08-12 13:20:52 +02:00
{
2018-08-02 14:23:27 +02:00
// auto app = getApp();
2018-08-02 14:23:27 +02:00
// auto &badges = app->resources->chatterinoBadges;
// auto it = badges.find(this->userName.toStdString());
2017-08-12 13:20:52 +02:00
2018-08-02 14:23:27 +02:00
// if (it == badges.end()) {
// return;
// }
2017-08-12 13:20:52 +02:00
2018-08-02 14:23:27 +02:00
// const auto badge = it->second;
2017-08-12 13:20:52 +02:00
2018-08-06 21:17:03 +02:00
// this->emplace<ImageElement>(badge->image,
2018-08-07 07:55:31 +02:00
// MessageElementFlag::BadgeChatterino)
2018-08-02 14:23:27 +02:00
// ->setTooltip(QString::fromStdString(badge->tooltip));
2017-08-12 13:20:52 +02:00
}
2018-08-02 14:23:27 +02:00
Outcome TwitchMessageBuilder::tryParseCheermote(const QString &string)
{
2018-08-02 14:23:27 +02:00
// auto app = getApp();
//// Try to parse custom cheermotes
// const auto &channelResources = app->resources->channels[this->roomID_];
// if (channelResources.loaded) {
// for (const auto &cheermoteSet : channelResources.cheermoteSets) {
// auto match = cheermoteSet.regex.match(string);
// if (!match.hasMatch()) {
// continue;
// }
// QString amount = match.captured(1);
// bool ok = false;
// int numBits = amount.toInt(&ok);
// if (!ok) {
// Log("Error parsing bit amount in tryParseCheermote");
// return Failure;
// }
// auto savedIt = cheermoteSet.cheermotes.end();
// // Fetch cheermote that matches our numBits
2018-08-06 21:17:03 +02:00
// for (auto it = cheermoteSet.cheermotes.begin(); it !=
// cheermoteSet.cheermotes.end();
2018-08-02 14:23:27 +02:00
// ++it) {
// if (numBits >= it->minBits) {
// savedIt = it;
// } else {
// break;
// }
// }
// if (savedIt == cheermoteSet.cheermotes.end()) {
2018-08-06 21:17:03 +02:00
// Log("Error getting a cheermote from a cheermote set for the
// bit amount {}",
2018-08-02 14:23:27 +02:00
// numBits);
// return Failure;
// }
// const auto &cheermote = *savedIt;
2018-08-06 21:17:03 +02:00
// this->emplace<EmoteElement>(cheermote.animatedEmote,
2018-08-07 07:55:31 +02:00
// MessageElementFlag::BitsAnimated);
// this->emplace<TextElement>(amount, MessageElementFlag::Text,
// cheermote.color);
2018-08-02 14:23:27 +02:00
// return Success;
// }
//}
2018-08-02 14:23:27 +02:00
return Failure;
}
} // namespace chatterino