Sync channels with browser (#4741)

* feat: keep channels from browser tabs alive

* chore: add changelog entry

* fix: add comment

* fix: rename key

---------

Co-authored-by: pajlada <rasmus.karlsson@pajlada.com>
This commit is contained in:
nerix
2023-08-05 14:23:26 +02:00
committed by GitHub
parent 9e2eb0dd29
commit 1438529e98
3 changed files with 74 additions and 0 deletions
+49
View File
@@ -3,6 +3,7 @@
#include "Application.hpp"
#include "common/Literals.hpp"
#include "common/QLogging.hpp"
#include "debug/AssertInGuiThread.hpp"
#include "providers/twitch/TwitchIrcServer.hpp"
#include "singletons/Paths.hpp"
#include "util/IpcQueue.hpp"
@@ -129,12 +130,22 @@ namespace nm::client {
} // namespace nm::client
// SERVER
NativeMessagingServer::NativeMessagingServer()
: thread(*this)
{
}
void NativeMessagingServer::start()
{
this->thread.start();
}
NativeMessagingServer::ReceiverThread::ReceiverThread(
NativeMessagingServer &parent)
: parent_(parent)
{
}
void NativeMessagingServer::ReceiverThread::run()
{
auto [messageQueue, error] =
@@ -177,6 +188,11 @@ void NativeMessagingServer::ReceiverThread::handleMessage(
this->handleDetach(root);
return;
}
if (action == "sync")
{
this->handleSync(root);
return;
}
qCDebug(chatterinoNativeMessage) << "NM unknown action" << action;
}
@@ -263,6 +279,39 @@ void NativeMessagingServer::ReceiverThread::handleDetach(
}
// NOLINTEND(readability-convert-member-functions-to-static)
void NativeMessagingServer::ReceiverThread::handleSync(const QJsonObject &root)
{
// Structure:
// { action: 'sync', twitchChannels?: string[] }
postToThread([&parent = this->parent_,
twitch = root["twitchChannels"_L1].toArray()] {
parent.syncChannels(twitch);
});
}
void NativeMessagingServer::syncChannels(const QJsonArray &twitchChannels)
{
assertInGuiThread();
auto *app = getApp();
std::vector<ChannelPtr> updated;
updated.reserve(twitchChannels.size());
for (const auto &value : twitchChannels)
{
auto name = value.toString();
if (name.isEmpty())
{
continue;
}
// the deduping is done on the extension side
updated.emplace_back(app->twitch->getOrAddChannel(name));
}
// This will destroy channels that aren't used anymore.
this->channelWarmer_ = std::move(updated);
}
Atomic<boost::optional<QString>> &nmIpcError()
{
static Atomic<boost::optional<QString>> x;