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-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-04-18 09:12:29 +02:00
|
|
|
Channel::Channel(const QString &_name, Type _type)
|
2018-05-06 14:16:41 +02:00
|
|
|
: name(_name)
|
2018-03-24 12:02:07 +01:00
|
|
|
, completionModel(this->name)
|
2018-05-06 14:16:41 +02:00
|
|
|
, type(_type)
|
2017-01-01 13:07:36 +01:00
|
|
|
{
|
2018-03-30 13:50:43 +02:00
|
|
|
this->clearCompletionModelTimer = new QTimer;
|
|
|
|
QObject::connect(this->clearCompletionModelTimer, &QTimer::timeout, [this]() {
|
|
|
|
this->completionModel.ClearExpiredStrings(); //
|
|
|
|
});
|
|
|
|
this->clearCompletionModelTimer->start(60 * 1000);
|
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-03-30 13:50:43 +02:00
|
|
|
|
|
|
|
this->clearCompletionModelTimer->stop();
|
|
|
|
this->clearCompletionModelTimer->deleteLater();
|
2018-02-05 15:11:50 +01:00
|
|
|
}
|
|
|
|
|
2018-04-18 09:12:29 +02:00
|
|
|
Channel::Type Channel::getType() const
|
|
|
|
{
|
|
|
|
return this->type;
|
|
|
|
}
|
|
|
|
|
2018-05-25 13:53:55 +02:00
|
|
|
bool Channel::isTwitchChannel() const
|
|
|
|
{
|
|
|
|
return this->type >= Twitch && this->type < TwitchEnd;
|
|
|
|
}
|
|
|
|
|
2017-04-12 17:46:44 +02:00
|
|
|
bool Channel::isEmpty() const
|
|
|
|
{
|
2018-02-05 15:11:50 +01: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
|
|
|
{
|
2017-09-21 12:15:01 +02:00
|
|
|
return this->messages.getSnapshot();
|
2017-04-12 17:46:44 +02:00
|
|
|
}
|
2017-01-26 17:26:20 +01:00
|
|
|
|
2018-01-11 20:16:25 +01:00
|
|
|
void Channel::addMessage(MessagePtr message)
|
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;
|
|
|
|
if (!username.isEmpty()) {
|
|
|
|
// TODO: Add recent chatters display name. This should maybe be a setting
|
|
|
|
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
|
|
|
|
if (this->isTwitchChannel()) {
|
|
|
|
app->logging->addMessage(this->name, message);
|
|
|
|
}
|
2017-01-26 17:26:20 +01:00
|
|
|
|
2018-05-17 13:43:01 +02:00
|
|
|
if (this->messages.pushBack(message, deleted)) {
|
|
|
|
this->messageRemovedFromStart.invoke(deleted);
|
|
|
|
}
|
|
|
|
|
|
|
|
this->messageAppended.invoke(message);
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
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;
|
|
|
|
}
|
2018-04-27 18:35:31 +02:00
|
|
|
|
2018-05-17 13:43:01 +02:00
|
|
|
if (s->flags.HasFlag(Message::Untimeout) && s->timeoutUser == message->timeoutUser) {
|
|
|
|
break;
|
|
|
|
}
|
2018-04-27 18:35:31 +02:00
|
|
|
|
2018-05-17 13:43:01 +02:00
|
|
|
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;
|
2018-04-27 18:35:31 +02:00
|
|
|
break;
|
|
|
|
}
|
2018-05-17 13:43:01 +02:00
|
|
|
if (!message->flags.HasFlag(Message::PubSub) && s->flags.HasFlag(Message::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-06-28 19:38:57 +02:00
|
|
|
MessagePtr replacement(Message::createSystemMessage(
|
2018-06-19 22:02:51 +02:00
|
|
|
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;
|
|
|
|
|
|
|
|
this->replaceMessage(s, replacement);
|
2018-04-27 18:35:31 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-17 13:43:01 +02:00
|
|
|
// 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);
|
|
|
|
}
|
2017-04-12 17:46:44 +02:00
|
|
|
}
|
2017-01-26 17:26:20 +01:00
|
|
|
|
2018-05-17 13:43:01 +02:00
|
|
|
if (addMessage) {
|
|
|
|
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();
|
|
|
|
int snapshotLength = snapshot.getLength();
|
|
|
|
for (int i = 0; i < snapshotLength; i++) {
|
|
|
|
auto &s = snapshot[i];
|
|
|
|
if (s->flags & Message::System || s->flags & Message::Timeout) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
s->flags.EnableFlag(Message::Disabled);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-28 19:38:57 +02:00
|
|
|
void Channel::addMessagesAtStart(std::vector<MessagePtr> &_messages)
|
2018-01-01 22:29:21 +01:00
|
|
|
{
|
2018-06-28 19:38:57 +02:00
|
|
|
std::vector<MessagePtr> addedMessages = this->messages.pushFront(_messages);
|
2018-01-01 22:29:21 +01: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
|
|
|
{
|
|
|
|
int index = this->messages.replaceItem(message, replacement);
|
|
|
|
|
|
|
|
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-06-28 19:38:57 +02:00
|
|
|
void Channel::addRecentChatter(const std::shared_ptr<Message> &message)
|
2017-12-17 17:49:32 +01:00
|
|
|
{
|
2018-03-30 12:16:12 +02:00
|
|
|
// Do nothing by default
|
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-02-05 15:11:50 +01:00
|
|
|
std::shared_ptr<Channel> Channel::getEmpty()
|
|
|
|
{
|
2018-04-18 09:12:29 +02:00
|
|
|
static std::shared_ptr<Channel> channel(new Channel("", None));
|
2018-02-05 15:11:50 +01:00
|
|
|
return channel;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Channel::onConnected()
|
|
|
|
{
|
|
|
|
}
|
2018-03-30 12:16:12 +02:00
|
|
|
|
2017-04-14 17:52:22 +02:00
|
|
|
} // namespace chatterino
|