mirror-chatterino2/channel.cpp

147 lines
4.6 KiB
C++
Raw Normal View History

#include "channel.h"
2017-01-26 17:48:14 +01:00
#include "emotes.h"
#include "logging/loggingmanager.h"
2017-01-18 21:30:23 +01:00
#include "messages/message.h"
#include "windows.h"
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-01-18 21:30:23 +01:00
namespace chatterino {
Channel::Channel(const QString &channel)
2017-01-18 04:33:30 +01: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))
{
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-01-26 17:26:20 +01:00
void
Channel::reloadBttvEmotes()
{
2017-01-26 17:48:14 +01:00
// bttv
2017-01-26 17:26:20 +01:00
QNetworkAccessManager *manager = new QNetworkAccessManager();
2017-01-26 17:48:14 +01:00
QUrl url("https://api.betterttv.net/2/channels/" + this->name);
2017-01-26 17:26:20 +01:00
QNetworkRequest request(url);
QNetworkReply *reply = manager->get(request);
QObject::connect(reply, &QNetworkReply::finished, [=] {
if (reply->error() == QNetworkReply::NetworkError::NoError) {
QByteArray data = reply->readAll();
QJsonDocument jsonDoc(QJsonDocument::fromJson(data));
QJsonObject root = jsonDoc.object();
2017-01-26 17:48:14 +01:00
auto emotes = root.value("emotes").toArray();
2017-01-26 17:26:20 +01:00
2017-01-26 17:48:14 +01:00
QString _template = "https:" + root.value("urlTemplate").toString();
2017-01-26 17:26:20 +01:00
2017-01-26 17:48:14 +01:00
for (const QJsonValue &emote : emotes) {
QString id = emote.toObject().value("id").toString();
QString code = emote.toObject().value("code").toString();
// emote.value("imageType").toString();
2017-01-26 17:26:20 +01:00
2017-01-26 17:48:14 +01:00
QString tmp = _template;
tmp.detach();
QString url =
tmp.replace("{{id}}", id).replace("{{image}}", "1x");
2017-01-26 17:26:20 +01:00
2017-01-26 17:48:14 +01:00
this->getBttvChannelEmotes().insert(
2017-03-11 11:39:59 +01:00
code, Emotes::getBttvChannelEmoteFromCaches().getOrAdd(
id, [&code, &url] {
return new messages::LazyLoadedImage(
url, 1, code,
code + "\nChannel Bttv Emote");
}));
2017-01-26 17:26:20 +01:00
}
}
reply->deleteLater();
manager->deleteLater();
});
}
void
Channel::reloadFfzEmotes()
{
QNetworkAccessManager *manager = new QNetworkAccessManager();
2017-01-26 18:22:04 +01:00
QUrl url("http://api.frankerfacez.com/v1/room/" + this->name);
2017-01-26 17:26:20 +01:00
QNetworkRequest request(url);
QNetworkReply *reply = manager->get(request);
QObject::connect(reply, &QNetworkReply::finished, [=] {
if (reply->error() == QNetworkReply::NetworkError::NoError) {
QByteArray data = reply->readAll();
QJsonDocument jsonDoc(QJsonDocument::fromJson(data));
QJsonObject root = jsonDoc.object();
auto sets = root.value("sets").toObject();
for (const QJsonValue &set : sets) {
auto emoticons = set.toObject().value("emoticons").toArray();
for (const QJsonValue &emote : emoticons) {
QJsonObject object = emote.toObject();
// margins
int id = object.value("id").toInt();
QString code = object.value("name").toString();
QJsonObject urls = object.value("urls").toObject();
QString url1 = "http:" + urls.value("1").toString();
2017-01-26 18:22:04 +01:00
this->getFfzChannelEmotes().insert(
2017-03-11 11:39:59 +01:00
code, Emotes::getFfzChannelEmoteFromCaches().getOrAdd(
id, [&code, &url1] {
return new messages::LazyLoadedImage(
url1, 1, code,
code + "\nGlobal Ffz Emote");
}));
2017-01-26 17:26:20 +01:00
}
}
}
reply->deleteLater();
manager->deleteLater();
});
}
2017-01-11 18:52:09 +01:00
void
2017-01-18 21:52:36 +01:00
Channel::addMessage(std::shared_ptr<messages::Message> message)
2017-01-05 16:07:20 +01:00
{
2017-02-02 20:35:12 +01:00
std::shared_ptr<messages::Message> deleted;
if (this->loggingChannel.get() != nullptr) {
this->loggingChannel->append(message);
}
2017-02-02 20:35:12 +01:00
if (this->messages.appendItem(message, deleted)) {
this->messageRemovedFromStart(deleted);
}
this->messageAppended(message);
2017-02-02 20:35:12 +01:00
Windows::repaintVisibleChatWidgets(this);
2017-01-05 16:07:20 +01:00
}
} // namespace chatterino