remove temp attachment files when theyre actually done being uploaded

This commit is contained in:
ouwou 2022-06-09 01:48:24 -04:00
parent d7177cac97
commit 49ff9a249e
5 changed files with 35 additions and 3 deletions

View File

@ -112,6 +112,13 @@ void ChatInputAttachmentContainer::Clear() {
m_attachments.clear();
}
void ChatInputAttachmentContainer::ClearNoPurge() {
for (auto *item : m_attachments) {
delete item;
}
m_attachments.clear();
}
bool ChatInputAttachmentContainer::AddImage(const Glib::RefPtr<Gdk::Pixbuf> &pb) {
if (m_attachments.size() == 10) return false;
@ -217,8 +224,8 @@ ChatInput::ChatInput()
const auto attachments = m_attachments.GetFilePaths();
bool b = m_signal_submit.emit(input, attachments);
if (b) {
m_attachments.Clear();
m_attachments_revealer.set_reveal_child(false);
m_attachments.ClearNoPurge();
}
return b;
});
@ -232,7 +239,9 @@ ChatInput::ChatInput()
show_all_children();
m_input.signal_image_paste().connect([this](const Glib::RefPtr<Gdk::Pixbuf> &pb) {
if (m_attachments.AddImage(pb))
const bool can_attach_files = m_signal_check_permission.emit(Permission::ATTACH_FILES);
if (can_attach_files && m_attachments.AddImage(pb))
m_attachments_revealer.set_reveal_child(true);
});
@ -266,3 +275,7 @@ ChatInput::type_signal_submit ChatInput::signal_submit() {
ChatInput::type_signal_escape ChatInput::signal_escape() {
return m_signal_escape;
}
ChatInput::type_signal_check_permission ChatInput::signal_check_permission() {
return m_signal_check_permission;
}

View File

@ -1,5 +1,6 @@
#pragma once
#include <gtkmm.h>
#include "discord/permissions.hpp"
class ChatInputAttachmentItem : public Gtk::EventBox {
public:
@ -32,6 +33,7 @@ public:
ChatInputAttachmentContainer();
void Clear();
void ClearNoPurge();
bool AddImage(const Glib::RefPtr<Gdk::Pixbuf> &pb);
[[nodiscard]] std::vector<std::string> GetFilePaths() const;
@ -98,11 +100,14 @@ public:
// maybe this should be reduced to a single struct, its bound to get more complicated (application commands?)
using type_signal_submit = sigc::signal<bool, Glib::ustring, std::vector<std::string>>;
using type_signal_escape = sigc::signal<void>;
using type_signal_check_permission = sigc::signal<bool, Permission>;
type_signal_submit signal_submit();
type_signal_escape signal_escape();
type_signal_check_permission signal_check_permission();
private:
type_signal_submit m_signal_submit;
type_signal_escape m_signal_escape;
type_signal_check_permission m_signal_check_permission;
};

View File

@ -47,6 +47,9 @@ ChatWindow::ChatWindow() {
m_input->set_valign(Gtk::ALIGN_END);
m_input->signal_check_permission().connect([this](Permission perm) {
return Abaddon::Get().GetDiscordClient().HasSelfChannelPermission(m_active_channel, perm);
});
m_input->signal_submit().connect(sigc::mem_fun(*this, &ChatWindow::OnInputSubmit));
m_input->signal_escape().connect([this]() {
if (m_is_replying)

View File

@ -318,6 +318,10 @@ bool DiscordClient::HasGuildPermission(Snowflake user_id, Snowflake guild_id, Pe
return (base & perm) == perm;
}
bool DiscordClient::HasSelfChannelPermission(Snowflake channel_id, Permission perm) const {
return HasChannelPermission(m_user_data.ID, channel_id, perm);
}
bool DiscordClient::HasAnyChannelPermission(Snowflake user_id, Snowflake channel_id, Permission perm) const {
const auto channel = m_store.GetChannel(channel_id);
if (!channel.has_value() || !channel->GuildID.has_value()) return false;
@ -439,7 +443,13 @@ void DiscordClient::SendChatMessageAttachments(const std::string &content, const
const auto field_name = "files[" + std::to_string(i) + "]";
req.add_file(field_name, attachment_paths.at(i), "unknown.png");
}
m_http.Execute(std::move(req), sigc::bind<0>(sigc::mem_fun(*this, &DiscordClient::ChatMessageCallback), nonce));
m_http.Execute(std::move(req), [this, attachment_paths, nonce](const http::response_type &res) {
for (const auto &path : attachment_paths) {
std::error_code ec;
std::filesystem::remove(path, ec);
}
ChatMessageCallback(nonce, res);
});
}
void DiscordClient::SendChatMessageText(const std::string &content, Snowflake channel, Snowflake referenced_message) {

View File

@ -96,6 +96,7 @@ public:
bool IsThreadJoined(Snowflake thread_id) const;
bool HasGuildPermission(Snowflake user_id, Snowflake guild_id, Permission perm) const;
bool HasSelfChannelPermission(Snowflake channel_id, Permission perm) const;
bool HasAnyChannelPermission(Snowflake user_id, Snowflake channel_id, Permission perm) const;
bool HasChannelPermission(Snowflake user_id, Snowflake channel_id, Permission perm) const;
Permission ComputePermissions(Snowflake member_id, Snowflake guild_id) const;