@ -1,14 +1,19 @@
# include "pleroma_api.h"
# include "http/cgi.h"
# include "http/server/request.h"
# include "model/account.h"
# include "model/status.h"
# include "model/notification.h"
# include "view/api/Notification.h"
# include "src/controller/api/client_apps.h"
# include "src/controller/api/status.h"
# include "http/cgi.h"
# include "http/server/request.h"
# include "json/json.h"
# include "json/layout.h"
# include <stdlib.h>
# include <string.h>
@ -113,6 +118,73 @@ bool handle_pleroma_birthdays( struct http_request* req )
return true ;
}
// Route: POST /api/v1/pleroma/notifications/read
struct read_notification_params_t
{
char * max_id ;
char * id ;
} ;
bool handle_read_notification ( struct http_request * req )
{
struct read_notification_params_t params ;
memset ( & params , 0 , sizeof ( params ) ) ;
# define OBJ_TYPE struct read_notification_params_t
static struct json_object_field params_layout [ ] = {
JSON_FIELD_STRING ( max_id , false ) ,
JSON_FIELD_STRING ( id , false ) ,
JSON_FIELD_END
} ;
# undef OBJ_TYPE
FILE * req_body = http_request_get_request_data ( req ) ;
json_read_object_layout_from_FILE ( req_body , params_layout , & params ) ;
http_request_send_headers ( req , 200 , " application/json " , true ) ;
FILE * f = http_request_get_response_body ( req ) ;
if ( params . max_id ) {
int max_id = 0 ;
fprintf ( f , " [ " ) ;
bool is_first = true ;
if ( 1 = = sscanf ( params . max_id , " %012d " , & max_id ) ) {
for ( int i = 0 ; i < 80 ; + + i ) {
struct notification * n = notification_from_id ( max_id - i ) ;
if ( ! n ) { break ; }
if ( n - > is_seen ) { goto done_one_max_id ; }
n - > is_seen = true ;
if ( ! is_first ) {
fprintf ( f , " , \n " ) ;
}
is_first = false ;
api_Notification_write ( n , f , 1 ) ;
notification_save ( n ) ;
done_one_max_id :
notification_free ( n ) ;
}
}
fprintf ( f , " ] " ) ;
} else if ( params . id ) {
int id = 0 ;
if ( 1 = = sscanf ( params . id , " %012d " , & id ) ) {
struct notification * n = notification_from_id ( id ) ;
if ( ! n ) { goto done_id ; } ;
n - > is_seen = true ;
api_Notification_write ( n , f , 0 ) ;
notification_save ( n ) ;
done_id :
notification_free ( n ) ;
}
}
free ( params . id ) ;
free ( params . max_id ) ;
return true ;
}
// Route: /api/v1/pleroma
bool route_pleroma_api ( struct http_request * req )
{
@ -135,6 +207,10 @@ bool route_pleroma_api( struct http_request* req )
}
} else if ( http_request_route ( req , " /birthdays " ) ) {
return handle_pleroma_birthdays ( req ) ;
} else if ( http_request_route ( req , " /notifications/read " ) ) {
if ( http_request_route_method ( req , " POST " ) ) {
return handle_read_notification ( req ) ;
}
}
return false ;