mirror-chatterino2/src/channel.cpp

169 lines
4.2 KiB
C++
Raw Normal View History

2017-06-11 09:31:45 +02:00
#include "channel.hpp"
#include "application.hpp"
#include "debug/log.hpp"
2017-06-11 09:31:45 +02:00
#include "messages/message.hpp"
#include "singletons/emotemanager.hpp"
#include "singletons/ircmanager.hpp"
#include "singletons/loggingmanager.hpp"
2017-12-31 00:50:07 +01:00
#include "singletons/windowmanager.hpp"
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
using namespace chatterino::messages;
2017-04-12 17:46:44 +02: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)
, completionModel(this->name)
2018-05-06 14:16:41 +02:00
, type(_type)
{
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;
}
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
}
messages::LimitedQueueSnapshot<messages::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
void Channel::addMessage(MessagePtr message)
2017-04-12 17:46:44 +02:00
{
auto app = getApp();
MessagePtr deleted;
2017-01-26 17:26:20 +01:00
bool isTimeout = (message->flags & Message::Timeout) != 0;
if (!isTimeout) {
const QString &username = message->loginName;
if (!username.isEmpty()) {
// TODO: Add recent chatters display name. This should maybe be a setting
this->addRecentChatter(message);
}
}
app->logging->addMessage(this->name, message);
2017-01-26 17:26:20 +01:00
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)) {
this->messageRemovedFromStart.invoke(deleted);
2017-04-12 17:46:44 +02:00
}
2017-01-26 17:26:20 +01:00
this->messageAppended.invoke(message);
2017-04-12 17:46:44 +02:00
}
2017-01-26 17:26:20 +01:00
void Channel::addMessagesAtStart(std::vector<messages::MessagePtr> &_messages)
{
std::vector<messages::MessagePtr> addedMessages = this->messages.pushFront(_messages);
if (addedMessages.size() != 0) {
this->messagesAddedAtStart.invoke(addedMessages);
}
}
void Channel::replaceMessage(messages::MessagePtr message, messages::MessagePtr replacement)
2018-01-05 23:14:55 +01:00
{
int index = this->messages.replaceItem(message, replacement);
if (index >= 0) {
this->messageReplaced.invoke((size_t)index, replacement);
2018-01-05 23:14:55 +01:00
}
}
void Channel::addRecentChatter(const std::shared_ptr<messages::Message> &message)
{
// Do nothing by default
}
bool Channel::canSendMessage() const
2017-04-12 17:46:44 +02:00
{
return false;
2017-04-12 17:46:44 +02:00
}
2017-01-26 17:26:20 +01:00
void Channel::sendMessage(const QString &message)
{
2017-01-05 16:07:20 +01:00
}
2018-04-18 09:12:29 +02:00
bool Channel::isMod() 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()
{
}
2017-04-14 17:52:22 +02:00
} // namespace chatterino