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

Sync channels with browser #4741

Merged
merged 5 commits into from
Aug 5, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
- Minor: 7TV badges now automatically update upon changing. (#4512)
- Minor: Stream status requests are now batched. (#4713)
- Minor: Added `/c2-theme-autoreload` command to automatically reload a custom theme. This is useful for when you're developing your own theme. (#4718)
- Minor: All channels opened in browser tabs are synced when using the extension for quicker switching between tabs. (#4741)
- Bugfix: Increased amount of blocked users loaded from 100 to 1,000. (#4721)
- Bugfix: Fixed generation of crashdumps by the browser-extension process when the browser was closed. (#4667)
- Bugfix: Fix spacing issue with mentions inside RTL text. (#4677)
Expand Down
49 changes: 49 additions & 0 deletions src/singletons/NativeMessaging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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] =
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down
24 changes: 24 additions & 0 deletions src/singletons/NativeMessaging.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@
#include <QString>
#include <QThread>

#include <vector>

namespace chatterino {

class Application;
class Paths;
class Channel;

using ChannelPtr = std::shared_ptr<Channel>;

void registerNmHost(Paths &paths);
std::string &getNmQueueName(Paths &paths);
Expand All @@ -26,21 +31,40 @@ namespace nm::client {
class NativeMessagingServer final
{
public:
NativeMessagingServer();
NativeMessagingServer(const NativeMessagingServer &) = delete;
NativeMessagingServer(NativeMessagingServer &&) = delete;
NativeMessagingServer &operator=(const NativeMessagingServer &) = delete;
NativeMessagingServer &operator=(NativeMessagingServer &&) = delete;

void start();

private:
class ReceiverThread : public QThread
{
public:
ReceiverThread(NativeMessagingServer &parent);

void run() override;

private:
void handleMessage(const QJsonObject &root);
void handleSelect(const QJsonObject &root);
void handleDetach(const QJsonObject &root);
void handleSync(const QJsonObject &root);

NativeMessagingServer &parent_;
};

void syncChannels(const QJsonArray &twitchChannels);

ReceiverThread thread;

/// This vector contains all channels that are open the user's browser.
/// These channels are joined to be able to switch channels more quickly.
std::vector<ChannelPtr> channelWarmer_;
pajlada marked this conversation as resolved.
Show resolved Hide resolved

friend ReceiverThread;
};

} // namespace chatterino