fixed timeouts for non moderators

This commit is contained in:
fourtf 2018-05-17 13:43:01 +02:00
parent 89ca71aec4
commit 4d3437e6d1
6 changed files with 112 additions and 114 deletions

View file

@ -182,8 +182,9 @@ void Application::initialize()
} }
auto msg = messages::Message::createTimeoutMessage(action); auto msg = messages::Message::createTimeoutMessage(action);
msg->flags |= messages::Message::PubSub;
util::postToThread([chan, msg] { chan->addMessage(msg); }); util::postToThread([chan, msg] { chan->addOrReplaceTimeout(msg); });
}); });
this->twitch.pubsub->sig.moderation.userUnbanned.connect([&](const auto &action) { this->twitch.pubsub->sig.moderation.userUnbanned.connect([&](const auto &action) {

View file

@ -60,57 +60,14 @@ void Channel::addMessage(MessagePtr message)
auto app = getApp(); auto app = getApp();
MessagePtr deleted; MessagePtr deleted;
bool isTimeout = (message->flags & Message::Timeout) != 0; const QString &username = message->loginName;
if (!username.isEmpty()) {
if (!isTimeout) { // TODO: Add recent chatters display name. This should maybe be a setting
const QString &username = message->loginName; this->addRecentChatter(message);
if (!username.isEmpty()) {
// TODO: Add recent chatters display name. This should maybe be a setting
this->addRecentChatter(message);
}
} }
app->logging->addMessage(this->name, message); app->logging->addMessage(this->name, message);
if (isTimeout) {
LimitedQueueSnapshot<MessagePtr> snapshot = this->getMessageSnapshot();
bool addMessage = true;
int snapshotLength = snapshot.getLength();
int end = std::max(0, snapshotLength - 20);
for (int i = snapshotLength - 1; i >= end; --i) {
auto &s = snapshot[i];
if (s->flags.HasFlag(Message::Untimeout) && s->timeoutUser == message->timeoutUser) {
break;
}
if (s->flags.HasFlag(Message::Timeout) && s->timeoutUser == message->timeoutUser) {
assert(message->banAction != nullptr);
MessagePtr replacement(
Message::createTimeoutMessage(*(message->banAction), s->count + 1));
this->replaceMessage(s, replacement);
addMessage = false;
}
}
// disable the messages from the user
for (int i = 0; i < snapshotLength; i++) {
auto &s = snapshot[i];
if ((s->flags & (Message::Timeout | Message::Untimeout)) == 0 &&
s->loginName == message->timeoutUser) {
s->flags.EnableFlag(Message::Disabled);
}
}
// XXX: Might need the following line
// WindowManager::getInstance().repaintVisibleChatWidgets(this);
if (!addMessage) {
return;
}
}
if (this->messages.pushBack(message, deleted)) { if (this->messages.pushBack(message, deleted)) {
this->messageRemovedFromStart.invoke(deleted); this->messageRemovedFromStart.invoke(deleted);
} }
@ -118,6 +75,73 @@ void Channel::addMessage(MessagePtr message)
this->messageAppended.invoke(message); this->messageAppended.invoke(message);
} }
void Channel::addOrReplaceTimeout(messages::MessagePtr message)
{
LimitedQueueSnapshot<MessagePtr> snapshot = this->getMessageSnapshot();
int snapshotLength = snapshot.getLength();
int end = std::max(0, snapshotLength - 20);
bool addMessage = true;
QTime minimumTime = QTime::currentTime().addSecs(-5);
for (int i = snapshotLength - 1; i >= end; --i) {
auto &s = snapshot[i];
qDebug() << s->parseTime << minimumTime;
if (s->parseTime < minimumTime) {
break;
}
if (s->flags.HasFlag(Message::Untimeout) && s->timeoutUser == message->timeoutUser) {
break;
}
if (s->flags.HasFlag(Message::Timeout) && s->timeoutUser == message->timeoutUser) {
if (message->flags.HasFlag(Message::PubSub) && !s->flags.HasFlag(Message::PubSub)) {
this->replaceMessage(s, message);
addMessage = false;
break;
}
if (!message->flags.HasFlag(Message::PubSub) && s->flags.HasFlag(Message::PubSub)) {
addMessage = false;
break;
}
int count = s->count + 1;
messages::MessagePtr replacement(Message::createSystemMessage(
message->searchText + QString("(") + QString::number(count) + " times)"));
replacement->timeoutUser = message->timeoutUser;
replacement->count = count;
replacement->flags = message->flags;
this->replaceMessage(s, replacement);
return;
}
}
// disable the messages from the user
for (int i = 0; i < snapshotLength; i++) {
auto &s = snapshot[i];
if ((s->flags & (Message::Timeout | Message::Untimeout)) == 0 &&
s->loginName == message->timeoutUser) {
s->flags.EnableFlag(Message::Disabled);
}
}
if (addMessage) {
this->addMessage(message);
}
// XXX: Might need the following line
// WindowManager::getInstance().repaintVisibleChatWidgets(this);
}
void Channel::addMessagesAtStart(std::vector<messages::MessagePtr> &_messages) void Channel::addMessagesAtStart(std::vector<messages::MessagePtr> &_messages)
{ {
std::vector<messages::MessagePtr> addedMessages = this->messages.pushFront(_messages); std::vector<messages::MessagePtr> addedMessages = this->messages.pushFront(_messages);

View file

@ -48,6 +48,7 @@ public:
void addMessage(messages::MessagePtr message); void addMessage(messages::MessagePtr message);
void addMessagesAtStart(std::vector<messages::MessagePtr> &messages); void addMessagesAtStart(std::vector<messages::MessagePtr> &messages);
void addOrReplaceTimeout(messages::MessagePtr message);
void replaceMessage(messages::MessagePtr message, messages::MessagePtr replacement); void replaceMessage(messages::MessagePtr message, messages::MessagePtr replacement);
virtual void addRecentChatter(const std::shared_ptr<messages::Message> &message); virtual void addRecentChatter(const std::shared_ptr<messages::Message> &message);

View file

@ -89,7 +89,6 @@ MessagePtr Message::createTimeoutMessage(const providers::twitch::BanAction &act
msg->timeoutUser = action.target.name; msg->timeoutUser = action.target.name;
msg->count = count; msg->count = count;
msg->banAction.reset(new providers::twitch::BanAction(action));
QString text; QString text;

View file

@ -18,6 +18,7 @@ namespace messages {
struct Message { struct Message {
Message() Message()
: parseTime(QTime::currentTime())
{ {
util::DebugCount::increase("messages"); util::DebugCount::increase("messages");
} }
@ -39,6 +40,7 @@ struct Message {
Collapsed = (1 << 7), Collapsed = (1 << 7),
DisconnectedMessage = (1 << 8), DisconnectedMessage = (1 << 8),
Untimeout = (1 << 9), Untimeout = (1 << 9),
PubSub = (1 << 10),
}; };
util::FlagsEnum<MessageFlags> flags; util::FlagsEnum<MessageFlags> flags;
@ -50,7 +52,6 @@ struct Message {
QString localizedName; QString localizedName;
QString timeoutUser; QString timeoutUser;
std::unique_ptr<providers::twitch::BanAction> banAction;
uint32_t count = 1; uint32_t count = 1;
// Messages should not be added after the message is done initializing. // Messages should not be added after the message is done initializing.

View file

@ -60,80 +60,52 @@ void IrcMessageHandler::handleRoomStateMessage(Communi::IrcMessage *message)
void IrcMessageHandler::handleClearChatMessage(Communi::IrcMessage *message) void IrcMessageHandler::handleClearChatMessage(Communi::IrcMessage *message)
{ {
return; // check parameter count
// // check parameter count if (message->parameters().length() < 1) {
// if (message->parameters().length() < 1) { return;
// return; }
// }
// QString chanName; QString chanName;
// if (!TrimChannelName(message->parameter(0), chanName)) { if (!TrimChannelName(message->parameter(0), chanName)) {
// return; return;
// } }
// auto app = getApp(); auto app = getApp();
// // get channel // get channel
// auto chan = app->twitch.server->getChannelOrEmpty(chanName); auto chan = app->twitch.server->getChannelOrEmpty(chanName);
// if (chan->isEmpty()) { if (chan->isEmpty()) {
// debug::Log("[IrcMessageHandler:handleClearChatMessage] Twitch channel {} not found", debug::Log("[IrcMessageHandler:handleClearChatMessage] Twitch channel {} not found",
// chanName); chanName);
// return; return;
// } }
// // check if the chat has been cleared by a moderator // check if the chat has been cleared by a moderator
// if (message->parameters().length() == 1) { if (message->parameters().length() == 1) {
// chan->addMessage(Message::createSystemMessage("Chat has been cleared by a chan->addMessage(Message::createSystemMessage("Chat has been cleared by a moderator."));
// moderator."));
// return; return;
// } }
// // get username, duration and message of the timed out user // get username, duration and message of the timed out user
// QString username = message->parameter(1); QString username = message->parameter(1);
// QString durationInSeconds, reason; QString durationInSeconds, reason;
// QVariant v = message->tag("ban-duration"); QVariant v = message->tag("ban-duration");
// if (v.isValid()) { if (v.isValid()) {
// durationInSeconds = v.toString(); durationInSeconds = v.toString();
// } }
// v = message->tag("ban-reason"); v = message->tag("ban-reason");
// if (v.isValid()) { if (v.isValid()) {
// reason = v.toString(); reason = v.toString();
// } }
// // add the notice that the user has been timed out auto timeoutMsg = Message::createTimeoutMessage(username, durationInSeconds, reason, false);
// LimitedQueueSnapshot<MessagePtr> snapshot = chan->getMessageSnapshot(); chan->addOrReplaceTimeout(timeoutMsg);
// bool addMessage = true;
// int snapshotLength = snapshot.getLength();
// for (int i = std::max(0, snapshotLength - 20); i < snapshotLength; i++) { // refresh all
// auto &s = snapshot[i]; app->windows->repaintVisibleChatWidgets(chan.get());
// if (s->flags.HasFlag(Message::Timeout) && s->timeoutUser == username) {
// MessagePtr replacement(
// Message::createTimeoutMessage(username, durationInSeconds, reason, true));
// chan->replaceMessage(s, replacement);
// addMessage = false;
// break;
// }
// }
// if (addMessage) {
// chan->addMessage(Message::createTimeoutMessage(username, durationInSeconds, reason,
// false));
// }
// // disable the messages from the user
// for (int i = 0; i < snapshotLength; i++) {
// auto &s = snapshot[i];
// if (!(s->flags & Message::Timeout) && s->loginName == username) {
// s->flags.EnableFlag(Message::Disabled);
// }
// }
// // refresh all
// app->windows->repaintVisibleChatWidgets(chan.get());
} }
void IrcMessageHandler::handleUserStateMessage(Communi::IrcMessage *message) void IrcMessageHandler::handleUserStateMessage(Communi::IrcMessage *message)