2019-09-08 22:27:57 +02:00
|
|
|
#include "SearchPopup.hpp"
|
|
|
|
|
|
|
|
#include <QHBoxLayout>
|
|
|
|
#include <QLineEdit>
|
|
|
|
#include <QPushButton>
|
|
|
|
|
|
|
|
#include "common/Channel.hpp"
|
2021-11-21 18:46:21 +01:00
|
|
|
#include "controllers/hotkeys/HotkeyController.hpp"
|
2019-09-09 15:21:49 +02:00
|
|
|
#include "messages/search/AuthorPredicate.hpp"
|
2022-10-01 14:30:29 +02:00
|
|
|
#include "messages/search/BadgePredicate.hpp"
|
2021-04-17 13:18:22 +02:00
|
|
|
#include "messages/search/ChannelPredicate.hpp"
|
2019-09-09 15:21:49 +02:00
|
|
|
#include "messages/search/LinkPredicate.hpp"
|
2021-05-01 14:21:45 +02:00
|
|
|
#include "messages/search/MessageFlagsPredicate.hpp"
|
2021-10-17 14:36:44 +02:00
|
|
|
#include "messages/search/RegexPredicate.hpp"
|
2019-09-09 15:21:49 +02:00
|
|
|
#include "messages/search/SubstringPredicate.hpp"
|
2022-10-01 14:30:29 +02:00
|
|
|
#include "messages/search/SubtierPredicate.hpp"
|
2022-09-11 16:37:13 +02:00
|
|
|
#include "singletons/WindowManager.hpp"
|
2019-09-08 22:27:57 +02:00
|
|
|
#include "widgets/helper/ChannelView.hpp"
|
|
|
|
|
|
|
|
namespace chatterino {
|
2019-09-09 15:21:49 +02:00
|
|
|
|
|
|
|
ChannelPtr SearchPopup::filter(const QString &text, const QString &channelName,
|
2022-05-23 02:47:16 +02:00
|
|
|
const LimitedQueueSnapshot<MessagePtr> &snapshot)
|
2019-09-09 15:21:49 +02:00
|
|
|
{
|
|
|
|
ChannelPtr channel(new Channel(channelName, Channel::Type::None));
|
|
|
|
|
|
|
|
// Parse predicates from tags in "text"
|
|
|
|
auto predicates = parsePredicates(text);
|
|
|
|
|
|
|
|
// Check for every message whether it fulfills all predicates that have
|
|
|
|
// been registered
|
|
|
|
for (size_t i = 0; i < snapshot.size(); ++i)
|
2019-09-08 22:27:57 +02:00
|
|
|
{
|
2019-09-09 15:21:49 +02:00
|
|
|
MessagePtr message = snapshot[i];
|
2019-09-08 22:27:57 +02:00
|
|
|
|
2019-09-09 15:21:49 +02:00
|
|
|
bool accept = true;
|
|
|
|
for (const auto &pred : predicates)
|
2019-09-08 22:27:57 +02:00
|
|
|
{
|
2019-09-09 15:21:49 +02:00
|
|
|
// Discard the message as soon as one predicate fails
|
|
|
|
if (!pred->appliesTo(*message))
|
2019-09-08 22:27:57 +02:00
|
|
|
{
|
2019-09-09 15:21:49 +02:00
|
|
|
accept = false;
|
|
|
|
break;
|
2019-09-08 22:27:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-09 15:21:49 +02:00
|
|
|
// If all predicates match, add the message to the channel
|
|
|
|
if (accept)
|
|
|
|
channel->addMessage(message);
|
2019-09-08 22:27:57 +02:00
|
|
|
}
|
2019-09-09 15:21:49 +02:00
|
|
|
|
|
|
|
return channel;
|
|
|
|
}
|
2019-09-08 22:27:57 +02:00
|
|
|
|
2022-07-31 12:45:25 +02:00
|
|
|
SearchPopup::SearchPopup(QWidget *parent, Split *split)
|
2020-11-15 11:07:20 +01:00
|
|
|
: BasePopup({}, parent)
|
2022-07-31 12:45:25 +02:00
|
|
|
, split_(split)
|
2019-09-08 22:27:57 +02:00
|
|
|
{
|
|
|
|
this->initLayout();
|
|
|
|
this->resize(400, 600);
|
2021-11-21 18:46:21 +01:00
|
|
|
this->addShortcuts();
|
|
|
|
}
|
2020-07-18 17:52:12 +02:00
|
|
|
|
2021-11-21 18:46:21 +01:00
|
|
|
void SearchPopup::addShortcuts()
|
|
|
|
{
|
|
|
|
HotkeyController::HotkeyMap actions{
|
|
|
|
{"search",
|
2022-05-23 02:47:16 +02:00
|
|
|
[this](const std::vector<QString> &) -> QString {
|
2021-11-21 18:46:21 +01:00
|
|
|
this->searchInput_->setFocus();
|
|
|
|
this->searchInput_->selectAll();
|
|
|
|
return "";
|
|
|
|
}},
|
|
|
|
{"delete",
|
2022-05-23 02:47:16 +02:00
|
|
|
[this](const std::vector<QString> &) -> QString {
|
2021-11-21 18:46:21 +01:00
|
|
|
this->close();
|
|
|
|
return "";
|
|
|
|
}},
|
|
|
|
|
|
|
|
{"reject", nullptr},
|
|
|
|
{"accept", nullptr},
|
|
|
|
{"openTab", nullptr},
|
|
|
|
{"scrollPage", nullptr},
|
|
|
|
};
|
|
|
|
|
|
|
|
this->shortcuts_ = getApp()->hotkeys->shortcutsForCategory(
|
|
|
|
HotkeyCategory::PopupWindow, actions, this);
|
2019-09-08 22:27:57 +02:00
|
|
|
}
|
|
|
|
|
2022-05-23 02:47:16 +02:00
|
|
|
void SearchPopup::addChannel(ChannelView &channel)
|
2020-10-18 15:16:56 +02:00
|
|
|
{
|
2022-05-23 02:47:16 +02:00
|
|
|
if (this->searchChannels_.empty())
|
|
|
|
{
|
|
|
|
this->channelView_->setSourceChannel(channel.channel());
|
|
|
|
this->channelName_ = channel.channel()->getName();
|
|
|
|
}
|
|
|
|
else if (this->searchChannels_.size() == 1)
|
|
|
|
{
|
|
|
|
this->channelView_->setSourceChannel(
|
|
|
|
std::make_shared<Channel>("multichannel", Channel::Type::None));
|
2020-10-18 15:16:56 +02:00
|
|
|
|
2022-05-23 02:47:16 +02:00
|
|
|
auto flags = this->channelView_->getFlags();
|
|
|
|
flags.set(MessageElementFlag::ChannelName);
|
|
|
|
flags.unset(MessageElementFlag::ModeratorTools);
|
|
|
|
this->channelView_->setOverrideFlags(flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
this->searchChannels_.append(std::ref(channel));
|
2019-09-08 22:27:57 +02:00
|
|
|
|
|
|
|
this->updateWindowTitle();
|
|
|
|
}
|
|
|
|
|
2022-09-11 16:37:13 +02:00
|
|
|
void SearchPopup::goToMessage(const MessagePtr &message)
|
|
|
|
{
|
|
|
|
for (const auto &view : this->searchChannels_)
|
|
|
|
{
|
|
|
|
if (view.get().channel()->getType() == Channel::Type::TwitchMentions)
|
|
|
|
{
|
|
|
|
getApp()->windows->scrollToMessage(message);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (view.get().scrollToMessage(message))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SearchPopup::goToMessageId(const QString &messageId)
|
|
|
|
{
|
|
|
|
for (const auto &view : this->searchChannels_)
|
|
|
|
{
|
|
|
|
if (view.get().scrollToMessageId(messageId))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-08 22:27:57 +02:00
|
|
|
void SearchPopup::updateWindowTitle()
|
|
|
|
{
|
2020-12-12 13:06:40 +01:00
|
|
|
QString historyName;
|
|
|
|
|
2022-06-01 14:41:16 +02:00
|
|
|
if (this->searchChannels_.size() > 1)
|
2020-12-12 13:06:40 +01:00
|
|
|
{
|
2022-06-01 14:41:16 +02:00
|
|
|
historyName = "multiple channels'";
|
2020-12-12 13:06:40 +01:00
|
|
|
}
|
|
|
|
else if (this->channelName_ == "/mentions")
|
|
|
|
{
|
|
|
|
historyName = "mentions";
|
|
|
|
}
|
2022-06-01 14:41:16 +02:00
|
|
|
else if (this->channelName_ == "/whispers")
|
2022-05-23 02:47:16 +02:00
|
|
|
{
|
2022-06-01 14:41:16 +02:00
|
|
|
historyName = "whispers";
|
2022-05-23 02:47:16 +02:00
|
|
|
}
|
2020-12-12 13:06:40 +01:00
|
|
|
else if (this->channelName_.isEmpty())
|
|
|
|
{
|
|
|
|
historyName = "<empty>'s";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
historyName = QString("%1's").arg(this->channelName_);
|
|
|
|
}
|
|
|
|
this->setWindowTitle("Searching in " + historyName + " history");
|
2019-09-08 22:27:57 +02:00
|
|
|
}
|
|
|
|
|
2022-05-23 02:47:16 +02:00
|
|
|
void SearchPopup::showEvent(QShowEvent *)
|
|
|
|
{
|
|
|
|
this->search();
|
|
|
|
}
|
|
|
|
|
2019-09-08 22:27:57 +02:00
|
|
|
void SearchPopup::search()
|
|
|
|
{
|
2022-05-23 02:47:16 +02:00
|
|
|
if (this->snapshot_.size() == 0)
|
|
|
|
{
|
|
|
|
this->snapshot_ = this->buildSnapshot();
|
|
|
|
}
|
|
|
|
|
2019-09-08 22:27:57 +02:00
|
|
|
this->channelView_->setChannel(filter(this->searchInput_->text(),
|
2022-05-23 02:47:16 +02:00
|
|
|
this->channelName_, this->snapshot_));
|
|
|
|
}
|
|
|
|
|
|
|
|
LimitedQueueSnapshot<MessagePtr> SearchPopup::buildSnapshot()
|
|
|
|
{
|
|
|
|
// no point in filtering/sorting if it's a single channel search
|
|
|
|
if (this->searchChannels_.length() == 1)
|
|
|
|
{
|
|
|
|
const auto channelPtr = this->searchChannels_.at(0);
|
|
|
|
return channelPtr.get().channel()->getMessageSnapshot();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto combinedSnapshot = std::vector<std::shared_ptr<const Message>>{};
|
|
|
|
for (auto &channel : this->searchChannels_)
|
|
|
|
{
|
|
|
|
ChannelView &sharedView = channel.get();
|
|
|
|
|
|
|
|
const FilterSetPtr filterSet = sharedView.getFilterSet();
|
|
|
|
const LimitedQueueSnapshot<MessagePtr> &snapshot =
|
|
|
|
sharedView.channel()->getMessageSnapshot();
|
|
|
|
|
|
|
|
// TODO: implement iterator on LimitedQueueSnapshot?
|
|
|
|
for (auto i = 0; i < snapshot.size(); ++i)
|
|
|
|
{
|
|
|
|
const MessagePtr &message = snapshot[i];
|
|
|
|
if (filterSet && !filterSet->filter(message, sharedView.channel()))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
combinedSnapshot.push_back(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove any duplicate messages from splits containing the same channel
|
|
|
|
std::sort(combinedSnapshot.begin(), combinedSnapshot.end(),
|
|
|
|
[](MessagePtr &a, MessagePtr &b) {
|
|
|
|
return a->id > b->id;
|
|
|
|
});
|
|
|
|
|
|
|
|
auto uniqueIterator =
|
|
|
|
std::unique(combinedSnapshot.begin(), combinedSnapshot.end(),
|
|
|
|
[](MessagePtr &a, MessagePtr &b) {
|
2022-07-24 12:18:25 +02:00
|
|
|
// nullptr check prevents system messages from being dropped
|
|
|
|
return (a->id != nullptr) && a->id == b->id;
|
2022-05-23 02:47:16 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
combinedSnapshot.erase(uniqueIterator, combinedSnapshot.end());
|
|
|
|
|
|
|
|
// resort by time for presentation
|
|
|
|
std::sort(combinedSnapshot.begin(), combinedSnapshot.end(),
|
|
|
|
[](MessagePtr &a, MessagePtr &b) {
|
|
|
|
return a->serverReceivedTime < b->serverReceivedTime;
|
|
|
|
});
|
|
|
|
|
|
|
|
auto queue = LimitedQueue<MessagePtr>(combinedSnapshot.size());
|
|
|
|
queue.pushFront(combinedSnapshot);
|
|
|
|
|
|
|
|
return queue.getSnapshot();
|
2019-09-08 22:27:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void SearchPopup::initLayout()
|
|
|
|
{
|
|
|
|
// VBOX
|
|
|
|
{
|
2022-05-23 02:47:16 +02:00
|
|
|
auto *layout1 = new QVBoxLayout(this);
|
2019-09-08 22:27:57 +02:00
|
|
|
layout1->setMargin(0);
|
|
|
|
layout1->setSpacing(0);
|
|
|
|
|
|
|
|
// HBOX
|
|
|
|
{
|
2022-05-23 02:47:16 +02:00
|
|
|
auto *layout2 = new QHBoxLayout(this);
|
2019-09-08 22:27:57 +02:00
|
|
|
layout2->setMargin(8);
|
|
|
|
layout2->setSpacing(8);
|
|
|
|
|
|
|
|
// SEARCH INPUT
|
|
|
|
{
|
|
|
|
this->searchInput_ = new QLineEdit(this);
|
|
|
|
layout2->addWidget(this->searchInput_);
|
|
|
|
|
2022-02-13 16:05:38 +01:00
|
|
|
this->searchInput_->setPlaceholderText("Type to search");
|
|
|
|
this->searchInput_->setClearButtonEnabled(true);
|
|
|
|
this->searchInput_->findChild<QAbstractButton *>()->setIcon(
|
|
|
|
QPixmap(":/buttons/clearSearch.png"));
|
|
|
|
QObject::connect(this->searchInput_, &QLineEdit::textChanged,
|
|
|
|
this, &SearchPopup::search);
|
2019-09-08 22:27:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
layout1->addLayout(layout2);
|
|
|
|
}
|
|
|
|
|
|
|
|
// CHANNELVIEW
|
|
|
|
{
|
2022-07-31 12:45:25 +02:00
|
|
|
this->channelView_ = new ChannelView(this, this->split_,
|
|
|
|
ChannelView::Context::Search);
|
2019-09-08 22:27:57 +02:00
|
|
|
|
|
|
|
layout1->addWidget(this->channelView_);
|
|
|
|
}
|
|
|
|
|
|
|
|
this->setLayout(layout1);
|
|
|
|
}
|
2021-04-11 15:37:53 +02:00
|
|
|
|
|
|
|
this->searchInput_->setFocus();
|
2019-09-08 22:27:57 +02:00
|
|
|
}
|
|
|
|
|
2019-09-09 15:21:49 +02:00
|
|
|
std::vector<std::unique_ptr<MessagePredicate>> SearchPopup::parsePredicates(
|
|
|
|
const QString &input)
|
|
|
|
{
|
2021-10-17 14:36:44 +02:00
|
|
|
// This regex captures all name:value predicate pairs into named capturing
|
|
|
|
// groups and matches all other inputs seperated by spaces as normal
|
|
|
|
// strings.
|
|
|
|
// It also ignores whitespaces in values when being surrounded by quotation
|
|
|
|
// marks, to enable inputs like this => regex:"kappa 123"
|
|
|
|
static QRegularExpression predicateRegex(
|
|
|
|
R"lit((?:(?<name>\w+):(?<value>".+?"|[^\s]+))|[^\s]+?(?=$|\s))lit");
|
|
|
|
static QRegularExpression trimQuotationMarksRegex(R"(^"|"$)");
|
|
|
|
|
|
|
|
QRegularExpressionMatchIterator it = predicateRegex.globalMatch(input);
|
2019-09-09 15:21:49 +02:00
|
|
|
|
2021-04-17 13:18:22 +02:00
|
|
|
std::vector<std::unique_ptr<MessagePredicate>> predicates;
|
|
|
|
QStringList authors;
|
|
|
|
QStringList channels;
|
2022-10-01 14:30:29 +02:00
|
|
|
QStringList badges;
|
|
|
|
QStringList subtiers;
|
2019-09-09 15:21:49 +02:00
|
|
|
|
2021-10-17 14:36:44 +02:00
|
|
|
while (it.hasNext())
|
2019-09-09 15:21:49 +02:00
|
|
|
{
|
2021-10-17 14:36:44 +02:00
|
|
|
QRegularExpressionMatch match = it.next();
|
2019-09-09 15:21:49 +02:00
|
|
|
|
2021-10-17 14:36:44 +02:00
|
|
|
QString name = match.captured("name");
|
2019-09-09 15:21:49 +02:00
|
|
|
|
2021-10-17 14:36:44 +02:00
|
|
|
QString value = match.captured("value");
|
|
|
|
value.remove(trimQuotationMarksRegex);
|
2019-09-09 15:21:49 +02:00
|
|
|
|
2021-10-17 14:36:44 +02:00
|
|
|
// match predicates
|
|
|
|
if (name == "from")
|
|
|
|
{
|
|
|
|
authors.append(value);
|
|
|
|
}
|
2022-10-01 14:30:29 +02:00
|
|
|
else if (name == "badge")
|
|
|
|
{
|
|
|
|
badges.append(value);
|
|
|
|
}
|
|
|
|
else if (name == "subtier")
|
|
|
|
{
|
|
|
|
subtiers.append(value);
|
|
|
|
}
|
2021-10-17 14:36:44 +02:00
|
|
|
else if (name == "has" && value == "link")
|
|
|
|
{
|
|
|
|
predicates.push_back(std::make_unique<LinkPredicate>());
|
|
|
|
}
|
|
|
|
else if (name == "in")
|
|
|
|
{
|
|
|
|
channels.append(value);
|
|
|
|
}
|
|
|
|
else if (name == "is")
|
|
|
|
{
|
|
|
|
predicates.push_back(
|
|
|
|
std::make_unique<MessageFlagsPredicate>(value));
|
|
|
|
}
|
|
|
|
else if (name == "regex")
|
|
|
|
{
|
|
|
|
predicates.push_back(std::make_unique<RegexPredicate>(value));
|
2019-09-09 15:21:49 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-10-17 14:36:44 +02:00
|
|
|
predicates.push_back(
|
|
|
|
std::make_unique<SubstringPredicate>(match.captured()));
|
2019-09-09 15:21:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!authors.empty())
|
2022-10-01 14:30:29 +02:00
|
|
|
{
|
2019-09-09 15:21:49 +02:00
|
|
|
predicates.push_back(std::make_unique<AuthorPredicate>(authors));
|
2022-10-01 14:30:29 +02:00
|
|
|
}
|
2019-09-09 15:21:49 +02:00
|
|
|
|
2021-04-17 13:18:22 +02:00
|
|
|
if (!channels.empty())
|
2022-10-01 14:30:29 +02:00
|
|
|
{
|
2021-04-17 13:18:22 +02:00
|
|
|
predicates.push_back(std::make_unique<ChannelPredicate>(channels));
|
2022-10-01 14:30:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!badges.empty())
|
|
|
|
{
|
|
|
|
predicates.push_back(std::make_unique<BadgePredicate>(badges));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!subtiers.empty())
|
|
|
|
{
|
|
|
|
predicates.push_back(std::make_unique<SubtierPredicate>(subtiers));
|
|
|
|
}
|
2021-04-17 13:18:22 +02:00
|
|
|
|
2019-09-09 15:21:49 +02:00
|
|
|
return predicates;
|
|
|
|
}
|
|
|
|
|
2019-09-08 22:27:57 +02:00
|
|
|
} // namespace chatterino
|