2018-08-29 20:38:10 +00:00
|
|
|
<?php
|
|
|
|
namespace activities;
|
|
|
|
|
|
|
|
function get_activity( $id ) {
|
|
|
|
global $wpdb;
|
2018-09-03 16:47:08 +00:00
|
|
|
$activity_json = $wpdb->get_var( $wpdb->prepare(
|
2018-09-19 21:33:20 +00:00
|
|
|
'SELECT activity FROM pterotype_activities WHERE id = %d', $id
|
2018-08-29 20:38:10 +00:00
|
|
|
) );
|
|
|
|
if ( is_null( $activity_json ) ) {
|
|
|
|
return new \WP_Error(
|
2018-09-19 15:16:41 +00:00
|
|
|
'not_found', __( 'Activity not found', 'pterotype' ), array( 'status' => 404 )
|
2018-08-29 20:38:10 +00:00
|
|
|
);
|
|
|
|
}
|
2018-08-29 21:04:24 +00:00
|
|
|
$activity = json_decode( $activity_json, true );
|
2018-09-19 02:55:47 +00:00
|
|
|
return $activity;
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_activity_by_activitypub_id( $activitypub_id ) {
|
|
|
|
global $wpdb;
|
|
|
|
$activity_json = $wpdb->get_var( $wpdb->prepare(
|
2018-09-19 21:33:20 +00:00
|
|
|
'SELECT activity FROM pterotype_activities WHERE id = %s', $activitypub_id
|
2018-09-19 02:55:47 +00:00
|
|
|
) );
|
|
|
|
if ( is_null( $activity_json ) ) {
|
|
|
|
return new \WP_Error(
|
2018-09-19 15:16:41 +00:00
|
|
|
'not_found', __( 'Activity not found', 'pterotype' ), array( 'status' => 404 )
|
2018-09-19 02:55:47 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
$activity = json_decode( $activity_json, true );
|
2018-08-29 20:38:10 +00:00
|
|
|
return $activity;
|
|
|
|
}
|
|
|
|
|
2018-09-14 01:52:13 +00:00
|
|
|
function strip_private_fields( $activity ) {
|
|
|
|
if ( array_key_exists( 'bto', $activity ) ) {
|
|
|
|
unset( $activity['bto'] );
|
|
|
|
}
|
|
|
|
if ( array_key_exists( 'bcc', $activity ) ) {
|
|
|
|
unset( $activity['bcc'] );
|
|
|
|
}
|
|
|
|
return $activity;
|
|
|
|
}
|
|
|
|
|
2018-08-29 20:38:10 +00:00
|
|
|
function persist_activity( $activity ) {
|
|
|
|
global $wpdb;
|
2018-09-19 02:55:47 +00:00
|
|
|
if ( !array_key_exists( 'id', $activity ) ) {
|
|
|
|
return new \WP_Error(
|
|
|
|
'invalid_activity',
|
2018-09-19 15:16:41 +00:00
|
|
|
__( 'Activity must have an "id" field', 'pterotype' ),
|
2018-09-19 02:55:47 +00:00
|
|
|
array( 'status' => 400 )
|
|
|
|
);
|
|
|
|
}
|
2018-09-20 18:45:14 +00:00
|
|
|
if ( !array_key_exists( 'type', $activity ) ) {
|
|
|
|
return new \WP_Error(
|
|
|
|
'invalid_activity',
|
|
|
|
__( 'Activity must have a "type" field', 'pterotype' ),
|
|
|
|
array( 'status' => 400 )
|
|
|
|
);
|
|
|
|
}
|
2018-09-19 02:55:47 +00:00
|
|
|
$activitypub_id = $activity['id'];
|
2018-09-20 18:45:14 +00:00
|
|
|
$type = $activity['type'];
|
2018-09-19 22:08:16 +00:00
|
|
|
$wpdb->replace( 'pterotype_activities', array(
|
2018-09-19 02:55:47 +00:00
|
|
|
'activitypub_id' => $activitypub_id,
|
2018-09-20 18:45:14 +00:00
|
|
|
'type' => $type,
|
2018-09-19 02:55:47 +00:00
|
|
|
'activity' => wp_json_encode( $activity )
|
|
|
|
) );
|
2018-08-29 20:38:10 +00:00
|
|
|
return $activity;
|
|
|
|
}
|
|
|
|
|
2018-09-19 11:56:57 +00:00
|
|
|
function create_local_activity( $activity ) {
|
|
|
|
global $wpdb;
|
2018-09-20 18:45:14 +00:00
|
|
|
if ( !array_key_exists( 'type', $activity ) ) {
|
|
|
|
return new \WP_Error(
|
|
|
|
'invalid_activity',
|
|
|
|
__( 'Activity must have a "type" field', 'pterotype' ),
|
|
|
|
array( 'status' => 400 )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
$type = $activity['type'];
|
2018-09-19 21:33:20 +00:00
|
|
|
$res = $wpdb->insert( 'pterotype_activities', array(
|
2018-09-19 11:56:57 +00:00
|
|
|
'activity' => wp_json_encode( $activity )
|
|
|
|
) );
|
|
|
|
if ( !$res ) {
|
|
|
|
return new \WP_Error(
|
2018-09-19 15:16:41 +00:00
|
|
|
'db_error', __( 'Failed to insert activity row', 'pterotype' )
|
2018-09-19 11:56:57 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
$activity_id = $wpdb->insert_id;
|
2018-09-19 15:16:41 +00:00
|
|
|
$activity_url = get_rest_url( null, sprintf( '/pterotype/v1/activity/%d', $id ) );
|
2018-09-19 11:56:57 +00:00
|
|
|
$activity['id'] = $activity_url;
|
|
|
|
$res = $wpdb->replace(
|
2018-09-19 21:33:20 +00:00
|
|
|
'pterotype_activities',
|
2018-09-19 11:56:57 +00:00
|
|
|
array(
|
|
|
|
'id' => $activity_id,
|
|
|
|
'activitypub_id' => $activity_url,
|
2018-09-20 18:45:14 +00:00
|
|
|
'type' => $type,
|
|
|
|
'activity' => wp_json_encode( $activity ),
|
2018-09-19 11:56:57 +00:00
|
|
|
),
|
2018-09-20 18:45:14 +00:00
|
|
|
array( '%d', '%s', '%s', '%s' )
|
2018-09-19 11:56:57 +00:00
|
|
|
);
|
|
|
|
if ( !$res ) {
|
|
|
|
return new \WP_Error(
|
2018-09-19 15:16:41 +00:00
|
|
|
'db_error', __( 'Failed to hydrate activity id', 'pterotype' )
|
2018-09-19 11:56:57 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
return $activity;
|
|
|
|
}
|
2018-08-29 20:38:10 +00:00
|
|
|
?>
|