Replace text template with JSON layout for Relationship

master
teknomunk 1 year ago
parent 016936eae8
commit c9b8d653f8

@ -20,6 +20,8 @@
#include "api/status.h"
#include "api/notice.h"
#include "view/api/Relationship.h"
#include <stddef.h>
#include <limits.h>
#include <stdio.h>
@ -183,6 +185,9 @@ static bool handle_search( struct http_request* req )
static bool handle_relationships( struct http_request* req )
{
http_request_send_headers( req, 200, "application/json", true );
struct account* owner_account = account_from_id( owner_account_id );
FILE* f = http_request_get_response_body( req );
fprintf( f, "[" );
bool first = true;
@ -199,13 +204,16 @@ static bool handle_relationships( struct http_request* req )
struct account* a = account_from_id(id);
if( !a ) { continue; }
fprintf( f, first ? "\n" : ",\n" ); first = false;
#include "src/view/api/relationship.json.inc"
fprintf( f, first ? "\n\t" : ",\n\t" ); first = false;
api_Relationship_write( owner_account, a, f, 1 );
//#include "src/view/api/relationship.json.inc"
account_free(a);
}
}
fprintf( f, "]" );
fprintf( f, "\n]" );
account_free(owner_account);
return true;
}

@ -20,6 +20,8 @@
#include "model/crypto/keys.h"
#include "model/notification.h"
#include "view/api/Relationship.h"
// Stdlib
#include <stdio.h>
#include <stdlib.h>
@ -595,5 +597,7 @@ bool account_does_follow( struct account* a, int account_id )
// TODO: move to controller/view
void api_account_write_as_json( struct account* a, FILE* f )
{
struct account* owner_account = account_from_id( owner_account_id );
#include "src/view/api/account.json.inc"
account_free(owner_account);
}

@ -0,0 +1,48 @@
#include "Relationship.h"
#include "json/layout.h"
#include "model/account.h"
#include <stdbool.h>
#include <string.h>
#define OBJ_TYPE struct Relationship
static struct json_object_field Relationship_layout[] = {
JSON_FIELD_INTEGER( id, true ),
JSON_FIELD_BOOL( following, true ),
JSON_FIELD_BOOL( showing_reblogs, true ),
JSON_FIELD_BOOL( notifying, true ),
JSON_FIELD_BOOL( followed_by, true ),
JSON_FIELD_BOOL( blocking, true ),
JSON_FIELD_BOOL( blocked_by, true ),
JSON_FIELD_BOOL( muting, true ),
JSON_FIELD_BOOL( muting_notifications, true ),
JSON_FIELD_BOOL( requested, true ),
JSON_FIELD_BOOL( domain_blocking, true ),
JSON_FIELD_BOOL( endorsed, true ),
JSON_FIELD_END,
};
#undef OBJ_TYPE
void api_Relationship_init( struct Relationship* r, struct account* a, struct account* rel )
{
memset( r, 0, sizeof(r) );
r->id = rel->id;
r->followed_by = account_does_follow( rel, a->id );
r->following = account_does_follow( a, rel->id );
}
void api_Relationship_write( struct account* a, struct account* rel, FILE* f, int indent )
{
struct Relationship r;
api_Relationship_init( &r, a, rel );
struct json_writer jw = {
.f = f,
.indentation = "\t",
.indent = indent,
};
json_write_pretty_object_layout( &jw, Relationship_layout, &r );
}

@ -0,0 +1,26 @@
#pragma once
#include <stdio.h>
#include <stdbool.h>
struct account;
struct Relationship
{
int id;
bool following;
bool showing_reblogs;
bool notifying;
bool followed_by;
bool blocking;
bool blocked_by;
bool muting;
bool muting_notifications;
bool requested;
bool domain_blocking;
bool endorsed;
};
void api_Relationship_init( struct Relationship* r, struct account* a, struct account* rel );
void api_Relationship_write( struct account* a, struct account* rel, FILE* f, int indent );

@ -30,21 +30,7 @@
"is_admin":%s{ a->id == owner_account_id ? "true" : "false" },
"is_confirmed":true,
"is_moderator":false,
"relationship":{
"id": "%d{a->id}",
"following": false,
"showing_reblogs": true,
"notifying": false,
"followed_by": false,
"blocking": false,
"blocked_by": false,
"muting": false,
"muting_notifications": false,
"requested": false,
"domain_blocking": false,
"endorsed": false,
"note": ""
},
"relationship":%( api_Relationship_write( owner_account, a, f, 2 ); ),
"skip_thread_containment":false,
"tags":[]
},

@ -1,14 +0,0 @@
{
"id": "%d{ a->id }",
"following": false,
"showing_reblogs": false,
"notifying": false,
"followed_by": false,
"blocking": false,
"blocked_by": false,
"muting": false,
"muting_notifications": false,
"requested": false,
"domain_blocking": false,
"endorsed": false
}
Loading…
Cancel
Save