2018-06-26 15:33:51 +02:00
|
|
|
#include "common/Channel.hpp"
|
2018-04-27 22:11:19 +02:00
|
|
|
|
2018-06-26 14:09:39 +02:00
|
|
|
#include "Application.hpp"
|
|
|
|
#include "debug/Log.hpp"
|
|
|
|
#include "messages/Message.hpp"
|
2018-08-07 01:35:24 +02:00
|
|
|
#include "messages/MessageBuilder.hpp"
|
2018-06-28 19:46:45 +02:00
|
|
|
#include "singletons/Emotes.hpp"
|
|
|
|
#include "singletons/Logging.hpp"
|
2018-06-26 14:09:39 +02:00
|
|
|
#include "singletons/WindowManager.hpp"
|
2017-01-01 13:07:36 +01:00
|
|
|
|
2017-01-26 17:48:14 +01:00
|
|
|
#include <QJsonArray>
|
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QJsonObject>
|
|
|
|
#include <QJsonValue>
|
|
|
|
#include <QNetworkAccessManager>
|
|
|
|
#include <QNetworkReply>
|
|
|
|
#include <QNetworkRequest>
|
2017-03-11 11:39:59 +01:00
|
|
|
|
2017-04-14 17:52:22 +02:00
|
|
|
namespace chatterino {
|
2017-01-18 21:30:23 +01:00
|
|
|
|
2018-08-10 19:00:14 +02:00
|
|
|
//
|
|
|
|
// Channel
|
|
|
|
//
|
2018-08-02 14:23:27 +02:00
|
|
|
Channel::Channel(const QString &name, Type type)
|
2018-08-13 14:38:03 +02:00
|
|
|
: completionModel(*this)
|
2018-08-02 14:23:27 +02:00
|
|
|
, name_(name)
|
2018-07-06 18:10:21 +02:00
|
|
|
, type_(type)
|
2017-01-01 13:07:36 +01:00
|
|
|
{
|
2017-01-07 20:43:55 +01:00
|
|
|
}
|
2017-01-05 16:07:20 +01:00
|
|
|
|
2018-02-05 15:11:50 +01:00
|
|
|
Channel::~Channel()
|
|
|
|
{
|
|
|
|
this->destroyed.invoke();
|
|
|
|
}
|
|
|
|
|
2018-04-18 09:12:29 +02:00
|
|
|
Channel::Type Channel::getType() const
|
|
|
|
{
|
2018-07-06 17:30:12 +02:00
|
|
|
return this->type_;
|
2018-04-18 09:12:29 +02:00
|
|
|
}
|
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
const QString &Channel::getName() const
|
|
|
|
{
|
|
|
|
return this->name_;
|
|
|
|
}
|
|
|
|
|
2019-03-01 21:18:32 +01:00
|
|
|
const QString &Channel::getDisplayName() const
|
|
|
|
{
|
|
|
|
return this->getName();
|
|
|
|
}
|
|
|
|
|
2018-05-25 13:53:55 +02:00
|
|
|
bool Channel::isTwitchChannel() const
|
|
|
|
{
|
2018-07-06 17:30:12 +02:00
|
|
|
return this->type_ >= Type::Twitch && this->type_ < Type::TwitchEnd;
|
2018-05-25 13:53:55 +02:00
|
|
|
}
|
|
|
|
|
2017-04-12 17:46:44 +02:00
|
|
|
bool Channel::isEmpty() const
|
|
|
|
{
|
2018-08-02 14:23:27 +02:00
|
|
|
return this->name_.isEmpty();
|
2017-01-26 17:26:20 +01:00
|
|
|
}
|
|
|
|
|
2018-06-28 19:38:57 +02:00
|
|
|
LimitedQueueSnapshot<MessagePtr> Channel::getMessageSnapshot()
|
2017-01-26 17:26:20 +01:00
|
|
|
{
|
2018-07-06 17:30:12 +02:00
|
|
|
return this->messages_.getSnapshot();
|
2017-04-12 17:46:44 +02:00
|
|
|
}
|
2017-01-26 17:26:20 +01:00
|
|
|
|
2018-10-05 23:33:01 +02:00
|
|
|
void Channel::addMessage(MessagePtr message,
|
|
|
|
boost::optional<MessageFlags> overridingFlags)
|
2017-04-12 17:46:44 +02:00
|
|
|
{
|
2018-04-27 22:11:19 +02:00
|
|
|
auto app = getApp();
|
2018-01-11 20:16:25 +01:00
|
|
|
MessagePtr deleted;
|
2017-01-26 17:26:20 +01:00
|
|
|
|
2018-05-17 13:43:01 +02:00
|
|
|
const QString &username = message->loginName;
|
2018-10-21 13:43:02 +02:00
|
|
|
if (!username.isEmpty())
|
|
|
|
{
|
2018-08-10 19:00:14 +02:00
|
|
|
// TODO: Add recent chatters display name
|
2018-05-17 13:43:01 +02:00
|
|
|
this->addRecentChatter(message);
|
2017-12-17 17:49:32 +01:00
|
|
|
}
|
|
|
|
|
2018-06-21 13:02:34 +02:00
|
|
|
// FOURTF: change this when adding more providers
|
2019-02-28 06:08:46 +01:00
|
|
|
if (this->isTwitchChannel() &&
|
|
|
|
(!overridingFlags || !overridingFlags->has(MessageFlag::DoNotLog)))
|
2018-10-21 13:43:02 +02:00
|
|
|
{
|
2018-08-02 14:23:27 +02:00
|
|
|
app->logging->addMessage(this->name_, message);
|
2018-06-21 13:02:34 +02:00
|
|
|
}
|
2017-01-26 17:26:20 +01:00
|
|
|
|
2018-10-21 13:43:02 +02:00
|
|
|
if (this->messages_.pushBack(message, deleted))
|
|
|
|
{
|
2018-05-17 13:43:01 +02:00
|
|
|
this->messageRemovedFromStart.invoke(deleted);
|
|
|
|
}
|
|
|
|
|
2018-10-05 23:33:01 +02:00
|
|
|
this->messageAppended.invoke(message, overridingFlags);
|
2018-05-17 13:43:01 +02:00
|
|
|
}
|
|
|
|
|
2018-06-28 19:38:57 +02:00
|
|
|
void Channel::addOrReplaceTimeout(MessagePtr message)
|
2018-05-17 13:43:01 +02:00
|
|
|
{
|
|
|
|
LimitedQueueSnapshot<MessagePtr> snapshot = this->getMessageSnapshot();
|
2018-11-03 21:26:57 +01:00
|
|
|
int snapshotLength = snapshot.size();
|
2018-05-17 13:43:01 +02:00
|
|
|
|
|
|
|
int end = std::max(0, snapshotLength - 20);
|
|
|
|
|
|
|
|
bool addMessage = true;
|
|
|
|
|
|
|
|
QTime minimumTime = QTime::currentTime().addSecs(-5);
|
|
|
|
|
2018-10-21 13:43:02 +02:00
|
|
|
for (int i = snapshotLength - 1; i >= end; --i)
|
|
|
|
{
|
2018-05-17 13:43:01 +02:00
|
|
|
auto &s = snapshot[i];
|
|
|
|
|
2018-10-21 13:43:02 +02:00
|
|
|
if (s->parseTime < minimumTime)
|
|
|
|
{
|
2018-05-17 13:43:01 +02:00
|
|
|
break;
|
|
|
|
}
|
2018-04-27 18:35:31 +02:00
|
|
|
|
2018-08-07 07:55:31 +02:00
|
|
|
if (s->flags.has(MessageFlag::Untimeout) &&
|
2018-10-21 13:43:02 +02:00
|
|
|
s->timeoutUser == message->timeoutUser)
|
|
|
|
{
|
2018-05-17 13:43:01 +02:00
|
|
|
break;
|
|
|
|
}
|
2018-04-27 18:35:31 +02:00
|
|
|
|
2018-08-07 07:55:31 +02:00
|
|
|
if (s->flags.has(MessageFlag::Timeout) &&
|
|
|
|
s->timeoutUser == message->timeoutUser) //
|
|
|
|
{
|
|
|
|
if (message->flags.has(MessageFlag::PubSub) &&
|
|
|
|
!s->flags.has(MessageFlag::PubSub)) //
|
|
|
|
{
|
2018-05-17 13:43:01 +02:00
|
|
|
this->replaceMessage(s, message);
|
|
|
|
addMessage = false;
|
2018-04-27 18:35:31 +02:00
|
|
|
break;
|
|
|
|
}
|
2018-08-07 07:55:31 +02:00
|
|
|
if (!message->flags.has(MessageFlag::PubSub) &&
|
|
|
|
s->flags.has(MessageFlag::PubSub)) //
|
|
|
|
{
|
2018-04-27 18:35:31 +02:00
|
|
|
addMessage = false;
|
2018-05-17 13:43:01 +02:00
|
|
|
break;
|
2018-04-27 18:35:31 +02:00
|
|
|
}
|
|
|
|
|
2018-05-17 13:43:01 +02:00
|
|
|
int count = s->count + 1;
|
2018-04-27 18:35:31 +02:00
|
|
|
|
2018-08-07 01:35:24 +02:00
|
|
|
MessageBuilder replacement(systemMessage,
|
|
|
|
message->searchText + QString(" (") +
|
|
|
|
QString::number(count) + " times)");
|
2018-05-17 13:43:01 +02:00
|
|
|
|
|
|
|
replacement->timeoutUser = message->timeoutUser;
|
|
|
|
replacement->count = count;
|
|
|
|
replacement->flags = message->flags;
|
|
|
|
|
2018-08-07 01:35:24 +02:00
|
|
|
this->replaceMessage(s, replacement.release());
|
2018-04-27 18:35:31 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-17 13:43:01 +02:00
|
|
|
// disable the messages from the user
|
2018-10-21 13:43:02 +02:00
|
|
|
for (int i = 0; i < snapshotLength; i++)
|
|
|
|
{
|
2018-05-17 13:43:01 +02:00
|
|
|
auto &s = snapshot[i];
|
2018-08-07 07:55:31 +02:00
|
|
|
if (s->flags.hasNone({MessageFlag::Timeout, MessageFlag::Untimeout}) &&
|
2018-10-21 13:43:02 +02:00
|
|
|
s->loginName == message->timeoutUser)
|
|
|
|
{
|
2018-08-07 01:35:24 +02:00
|
|
|
// FOURTF: disabled for now
|
2018-09-01 15:43:17 +02:00
|
|
|
// PAJLADA: Shitty solution described in Message.hpp
|
|
|
|
s->flags.set(MessageFlag::Disabled);
|
2018-05-17 13:43:01 +02:00
|
|
|
}
|
2017-04-12 17:46:44 +02:00
|
|
|
}
|
2017-01-26 17:26:20 +01:00
|
|
|
|
2018-10-21 13:43:02 +02:00
|
|
|
if (addMessage)
|
|
|
|
{
|
2018-05-17 13:43:01 +02:00
|
|
|
this->addMessage(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX: Might need the following line
|
|
|
|
// WindowManager::getInstance().repaintVisibleChatWidgets(this);
|
2017-04-12 17:46:44 +02:00
|
|
|
}
|
2017-01-26 17:26:20 +01:00
|
|
|
|
2018-06-22 23:44:02 +02:00
|
|
|
void Channel::disableAllMessages()
|
|
|
|
{
|
|
|
|
LimitedQueueSnapshot<MessagePtr> snapshot = this->getMessageSnapshot();
|
2018-11-03 21:26:57 +01:00
|
|
|
int snapshotLength = snapshot.size();
|
2018-10-21 13:43:02 +02:00
|
|
|
for (int i = 0; i < snapshotLength; i++)
|
|
|
|
{
|
2018-08-14 17:45:17 +02:00
|
|
|
auto &message = snapshot[i];
|
2018-10-21 13:43:02 +02:00
|
|
|
if (message->flags.hasAny({MessageFlag::System, MessageFlag::Timeout}))
|
|
|
|
{
|
2018-06-22 23:44:02 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-08-07 01:35:24 +02:00
|
|
|
// FOURTF: disabled for now
|
2018-08-14 17:45:17 +02:00
|
|
|
const_cast<Message *>(message.get())->flags.set(MessageFlag::Disabled);
|
2018-06-22 23:44:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-28 19:38:57 +02:00
|
|
|
void Channel::addMessagesAtStart(std::vector<MessagePtr> &_messages)
|
2018-01-01 22:29:21 +01:00
|
|
|
{
|
2018-08-06 21:17:03 +02:00
|
|
|
std::vector<MessagePtr> addedMessages =
|
|
|
|
this->messages_.pushFront(_messages);
|
2018-01-01 22:29:21 +01:00
|
|
|
|
2018-10-21 13:43:02 +02:00
|
|
|
if (addedMessages.size() != 0)
|
|
|
|
{
|
2018-04-03 02:55:32 +02:00
|
|
|
this->messagesAddedAtStart.invoke(addedMessages);
|
2018-01-01 22:29:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-28 19:38:57 +02:00
|
|
|
void Channel::replaceMessage(MessagePtr message, MessagePtr replacement)
|
2018-01-05 23:14:55 +01:00
|
|
|
{
|
2018-07-06 17:30:12 +02:00
|
|
|
int index = this->messages_.replaceItem(message, replacement);
|
2018-01-05 23:14:55 +01:00
|
|
|
|
2018-10-21 13:43:02 +02:00
|
|
|
if (index >= 0)
|
|
|
|
{
|
2018-04-03 02:55:32 +02:00
|
|
|
this->messageReplaced.invoke((size_t)index, replacement);
|
2018-01-05 23:14:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-07 01:35:24 +02:00
|
|
|
void Channel::addRecentChatter(const MessagePtr &message)
|
2017-12-17 17:49:32 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-09-16 00:05:06 +02:00
|
|
|
bool Channel::canSendMessage() const
|
2017-04-12 17:46:44 +02:00
|
|
|
{
|
2017-09-16 00:05:06 +02:00
|
|
|
return false;
|
2017-04-12 17:46:44 +02:00
|
|
|
}
|
2017-01-26 17:26:20 +01:00
|
|
|
|
2017-05-27 17:45:40 +02:00
|
|
|
void Channel::sendMessage(const QString &message)
|
|
|
|
{
|
2017-01-05 16:07:20 +01:00
|
|
|
}
|
2017-03-11 11:32:19 +01:00
|
|
|
|
2018-04-18 09:12:29 +02:00
|
|
|
bool Channel::isMod() const
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-06-22 23:19:52 +02:00
|
|
|
bool Channel::isBroadcaster() const
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-07-04 19:43:41 +02:00
|
|
|
bool Channel::hasModRights() const
|
|
|
|
{
|
|
|
|
// fourtf: check if staff
|
|
|
|
return this->isMod() || this->isBroadcaster();
|
|
|
|
}
|
2018-10-13 14:20:06 +02:00
|
|
|
|
2019-04-13 15:26:47 +02:00
|
|
|
bool Channel::hasHighRateLimit() const
|
|
|
|
{
|
|
|
|
return this->isMod() || this->isBroadcaster();
|
|
|
|
}
|
|
|
|
|
2018-10-13 14:20:06 +02:00
|
|
|
bool Channel::isLive() const
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2018-10-21 13:29:52 +02:00
|
|
|
|
|
|
|
bool Channel::shouldIgnoreHighlights() const
|
|
|
|
{
|
|
|
|
return this->type_ == Type::TwitchMentions ||
|
|
|
|
this->type_ == Type::TwitchWhispers;
|
|
|
|
}
|
2018-07-04 19:43:41 +02:00
|
|
|
|
2018-02-05 15:11:50 +01:00
|
|
|
std::shared_ptr<Channel> Channel::getEmpty()
|
|
|
|
{
|
2018-07-06 17:30:12 +02:00
|
|
|
static std::shared_ptr<Channel> channel(new Channel("", Type::None));
|
2018-02-05 15:11:50 +01:00
|
|
|
return channel;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Channel::onConnected()
|
|
|
|
{
|
|
|
|
}
|
2018-03-30 12:16:12 +02:00
|
|
|
|
2018-08-10 19:00:14 +02:00
|
|
|
//
|
|
|
|
// Indirect channel
|
|
|
|
//
|
|
|
|
IndirectChannel::Data::Data(ChannelPtr _channel, Channel::Type _type)
|
|
|
|
: channel(_channel)
|
|
|
|
, type(_type)
|
2018-07-14 14:24:18 +02:00
|
|
|
{
|
2018-08-10 19:00:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
IndirectChannel::IndirectChannel(ChannelPtr channel, Channel::Type type)
|
|
|
|
: data_(std::make_unique<Data>(channel, type))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ChannelPtr IndirectChannel::get()
|
|
|
|
{
|
|
|
|
return data_->channel;
|
|
|
|
}
|
|
|
|
|
|
|
|
void IndirectChannel::reset(ChannelPtr channel)
|
|
|
|
{
|
|
|
|
assert(this->data_->type != Channel::Type::Direct);
|
|
|
|
|
|
|
|
this->data_->channel = channel;
|
|
|
|
this->data_->changed.invoke();
|
|
|
|
}
|
|
|
|
|
|
|
|
pajlada::Signals::NoArgSignal &IndirectChannel::getChannelChanged()
|
|
|
|
{
|
|
|
|
return this->data_->changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
Channel::Type IndirectChannel::getType()
|
|
|
|
{
|
2018-10-21 13:43:02 +02:00
|
|
|
if (this->data_->type == Channel::Type::Direct)
|
|
|
|
{
|
2018-08-10 19:00:14 +02:00
|
|
|
return this->get()->getType();
|
2018-10-21 13:43:02 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-08-10 19:00:14 +02:00
|
|
|
return this->data_->type;
|
|
|
|
}
|
2018-07-14 14:24:18 +02:00
|
|
|
}
|
|
|
|
|
2017-04-14 17:52:22 +02:00
|
|
|
} // namespace chatterino
|