aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCara Salter <cara@devcara.com>2023-04-15 11:24:28 -0400
committerCara Salter <cara@devcara.com>2023-04-15 11:24:28 -0400
commitac4a926685c754f4af5adb20edf6eb23e2a0f18a (patch)
tree2a28e7c798ed1f845bd38a7d5cd0ba465782fb80
parent5058c8bbeff6b659787a8ab482ac89fc4a194076 (diff)
download142bot-ac4a926685c754f4af5adb20edf6eb23e2a0f18a.tar.gz
142bot-ac4a926685c754f4af5adb20edf6eb23e2a0f18a.zip
Automagically refresh spotify tokens
Change-Id: I883f28a88776e0a04b90e6d07c255a3fd9f05977
-rw-r--r--.vscode/launch.json17
-rw-r--r--include/142bot/db.hpp16
-rw-r--r--modules/spotify/spotify.cpp13
3 files changed, 39 insertions, 7 deletions
diff --git a/.vscode/launch.json b/.vscode/launch.json
new file mode 100644
index 0000000..89efb96
--- /dev/null
+++ b/.vscode/launch.json
@@ -0,0 +1,17 @@
+{
+ // Use IntelliSense to learn about possible attributes.
+ // Hover to view descriptions of existing attributes.
+ // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
+ "version": "0.2.0",
+ "configurations": [
+ {
+ "name": "C++ Launch",
+ "type": "cppdbg",
+ "request": "launch",
+ "program": "${workspaceFolder}/build/142bot",
+ "args": [],
+ "environment": [],
+ "cwd": "${workspaceFolder}"
+ }
+ ]
+} \ No newline at end of file
diff --git a/include/142bot/db.hpp b/include/142bot/db.hpp
index f4f2769..0a82b84 100644
--- a/include/142bot/db.hpp
+++ b/include/142bot/db.hpp
@@ -94,13 +94,25 @@ namespace pqxx
{
return asdf::to_iso8601_str( ts );
}
+
+ static char* into_buf(char* begin, char* end, asdf::timestamp const &value) {
+ return pqxx::internal::generic_into_buf(begin, end, value);
+ }
+
+ static zview to_buf(char* begin, char* end, asdf::timestamp const &value) {
+ return pqxx::string_traits<std::string>::to_buf(begin, end, asdf::to_iso8601_str(value));
+ }
+
+ static std::size_t size_buffer(asdf::timestamp const &value) noexcept {
+ return pqxx::string_traits<std::string>::size_buffer(asdf::to_iso8601_str(value)) + 1;
+ }
};
template<> struct nullness<asdf::timestamp> {
static constexpr bool has_null = false;
static constexpr bool always_null = false;
- static constexpr bool is_null(asdf::timestamp* t) noexcept {
- return t == nullptr;
+ static constexpr bool is_null(asdf::timestamp t) noexcept {
+ return false;
}
static constexpr asdf::timestamp *null() { return nullptr; }
};
diff --git a/modules/spotify/spotify.cpp b/modules/spotify/spotify.cpp
index 8f44f2e..8df83a4 100644
--- a/modules/spotify/spotify.cpp
+++ b/modules/spotify/spotify.cpp
@@ -49,7 +49,7 @@ public:
return "Manage spotify queues for 142";
}
- void refreshSpotify(std::string refreshToken, pqxx::work &tx) {
+ void refreshSpotify(std::string refreshToken) {
bot->core->log(dpp::ll_debug, "Attempting to refresh spotify token...");
cpr::Response r = cpr::Post(cpr::Url("https://accounts.spotify.com/api/token"),
cpr::Authentication{bot->cfg["spotify"]["id"], bot->cfg["spotify"]["secret"], cpr::AuthMode::BASIC}, cpr::Payload{{"grant_type", "refresh_token"}, {"refresh_token", refreshToken}});
@@ -61,16 +61,16 @@ public:
throw std::exception();
}
bot->core->log(dpp::ll_trace, "Request successful");
- printf("%s\n", r.text.c_str());
auto tmp = json::parse(r.text);
bot->core->log(dpp::ll_trace, "Parsed JSON");
uint64_t expires = tmp["expires_in"].get<uint64_t>();
+ pqxx::work tx(bot->conn);
asdf::timestamp parsed_expires = asdf::from_unix_time(time(0) + expires);
- bot->core->log(dpp::ll_trace, "Got expires_in");
+ bot->core->log(dpp::ll_trace, fmt::format("Got expires_in: {}", asdf::to_iso8601_str(parsed_expires)));
std::string access = tmp["access_token"].get<std::string>();
- bot->core->log(dpp::ll_trace, "Got access token");
+ bot->core->log(dpp::ll_trace, fmt::format("Got access token"));
tx.exec_params("UPDATE spotify SET spotify_token=$1, spotify_token_expires=$2 WHERE id=$3", access, parsed_expires, this->defaultSpotifyAccount);
bot->core->log(dpp::ll_trace, "Updated DB");
tx.commit();
@@ -85,13 +85,16 @@ public:
try {
bot->core->log(dpp::ll_debug, fmt::format("Default spotify account: {}", this->defaultSpotifyAccount));
auto res = tx.exec_params1("SELECT spotify_username,spotify_token,spotify_token_expires,spotify_refresh_token FROM spotify WHERE id=$1", atoi(this->defaultSpotifyAccount.c_str()));
+ tx.commit();
bot->core->log(dpp::ll_trace, "Retrieved from DB.");
auto ts = res[2].as<asdf::timestamp>();
if (ts < std::chrono::system_clock::now()) {
- refreshSpotify(res[3].as<std::string>(), tx);
+ refreshSpotify(res[3].as<std::string>());
+ pqxx::work tx(bot->conn);
res = tx.exec_params1("SELECT spotify_username, spotify_token FROM spotify WHERE id=$1", atoi(this->defaultSpotifyAccount.c_str()));
+ tx.commit();
bot->core->log(dpp::ll_trace, "Retrieved from database *again*");
}