fix guild positioning

This commit is contained in:
ouwou 2020-10-13 23:54:50 -04:00
parent 00770e4bcb
commit ae85d7e441
3 changed files with 35 additions and 34 deletions

View File

@ -62,40 +62,24 @@ const User &DiscordClient::GetUserData() const {
}
std::vector<Snowflake> DiscordClient::GetUserSortedGuilds() const {
std::vector<std::pair<Snowflake, const Guild *>> sorted_guilds;
if (m_user_settings.GuildPositions.size()) {
std::unordered_set<Snowflake> positioned_guilds(m_user_settings.GuildPositions.begin(), m_user_settings.GuildPositions.end());
// guilds not in the guild_positions object are at the top of the list, descending by guild ID
std::set<Snowflake> unpositioned_guilds;
for (const auto &[id, guild] : m_store.GetGuilds()) {
if (positioned_guilds.find(id) == positioned_guilds.end())
unpositioned_guilds.insert(id);
// sort order is unfolder'd guilds sorted by id descending, then guilds in folders in array order
// todo: make sure folder'd guilds are sorted properly
std::vector<Snowflake> folder_order;
auto guilds = GetGuildsID();
for (const auto &entry : m_user_settings.GuildFolders) { // can contain guilds not a part of
for (const auto &id : entry.GuildIDs) {
if (std::find(guilds.begin(), guilds.end(), id) != guilds.end())
folder_order.push_back(id);
}
// unpositioned_guilds now has unpositioned guilds in ascending order
for (auto it = unpositioned_guilds.rbegin(); it != unpositioned_guilds.rend(); it++) {
auto *data = m_store.GetGuild(*it);
if (data != nullptr)
sorted_guilds.push_back(std::make_pair(*it, data));
}
// now the rest go at the end in the order they are sorted
for (const auto &id : m_user_settings.GuildPositions) {
auto *data = m_store.GetGuild(id);
if (data != nullptr)
sorted_guilds.push_back(std::make_pair(id, data));
}
} else { // default sort is alphabetic
for (auto &it : m_store.GetGuilds())
sorted_guilds.push_back(std::make_pair(it.first, &it.second));
AlphabeticalSort(sorted_guilds.begin(), sorted_guilds.end(), [](auto &pair) { return pair.second->Name; });
}
std::vector<Snowflake> ret;
for (const auto &pair : sorted_guilds)
ret.push_back(pair.first);
for (const auto &gid : guilds) {
if (std::find(folder_order.begin(), folder_order.end(), gid) == folder_order.end()) {
ret.push_back(gid);
}
}
for (const auto &gid : folder_order)
ret.push_back(gid);
return ret;
}

View File

@ -1,5 +1,13 @@
#include "usersettings.hpp"
void from_json(const nlohmann::json &j, UserSettingsGuildFoldersEntry &m) {
JS_N("color", m.Color);
JS_D("guild_ids", m.GuildIDs);
JS_N("id", m.ID);
JS_N("name", m.Name);
}
void from_json(const nlohmann::json &j, UserSettings &m) {
JS_D("timezone_offset", m.TimezoneOffset);
JS_D("theme", m.Theme);
@ -15,7 +23,7 @@ void from_json(const nlohmann::json &j, UserSettings &m) {
JS_D("inline_embed_media", m.ShouldInlineEmbedMedia);
JS_D("inline_attachment_media", m.ShouldInlineAttachmentMedia);
JS_D("guild_positions", m.GuildPositions);
// JS_D("guild_folders", m.GuildFolders);
JS_D("guild_folders", m.GuildFolders);
JS_D("gif_auto_play", m.ShouldGIFAutoplay);
// JS_D("friend_source_flags", m.FriendSourceFlags);
JS_D("explicit_content_filter", m.ExplicitContentFilter);

View File

@ -3,6 +3,15 @@
#include "snowflake.hpp"
#include <string>
struct UserSettingsGuildFoldersEntry {
int Color = -1; // null
std::vector<Snowflake> GuildIDs;
int ID = -1; // null
std::string Name; // null
friend void from_json(const nlohmann::json &j, UserSettingsGuildFoldersEntry &m);
};
struct UserSettings {
int TimezoneOffset; //
std::string Theme; //
@ -17,8 +26,8 @@ struct UserSettings {
std::string Locale; //
bool ShouldInlineEmbedMedia; //
bool ShouldInlineAttachmentMedia; //
std::vector<Snowflake> GuildPositions; //
// std::vector<GuildFolderEntryData> GuildFolders; //
std::vector<Snowflake> GuildPositions; // deprecated?
std::vector<UserSettingsGuildFoldersEntry> GuildFolders; //
bool ShouldGIFAutoplay; //
// Unknown FriendSourceFlags; //
int ExplicitContentFilter; //