Add room ID to pubsub moderator actions

This commit is contained in:
Rasmus Karlsson 2018-04-22 15:37:02 +02:00
parent d62e45d9dd
commit 92a19d61cf
4 changed files with 48 additions and 61 deletions

View file

@ -5,8 +5,9 @@
namespace chatterino { namespace chatterino {
namespace singletons { namespace singletons {
PubSubAction::PubSubAction(const rapidjson::Value &data) PubSubAction::PubSubAction(const rapidjson::Value &data, const QString &_roomID)
: timestamp(std::chrono::steady_clock::now()) : timestamp(std::chrono::steady_clock::now())
, roomID(_roomID)
{ {
getCreatedByUser(data, this->source); getCreatedByUser(data, this->source);
} }

View file

@ -16,18 +16,16 @@ struct ActionUser {
}; };
struct PubSubAction { struct PubSubAction {
PubSubAction(const rapidjson::Value &data); PubSubAction(const rapidjson::Value &data, const QString &_roomID);
ActionUser source; ActionUser source;
std::chrono::steady_clock::time_point timestamp; std::chrono::steady_clock::time_point timestamp;
QString roomID;
}; };
// Used when a chat mode (i.e. slowmode, subscribers only mode) is enabled or disabled // Used when a chat mode (i.e. slowmode, subscribers only mode) is enabled or disabled
struct ModeChangedAction : PubSubAction { struct ModeChangedAction : PubSubAction {
ModeChangedAction(const rapidjson::Value &data) using PubSubAction::PubSubAction;
: PubSubAction(data)
{
}
enum Mode { enum Mode {
Unknown, Unknown,
@ -49,10 +47,7 @@ struct ModeChangedAction : PubSubAction {
}; };
struct TimeoutAction : PubSubAction { struct TimeoutAction : PubSubAction {
TimeoutAction(const rapidjson::Value &data) using PubSubAction::PubSubAction;
: PubSubAction(data)
{
}
ActionUser target; ActionUser target;
@ -61,10 +56,7 @@ struct TimeoutAction : PubSubAction {
}; };
struct BanAction : PubSubAction { struct BanAction : PubSubAction {
BanAction(const rapidjson::Value &data) using PubSubAction::PubSubAction;
: PubSubAction(data)
{
}
ActionUser target; ActionUser target;
@ -72,10 +64,7 @@ struct BanAction : PubSubAction {
}; };
struct UnbanAction : PubSubAction { struct UnbanAction : PubSubAction {
UnbanAction(const rapidjson::Value &data) using PubSubAction::PubSubAction;
: PubSubAction(data)
{
}
ActionUser target; ActionUser target;
@ -86,17 +75,11 @@ struct UnbanAction : PubSubAction {
}; };
struct ClearChatAction : PubSubAction { struct ClearChatAction : PubSubAction {
ClearChatAction(const rapidjson::Value &data) using PubSubAction::PubSubAction;
: PubSubAction(data)
{
}
}; };
struct ModerationStateAction : PubSubAction { struct ModerationStateAction : PubSubAction {
ModerationStateAction(const rapidjson::Value &data) using PubSubAction::PubSubAction;
: PubSubAction(data)
{
}
ActionUser target; ActionUser target;

View file

@ -172,14 +172,14 @@ bool PubSubClient::Send(const char *payload)
PubSubManager::PubSubManager() PubSubManager::PubSubManager()
{ {
this->moderationActionHandlers["clear"] = [this](const auto &data) { this->moderationActionHandlers["clear"] = [this](const auto &data, const auto &roomID) {
ClearChatAction action(data); ClearChatAction action(data, roomID);
this->sig.moderation.chatCleared.invoke(action); this->sig.moderation.chatCleared.invoke(action);
}; };
this->moderationActionHandlers["slowoff"] = [this](const auto &data) { this->moderationActionHandlers["slowoff"] = [this](const auto &data, const auto &roomID) {
ModeChangedAction action(data); ModeChangedAction action(data, roomID);
action.mode = ModeChangedAction::Mode::Slow; action.mode = ModeChangedAction::Mode::Slow;
action.state = ModeChangedAction::State::Off; action.state = ModeChangedAction::State::Off;
@ -187,8 +187,8 @@ PubSubManager::PubSubManager()
this->sig.moderation.modeChanged.invoke(action); this->sig.moderation.modeChanged.invoke(action);
}; };
this->moderationActionHandlers["slow"] = [this](const auto &data) { this->moderationActionHandlers["slow"] = [this](const auto &data, const auto &roomID) {
ModeChangedAction action(data); ModeChangedAction action(data, roomID);
action.mode = ModeChangedAction::Mode::Slow; action.mode = ModeChangedAction::Mode::Slow;
action.state = ModeChangedAction::State::On; action.state = ModeChangedAction::State::On;
@ -224,8 +224,8 @@ PubSubManager::PubSubManager()
this->sig.moderation.modeChanged.invoke(action); this->sig.moderation.modeChanged.invoke(action);
}; };
this->moderationActionHandlers["r9kbetaoff"] = [this](const auto &data) { this->moderationActionHandlers["r9kbetaoff"] = [this](const auto &data, const auto &roomID) {
ModeChangedAction action(data); ModeChangedAction action(data, roomID);
action.mode = ModeChangedAction::Mode::R9K; action.mode = ModeChangedAction::Mode::R9K;
action.state = ModeChangedAction::State::Off; action.state = ModeChangedAction::State::Off;
@ -233,8 +233,8 @@ PubSubManager::PubSubManager()
this->sig.moderation.modeChanged.invoke(action); this->sig.moderation.modeChanged.invoke(action);
}; };
this->moderationActionHandlers["r9kbeta"] = [this](const auto &data) { this->moderationActionHandlers["r9kbeta"] = [this](const auto &data, const auto &roomID) {
ModeChangedAction action(data); ModeChangedAction action(data, roomID);
action.mode = ModeChangedAction::Mode::R9K; action.mode = ModeChangedAction::Mode::R9K;
action.state = ModeChangedAction::State::On; action.state = ModeChangedAction::State::On;
@ -242,8 +242,9 @@ PubSubManager::PubSubManager()
this->sig.moderation.modeChanged.invoke(action); this->sig.moderation.modeChanged.invoke(action);
}; };
this->moderationActionHandlers["subscribersoff"] = [this](const auto &data) { this->moderationActionHandlers["subscribersoff"] = [this](const auto &data,
ModeChangedAction action(data); const auto &roomID) {
ModeChangedAction action(data, roomID);
action.mode = ModeChangedAction::Mode::SubscribersOnly; action.mode = ModeChangedAction::Mode::SubscribersOnly;
action.state = ModeChangedAction::State::Off; action.state = ModeChangedAction::State::Off;
@ -251,8 +252,8 @@ PubSubManager::PubSubManager()
this->sig.moderation.modeChanged.invoke(action); this->sig.moderation.modeChanged.invoke(action);
}; };
this->moderationActionHandlers["subscribers"] = [this](const auto &data) { this->moderationActionHandlers["subscribers"] = [this](const auto &data, const auto &roomID) {
ModeChangedAction action(data); ModeChangedAction action(data, roomID);
action.mode = ModeChangedAction::Mode::SubscribersOnly; action.mode = ModeChangedAction::Mode::SubscribersOnly;
action.state = ModeChangedAction::State::On; action.state = ModeChangedAction::State::On;
@ -260,8 +261,8 @@ PubSubManager::PubSubManager()
this->sig.moderation.modeChanged.invoke(action); this->sig.moderation.modeChanged.invoke(action);
}; };
this->moderationActionHandlers["emoteonlyoff"] = [this](const auto &data) { this->moderationActionHandlers["emoteonlyoff"] = [this](const auto &data, const auto &roomID) {
ModeChangedAction action(data); ModeChangedAction action(data, roomID);
action.mode = ModeChangedAction::Mode::EmoteOnly; action.mode = ModeChangedAction::Mode::EmoteOnly;
action.state = ModeChangedAction::State::Off; action.state = ModeChangedAction::State::Off;
@ -269,8 +270,8 @@ PubSubManager::PubSubManager()
this->sig.moderation.modeChanged.invoke(action); this->sig.moderation.modeChanged.invoke(action);
}; };
this->moderationActionHandlers["emoteonly"] = [this](const auto &data) { this->moderationActionHandlers["emoteonly"] = [this](const auto &data, const auto &roomID) {
ModeChangedAction action(data); ModeChangedAction action(data, roomID);
action.mode = ModeChangedAction::Mode::EmoteOnly; action.mode = ModeChangedAction::Mode::EmoteOnly;
action.state = ModeChangedAction::State::On; action.state = ModeChangedAction::State::On;
@ -278,8 +279,8 @@ PubSubManager::PubSubManager()
this->sig.moderation.modeChanged.invoke(action); this->sig.moderation.modeChanged.invoke(action);
}; };
this->moderationActionHandlers["unmod"] = [this](const auto &data) { this->moderationActionHandlers["unmod"] = [this](const auto &data, const auto &roomID) {
ModerationStateAction action(data); ModerationStateAction action(data, roomID);
getTargetUser(data, action.target); getTargetUser(data, action.target);
action.modded = false; action.modded = false;
@ -287,8 +288,8 @@ PubSubManager::PubSubManager()
this->sig.moderation.moderationStateChanged.invoke(action); this->sig.moderation.moderationStateChanged.invoke(action);
}; };
this->moderationActionHandlers["mod"] = [this](const auto &data) { this->moderationActionHandlers["mod"] = [this](const auto &data, const auto &roomID) {
ModerationStateAction action(data); ModerationStateAction action(data, roomID);
getTargetUser(data, action.target); getTargetUser(data, action.target);
action.modded = true; action.modded = true;
@ -296,8 +297,8 @@ PubSubManager::PubSubManager()
this->sig.moderation.moderationStateChanged.invoke(action); this->sig.moderation.moderationStateChanged.invoke(action);
}; };
this->moderationActionHandlers["timeout"] = [this](const auto &data) { this->moderationActionHandlers["timeout"] = [this](const auto &data, const auto &roomID) {
TimeoutAction action(data); TimeoutAction action(data, roomID);
getCreatedByUser(data, action.source); getCreatedByUser(data, action.source);
getTargetUser(data, action.target); getTargetUser(data, action.target);
@ -332,8 +333,8 @@ PubSubManager::PubSubManager()
} }
}; };
this->moderationActionHandlers["ban"] = [this](const auto &data) { this->moderationActionHandlers["ban"] = [this](const auto &data, const auto &roomID) {
BanAction action(data); BanAction action(data, roomID);
getCreatedByUser(data, action.source); getCreatedByUser(data, action.source);
getTargetUser(data, action.target); getTargetUser(data, action.target);
@ -361,8 +362,8 @@ PubSubManager::PubSubManager()
} }
}; };
this->moderationActionHandlers["unban"] = [this](const auto &data) { this->moderationActionHandlers["unban"] = [this](const auto &data, const auto &roomID) {
UnbanAction action(data); UnbanAction action(data, roomID);
getCreatedByUser(data, action.source); getCreatedByUser(data, action.source);
getTargetUser(data, action.target); getTargetUser(data, action.target);
@ -386,8 +387,8 @@ PubSubManager::PubSubManager()
} }
}; };
this->moderationActionHandlers["untimeout"] = [this](const auto &data) { this->moderationActionHandlers["untimeout"] = [this](const auto &data, const auto &roomID) {
UnbanAction action(data); UnbanAction action(data, roomID);
getCreatedByUser(data, action.source); getCreatedByUser(data, action.source);
getTargetUser(data, action.target); getTargetUser(data, action.target);
@ -667,7 +668,7 @@ void PubSubManager::HandleListenResponse(const rapidjson::Document &msg)
void PubSubManager::HandleMessageResponse(const rapidjson::Value &outerData) void PubSubManager::HandleMessageResponse(const rapidjson::Value &outerData)
{ {
std::string topic; QString topic;
if (!rj::getSafe(outerData, "topic", topic)) { if (!rj::getSafe(outerData, "topic", topic)) {
debug::Log("Missing required string member `topic` in outerData"); debug::Log("Missing required string member `topic` in outerData");
@ -691,7 +692,7 @@ void PubSubManager::HandleMessageResponse(const rapidjson::Value &outerData)
return; return;
} }
if (topic.find("whispers.") == 0) { if (topic.startsWith("whispers.")) {
std::string whisperType; std::string whisperType;
if (!rj::getSafe(msg, "type", whisperType)) { if (!rj::getSafe(msg, "type", whisperType)) {
@ -710,7 +711,9 @@ void PubSubManager::HandleMessageResponse(const rapidjson::Value &outerData)
assert(false); assert(false);
return; return;
} }
} else if (topic.find("chat_moderator_actions.") == 0) { } else if (topic.startsWith("chat_moderator_actions.")) {
auto topicParts = topic.split(".");
assert(topicParts.length() == 3);
const auto &data = msg["data"]; const auto &data = msg["data"];
std::string moderationAction; std::string moderationAction;
@ -728,7 +731,7 @@ void PubSubManager::HandleMessageResponse(const rapidjson::Value &outerData)
} }
// Invoke handler function // Invoke handler function
handlerIt->second(data); handlerIt->second(data, topicParts[2]);
} else { } else {
debug::Log("Unknown topic: {}", topic); debug::Log("Unknown topic: {}", topic);
return; return;

View file

@ -140,7 +140,7 @@ private:
std::map<WebsocketHandle, std::shared_ptr<PubSubClient>, std::owner_less<WebsocketHandle>> std::map<WebsocketHandle, std::shared_ptr<PubSubClient>, std::owner_less<WebsocketHandle>>
clients; clients;
std::unordered_map<std::string, std::function<void(const rapidjson::Value &)>> std::unordered_map<std::string, std::function<void(const rapidjson::Value &, const QString &)>>
moderationActionHandlers; moderationActionHandlers;
void OnMessage(websocketpp::connection_hdl hdl, WebsocketMessagePtr msg); void OnMessage(websocketpp::connection_hdl hdl, WebsocketMessagePtr msg);