Implement Like activity
This commit is contained in:
parent
c4d8b7ad57
commit
21e6622fe5
@ -6,7 +6,9 @@ require_once plugin_dir_path( __FILE__ ) . '/../objects.php';
|
||||
function handle( $actor, $activity ) {
|
||||
if ( !array_key_exists( 'object', $activity ) ) {
|
||||
return new \WP_Error(
|
||||
'invalid_activity', __( 'Expected an object', 'activitypub' )
|
||||
'invalid_activity',
|
||||
__( 'Expected an object', 'activitypub' ),
|
||||
array( 'status' => 40 )
|
||||
);
|
||||
}
|
||||
$object = $activity['object'];
|
||||
|
@ -1,4 +1,31 @@
|
||||
<?php
|
||||
namespace activites\like;
|
||||
|
||||
require_once plugin_dir_path( __FILE__ ) . '/../likes.php';
|
||||
require_once plugin_dir_path( __FILE__ ) . '/../actors.php';
|
||||
|
||||
function handle( $actor, $activity ) {
|
||||
if ( !array_key_exists( 'object', $activity ) ) {
|
||||
return new \WP_Error(
|
||||
'invalid_activity',
|
||||
__( 'Expected an object', 'activitypub' ),
|
||||
array( 'status' => 400 )
|
||||
);
|
||||
}
|
||||
$object = $activity['object'];
|
||||
if ( !array_key_exists( 'id', $object ) ) {
|
||||
return new \WP_Error(
|
||||
'invalid_object',
|
||||
__( 'Expected an object id', 'activitypub' ),
|
||||
array( 'status' => 400 )
|
||||
);
|
||||
}
|
||||
$object_id = $object['id'];
|
||||
$actor_id = \actors\get_actor_id( $actor );
|
||||
$res = \likes\create_like( $actor_id, $object_id );
|
||||
if ( is_wp_error( $res ) ) {
|
||||
return $res;
|
||||
}
|
||||
return $activity;
|
||||
}
|
||||
?>
|
||||
|
@ -17,6 +17,13 @@ function get_actor_by_slug ( $slug ) {
|
||||
return get_user_from_row( $row );
|
||||
}
|
||||
|
||||
function get_actor_id( $slug ) {
|
||||
global $wpdb;
|
||||
return $wpdb->get_var( $wpdb->prepare(
|
||||
"SELECT slug FROM activitypub_actors WHERE slug = %s", $slug
|
||||
) );
|
||||
}
|
||||
|
||||
function get_user_from_row( $row ) {
|
||||
switch ( $row->type ) {
|
||||
case "user":
|
||||
|
@ -6,6 +6,7 @@ require_once plugin_dir_path( __FILE__ ) . '/api.php';
|
||||
require_once plugin_dir_path( __FILE__ ) . '/objects.php';
|
||||
require_once plugin_dir_path( __FILE__ ) . '/activities.php';
|
||||
require_once plugin_dir_path( __FILE__ ) . '/actors.php';
|
||||
require_once plugin_dir_path( __FILE__ ) . '/likes.php';
|
||||
|
||||
add_action( 'rest_api_init', function() {
|
||||
\api\register_routes();
|
||||
@ -22,5 +23,6 @@ add_action( 'activitypub_init', function() {
|
||||
\outbox\create_outbox_table();
|
||||
\actors\create_actors_table();
|
||||
\actors\initialize_user_actors();
|
||||
\likes\create_likes_table();
|
||||
} );
|
||||
?>
|
||||
|
@ -1,6 +1,13 @@
|
||||
<?php
|
||||
namespace likes;
|
||||
|
||||
function create_like( $actor_id, $object_id ) {
|
||||
global $wpdb;
|
||||
return $wpdb->insert(
|
||||
'activitypub_likes', array( 'actor_id' => $actor_id, 'object_url' => $object_id )
|
||||
);
|
||||
}
|
||||
|
||||
function create_likes_table() {
|
||||
global $wpdb;
|
||||
$wpdb->query(
|
||||
@ -8,7 +15,8 @@ function create_likes_table() {
|
||||
CREATE TABLE IF NOT EXISTS activitypub_likes (
|
||||
actor_id INT NOT NULL,
|
||||
object_url TEXT NOT NULL,
|
||||
|
||||
FOREIGN KEY actor_fk(actor_id)
|
||||
REFERENCES activitypub_actors(id)
|
||||
);
|
||||
"
|
||||
);
|
||||
|
Loading…
Reference in New Issue
Block a user