fix per-guild avatars

This commit is contained in:
ouwou 2022-02-14 02:53:21 -05:00
parent 75213fcede
commit 25fd2c3840
3 changed files with 31 additions and 10 deletions

View File

@ -1,7 +1,7 @@
#include "chatmessage.hpp"
#include "abaddon.hpp"
#include "util.hpp"
#include "chatmessage.hpp"
#include "lazyimage.hpp"
#include "util.hpp"
#include <unordered_map>
constexpr static int EmojiSize = 24; // settings eventually
@ -1068,11 +1068,11 @@ ChatMessageHeader::ChatMessageHeader(const Message &data)
};
img.LoadFromURL(author->GetAvatarURL(data.GuildID), sigc::track_obj(cb, *this));
if (author->HasAnimatedAvatar()) {
if (author->HasAnimatedAvatar(data.GuildID)) {
auto cb = [this](const Glib::RefPtr<Gdk::PixbufAnimation> &pb) {
m_anim_avatar = pb;
};
img.LoadAnimationFromURL(author->GetAvatarURL("gif"), AvatarSize, AvatarSize, sigc::track_obj(cb, *this));
img.LoadAnimationFromURL(author->GetAvatarURL(data.GuildID, "gif"), AvatarSize, AvatarSize, sigc::track_obj(cb, *this));
}
get_style_context()->add_class("message-container");

View File

@ -6,22 +6,41 @@ bool UserData::IsDeleted() const {
}
bool UserData::HasAvatar() const {
return Avatar.size() > 0;
return !Avatar.empty();
}
bool UserData::HasAnimatedAvatar() const {
return Avatar.size() > 0 && Avatar[0] == 'a' && Avatar[1] == '_';
bool UserData::HasAnimatedAvatar() const noexcept {
return !Avatar.empty() && Avatar[0] == 'a' && Avatar[1] == '_';
}
bool UserData::HasAnimatedAvatar(Snowflake guild_id) const {
const auto member = Abaddon::Get().GetDiscordClient().GetMember(ID, guild_id);
if (member.has_value() && member->Avatar.has_value() && member->Avatar.value()[0] == 'a' && member->Avatar.value()[1] == '_')
return true;
else if (!member->Avatar.has_value())
return HasAnimatedAvatar();
return false;
}
bool UserData::HasAnimatedAvatar(const std::optional<Snowflake> &guild_id) const {
if (guild_id.has_value())
return HasAnimatedAvatar(*guild_id);
else
return HasAnimatedAvatar();
}
std::string UserData::GetAvatarURL(Snowflake guild_id, std::string ext, std::string size) const {
const auto member = Abaddon::Get().GetDiscordClient().GetMember(ID, guild_id);
if (member.has_value() && member->Avatar.has_value())
if (member.has_value() && member->Avatar.has_value()) {
if (ext == "gif" && !(member->Avatar.value()[0] == 'a' && member->Avatar.value()[1] == '_'))
return GetAvatarURL(ext, size);
return "https://cdn.discordapp.com/guilds/" +
std::to_string(guild_id) + "/users/" + std::to_string(ID) +
"/avatars/" + *member->Avatar + "." +
ext + "?" + "size=" + size;
else
} else {
return GetAvatarURL(ext, size);
}
}
std::string UserData::GetAvatarURL(const std::optional<Snowflake> &guild_id, std::string ext, std::string size) const {

View File

@ -62,7 +62,9 @@ struct UserData {
bool IsDeleted() const;
bool HasAvatar() const;
bool HasAnimatedAvatar() const;
bool HasAnimatedAvatar() const noexcept;
bool HasAnimatedAvatar(Snowflake guild_id) const;
bool HasAnimatedAvatar(const std::optional<Snowflake> &guild_id) const;
std::string GetAvatarURL(Snowflake guild_id, std::string ext = "png", std::string size = "32") const;
std::string GetAvatarURL(const std::optional<Snowflake> &guild_id, std::string ext = "png", std::string size = "32") const;
std::string GetAvatarURL(std::string ext = "png", std::string size = "32") const;