Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve reply popup after thread update #4923

Merged
merged 21 commits into from
Nov 5, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
1f0cbcf
Avoid thread root id when using reply command
iProdigy Oct 25, 2023
0dc6b84
Show correct reply context
iProdigy Oct 28, 2023
85de2f6
Reply to correct parent
iProdigy Oct 28, 2023
598c861
refactor: use threads getter
iProdigy Oct 29, 2023
629cb05
chore: use getter in rest of IrcMessageHandler
iProdigy Oct 29, 2023
c96dfac
refactor: use MessagePtr alias
iProdigy Oct 29, 2023
8dffbb6
chore: reformat
iProdigy Oct 29, 2023
c86a5e2
chore: update changelog
iProdigy Oct 29, 2023
b9cca1e
fix: avoid illegal access when direct parent is not loaded
iProdigy Oct 31, 2023
2bf88b4
feat: add menu option to reply to root thread message
iProdigy Oct 31, 2023
e8acab5
Merge branch 'master' into fix/thread-ui-update
iProdigy Nov 1, 2023
5ae2f2f
chore(LoggingChannel): reformat
iProdigy Nov 1, 2023
839ff49
fix: use correct message reference
iProdigy Nov 1, 2023
46715de
chore: update changelog
iProdigy Nov 1, 2023
59e580c
Merge branch 'master' into fix/thread-ui-update
iProdigy Nov 3, 2023
b63918d
Directly call builder.setParent if message is a reply to the root thread
pajlada Nov 5, 2023
827cc2f
Directly call builder.setParent when looking through channel threads,
pajlada Nov 5, 2023
38b74ce
Directly call builder.setParent if we have to iterate through messages.
pajlada Nov 5, 2023
d319503
Merge branch 'master' into fix/thread-ui-update
pajlada Nov 5, 2023
6745c5e
Merge remote-tracking branch 'iprodigy/fix/thread-ui-update' into fix…
pajlada Nov 5, 2023
a35a43d
Apply same setParent changes to addMessage
pajlada Nov 5, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions src/controllers/commands/CommandController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1563,20 +1563,16 @@ void CommandController::initialize(Settings &, Paths &paths)
const auto &msg = *it;
if (msg->loginName.compare(username, Qt::CaseInsensitive) == 0)
{
std::shared_ptr<MessageThread> thread;
// found most recent message by user
if (msg->replyThread == nullptr)
{
thread = std::make_shared<MessageThread>(msg);
// prepare thread if one does not exist
auto thread = std::make_shared<MessageThread>(msg);
twitchChannel->addReplyThread(thread);
}
else
{
thread = msg->replyThread;
}

QString reply = words.mid(2).join(" ");
twitchChannel->sendReply(reply, thread->rootId());
twitchChannel->sendReply(reply, msg->id);
return "";
}
}
Expand Down
1 change: 1 addition & 0 deletions src/messages/Message.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ struct Message {
// the reply thread will be cleaned up by the TwitchChannel.
// The root of the thread does not have replyThread set.
std::shared_ptr<MessageThread> replyThread;
std::shared_ptr<const Message> replyParent;
iProdigy marked this conversation as resolved.
Show resolved Hide resolved
uint32_t count = 1;
std::vector<std::unique_ptr<MessageElement>> elements;

Expand Down
118 changes: 89 additions & 29 deletions src/providers/twitch/IrcMessageHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -453,10 +453,11 @@ void IrcMessageHandler::populateReply(
const std::vector<MessagePtr> &otherLoaded, TwitchMessageBuilder &builder)
{
const auto &tags = message->tags();
if (const auto it = tags.find("reply-parent-msg-id"); it != tags.end())
if (const auto it = tags.find("reply-thread-parent-msg-id"); it != tags.end())
{
const QString replyID = it.value().toString();
auto threadIt = channel->threads_.find(replyID);
std::shared_ptr<MessageThread> rootThread;
if (threadIt != channel->threads_.end())
{
auto owned = threadIt->second.lock();
Expand All @@ -466,43 +467,73 @@ void IrcMessageHandler::populateReply(
updateReplyParticipatedStatus(tags, message->nick(), builder,
owned, false);
builder.setThread(owned);
return;
rootThread = owned;
}
}

MessagePtr foundMessage;

// Thread does not yet exist, find root reply and create thread.
// Linear search is justified by the infrequent use of replies
for (auto &otherMsg : otherLoaded)
if (!rootThread)
{
if (otherMsg->id == replyID)
MessagePtr foundMessage;

// Thread does not yet exist, find root reply and create thread.
// Linear search is justified by the infrequent use of replies
for (auto &otherMsg : otherLoaded)
iProdigy marked this conversation as resolved.
Show resolved Hide resolved
{
// Found root reply message
foundMessage = otherMsg;
break;
if (otherMsg->id == replyID)
{
// Found root reply message
foundMessage = otherMsg;
break;
}
}
}

if (!foundMessage)
{
// We didn't find the reply root message in the otherLoaded messages
// which are typically the already-parsed recent messages from the
// Recent Messages API. We could have a really old message that
// still exists being replied to, so check for that here.
foundMessage = channel->findMessage(replyID);
if (!foundMessage)
{
// We didn't find the reply root message in the otherLoaded messages
// which are typically the already-parsed recent messages from the
// Recent Messages API. We could have a really old message that
// still exists being replied to, so check for that here.
foundMessage = channel->findMessage(replyID);
}

if (foundMessage)
{
std::shared_ptr<MessageThread> newThread =
std::make_shared<MessageThread>(foundMessage);
updateReplyParticipatedStatus(tags, message->nick(), builder,
newThread, true);

builder.setThread(newThread);
rootThread = newThread;
// Store weak reference to thread in channel
channel->addReplyThread(newThread);
}
}

if (foundMessage)
if (const auto parentIt = tags.find("reply-parent-msg-id"); parentIt != tags.end())
{
std::shared_ptr<MessageThread> newThread =
std::make_shared<MessageThread>(foundMessage);
updateReplyParticipatedStatus(tags, message->nick(), builder,
newThread, true);

builder.setThread(newThread);
// Store weak reference to thread in channel
channel->addReplyThread(newThread);
const QString parentID = parentIt.value().toString();
std::shared_ptr<const Message> parent;
if (replyID == parentID)
{
if (rootThread)
{
parent = rootThread->root();
}
}
else
{
auto parentThreadIt = channel->threads_.find(parentID);
if (parentThreadIt != channel->threads_.end() && !parentThreadIt->second.expired())
iProdigy marked this conversation as resolved.
Show resolved Hide resolved
{
parent = parentThreadIt->second.lock()->root();
}
else
{
parent = channel->findMessage(parentID);
}
}
builder.setParent(parent);
}
}
}
Expand Down Expand Up @@ -570,17 +601,19 @@ void IrcMessageHandler::addMessage(Communi::IrcMessage *_message,
TwitchMessageBuilder builder(chan.get(), _message, args, content, isAction);
builder.setMessageOffset(messageOffset);

if (const auto it = tags.find("reply-parent-msg-id"); it != tags.end())
if (const auto it = tags.find("reply-thread-parent-msg-id"); it != tags.end())
{
const QString replyID = it.value().toString();
auto threadIt = channel->threads_.find(replyID);
std::shared_ptr<MessageThread> rootThread;
if (threadIt != channel->threads_.end() && !threadIt->second.expired())
{
// Thread already exists (has a reply)
auto thread = threadIt->second.lock();
updateReplyParticipatedStatus(tags, _message->nick(), builder,
thread, false);
builder.setThread(thread);
rootThread = thread;
}
else
{
Expand All @@ -594,10 +627,37 @@ void IrcMessageHandler::addMessage(Communi::IrcMessage *_message,
newThread, true);

builder.setThread(newThread);
rootThread = newThread;
// Store weak reference to thread in channel
channel->addReplyThread(newThread);
}
}

if (const auto parentIt = tags.find("reply-parent-msg-id"); parentIt != tags.end())
{
const QString parentID = parentIt.value().toString();
std::shared_ptr<const Message> parent;
if (replyID == parentID)
{
if (rootThread)
{
parent = rootThread->root();
}
}
else
{
auto parentThreadIt = channel->threads_.find(parentID);
if (parentThreadIt != channel->threads_.end() && !parentThreadIt->second.expired())
{
parent = parentThreadIt->second.lock()->root();
}
else
{
parent = channel->findMessage(parentID);
}
}
builder.setParent(parent);
}
}

if (isSub || !builder.isIgnored())
Expand Down
15 changes: 13 additions & 2 deletions src/providers/twitch/TwitchMessageBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -623,15 +623,21 @@ void TwitchMessageBuilder::parseThread()
{
// set references
this->message().replyThread = this->thread_;
this->message().replyParent = this->parent_;
this->thread_->addToThread(this->weakOf());

// enable reply flag
this->message().flags.set(MessageFlag::ReplyMessage);

const auto &threadRoot = this->thread_->root();
std::shared_ptr<const Message> threadRoot;
if (!this->parent_) {
threadRoot = this->thread_->root();
} else {
threadRoot = this->parent_;
}

QString usernameText = SharedMessageBuilder::stylizeUsername(
threadRoot->loginName, *threadRoot.get());
threadRoot->loginName, *threadRoot);

this->emplace<ReplyCurveElement>();

Expand Down Expand Up @@ -1811,6 +1817,11 @@ void TwitchMessageBuilder::setThread(std::shared_ptr<MessageThread> thread)
this->thread_ = std::move(thread);
}

void TwitchMessageBuilder::setParent(std::shared_ptr<const Message> parent)
{
this->parent_ = std::move(parent);
}

void TwitchMessageBuilder::setMessageOffset(int offset)
{
this->messageOffset_ = offset;
Expand Down
2 changes: 2 additions & 0 deletions src/providers/twitch/TwitchMessageBuilder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class TwitchMessageBuilder : public SharedMessageBuilder
MessagePtr build() override;

void setThread(std::shared_ptr<MessageThread> thread);
void setParent(std::shared_ptr<const Message> parent);
void setMessageOffset(int offset);

static void appendChannelPointRewardMessage(
Expand Down Expand Up @@ -131,6 +132,7 @@ class TwitchMessageBuilder : public SharedMessageBuilder
bool bitsStacked = false;
bool historicalMessage_ = false;
std::shared_ptr<MessageThread> thread_;
std::shared_ptr<const Message> parent_;

/**
* Starting offset to be used on index-based operations on `originalMessage_`.
Expand Down
3 changes: 1 addition & 2 deletions src/singletons/helper/LoggingChannel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,7 @@ void LoggingChannel::addMessage(MessagePtr message)
qsizetype colonIndex = messageText.indexOf(':');
if (colonIndex != -1)
{
QString rootMessageChatter =
message->replyThread->root()->loginName;
QString rootMessageChatter = message->replyParent->loginName;
iProdigy marked this conversation as resolved.
Show resolved Hide resolved
messageText.insert(colonIndex + 1, " @" + rootMessageChatter);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/widgets/dialogs/ReplyThreadPopup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ ReplyThreadPopup::ReplyThreadPopup(bool closeAutomatically, QWidget *parent,
void ReplyThreadPopup::setThread(std::shared_ptr<MessageThread> thread)
{
this->thread_ = std::move(thread);
this->ui_.replyInput->setReply(this->thread_);
this->ui_.replyInput->setReply(this->thread_->root());
this->addMessagesFromThread();
this->updateInputUI();

Expand Down
20 changes: 4 additions & 16 deletions src/widgets/helper/ChannelView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2830,10 +2830,9 @@ void ChannelView::setInputReply(const MessagePtr &message)
return;
}

std::shared_ptr<MessageThread> thread;

if (message->replyThread == nullptr)
{
// Create thread if one does not exist
auto getThread = [&](TwitchChannel *tc) {
auto threadIt = tc->threads().find(message->id);
if (threadIt != tc->threads().end() && !threadIt->second.expired())
Expand All @@ -2851,26 +2850,15 @@ void ChannelView::setInputReply(const MessagePtr &message)
if (auto tc =
dynamic_cast<TwitchChannel *>(this->underlyingChannel_.get()))
{
thread = getThread(tc);
getThread(tc);
}
else if (auto tc = dynamic_cast<TwitchChannel *>(this->channel_.get()))
{
thread = getThread(tc);
}
else
{
qCWarning(chatterinoCommon) << "Failed to create new reply thread";
// Unable to create new reply thread.
// TODO(dnsge): Should probably notify user?
return;
getThread(tc);
}
}
else
{
thread = message->replyThread;
}

this->split_->setInputReply(thread);
this->split_->setInputReply(message);
}

void ChannelView::showReplyThreadPopup(const MessagePtr &message)
Expand Down
2 changes: 1 addition & 1 deletion src/widgets/splits/Split.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1542,7 +1542,7 @@ void Split::drag()
stopDraggingSplit();
}

void Split::setInputReply(const std::shared_ptr<MessageThread> &reply)
void Split::setInputReply(const std::shared_ptr<const Message> &reply)
{
this->input_->setReply(reply);
}
Expand Down
3 changes: 1 addition & 2 deletions src/widgets/splits/Split.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
namespace chatterino {

class ChannelView;
class MessageThread;
class SplitHeader;
class SplitInput;
class SplitContainer;
Expand Down Expand Up @@ -75,7 +74,7 @@ class Split : public BaseWidget

void setContainer(SplitContainer *container);

void setInputReply(const std::shared_ptr<MessageThread> &reply);
void setInputReply(const std::shared_ptr<const Message> &reply);

static pajlada::Signals::Signal<Qt::KeyboardModifiers>
modifierStatusChanged;
Expand Down
12 changes: 6 additions & 6 deletions src/widgets/splits/SplitInput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ QString SplitInput::handleSendMessage(std::vector<QString> &arguments)
if (this->enableInlineReplying_)
{
// Remove @username prefix that is inserted when doing inline replies
message.remove(0, this->replyThread_->root()->displayName.length() +
message.remove(0, this->replyThread_->displayName.length() +
1); // remove "@username"

if (!message.isEmpty() && message.at(0) == ' ')
Expand All @@ -373,7 +373,7 @@ QString SplitInput::handleSendMessage(std::vector<QString> &arguments)
getApp()->commands->execCommand(message, c, false);

// Reply within TwitchChannel
tc->sendReply(sendMessage, this->replyThread_->rootId());
tc->sendReply(sendMessage, this->replyThread_->id);

this->postMessageSend(message, arguments);
return "";
Expand Down Expand Up @@ -992,7 +992,7 @@ void SplitInput::editTextChanged()
// We need to verify that
// 1. the @username prefix exists and
// 2. if a character exists after the @username, it is a space
QString replyPrefix = "@" + this->replyThread_->root()->displayName;
QString replyPrefix = "@" + this->replyThread_->displayName;
iProdigy marked this conversation as resolved.
Show resolved Hide resolved
if (!text.startsWith(replyPrefix) ||
(text.length() > replyPrefix.length() &&
text.at(replyPrefix.length()) != ' '))
Expand Down Expand Up @@ -1065,15 +1065,15 @@ void SplitInput::giveFocus(Qt::FocusReason reason)
this->ui_.textEdit->setFocus(reason);
}

void SplitInput::setReply(std::shared_ptr<MessageThread> reply,
void SplitInput::setReply(std::shared_ptr<const Message> reply,
bool showReplyingLabel)
{
this->replyThread_ = std::move(reply);

if (this->enableInlineReplying_)
{
// Only enable reply label if inline replying
auto replyPrefix = "@" + this->replyThread_->root()->displayName;
auto replyPrefix = "@" + this->replyThread_->displayName;
auto plainText = this->ui_.textEdit->toPlainText().trimmed();
if (!plainText.startsWith(replyPrefix))
{
Expand All @@ -1086,7 +1086,7 @@ void SplitInput::setReply(std::shared_ptr<MessageThread> reply,
this->ui_.textEdit->resetCompletion();
}
this->ui_.replyLabel->setText("Replying to @" +
this->replyThread_->root()->displayName);
this->replyThread_->displayName);
}
}

Expand Down
Loading
Loading