@ -3,11 +3,14 @@
# include "status/react.h"
# include "model/account.h"
# include "model/timeline.h"
# include "model/ap/activity.h"
# include "model/notification.h"
// Submodules
# include "json/json.h"
# include "json/layout.h"
# include "ffdb/hash_index.h"
# include "sha256/sha256.h"
# include <stdio.h>
@ -17,12 +20,18 @@
static struct json_object_field status_layout [ ] = {
{ " account_id " , offsetof ( struct status , account_id ) , true , & json_field_integer } ,
{ " url " , offsetof ( struct status , url ) , false , & json_field_string } ,
{ " sensitive " , offsetof ( struct status , sensitive ) , true , & json_field_bool } ,
{ " source " , offsetof ( struct status , source ) , false , & json_field_string } ,
{ " content " , offsetof ( struct status , source ) , false , & json_field_string } ,
{ " source " , offsetof ( struct status , source ) , false , & json_field_string } ,
{ " sensitive " , offsetof ( struct status , sensitive ) , true , & json_field_bool } ,
{ " media " , offsetof ( struct status , media ) , false , & json_field_array_of , & json_field_string } ,
//{ "reacts", offsetof( struct status, reacts ), false, &json_field_array_of, &status_react_type },
{ " likes " , offsetof ( struct status , likes ) , false , & json_field_array_of , & json_field_integer } ,
{ " reposts " , offsetof ( struct status , reposts ) , false , & json_field_array_of , & json_field_integer } ,
{ NULL }
} ;
@ -54,6 +63,13 @@ struct status* status_from_id( unsigned int id )
return s ;
}
struct status * status_from_uri ( const char * uri )
{
int id = - 1 ;
if ( ! hash_index_get ( " data/statuses/uri " , uri , & id ) ) { return NULL ; }
return status_from_id ( id ) ;
}
struct status * status_new_system_unfollow ( int account_id )
{
struct account * a = account_from_id ( account_id ) ;
@ -99,6 +115,12 @@ struct status* status_from_activity( struct ap_activity* act )
struct account * a = account_from_uri ( act - > actor ) ;
s - > account_id = a - > id ;
// Add status to account timeline
struct timeline * tl = timeline_from_id ( a - > id ) ;
printf ( " tl->id=%d \n " , tl - > id ) ;
timeline_add_post ( tl , s ) ;
timeline_free ( tl ) ;
account_free ( a ) ;
s - > source = strdup ( act - > source ) ;
@ -134,6 +156,11 @@ void status_save( struct status* s )
snprintf ( filename , 512 , " data/statuses/%d.json " , s - > id ) ;
json_write_object_layout_to_file ( filename , " \t " , status_layout , s ) ;
// Index the status
if ( s - > url ) {
hash_index_set ( " data/statuses/uri " , s - > url , s - > id ) ;
}
}
void status_free ( struct status * s )
@ -150,6 +177,8 @@ void status_free( struct status* s )
}
free ( s - > reacts . items ) ;
free ( s - > reposts . items ) ;
free ( s - > content ) ;
free ( s ) ;
}
@ -162,4 +191,24 @@ void status_add_react( struct status* s, const char* react )
// TODO generate outbox element
}
}
void status_add_like ( struct status * s , struct account * a )
{
for ( int i = 0 ; i < s - > likes . count ; + + i ) {
if ( s - > likes . items [ i ] = = a - > id ) {
return ;
}
}
if ( s - > account_id = = 0 ) {
// Create notification for liking the owner's post
struct notification * note = notification_new ( ) ;
note - > type = nt_like ;
note - > account_id = a - > id ;
note - > created_at = time ( NULL ) ;
notification_save ( note ) ;
notification_free ( note ) ;
}
status_save ( s ) ;
}