Improve activitypup stuff

FossilOrigin-Name: 03b65097f4845d858c012caeb69abcfff344e1646f556b0763d7ebc831ffa0d5
This commit is contained in:
nekobit 2022-10-10 18:47:23 +00:00
parent 1d18f47baa
commit 408b84f1aa
4 changed files with 89 additions and 9 deletions

View file

@ -137,13 +137,15 @@ SQLiteCacheIndex SQLite::sqlite_compile_to_cache(SQLiteCacheIndex idx, std::stri
User SQLite::get_user(unsigned long id)
{
User luser{id, {}, false, {}, {}, {}};
User luser{id, {}, false, {}, {}, {}, {}};
// Setup function
sqlite_compile_to_cache(GET_USER_BY_ID, "SELECT acct FROM users WHERE id=?1");
sqlite_compile_to_cache(GET_USER_BY_ID, "SELECT acct, display_name, bio FROM users WHERE id=?1");
int code = sqlite_exec(GET_USER_BY_ID, {{id}}, [&luser](sqlite3_stmt* stmt){
luser.acct = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0));
luser.display_name = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 1));
luser.bio = reinterpret_cast<const char*>(sqlite3_column_int64(stmt, 2));
});
if (!code)
@ -156,13 +158,15 @@ User SQLite::get_user(unsigned long id)
User SQLite::get_user(const std::string& acct)
{
User luser{0, {}, false, acct, {}, {}};
User luser{0, {}, false, {}, acct, {}, {}};
// Setup function
sqlite_compile_to_cache(GET_USER_BY_ACCT, "SELECT acct FROM users WHERE acct=?1");
sqlite_compile_to_cache(GET_USER_BY_ACCT, "SELECT id, display_name, bio FROM users WHERE acct=?1");
int code = sqlite_exec(GET_USER_BY_ACCT, {acct}, [&luser](sqlite3_stmt* stmt){
luser.id = sqlite3_column_int64(stmt, 0);
luser.display_name = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 1));
luser.bio = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 2));
});
if (!code)

View file

@ -33,7 +33,7 @@
using namespace Protocol;
HTTP::Response Route::ActivityPub::inbox(std::any& args, const HTTP::Request& req, const HTTP::RequestArgs_t& arg)
HTTP::Response Route::ActivityPub::user_inbox(std::any& args, const HTTP::Request& req, const HTTP::RequestArgs_t& arg)
{
using namespace std::string_literals;
@ -43,8 +43,6 @@ HTTP::Response Route::ActivityPub::inbox(std::any& args, const HTTP::Request& re
rjson::Document::AllocatorType& a = root.GetAllocator();
rjson::Value links(rjson::kArrayType);
// Resource to read into
std::string resource;
// Stored result of account string, stripped from resource
User user;
@ -58,10 +56,85 @@ HTTP::Response Route::ActivityPub::inbox(std::any& args, const HTTP::Request& re
root.AddMember("@context", "https://www.w3.org/ns/activitystreams", a);
const std::string inbox_url = Instance::instance_host_https(true) + "user/" + user.acct + "/inbox";
root.AddMember("id", inbox_url, a);
root.AddMember("first", inbox_url + "?page=true", a);
root.AddMember("type", "OrderedCollection", a);
return HTTP::Response( rjson::to_string(root), HTTP::MIME::JSON );
}
// TODO not dupe
HTTP::Response Route::ActivityPub::user_outbox(std::any& args, const HTTP::Request& req, const HTTP::RequestArgs_t& arg)
{
using namespace std::string_literals;
DESTRUCT_WORMHOLE_ARGS(args);
rjson::Document root(rjson::kObjectType);
rjson::Document::AllocatorType& a = root.GetAllocator();
// Stored result of account string, stripped from resource
User user;
try
{
user = db->get_user(arg.at(0));
}
catch (...) {
return HTTP::Error::make_json_error_response("Couldn't find user", HTTP::Code::NOT_FOUND);
}
root.AddMember("@context", "https://www.w3.org/ns/activitystreams", a);
const std::string outbox_url = Instance::instance_host_https(true) + "user/" + user.acct + "/outbox";
root.AddMember("id", outbox_url, a);
root.AddMember("first", outbox_url + "?page=true", a);
root.AddMember("type", "OrderedCollection", a);
return HTTP::Response( rjson::to_string(root), HTTP::MIME::JSON );
}
HTTP::Response Route::ActivityPub::user(std::any& args, const HTTP::Request& req, const HTTP::RequestArgs_t& arg)
{
using namespace std::string_literals;
DESTRUCT_WORMHOLE_ARGS(args);
rjson::Document root(rjson::kObjectType);
rjson::Document::AllocatorType& a = root.GetAllocator();
// Stored result of account string, stripped from resource
User user;
try
{
user = db->get_user(arg.at(0));
}
catch (...) {
return HTTP::Error::make_json_error_response("Couldn't find user", HTTP::Code::NOT_FOUND);
}
root.AddMember("@context", "https://www.w3.org/ns/activitystreams", a);
const std::string user_url = Instance::instance_host_https(true) + "user/" + user.acct;
root.AddMember("type", "Person", a);
root.AddMember("id", user_url, a);
root.AddMember("name", user.display_name, a);
root.AddMember("preferredUsername", user.acct, a);
root.AddMember("summary", user.bio, a);
root.AddMember("inbox", user_url+"/inbox", a);
root.AddMember("outbox", user_url+"/outbox", a);
root.AddMember("followers", user_url+"/followers", a);
root.AddMember("following", user_url+"/following", a);
root.AddMember("liked", user_url+"/liked", a);
return HTTP::Response( rjson::to_string(root), HTTP::MIME::JSON );
}
void ActivityPub::init_activitypub(HTTP::Server* server)
{
server->add_route({{HTTP::Request::Type::GET, "/user/:/inbox"}, Route::ActivityPub::inbox});
// TODO Fix these, very greedy
server->add_route({{HTTP::Request::Type::GET, "/user/:/inbox"}, Route::ActivityPub::user_inbox});
server->add_route({{HTTP::Request::Type::GET, "/user/:/outbox"}, Route::ActivityPub::user_outbox});
server->add_route({{HTTP::Request::Type::GET, "/user/:"}, Route::ActivityPub::user});
}

View file

@ -25,7 +25,9 @@ namespace Route
{
namespace ActivityPub
{
HTTP::Response inbox(std::any& args, const HTTP::Request& req, const HTTP::RequestArgs_t& arg);
HTTP::Response user_inbox(std::any& args, const HTTP::Request& req, const HTTP::RequestArgs_t& arg);
HTTP::Response user_outbox(std::any& args, const HTTP::Request& req, const HTTP::RequestArgs_t& arg);
HTTP::Response user(std::any& args, const HTTP::Request& req, const HTTP::RequestArgs_t& arg);
}
}

View file

@ -26,6 +26,7 @@ public:
unsigned long id;
std::string email;
bool is_local;
std::string bio;
std::string acct;
std::string birthday;
std::string display_name;