Slon/Api/V1/Notifications: Add initial support for Notifications
This commit is contained in:
parent
bd8c5009fc
commit
022fceb21b
8 changed files with 123 additions and 3 deletions
|
@ -3,12 +3,38 @@ U0 @slon_api_v1_notifications_get(SlonHttpSession* session)
|
||||||
// SLON_SCRATCH_BUFFER_AND_REQUEST_JSON
|
// SLON_SCRATCH_BUFFER_AND_REQUEST_JSON
|
||||||
|
|
||||||
if (@slon_api_authorized(session)) {
|
if (@slon_api_authorized(session)) {
|
||||||
// SLON_AUTH_ACCOUNT_ID
|
SLON_AUTH_ACCOUNT_ID
|
||||||
// FIXME: Implement this
|
|
||||||
|
JsonObject* acct = @slon_api_account_by_id(account_id);
|
||||||
|
|
||||||
if (String.EndsWith("policy", session->path())) {
|
if (String.EndsWith("policy", session->path())) {
|
||||||
session->send(SLON_EMPTY_JSON_OBJECT);
|
session->send(SLON_EMPTY_JSON_OBJECT);
|
||||||
} else {
|
} else {
|
||||||
session->send(SLON_EMPTY_JSON_ARRAY);
|
if (!db->o("notifications")->@(acct->@("username"))) {
|
||||||
|
db->o("notifications")->set(acct->@("username"), Json.CreateArray(slon_db_mem_task), JSON_ARRAY);
|
||||||
|
}
|
||||||
|
session->send(db->o("notifications")->a(acct->@("username")));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
session->status(401);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
U0 @slon_api_v1_notifications_post(SlonHttpSession* session)
|
||||||
|
{
|
||||||
|
// SLON_SCRATCH_BUFFER_AND_REQUEST_JSON
|
||||||
|
|
||||||
|
if (@slon_api_authorized(session)) {
|
||||||
|
SLON_AUTH_ACCOUNT_ID
|
||||||
|
|
||||||
|
JsonObject* acct = @slon_api_account_by_id(account_id);
|
||||||
|
|
||||||
|
if (String.EndsWith("/clear", session->path())) {
|
||||||
|
db->o("notifications")->set(acct->@("username"), Json.CreateArray(slon_db_mem_task), JSON_ARRAY);
|
||||||
|
@slon_db_save_notifications_to_disk;
|
||||||
|
session->send(SLON_EMPTY_JSON_OBJECT);
|
||||||
|
} else {
|
||||||
|
session->send(400);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
session->status(401);
|
session->status(401);
|
||||||
|
|
16
Slon/Api/V2/Notifications.HC
Normal file
16
Slon/Api/V2/Notifications.HC
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
U0 @slon_api_v2_notifications_get(SlonHttpSession* session)
|
||||||
|
{
|
||||||
|
// SLON_SCRATCH_BUFFER_AND_REQUEST_JSON
|
||||||
|
|
||||||
|
if (@slon_api_authorized(session)) {
|
||||||
|
// SLON_AUTH_ACCOUNT_ID
|
||||||
|
// FIXME: Implement this
|
||||||
|
if (String.EndsWith("policy", session->path())) {
|
||||||
|
session->send(SLON_EMPTY_JSON_OBJECT);
|
||||||
|
} else {
|
||||||
|
session->send(SLON_EMPTY_JSON_ARRAY);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
session->status(401);
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,3 +2,8 @@ if (String.BeginsWith("/api/v1/notifications", session->path())) {
|
||||||
@slon_api_v1_notifications_get(session);
|
@slon_api_v1_notifications_get(session);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (String.BeginsWith("/api/v2/notifications", session->path())) {
|
||||||
|
@slon_api_v2_notifications_get(session);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
4
Slon/Endpoints/Post/Notifications.HC
Normal file
4
Slon/Endpoints/Post/Notifications.HC
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
if (String.BeginsWith("/api/v1/notifications", session->path())) {
|
||||||
|
@slon_api_v1_notifications_post(session);
|
||||||
|
return;
|
||||||
|
}
|
|
@ -743,6 +743,7 @@ U0 @slon_http_handle_post_request(SlonHttpSession* session)
|
||||||
#include "Endpoints/Post/Apps";
|
#include "Endpoints/Post/Apps";
|
||||||
#include "Endpoints/Post/Markers";
|
#include "Endpoints/Post/Markers";
|
||||||
#include "Endpoints/Post/Media";
|
#include "Endpoints/Post/Media";
|
||||||
|
#include "Endpoints/Post/Notifications";
|
||||||
#include "Endpoints/Post/OAuth";
|
#include "Endpoints/Post/OAuth";
|
||||||
#include "Endpoints/Post/Polls";
|
#include "Endpoints/Post/Polls";
|
||||||
#include "Endpoints/Post/Statuses";
|
#include "Endpoints/Post/Statuses";
|
||||||
|
|
|
@ -29,6 +29,7 @@ WinMax(Fs);
|
||||||
#include "Api/V2/Filters";
|
#include "Api/V2/Filters";
|
||||||
#include "Api/V2/Instance";
|
#include "Api/V2/Instance";
|
||||||
#include "Api/V2/Media";
|
#include "Api/V2/Media";
|
||||||
|
#include "Api/V2/Notifications";
|
||||||
#include "Api/V2/Search";
|
#include "Api/V2/Search";
|
||||||
#include "Api/V2/Suggestions";
|
#include "Api/V2/Suggestions";
|
||||||
|
|
||||||
|
|
|
@ -5,6 +5,9 @@
|
||||||
account_id = session->auth->@("account_id"); \
|
account_id = session->auth->@("account_id"); \
|
||||||
};
|
};
|
||||||
|
|
||||||
|
SlonHttpSession* SLON_API_DUMMY_SESSION = CAlloc(sizeof(SlonHttpSession));
|
||||||
|
SLON_API_DUMMY_SESSION->mem_task = slon_mem_task;
|
||||||
|
|
||||||
extern @http_response* @slon_activitypub_signed_request(U8* url_string, U8* fetch_buffer, JsonObject* request_object = NULL, I64 verb = SLON_HTTP_VERB_POST, U8* signatory = NULL);
|
extern @http_response* @slon_activitypub_signed_request(U8* url_string, U8* fetch_buffer, JsonObject* request_object = NULL, I64 verb = SLON_HTTP_VERB_POST, U8* signatory = NULL);
|
||||||
|
|
||||||
class SlonCatboxUpload {
|
class SlonCatboxUpload {
|
||||||
|
@ -262,6 +265,36 @@ JsonObject* @slon_api_find_status_by_uri(U8* uri, U8* account_id = NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
U0 @slon_api_notify_account(JsonObject* target_acct, JsonObject* source_acct, U8* type, JsonObject* status = NULL)
|
||||||
|
{
|
||||||
|
U8 scratch_buffer[256];
|
||||||
|
SlonHttpSession* session = SLON_API_DUMMY_SESSION;
|
||||||
|
|
||||||
|
U8* id = @slon_api_generate_unique_id(session);
|
||||||
|
U8* created_at = @slon_api_timestamp_from_cdate(session, Now);
|
||||||
|
|
||||||
|
if (!db->o("notifications")->@(target_acct->@("username"))) {
|
||||||
|
db->o("notifications")->set(target_acct->@("username"), Json.CreateArray(slon_db_mem_task), JSON_ARRAY);
|
||||||
|
}
|
||||||
|
|
||||||
|
JsonArray* notifications = db->o("notifications")->a(target_acct->@("username"));
|
||||||
|
|
||||||
|
JsonObject* notification = Json.CreateObject(slon_db_mem_task);
|
||||||
|
notification->set("id", id, JSON_STRING);
|
||||||
|
notification->set("type", type, JSON_STRING);
|
||||||
|
notification->set("created_at", created_at, JSON_STRING);
|
||||||
|
StrPrint(scratch_buffer, "ungrouped-%s", id);
|
||||||
|
notification->set("group_key", scratch_buffer, JSON_STRING);
|
||||||
|
notification->set("account", source_acct, JSON_OBJECT);
|
||||||
|
if (status) {
|
||||||
|
notification->set("status", status, JSON_OBJECT);
|
||||||
|
}
|
||||||
|
notifications->append(notification);
|
||||||
|
@slon_db_save_notifications_to_disk;
|
||||||
|
@slon_free(session, created_at);
|
||||||
|
@slon_free(session, id);
|
||||||
|
}
|
||||||
|
|
||||||
U0 @slon_api_create_status(JsonObject* status, U8* account_id, U8* to_ap_user = NULL)
|
U0 @slon_api_create_status(JsonObject* status, U8* account_id, U8* to_ap_user = NULL)
|
||||||
{
|
{
|
||||||
if (!status || !account_id) {
|
if (!status || !account_id) {
|
||||||
|
|
|
@ -226,6 +226,26 @@ U0 @slon_db_load_markers_from_disk()
|
||||||
db->set("markers", markers, JSON_OBJECT);
|
db->set("markers", markers, JSON_OBJECT);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
U0 @slon_db_load_notifications_from_disk()
|
||||||
|
{
|
||||||
|
JsonObject* notifications = Json.CreateObject(slon_db_mem_task);
|
||||||
|
U8 scratch_buffer[256];
|
||||||
|
StrPrint(scratch_buffer, "%s/notifications/*.json", SLON_DB_PATH);
|
||||||
|
CDirEntry* files = FilesFind(scratch_buffer);
|
||||||
|
CDirEntry* de = files;
|
||||||
|
JsonArray* notification_array = NULL;
|
||||||
|
while (de) {
|
||||||
|
notification_array = Json.ParseFile(de->full_name, slon_db_mem_task);
|
||||||
|
if (notification_array) {
|
||||||
|
StrFind(".json", de->name)[0] = NULL;
|
||||||
|
notifications->set(de->name, notification_array, JSON_ARRAY);
|
||||||
|
}
|
||||||
|
de = de->next;
|
||||||
|
}
|
||||||
|
DirTreeDel(files);
|
||||||
|
db->set("notifications", notifications, JSON_OBJECT);
|
||||||
|
}
|
||||||
|
|
||||||
U0 @slon_db_load_settings_from_disk()
|
U0 @slon_db_load_settings_from_disk()
|
||||||
{
|
{
|
||||||
U8 scratch_buffer[256];
|
U8 scratch_buffer[256];
|
||||||
|
@ -436,6 +456,17 @@ U0 @slon_db_save_markers_to_disk()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
U0 @slon_db_save_notifications_to_disk()
|
||||||
|
{
|
||||||
|
U8 scratch_buffer[256];
|
||||||
|
JsonKey* key = db->o("notifications")->keys;
|
||||||
|
while (key) {
|
||||||
|
StrPrint(scratch_buffer, "%s/notifications/%s.json", SLON_DB_PATH, key->name);
|
||||||
|
Json.DumpToFile(scratch_buffer, key->value, slon_db_mem_task);
|
||||||
|
key = key->next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
U0 @slon_db_save_settings_to_disk()
|
U0 @slon_db_save_settings_to_disk()
|
||||||
{
|
{
|
||||||
U8 scratch_buffer[256];
|
U8 scratch_buffer[256];
|
||||||
|
@ -489,6 +520,7 @@ U0 @slon_db_save_to_disk()
|
||||||
@slon_db_save_following_to_disk();
|
@slon_db_save_following_to_disk();
|
||||||
@slon_db_save_instance_to_disk();
|
@slon_db_save_instance_to_disk();
|
||||||
@slon_db_save_markers_to_disk();
|
@slon_db_save_markers_to_disk();
|
||||||
|
@slon_db_save_notifications_to_disk();
|
||||||
@slon_db_save_oauth_to_disk();
|
@slon_db_save_oauth_to_disk();
|
||||||
@slon_db_save_private_keys_to_disk();
|
@slon_db_save_private_keys_to_disk();
|
||||||
@slon_db_save_reblogs_to_disk();
|
@slon_db_save_reblogs_to_disk();
|
||||||
|
@ -516,6 +548,7 @@ U0 @slon_db_load_from_defaults()
|
||||||
db->set("instance", Json.ParseFile("M:/Slon/Static/defaults/instance.json", slon_db_mem_task), JSON_OBJECT);
|
db->set("instance", Json.ParseFile("M:/Slon/Static/defaults/instance.json", slon_db_mem_task), JSON_OBJECT);
|
||||||
db->set("markers", Json.CreateObject(slon_db_mem_task), JSON_OBJECT);
|
db->set("markers", Json.CreateObject(slon_db_mem_task), JSON_OBJECT);
|
||||||
db->set("media", Json.CreateObject(slon_db_mem_task), JSON_OBJECT);
|
db->set("media", Json.CreateObject(slon_db_mem_task), JSON_OBJECT);
|
||||||
|
db->set("notifications", Json.CreateObject(slon_db_mem_task), JSON_OBJECT);
|
||||||
db->set("reblogs", Json.CreateObject(slon_db_mem_task), JSON_OBJECT);
|
db->set("reblogs", Json.CreateObject(slon_db_mem_task), JSON_OBJECT);
|
||||||
db->set("settings", Json.CreateObject(slon_db_mem_task), JSON_OBJECT);
|
db->set("settings", Json.CreateObject(slon_db_mem_task), JSON_OBJECT);
|
||||||
db->set("statuses", Json.CreateObject(slon_db_mem_task), JSON_OBJECT);
|
db->set("statuses", Json.CreateObject(slon_db_mem_task), JSON_OBJECT);
|
||||||
|
@ -550,6 +583,7 @@ U0 @slon_db_load_from_disk()
|
||||||
@slon_db_load_instance_from_disk();
|
@slon_db_load_instance_from_disk();
|
||||||
@slon_db_load_markers_from_disk();
|
@slon_db_load_markers_from_disk();
|
||||||
db->set("media", Json.CreateObject(slon_db_mem_task), JSON_OBJECT);
|
db->set("media", Json.CreateObject(slon_db_mem_task), JSON_OBJECT);
|
||||||
|
@slon_db_load_notifications_from_disk();
|
||||||
@slon_db_load_oauth_from_disk();
|
@slon_db_load_oauth_from_disk();
|
||||||
@slon_db_load_reblogs_from_disk();
|
@slon_db_load_reblogs_from_disk();
|
||||||
@slon_db_load_settings_from_disk();
|
@slon_db_load_settings_from_disk();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue