summaryrefslogtreecommitdiff
path: root/discord
diff options
context:
space:
mode:
authorouwou <26526779+ouwou@users.noreply.github.com>2021-06-20 20:32:16 -0400
committerouwou <26526779+ouwou@users.noreply.github.com>2021-06-20 20:32:16 -0400
commit7db2675087a563ec82574937759137fa2e57298c (patch)
treedc1dac731ef1ca4f4f89edb2913e34abc2eaeda9 /discord
parent78f3164063f90c2be53d56fef6fd4a1cd085d923 (diff)
downloadabaddon-portaudio-7db2675087a563ec82574937759137fa2e57298c.tar.gz
abaddon-portaudio-7db2675087a563ec82574937759137fa2e57298c.zip
fetch pins from store if already requested
Diffstat (limited to 'discord')
-rw-r--r--discord/discord.cpp7
-rw-r--r--discord/discord.hpp2
-rw-r--r--discord/store.cpp30
-rw-r--r--discord/store.hpp2
4 files changed, 41 insertions, 0 deletions
diff --git a/discord/discord.cpp b/discord/discord.cpp
index ab5ab9f..89107df 100644
--- a/discord/discord.cpp
+++ b/discord/discord.cpp
@@ -720,6 +720,13 @@ void DiscordClient::Unpin(Snowflake channel_id, Snowflake message_id, sigc::slot
}
void DiscordClient::FetchPinned(Snowflake id, sigc::slot<void(std::vector<Message>, DiscordError code)> callback) {
+ // return from db if we know the pins have already been requested
+ if (m_channels_pinned_requested.find(id) != m_channels_pinned_requested.end()) {
+ callback(m_store.GetPinnedMessages(id), DiscordError::NONE);
+ return;
+ }
+ m_channels_pinned_requested.insert(id);
+
m_http.MakeGET("/channels/" + std::to_string(id) + "/pins", [this, callback](const http::response_type &response) {
if (!CheckCode(response)) {
callback({}, GetCodeFromResponse(response));
diff --git a/discord/discord.hpp b/discord/discord.hpp
index 1352499..5c10033 100644
--- a/discord/discord.hpp
+++ b/discord/discord.hpp
@@ -292,6 +292,8 @@ private:
Glib::Dispatcher m_generic_dispatch;
std::queue<std::function<void()>> m_generic_queue;
+ std::set<Snowflake> m_channels_pinned_requested;
+
// signals
public:
typedef sigc::signal<void> type_signal_gateway_ready;
diff --git a/discord/store.cpp b/discord/store.cpp
index 7aa74c0..b282100 100644
--- a/discord/store.cpp
+++ b/discord/store.cpp
@@ -463,6 +463,25 @@ std::vector<Snowflake> Store::GetChannelMessageIDs(Snowflake id) const {
return ret;
}
+std::vector<Message> Store::GetPinnedMessages(Snowflake channel_id) const {
+ std::vector<Message> ret;
+
+ Bind(m_get_pins_stmt, 1, channel_id);
+ while (FetchOne(m_get_pins_stmt)) {
+ Snowflake x;
+ Get(m_get_pins_stmt, 0, x);
+ auto msg = GetMessage(x);
+ if (msg.has_value())
+ ret.push_back(*msg);
+ }
+
+ Reset(m_get_pins_stmt);
+
+ if (m_db_err != SQLITE_DONE)
+ fprintf(stderr, "error while fetching pins: %s\n", sqlite3_errstr(m_db_err));
+ return ret;
+}
+
std::optional<ChannelData> Store::GetChannel(Snowflake id) const {
Bind(m_get_chan_stmt, 1, id);
if (!FetchOne(m_get_chan_stmt)) {
@@ -1138,6 +1157,10 @@ bool Store::CreateStatements() {
SELECT id FROM messages WHERE channel_id = ? ORDER BY id ASC
)";
+ const char *get_pins = R"(
+ SELECT id FROM messages WHERE channel_id = ? AND pinned = 1 ORDER BY id ASC
+ )";
+
m_db_err = sqlite3_prepare_v2(m_db, set_user, -1, &m_set_user_stmt, nullptr);
if (m_db_err != SQLITE_OK) {
fprintf(stderr, "failed to prepare set user statement: %s\n", sqlite3_errstr(m_db_err));
@@ -1276,6 +1299,12 @@ bool Store::CreateStatements() {
return false;
}
+ m_db_err = sqlite3_prepare_v2(m_db, get_pins, -1, &m_get_pins_stmt, nullptr);
+ if (m_db_err != SQLITE_OK) {
+ fprintf(stderr, "failed to prepare getp ins statement: %s\n", sqlite3_errstr(m_db_err));
+ return false;
+ }
+
return true;
}
@@ -1303,6 +1332,7 @@ void Store::Cleanup() {
sqlite3_finalize(m_set_msg_interaction_stmt);
sqlite3_finalize(m_get_last_msgs_stmt);
sqlite3_finalize(m_get_msg_ids_stmt);
+ sqlite3_finalize(m_get_pins_stmt);
}
void Store::Bind(sqlite3_stmt *stmt, int index, int num) const {
diff --git a/discord/store.hpp b/discord/store.hpp
index 1e0b4c7..7e7d6ea 100644
--- a/discord/store.hpp
+++ b/discord/store.hpp
@@ -43,6 +43,7 @@ public:
std::vector<Message> GetLastMessages(Snowflake id, size_t num) const;
std::vector<Snowflake> GetChannelMessageIDs(Snowflake id) const;
+ std::vector<Message> GetPinnedMessages(Snowflake channel_id) const;
void ClearGuild(Snowflake id);
void ClearChannel(Snowflake id);
@@ -133,6 +134,7 @@ private:
mutable sqlite3_stmt *m_set_msg_interaction_stmt;
mutable sqlite3_stmt *m_get_last_msgs_stmt;
mutable sqlite3_stmt *m_get_msg_ids_stmt;
+ mutable sqlite3_stmt *m_get_pins_stmt;
};
template<typename T>