put PermissionOverwrite in the store where it belongs

This commit is contained in:
ouwou 2020-09-24 02:43:27 -04:00
parent 8341781918
commit a8630f53b1
7 changed files with 58 additions and 18 deletions

View File

@ -165,10 +165,15 @@ void ChatMessageItem::AttachMenuHandler(Gtk::Widget *widget) {
void ChatMessageItem::ShowMenu(const GdkEvent *event) {
const auto &client = Abaddon::Get().GetDiscordClient();
const auto *data = client.GetMessage(ID);
const bool can_edit = client.GetUserData().ID == data->Author.ID;
const bool can_delete = can_edit || client.HasChannelPermission(client.GetUserData().ID, ChannelID, Permission::MANAGE_MESSAGES);
m_menu_delete_message->set_sensitive(can_delete);
m_menu_edit_message->set_sensitive(can_edit);
if (data->IsDeleted()) {
m_menu_delete_message->set_sensitive(false);
m_menu_edit_message->set_sensitive(false);
} else {
const bool can_edit = client.GetUserData().ID == data->Author.ID;
const bool can_delete = can_edit || client.HasChannelPermission(client.GetUserData().ID, ChannelID, Permission::MANAGE_MESSAGES);
m_menu_delete_message->set_sensitive(can_delete);
m_menu_edit_message->set_sensitive(can_edit);
}
m_menu.popup_at_pointer(event);
}

View File

@ -1,3 +1,4 @@
#include "../abaddon.hpp"
#include "channel.hpp"
void from_json(const nlohmann::json &j, Channel &m) {
@ -21,9 +22,6 @@ void from_json(const nlohmann::json &j, Channel &m) {
JS_ON("last_pin_timestamp", m.LastPinTimestamp);
}
std::optional<PermissionOverwrite> Channel::GetOverwrite(Snowflake id) const {
auto ret = std::find_if(PermissionOverwrites.begin(), PermissionOverwrites.end(), [id](const auto x) { return x.ID == id; });
if (ret != PermissionOverwrites.end())
return *ret;
return std::nullopt;
const PermissionOverwrite *Channel::GetOverwrite(Snowflake id) const {
return Abaddon::Get().GetDiscordClient().GetPermissionOverwrite(ID, id);
}

View File

@ -3,7 +3,6 @@
#include "json.hpp"
#include "user.hpp"
#include "permissions.hpp"
#include <optional>
#include <string>
#include <vector>
@ -39,5 +38,5 @@ struct Channel {
friend void from_json(const nlohmann::json &j, Channel &m);
std::optional<PermissionOverwrite> GetOverwrite(Snowflake id) const;
const PermissionOverwrite *GetOverwrite(Snowflake id) const;
};

View File

@ -198,6 +198,10 @@ const GuildMember *DiscordClient::GetMember(Snowflake user_id, Snowflake guild_i
return m_store.GetGuildMemberData(guild_id, user_id);
}
const PermissionOverwrite *DiscordClient::GetPermissionOverwrite(Snowflake channel_id, Snowflake id) const {
return m_store.GetPermissionOverwrite(channel_id, id);
}
Snowflake DiscordClient::GetMemberHoistedRole(Snowflake guild_id, Snowflake user_id, bool with_color) const {
auto *data = m_store.GetGuildMemberData(guild_id, user_id);
if (data == nullptr) return Snowflake::Invalid;
@ -285,8 +289,8 @@ Permission DiscordClient::ComputeOverwrites(Permission base, Snowflake member_id
return Permission::NONE;
Permission perms = base;
auto overwrite_everyone = channel->GetOverwrite(channel->GuildID);
if (overwrite_everyone.has_value()) {
const auto *overwrite_everyone = GetPermissionOverwrite(channel_id, channel->GuildID);
if (overwrite_everyone != nullptr) {
perms &= ~overwrite_everyone->Deny;
perms |= overwrite_everyone->Allow;
}
@ -294,8 +298,8 @@ Permission DiscordClient::ComputeOverwrites(Permission base, Snowflake member_id
Permission allow = Permission::NONE;
Permission deny = Permission::NONE;
for (const auto role_id : member->Roles) {
const auto overwrite = channel->GetOverwrite(role_id);
if (overwrite.has_value()) {
const auto *overwrite = GetPermissionOverwrite(channel_id, role_id);
if (overwrite != nullptr) {
allow |= overwrite->Allow;
deny |= overwrite->Deny;
}
@ -304,8 +308,8 @@ Permission DiscordClient::ComputeOverwrites(Permission base, Snowflake member_id
perms &= ~deny;
perms |= allow;
const auto member_overwrite = channel->GetOverwrite(member_id);
if (member_overwrite.has_value()) {
const auto *member_overwrite = GetPermissionOverwrite(channel_id, member_id);
if (member_overwrite != nullptr) {
perms &= ~member_overwrite->Deny;
perms |= member_overwrite->Allow;
}
@ -469,6 +473,9 @@ void DiscordClient::ProcessNewGuild(Guild &guild) {
for (auto &c : guild.Channels) {
c.GuildID = guild.ID;
m_store.SetChannel(c.ID, c);
for (auto& p : c.PermissionOverwrites) {
m_store.SetPermissionOverwrite(c.ID, p.ID, p);
}
}
for (auto &r : guild.Roles)

View File

@ -59,6 +59,7 @@ public:
using users_type = Store::users_type;
using roles_type = Store::roles_type;
using members_type = Store::members_type;
using permission_overwrites_type = Store::permission_overwrites_type;
std::unordered_set<Snowflake> GetGuildsID() const;
const guilds_type &GetGuilds() const;
@ -78,6 +79,7 @@ public:
const Role *GetRole(Snowflake id) const;
const Guild *GetGuild(Snowflake id) const;
const GuildMember *GetMember(Snowflake user_id, Snowflake guild_id) const;
const PermissionOverwrite *GetPermissionOverwrite(Snowflake channel_id, Snowflake id) const;
Snowflake GetMemberHoistedRole(Snowflake guild_id, Snowflake user_id, bool with_color = false) const;
std::unordered_set<Snowflake> GetUsersInGuild(Snowflake id) const;
std::unordered_set<Snowflake> GetRolesInGuild(Snowflake id) const;

View File

@ -24,6 +24,10 @@ void Store::SetGuildMemberData(Snowflake guild_id, Snowflake user_id, const Guil
m_members[guild_id][user_id] = data;
}
void Store::SetPermissionOverwrite(Snowflake channel_id, Snowflake id, const PermissionOverwrite &perm) {
m_permissions[channel_id][id] = perm;
}
User *Store::GetUser(Snowflake id) {
auto it = m_users.find(id);
if (it == m_users.end())
@ -104,6 +108,16 @@ GuildMember *Store::GetGuildMemberData(Snowflake guild_id, Snowflake user_id) {
return &mit->second;
}
PermissionOverwrite *Store::GetPermissionOverwrite(Snowflake channel_id, Snowflake id) {
auto cit = m_permissions.find(channel_id);
if (cit == m_permissions.end())
return nullptr;
auto pit = cit->second.find(id);
if (pit == cit->second.end())
return nullptr;
return &pit->second;
}
const GuildMember *Store::GetGuildMemberData(Snowflake guild_id, Snowflake user_id) const {
auto git = m_members.find(guild_id);
if (git == m_members.end())
@ -114,6 +128,16 @@ const GuildMember *Store::GetGuildMemberData(Snowflake guild_id, Snowflake user_
return &mit->second;
}
const PermissionOverwrite *Store::GetPermissionOverwrite(Snowflake channel_id, Snowflake id) const {
auto cit = m_permissions.find(channel_id);
if (cit == m_permissions.end())
return nullptr;
auto pit = cit->second.find(id);
if (pit == cit->second.end())
return nullptr;
return &pit->second;
}
void Store::ClearGuild(Snowflake id) {
m_guilds.erase(id);
}

View File

@ -15,6 +15,7 @@ public:
void SetRole(Snowflake id, const Role &role);
void SetMessage(Snowflake id, const Message &message);
void SetGuildMemberData(Snowflake guild_id, Snowflake user_id, const GuildMember &data);
void SetPermissionOverwrite(Snowflake channel_id, Snowflake id, const PermissionOverwrite &perm);
User *GetUser(Snowflake id);
Channel *GetChannel(Snowflake id);
@ -22,12 +23,14 @@ public:
Role *GetRole(Snowflake id);
Message *GetMessage(Snowflake id);
GuildMember *GetGuildMemberData(Snowflake guild_id, Snowflake user_id);
PermissionOverwrite *GetPermissionOverwrite(Snowflake channel_id, Snowflake id);
const User *GetUser(Snowflake id) const;
const Channel *GetChannel(Snowflake id) const;
const Guild *GetGuild(Snowflake id) const;
const Role *GetRole(Snowflake id) const;
const Message *GetMessage(Snowflake id) const;
const GuildMember *GetGuildMemberData(Snowflake guild_id, Snowflake user_id) const;
const PermissionOverwrite *GetPermissionOverwrite(Snowflake channel_id, Snowflake id) const;
void ClearGuild(Snowflake id);
void ClearChannel(Snowflake id);
@ -37,7 +40,8 @@ public:
using guilds_type = std::unordered_map<Snowflake, Guild>;
using roles_type = std::unordered_map<Snowflake, Role>;
using messages_type = std::unordered_map<Snowflake, Message>;
using members_type = std::unordered_map<Snowflake, std::unordered_map<Snowflake, GuildMember>>;
using members_type = std::unordered_map<Snowflake, std::unordered_map<Snowflake, GuildMember>>; // [guild][user]
using permission_overwrites_type = std::unordered_map<Snowflake, std::unordered_map<Snowflake, PermissionOverwrite>>; // [channel][user/role]
const channels_type &GetChannels() const;
const guilds_type &GetGuilds() const;
@ -52,4 +56,5 @@ private:
roles_type m_roles;
messages_type m_messages;
members_type m_members;
permission_overwrites_type m_permissions;
};