mirror-chatterino2/src/providers/twitch/TwitchBadges.cpp

82 lines
2.5 KiB
C++
Raw Normal View History

2018-08-02 14:23:27 +02:00
#include "TwitchBadges.hpp"
#include <QJsonArray>
#include <QJsonObject>
#include <QJsonValue>
#include <QThread>
2018-08-14 17:45:17 +02:00
2018-08-02 14:23:27 +02:00
#include "common/NetworkRequest.hpp"
#include "common/Outcome.hpp"
2018-08-14 17:45:17 +02:00
#include "debug/Log.hpp"
#include "messages/Emote.hpp"
2018-08-02 14:23:27 +02:00
namespace chatterino {
void TwitchBadges::loadTwitchBadges()
{
2018-08-06 21:17:03 +02:00
static QString url(
"https://badges.twitch.tv/v1/badges/global/display?language=en");
2018-08-02 14:23:27 +02:00
2019-08-20 21:50:36 +02:00
NetworkRequest(url)
Improvements to Message Search (#1237) * Ran clang-format * Implement user-specific search in message history This functionality was originally requested in #1236. This commit changes the SearchPopup::performSearch method so that only messages from specific users can be shown. In order to filter for a specific user, enter their username with a leading '@' in the search popup. You can also add an additional search phrase which will also be considered in the search. * Naive implementation for "from:" tags Rebase later? * Cleverer (?) version using Predicates Commit adds two POC predicates: one for the author of messages, and one for substring search in messages. Problems/TODOs: * Best way to register new predicates? * Clean up tags (e.g. "from:") or not? * Test combinations of different predicates * Add a predicate to check for links in messages * Remove a dumb TODO * Rewrite SearchPopup::performSearch to be cleaner * Ran clang-format on all files * Remove TODO I missed earlier * Forgot to run clang-format peepoSadDank * Re-use {}-initialization Was accidentally removed when fixing earlier merge conflict. * Does this fix line endings? No diffs are shown locally, hopefully Git doesn't lie to me. * Rename "predicates" directory to "search" Resolving one conversation in the review of #1237. * Use LinkParser in LinkPredicate Resolving a conversation in the review of #1237. * Predicates: Use unique_ptr instead of shared_ptr Resolves a conversation in the review of #1237. * Refactor of SearchPopup and AuthorPredicate Resolving some points from the review in #1237. * Moved parsing of comma-seperated values into AuthorPredicate constructor. * Rewrite SearchPopup::parsePredicates as suggested. * Deleted now redundant methods in SearchPopup. * MessagePredicate::appliesTo now takes a Message& ... instead of a MessagePtr. This resolves a conversation in the review of #1237. * Run clang-format on two files I missed * AuthorPredicate: Check for displayName & loginName Resolving conversation on #1237.
2019-09-09 15:21:49 +02:00
2019-08-20 21:50:36 +02:00
.onSuccess([this](auto result) -> Outcome {
auto root = result.parseJson();
auto badgeSets = this->badgeSets_.access();
2018-08-02 14:23:27 +02:00
2019-08-20 21:50:36 +02:00
auto jsonSets = root.value("badge_sets").toObject();
for (auto sIt = jsonSets.begin(); sIt != jsonSets.end(); ++sIt)
2018-10-21 13:43:02 +02:00
{
2019-08-20 21:50:36 +02:00
auto key = sIt.key();
auto versions =
sIt.value().toObject().value("versions").toObject();
2018-08-06 21:17:03 +02:00
2019-08-20 21:50:36 +02:00
for (auto vIt = versions.begin(); vIt != versions.end(); ++vIt)
{
auto versionObj = vIt.value().toObject();
2018-08-02 14:23:27 +02:00
2019-08-20 21:50:36 +02:00
auto emote = Emote{
{""},
ImageSet{
Image::fromUrl(
{versionObj.value("image_url_1x").toString()},
1),
Image::fromUrl(
{versionObj.value("image_url_2x").toString()},
.5),
Image::fromUrl(
{versionObj.value("image_url_4x").toString()},
.25),
},
Tooltip{versionObj.value("description").toString()},
Url{versionObj.value("click_url").toString()}};
// "title"
// "clickAction"
2018-08-02 14:23:27 +02:00
2019-08-20 21:50:36 +02:00
(*badgeSets)[key][vIt.key()] =
std::make_shared<Emote>(emote);
}
}
2018-08-02 14:23:27 +02:00
2019-08-20 21:50:36 +02:00
return Success;
})
.execute();
2018-08-02 14:23:27 +02:00
}
2018-08-14 17:45:17 +02:00
boost::optional<EmotePtr> TwitchBadges::badge(const QString &set,
const QString &version) const
{
auto badgeSets = this->badgeSets_.access();
auto it = badgeSets->find(set);
2018-10-21 13:43:02 +02:00
if (it != badgeSets->end())
{
2018-08-14 17:45:17 +02:00
auto it2 = it->second.find(version);
2018-10-21 13:43:02 +02:00
if (it2 != it->second.end())
{
2018-08-14 17:45:17 +02:00
return it2->second;
}
}
return boost::none;
}
2018-08-02 14:23:27 +02:00
} // namespace chatterino