[WIP] Start moving actors to their own table; start Like activity

This commit is contained in:
Jeremy Dormitzer 2018-09-03 12:47:08 -04:00
parent 63a0328648
commit 3fc6fd546c
8 changed files with 69 additions and 4 deletions

View File

@ -3,7 +3,7 @@ namespace activities;
function get_activity( $id ) {
global $wpdb;
$activity_json = $wpdb->get_var( sprintf(
$activity_json = $wpdb->get_var( $wpdb->prepare(
'SELECT activity FROM activitypub_activities WHERE id = %d', $id
) );
if ( is_null( $activity_json ) ) {

View File

@ -1,5 +1,5 @@
<?php
namespace delete;
namespace activities\delete;
require_once plugin_dir_path( __FILE__ ) . '/../objects.php';

4
inc/activities/like.php Normal file
View File

@ -0,0 +1,4 @@
<?php
namespace activites\like;
?>

View File

@ -1,7 +1,35 @@
<?php
namespace actors;
function get_actor( $user ) {
function get_actor( $id ) {
global $wpdb;
$row = $wpdb->get_row( $wpdb->prepare(
'SELECT * FROM activitypub_actors WHERE id = %d', $id
) );
return get_user_from_row( $row );
}
function get_actor_by_slug ( $slug ) {
global $wpdb;
$row = $wpdb->get_row( $wpdb->prepare(
'SELECT * FROM activitypub_actors WHERE slug = %s', $slug
) );
return get_user_from_row( $row );
}
function get_user_from_row( $row ) {
switch ( $row->type ) {
case "user":
$user = get_user_by( 'slug', $row->slug );
return get_user_actor( $user );
case "commenter":
return new \WP_Error(
'not_implemented', __( 'Commenter actors not yet implemented', 'activitypub' )
);
}
}
function get_user_actor( $user ) {
$handle = get_the_author_meta( 'user_nicename', $user->get('ID'));
$actor = array(
"@context" => array( "https://www.w3.org/ns/activitystreams" ),
@ -25,4 +53,17 @@ function get_actor( $user ) {
);
return $actor;
}
function create_actors_table() {
global $wpdb;
$wpdb->query(
"
CREATE TABLE IF NOT EXISTS activitypub_actors(
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
slug VARCHAR(255) UNIQUE NOT NULL,
type VARCHAR(64)
);
"
);
}
?>

View File

@ -5,6 +5,7 @@ require_once plugin_dir_path( __FILE__ ) . '/outbox.php';
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';
add_action( 'rest_api_init', function() {
\api\register_routes();
@ -14,5 +15,6 @@ add_action( 'activitypub_init', function() {
\activities\create_activities_table();
\objects\create_object_table();
\outbox\create_outbox_table();
\actors\create_actors_table();
} );
?>

16
inc/likes.php Normal file
View File

@ -0,0 +1,16 @@
<?php
namespace likes;
function create_likes_table() {
global $wpdb;
$wpdb->query(
"
CREATE TABLE IF NOT EXISTS activitypub_likes (
actor_id INT NOT NULL,
object_url TEXT NOT NULL,
);
"
);
}
?>

View File

@ -44,7 +44,7 @@ function update_object( $object ) {
function get_object( $id ) {
global $wpdb;
$object_json = $wpdb->get_var( sprintf(
$object_json = $wpdb->get_var( $wpdb->prepare(
'SELECT object FROM activitypub_objects WHERE id = %d', $id
) );
if ( is_null( $object_json ) ) {

View File

@ -16,6 +16,7 @@ require_once plugin_dir_path( __FILE__ ) . '/activities.php';
require_once plugin_dir_path( __FILE__ ) . '/activities/create.php';
require_once plugin_dir_path( __FILE__ ) . '/activities/update.php';
require_once plugin_dir_path( __FILE__ ) . '/activities/delete.php';
require_once plugin_dir_path( __FILE__ ) . '/activities/like.php';
function handle_activity( $actor, $activity ) {
// TODO handle authentication/authorization
@ -43,6 +44,7 @@ function handle_activity( $actor, $activity ) {
case 'Remove':
break;
case 'Like':
$activity = \activities\like\handle( $actor, $activity );
break;
case 'Block':
break;