mirror-chatterino2/channel.cpp

203 lines
5.1 KiB
C++
Raw Normal View History

#include "channel.h"
2017-04-12 17:46:44 +02:00
#include "emotemanager.h"
#include "ircmanager.h"
#include "logging/loggingmanager.h"
2017-01-18 21:30:23 +01:00
#include "messages/message.h"
2017-04-12 17:46:44 +02:00
#include "util/urlfetch.h"
#include "windowmanager.h"
#include <QDebug>
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-01-11 01:08:20 +01:00
#include <memory>
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
Channel::Channel(const QString &channel)
2017-04-12 17:46:44 +02:00
: _messages()
, _name((channel.length() > 0 && channel[0] == '#') ? channel.mid(1) : channel)
, _bttvChannelEmotes()
, _ffzChannelEmotes()
, _subLink("https://www.twitch.tv/" + _name + "/subscribe?ref=in_chat_subscriber_link")
, _channelLink("https://twitch.tv/" + _name)
, _popoutPlayerLink("https://player.twitch.tv/?channel=" + _name)
// , _loggingChannel(logging::get(_name))
{
qDebug() << "Open channel:" << channel << ". Name: " << _name;
printf("Channel pointer: %p\n", this);
2017-01-26 18:22:04 +01:00
reloadChannelEmotes();
2017-01-07 20:43:55 +01:00
}
2017-01-05 16:07:20 +01:00
2017-04-12 17:46:44 +02:00
//
// properties
//
ConcurrentMap<QString, messages::LazyLoadedImage *> &Channel::getBttvChannelEmotes()
2017-01-26 17:26:20 +01:00
{
2017-04-12 17:46:44 +02:00
return _bttvChannelEmotes;
}
2017-01-26 17:26:20 +01:00
2017-04-12 17:46:44 +02:00
ConcurrentMap<QString, messages::LazyLoadedImage *> &Channel::getFfzChannelEmotes()
{
return _ffzChannelEmotes;
}
2017-01-26 17:26:20 +01:00
2017-04-12 17:46:44 +02:00
bool Channel::isEmpty() const
{
return _name.isEmpty();
}
2017-01-26 17:26:20 +01:00
2017-04-12 17:46:44 +02:00
const QString &Channel::getName() const
{
return _name;
}
int Channel::getRoomID() const
{
return _roomID;
}
2017-01-26 17:26:20 +01:00
2017-04-12 17:46:44 +02:00
const QString &Channel::getSubLink() const
{
return _subLink;
}
2017-01-26 17:26:20 +01:00
2017-04-12 17:46:44 +02:00
const QString &Channel::getChannelLink() const
{
return _channelLink;
}
2017-01-26 17:26:20 +01:00
2017-04-12 17:46:44 +02:00
const QString &Channel::getPopoutPlayerLink() const
{
return _popoutPlayerLink;
}
2017-01-26 17:26:20 +01:00
2017-04-12 17:46:44 +02:00
bool Channel::getIsLive() const
{
return _isLive;
}
2017-01-26 17:26:20 +01:00
2017-04-12 17:46:44 +02:00
int Channel::getStreamViewerCount() const
{
return _streamViewerCount;
}
2017-01-26 17:26:20 +01:00
2017-04-12 17:46:44 +02:00
const QString &Channel::getStreamStatus() const
{
return _streamStatus;
}
const QString &Channel::getStreamGame() const
{
return _streamGame;
2017-01-26 17:26:20 +01:00
}
2017-04-12 17:46:44 +02:00
messages::LimitedQueueSnapshot<messages::SharedMessage> Channel::getMessageSnapshot()
2017-01-26 17:26:20 +01:00
{
2017-04-12 17:46:44 +02:00
return _messages.getSnapshot();
}
2017-01-26 17:26:20 +01:00
2017-04-12 17:46:44 +02:00
//
// methods
//
void Channel::addMessage(std::shared_ptr<Message> message)
{
std::shared_ptr<Message> deleted;
2017-01-26 17:26:20 +01:00
2017-04-12 17:46:44 +02:00
// if (_loggingChannel.get() != nullptr) {
// _loggingChannel->append(message);
// }
2017-01-26 17:26:20 +01:00
2017-04-12 17:46:44 +02:00
if (_messages.appendItem(message, deleted)) {
messageRemovedFromStart(deleted);
}
2017-01-26 17:26:20 +01:00
2017-04-12 17:46:44 +02:00
this->messageAppended(message);
2017-01-26 17:26:20 +01:00
2017-04-13 19:25:33 +02:00
WindowManager::getInstance().repaintVisibleChatWidgets(this);
2017-04-12 17:46:44 +02:00
}
2017-01-26 17:26:20 +01:00
2017-04-12 17:46:44 +02:00
// private methods
void Channel::reloadChannelEmotes()
{
reloadBttvEmotes();
reloadFfzEmotes();
}
2017-01-26 17:26:20 +01:00
void Channel::sendMessage(const QString &message)
{
qDebug() << "Channel send message: " << message;
IrcManager &instance = IrcManager::getInstance();
instance.sendMessage(_name, message);
}
2017-04-12 17:46:44 +02:00
void Channel::reloadBttvEmotes()
{
util::urlJsonFetch(
"https://api.betterttv.net/2/channels/" + _name, [this](QJsonObject &rootNode) {
auto emotesNode = rootNode.value("emotes").toArray();
2017-01-26 17:26:20 +01:00
2017-04-12 17:46:44 +02:00
QString linkTemplate = "https:" + rootNode.value("urlTemplate").toString();
2017-01-26 17:26:20 +01:00
2017-04-12 17:46:44 +02:00
for (const QJsonValue &emoteNode : emotesNode) {
QJsonObject emoteObject = emoteNode.toObject();
2017-01-26 17:26:20 +01:00
2017-04-12 17:46:44 +02:00
QString id = emoteObject.value("id").toString();
QString code = emoteObject.value("code").toString();
// emoteObject.value("imageType").toString();
2017-01-26 17:26:20 +01:00
2017-04-12 17:46:44 +02:00
QString link = linkTemplate;
link.detach();
link = link.replace("{{id}}", id).replace("{{image}}", "1x");
auto emote = EmoteManager::getInstance().getBttvChannelEmoteFromCaches().getOrAdd(
id, [&code, &link] {
return new LazyLoadedImage(link, 1, code, code + "\nChannel Bttv Emote");
});
this->getBttvChannelEmotes().insert(code, emote);
}
});
2017-01-26 17:26:20 +01:00
}
2017-04-12 17:46:44 +02:00
void Channel::reloadFfzEmotes()
2017-01-05 16:07:20 +01:00
{
2017-04-12 17:46:44 +02:00
util::urlJsonFetch("http://api.frankerfacez.com/v1/room/" + _name, [this](
QJsonObject &rootNode) {
auto setsNode = rootNode.value("sets").toObject();
2017-02-02 20:35:12 +01:00
2017-04-12 17:46:44 +02:00
for (const QJsonValue &setNode : setsNode) {
auto emotesNode = setNode.toObject().value("emoticons").toArray();
2017-04-12 17:46:44 +02:00
for (const QJsonValue &emoteNode : emotesNode) {
QJsonObject emoteObject = emoteNode.toObject();
2017-02-02 20:35:12 +01:00
2017-04-12 17:46:44 +02:00
// margins
2017-04-12 17:46:44 +02:00
int id = emoteObject.value("id").toInt();
QString code = emoteObject.value("name").toString();
QJsonObject urls = emoteObject.value("urls").toObject();
QString url1 = "http:" + urls.value("1").toString();
auto emote = EmoteManager::getInstance().getFfzChannelEmoteFromCaches().getOrAdd(
id, [&code, &url1] {
return new LazyLoadedImage(url1, 1, code, code + "\nGlobal Ffz Emote");
});
getFfzChannelEmotes().insert(code, emote);
}
}
});
2017-01-05 16:07:20 +01:00
}
2017-04-14 17:52:22 +02:00
} // namespace chatterino