2019-09-08 22:27:57 +02:00
|
|
|
#include "SearchPopup.hpp"
|
|
|
|
|
|
|
|
#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"
|
|
|
|
|
Sort and force grouping of includes (#4172)
This change enforces strict include grouping using IncludeCategories
In addition to adding this to the .clang-format file and applying it in the tests/src and src directories, I also did the following small changes:
In ChatterSet.hpp, I changed lrucache to a <>include
In Irc2.hpp, I change common/SignalVector.hpp to a "project-include"
In AttachedWindow.cpp, NativeMessaging.cpp, WindowsHelper.hpp, BaseWindow.cpp, and StreamerMode.cpp, I disabled clang-format for the windows-includes
In WindowDescriptors.hpp, I added the missing vector include. It was previously not needed because the include was handled by another file that was previously included first.
clang-format minimum version has been bumped, so Ubuntu version used in the check-formatting job has been bumped to 22.04 (which is the latest LTS)
2022-11-27 19:32:53 +01:00
|
|
|
#include <QHBoxLayout>
|
|
|
|
#include <QLineEdit>
|
|
|
|
#include <QPushButton>
|
|
|
|
|
2019-09-08 22:27:57 +02:00
|
|
|
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)
|
2022-10-16 12:28:22 +02:00
|
|
|
{
|
|
|
|
auto overrideFlags = boost::optional<MessageFlags>(message->flags);
|
|
|
|
overrideFlags->set(MessageFlag::DoNotLog);
|
|
|
|
|
|
|
|
channel->addMessage(message, overrideFlags);
|
|
|
|
}
|
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)
|
2022-12-03 11:50:22 +01:00
|
|
|
: BasePopup({BaseWindow::DisableLayoutSave}, parent)
|
2022-07-31 12:45:25 +02:00
|
|
|
, split_(split)
|
2019-09-08 22:27:57 +02:00
|
|
|
{
|
|
|
|
this->initLayout();
|
2022-12-03 19:14:15 +01:00
|
|
|
if (this->split_ && this->split_->getChannelView().hasSelection())
|
|
|
|
{
|
|
|
|
this->searchInput_->setText(
|
|
|
|
this->split_->getChannelView().getSelectedText().trimmed());
|
|
|
|
this->searchInput_->selectAll();
|
|
|
|
}
|
2019-09-08 22:27:57 +02:00
|
|
|
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);
|
2022-11-10 20:11:40 +01:00
|
|
|
layout1->setContentsMargins(0, 0, 0, 0);
|
2019-09-08 22:27:57 +02:00
|
|
|
layout1->setSpacing(0);
|
|
|
|
|
|
|
|
// HBOX
|
|
|
|
{
|
2022-05-23 02:47:16 +02:00
|
|
|
auto *layout2 = new QHBoxLayout(this);
|
2022-11-10 20:11:40 +01:00
|
|
|
layout2->setContentsMargins(8, 8, 8, 8);
|
2019-09-08 22:27:57 +02:00
|
|
|
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(
|
2022-12-04 12:34:13 +01:00
|
|
|
R"lit((?<negation>[!\-])?(?:(?<name>\w+):(?<value>".+?"|[^\s]+))|[^\s]+?(?=$|\s))lit");
|
2021-10-17 14:36:44 +02:00
|
|
|
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;
|
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");
|
2022-12-04 12:34:13 +01:00
|
|
|
bool isNegated = !match.captured("negation").isEmpty();
|
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
|
2022-12-04 12:34:13 +01:00
|
|
|
|
2021-10-17 14:36:44 +02:00
|
|
|
if (name == "from")
|
|
|
|
{
|
2022-12-04 12:34:13 +01:00
|
|
|
predicates.push_back(
|
|
|
|
std::make_unique<AuthorPredicate>(value, isNegated));
|
2021-10-17 14:36:44 +02:00
|
|
|
}
|
2022-10-01 14:30:29 +02:00
|
|
|
else if (name == "badge")
|
|
|
|
{
|
2022-12-04 12:34:13 +01:00
|
|
|
predicates.push_back(
|
|
|
|
std::make_unique<BadgePredicate>(value, isNegated));
|
2022-10-01 14:30:29 +02:00
|
|
|
}
|
|
|
|
else if (name == "subtier")
|
|
|
|
{
|
2022-12-04 12:34:13 +01:00
|
|
|
predicates.push_back(
|
|
|
|
std::make_unique<SubtierPredicate>(value, isNegated));
|
2022-10-01 14:30:29 +02:00
|
|
|
}
|
2021-10-17 14:36:44 +02:00
|
|
|
else if (name == "has" && value == "link")
|
|
|
|
{
|
2022-12-04 12:34:13 +01:00
|
|
|
predicates.push_back(std::make_unique<LinkPredicate>(isNegated));
|
2021-10-17 14:36:44 +02:00
|
|
|
}
|
|
|
|
else if (name == "in")
|
|
|
|
{
|
2022-12-04 12:34:13 +01:00
|
|
|
predicates.push_back(
|
|
|
|
std::make_unique<ChannelPredicate>(value, isNegated));
|
2021-10-17 14:36:44 +02:00
|
|
|
}
|
|
|
|
else if (name == "is")
|
|
|
|
{
|
|
|
|
predicates.push_back(
|
2022-12-04 12:34:13 +01:00
|
|
|
std::make_unique<MessageFlagsPredicate>(value, isNegated));
|
2021-10-17 14:36:44 +02:00
|
|
|
}
|
|
|
|
else if (name == "regex")
|
|
|
|
{
|
2022-12-04 12:34:13 +01:00
|
|
|
predicates.push_back(
|
|
|
|
std::make_unique<RegexPredicate>(value, isNegated));
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return predicates;
|
|
|
|
}
|
|
|
|
|
2019-09-08 22:27:57 +02:00
|
|
|
} // namespace chatterino
|