pterotype/inc/objects.php

263 lines
7.9 KiB
PHP
Raw Normal View History

<?php
namespace objects;
2018-09-01 14:14:11 +00:00
// TODO for 'post' objects, store a post id instead of the full post text,
// then hydrate the text on read
2018-09-19 11:56:57 +00:00
function create_local_object( $object ) {
global $wpdb;
if ( !array_key_exists( 'type', $object) ) {
return new \WP_Error(
'invalid_object',
__( 'Object must have a "type" field', 'pterotype' ),
array( 'status' => 400 )
);
}
2018-09-19 21:33:20 +00:00
$res = $wpdb->insert( 'pterotype_objects', array(
'object' => wp_json_encode( $object )
) );
2018-09-01 13:37:01 +00:00
if ( !$res ) {
return new \WP_Error(
2018-09-19 15:16:41 +00:00
'db_error', __( 'Failed to insert object row', 'pterotype' )
2018-09-01 13:37:01 +00:00
);
}
2018-09-19 11:56:57 +00:00
$object_id = $wpdb->insert_id;
$type = $object['type'];
2018-09-19 15:16:41 +00:00
$object_url = get_rest_url( null, sprintf( '/pterotype/v1/object/%d', $object_id ) );
2018-09-24 12:51:50 +00:00
$object_likes = get_rest_url( null, sprintf( '/pterotype/v1/object/%d/likes', $object_id ) );
$object_shares = get_rest_url(
null, sprintf( '/pterotype/v1/object/%d/shares', $object_id )
);
2018-09-19 11:56:57 +00:00
$object['id'] = $object_url;
2018-09-24 12:51:50 +00:00
$object['likes'] = $object_likes;
$object['shares'] = $object_shares;
2018-09-19 11:56:57 +00:00
$res = $wpdb->replace(
2018-09-19 21:33:20 +00:00
'pterotype_objects',
2018-09-19 11:56:57 +00:00
array (
'id' => $object_id,
'activitypub_id' => $object_url,
'type' => $type,
2018-09-19 11:56:57 +00:00
'object' => wp_json_encode( $object )
),
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 object id', 'pterotype' )
2018-09-19 11:56:57 +00:00
);
}
return $object;
}
2018-09-19 12:54:07 +00:00
function upsert_object( $object ) {
global $wpdb;
if ( !array_key_exists( 'id', $object ) ) {
return new \WP_Error(
'invalid_object',
2018-09-19 15:16:41 +00:00
__( 'Objects must have an "id" field', 'pterotype' ),
2018-09-19 12:54:07 +00:00
array( 'status' => 400 )
);
}
if ( !array_key_exists( 'type', $object) ) {
return new \WP_Error(
'invalid_object',
__( 'Object must have a "type" field', 'pterotype' ),
array( 'status' => 400 )
);
}
2018-09-19 12:54:07 +00:00
$row = $wpdb->get_row( $wpdb->prepare(
2018-09-19 21:33:20 +00:00
'SELECT * FROM pterotype_objects WHERE activitypub_url = %s', $object['id']
2018-09-19 12:54:07 +00:00
) );
$res = true;
if ( $row === null ) {
$res = $wpdb->insert(
2018-09-19 21:33:20 +00:00
'pterotype_objects',
2018-09-19 12:54:07 +00:00
array(
'activitypub_id' => $object['id'],
'type' => $object['type'],
2018-09-19 12:54:07 +00:00
'object' => wp_json_encode( $object )
)
);
} else {
$res = $wpdb->replace(
2018-09-19 21:33:20 +00:00
'pterotype_objects',
2018-09-19 12:54:07 +00:00
array(
'id' => $row->id,
'activitypub_id' => $object['id'],
'type' => $object['type'],
2018-09-19 12:54:07 +00:00
'object' => wp_json_encode( $object )
),
array( '%d', '%s', '%s', '%s' )
2018-09-19 12:54:07 +00:00
);
$row = new stdClass();
$row->id = $wpdb->insert_id;
$activites_res = $wpdb->query( $wpdb->prepare(
'
UPDATE pterotype_activities
SET activity = JSON_SET(activity, "$.object", %s)
WHERE activity->"$.object.id" = %s;
',
wp_json_encode( $object ), $object['id']
) );
if ( $activities_res === false ) {
return new \WP_Error(
'db_error', __( 'Failed to update associated activities', 'pterotype' )
);
}
2018-09-19 12:54:07 +00:00
}
if ( !$res ) {
return new \WP_Error(
2018-09-19 15:16:41 +00:00
'db_error', __( 'Failed to upsert object row', 'pterotype' )
2018-09-19 12:54:07 +00:00
);
}
$row->object = $object;
return $row;
}
2018-09-01 13:37:01 +00:00
function update_object( $object ) {
global $wpdb;
if ( !array_key_exists( 'id', $object ) ) {
return new \WP_Error(
'invalid_object',
2018-09-19 15:16:41 +00:00
__( 'Object must have an "id" field', 'pterotype' ),
2018-09-01 13:37:01 +00:00
array( 'status' => 400 )
);
}
$object_json = wp_json_encode( $object );
$res = $wpdb->update(
2018-09-22 13:24:03 +00:00
'pterotype_objects',
2018-09-01 13:37:01 +00:00
array( 'object' => $object_json ),
array( 'id' => $id ),
'%s', '%d' );
if ( !$res ) {
return new \WP_Error(
2018-09-19 15:16:41 +00:00
'db_error', __( 'Failed to update object row', 'pterotype' )
2018-09-01 13:37:01 +00:00
);
}
$activites_res = $wpdb->query( $wpdb->prepare(
'
UPDATE pterotype_activities
SET activity = JSON_SET(activity, "$.object", %s)
WHERE activity->"$.object.id" = %s;
',
$object_json, $object['id']
) );
if ( $activities_res === false ) {
return new \WP_Error(
'db_error', __( 'Failed to update associated activities', 'pterotype' )
);
}
2018-09-01 13:37:01 +00:00
return $object;
}
function get_object( $id ) {
global $wpdb;
$object_json = $wpdb->get_var( $wpdb->prepare(
2018-09-19 21:33:20 +00:00
'SELECT object FROM pterotype_objects WHERE id = %d', $id
2018-08-29 20:38:10 +00:00
) );
if ( is_null( $object_json ) ) {
return new \WP_Error(
2018-09-19 15:16:41 +00:00
'not_found', __( 'Object not found', 'pterotype' ), array( 'status' => 404 )
2018-08-29 20:38:10 +00:00
);
}
return json_decode( $object_json, true );
}
function get_object_by_activitypub_id( $activitypub_id ) {
global $wpdb;
$object_json = $wpdb->get_var( $wpdb->prepare(
2018-09-19 21:33:20 +00:00
'SELECT object FROM pterotype_objects WHERE activitypub_id = %s', $activitypub_id
) );
if ( is_null( $object_json ) ) {
return new \WP_Error(
2018-09-19 15:16:41 +00:00
'not_found', __( 'Object not found', 'pterotype' ), array( 'status' => 404 )
);
}
return json_decode( $object_json, true );
}
2018-09-23 23:29:20 +00:00
function get_object_id( $activitypub_id ) {
global $wpdb;
return $wpdb->get_var( $wpdb->prepare(
'SELECT id FROM pterotype_objects WHERE activitypub_id = %s', $activitypub_id
) );
}
2018-09-01 14:14:11 +00:00
function delete_object( $object ) {
global $wpdb;
if ( !array_key_exists( 'id', $object ) ) {
return new \WP_Error(
'invalid_object',
2018-09-19 15:16:41 +00:00
__( 'Object must have an "id" field', 'pterotype' ),
2018-09-01 14:14:11 +00:00
array( 'status' => 400 )
);
}
if ( !array_key_exists( 'type', $object ) ) {
return new \WP_Error(
'invalid_object',
__( 'Object must have a "type" field', 'pterotype' ),
array( 'status' => 400 )
);
}
$activitypub_id = $object['id'];
$tombstone = make_tombstone( $object );
$res = $wpdb->replace(
'pterotype_objects',
array(
'activitypub_id' => $activitypub_id,
'type' => $tombstone['type'],
'object' => wp_json_encode( $tombstone ),
),
2018-09-22 04:42:09 +00:00
array( '%s', '%s', '%s' )
);
2018-09-01 14:14:11 +00:00
if ( !$res ) {
2018-09-19 15:16:41 +00:00
return new \WP_Error( 'db_error', __( 'Error deleting object', 'pterotype' ) );
2018-09-01 14:14:11 +00:00
}
$res = $wpdb->query( $wpdb->prepare(
'
UPDATE pterotype_activities
SET activity = JSON_SET(activity, "$.object", %s)
WHERE activity->"$.object.id" = %s;
',
wp_json_encode( $tombstone ), $object['id']
) );
if ( $res === false ) {
return new \WP_Error(
'db_error', __( 'Failed to update associated activities', 'pterotype' )
);
}
return $tombstone;
}
function make_tombstone( $object ) {
$tombstone = array(
'type' => 'Tombstone',
'formerType' => $object['type'],
'id' => $object['id'],
'deleted' => date( \DateTime::ISO8601, time() ),
);
return $tombstone;
2018-09-01 14:14:11 +00:00
}
2018-09-24 02:22:30 +00:00
function is_local_object( $object ) {
2018-09-26 03:46:31 +00:00
if ( is_array( $object ) ) {
if ( array_key_exists( 'id', $object ) ) {
$parsed = parse_url( $object['id'] );
if ( $parsed ) {
$site_host = parse_url( get_site_url() )['host'];
return $parsed['host'] === $site_host;
}
} else {
return false;
}
} else {
$parsed = parse_url( $object );
2018-09-24 02:22:30 +00:00
if ( $parsed ) {
$site_host = parse_url( get_site_url() )['host'];
return $parsed['host'] === $site_host;
}
}
return false;
}
?>