Get application stuff

FossilOrigin-Name: ed7f0c05cbea6c2083fe93ea4e610a0edda6e339610bb8f6bfd91296c8d57fe6
This commit is contained in:
nekobit 2022-12-24 04:40:53 +00:00
parent c7c9afb2a3
commit e4dd6d61a9
5 changed files with 92 additions and 25 deletions

View file

@ -130,9 +130,11 @@ User SQLite::get_user(decltype(User::id) id)
luser.local = false;
// Setup function
sqlite_compile_to_cache(GET_USER_BY_ID, "SELECT acct, display_name, bio, key FROM users WHERE id=?1");
sqlite_compile_to_cache(SQLiteCacheIndex::GET_USER_BY_ID,
"SELECT acct, display_name, bio, key"
" FROM users WHERE id=?1");
int code = sqlite_exec(GET_USER_BY_ID, {{id}}, [&luser](sqlite3_stmt* stmt){
int code = sqlite_exec(SQLiteCacheIndex::GET_USER_BY_ID, {{id}}, [&luser](sqlite3_stmt* stmt){
luser.acct = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0));
if (sqlite3_column_type(stmt, 1) != SQLITE_NULL)
luser.display_name = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 1));
@ -154,12 +156,14 @@ void SQLite::create_user(User& p)
{
// INSERT INTO users (local, display_name, acct, bio, created_at) VALUES (TRUE, 'Test user', 'test1', 'sneed!!!', unixepoch());
sqlite_compile_to_cache(CREATE_USER, "INSERT INTO users ( local, email, display_name, acct, bio, created_at, updated_at, birthday, key )"
"VALUES ( ?1, ?2, ?3, ?4, ?5, unixepoch(), unixepoch(), ?6, ?7 )");
sqlite_compile_to_cache(SQLiteCacheIndex::CREATE_USER,
"INSERT INTO users ( "
"local, email, display_name, acct, bio, created_at, updated_at, birthday, key"
") VALUES ( ?1, ?2, ?3, ?4, ?5, unixepoch(), unixepoch(), ?6, ?7 )");
p.generate_key(rsa_cstor);
sqlite_exec(CREATE_USER,
sqlite_exec(SQLiteCacheIndex::CREATE_USER,
{p.local, p.email, p.display_name, p.acct,
p.bio, p.birthday, p.key},
nullptr);
@ -168,9 +172,10 @@ void SQLite::create_user(User& p)
void SQLite::delete_user(decltype(User::id) id)
{
// Setup function
sqlite_compile_to_cache(DELETE_USER_BY_ID, "DELETE FROM users WHERE id=?1");
sqlite_compile_to_cache(SQLiteCacheIndex::DELETE_USER_BY_ID,
"DELETE FROM users WHERE id=?1");
int code = sqlite_exec(DELETE_USER_BY_ID, {{id}}, nullptr);
int code = sqlite_exec(SQLiteCacheIndex::DELETE_USER_BY_ID, {{id}}, nullptr);
if (!code)
{
@ -181,9 +186,10 @@ void SQLite::delete_user(decltype(User::id) id)
void SQLite::delete_user(const decltype(User::acct)& acct)
{
// Setup function
sqlite_compile_to_cache(DELETE_USER_BY_ID, "DELETE FROM users WHERE acct=?1");
sqlite_compile_to_cache(SQLiteCacheIndex::DELETE_USER_BY_ID,
"DELETE FROM users WHERE acct=?1");
int code = sqlite_exec(DELETE_USER_BY_ID, {{acct}}, nullptr);
int code = sqlite_exec(SQLiteCacheIndex::DELETE_USER_BY_ID, {{acct}}, nullptr);
if (!code)
{
@ -193,11 +199,65 @@ void SQLite::delete_user(const decltype(User::acct)& acct)
App SQLite::get_app(decltype(App::id) id)
{
App app{};
app.id = id;
// Setup function
sqlite_compile_to_cache(SQLiteCacheIndex::GET_APP_BY_ID,
"SELECT client_name, client_id, client_secret,"
"app_website FROM users WHERE id=?1");
int code = sqlite_exec(SQLiteCacheIndex::GET_APP_BY_ID, {{id}}, [&app](sqlite3_stmt* stmt){
if (sqlite3_column_type(stmt, 0) != SQLITE_NULL)
app.client_name = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0));
// Check each column if its valid
if (sqlite3_column_type(stmt, 1) != SQLITE_NULL)
app.client_id = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 1));
if (sqlite3_column_type(stmt, 2) != SQLITE_NULL)
app.client_secret = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 2));
if (sqlite3_column_type(stmt, 3) != SQLITE_NULL)
app.app_website = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 3));
});
if (!code)
{
throw std::runtime_error("App \""s + std::to_string(id) + "\" couldn't be created");
}
return app;
}
App SQLite::get_app_by_client_id(const decltype(App::client_id)& client_id)
{
App app{};
// Very much assumed
app.client_id = client_id;
// Setup function
sqlite_compile_to_cache(SQLiteCacheIndex::GET_APP_BY_ID,
"SELECT id, client_name, client_secret,"
"app_website FROM applications WHERE client_id=?1");
int code = sqlite_exec(SQLiteCacheIndex::GET_APP_BY_ID, {{client_id}}, [&app](sqlite3_stmt* stmt){
// Check each column if its valid
if (sqlite3_column_type(stmt, 0) != SQLITE_NULL)
app.id = sqlite3_column_int64(stmt, 0);
if (sqlite3_column_type(stmt, 1) != SQLITE_NULL)
app.client_secret = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 1));
if (sqlite3_column_type(stmt, 2) != SQLITE_NULL)
app.app_website = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 2));
});
if (!code)
{
throw std::runtime_error("App \""s + client_id + "\" wasn't found");
}
return app;
}
App SQLite::get_app_by_client_secret(const decltype(App::client_id)& client_secret)
@ -206,12 +266,14 @@ App SQLite::get_app_by_client_secret(const decltype(App::client_id)& client_secr
void SQLite::create_app(App& p)
{
sqlite_compile_to_cache(CREATE_APP, "INSERT INTO applications ( client_name, client_id, client_secret, app_website, created_at, updated_at )"
"VALUES ( ?1, ?2, ?3, ?4, unixepoch(), unixepoch() )");
sqlite_compile_to_cache(SQLiteCacheIndex::CREATE_APP,
"INSERT INTO applications ("
"client_name, client_id, client_secret, app_website, created_at, updated_at"
")VALUES ( ?1, ?2, ?3, ?4, unixepoch(), unixepoch() )");
p.generate_tokens(rng);
sqlite_exec(CREATE_APP,
sqlite_exec(SQLiteCacheIndex::CREATE_APP,
{p.client_name, p.client_id, p.client_secret, p.app_website},
nullptr);
}
@ -236,9 +298,10 @@ User SQLite::get_user(const decltype(User::acct)& acct)
luser.local = false;
// Setup function
sqlite_compile_to_cache(GET_USER_BY_ACCT, "SELECT id, display_name, bio, key FROM users WHERE acct=?1");
sqlite_compile_to_cache(SQLiteCacheIndex::GET_USER_BY_ACCT,
"SELECT id, display_name, bio, key FROM users WHERE acct=?1");
int code = sqlite_exec(GET_USER_BY_ACCT, {acct}, [&luser](sqlite3_stmt* stmt){
int code = sqlite_exec(SQLiteCacheIndex::GET_USER_BY_ACCT, {acct}, [&luser](sqlite3_stmt* stmt){
luser.id = sqlite3_column_int64(stmt, 0);
if (sqlite3_column_type(stmt, 1) != SQLITE_NULL)
luser.display_name = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 1));

View file

@ -22,11 +22,14 @@ namespace DB
std::string>;
enum SQLiteCacheIndex {
// User
GET_USER_BY_ID = 0,
GET_USER_BY_ACCT,
CREATE_USER,
DELETE_USER_BY_ID,
// App
CREATE_APP,
GET_APP_BY_ID,
COUNT,
};

View file

@ -77,3 +77,13 @@ void Logger::Logger::operator<<(std::string message)
// Call hook
hook(l);
}
std::ostream& operator<<(std::ostream& os, const Logger::Log& log)
{
std::stringstream stream;
stream << "[" << log.pretty_time()<< "] " <<
"<" << Logger::level_string[static_cast<int>(log.level)] << "> " <<
log.message;
os << stream.str();
return os;
}

View file

@ -110,12 +110,4 @@ namespace Logger
Logger& instance();
}
static std::ostream& operator<<(std::ostream& os, const Logger::Log& log)
{
std::stringstream stream;
stream << "[" << log.pretty_time()<< "] " <<
"<" << Logger::level_string[static_cast<int>(log.level)] << "> " <<
log.message;
os << stream.str();
return os;
}
std::ostream& operator<<(std::ostream& os, const Logger::Log& log);

View file

@ -8,9 +8,8 @@
#include <string>
#include "random.h"
class App
struct App
{
public:
App() = default;
~App() = default;