activitypub-1.0.x-dev/activitypub.module

activitypub.module
<?php

/**
 * @file
 * ActivityPub module file.
 */

use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Site\Settings;
use Drupal\Core\Url;

const ACTIVITYPUB_OUTBOX_QUEUE = 'activitypub_outbox';
const ACTIVITYPUB_OUTBOX_SEND_QUEUE = 'activitypub_outbox_send';
const ACTIVITYPUB_INBOX_QUEUE = 'activitypub_inbox';
const ACTIVITYPUB_TEST_USER = 'https://example.com/user/random';

/**
 * Implements hook_help().
 */
function activitypub_help($route_name, RouteMatchInterface $route_match) {
  if ($route_name == 'help.page.activitypub') {
    $output = '<h3>' . t('Overview') . '</h3>';
    $output .= '<p>' . t('The ActivityPub module allows you to enable ActivityPub support on your Drupal site. ActivityPub is a decentralized social networking protocol that allows users to interact with each other across different platforms and services.') . '</p>';
    $output .= '<h3>' . t('Configuration') . '</h3>';
    $output .= '<p>' . t('To configure ActivityPub, go to the "Configuration" page and click on "ActivityPub" under the "Web services" section. Here you can configure various settings, such as the default ActivityPub actor, how to handle incoming and outgoing activities, and which content types to enable for ActivityPub.') . '</p>';
    $output .= '<h3>' . t('Using ActivityPub') . '</h3>';
    $output .= '<p>' . t('Once you have configured ActivityPub, you can use it to interact with other ActivityPub-enabled platforms and services. You can follow and be followed by other users, and you can share content (such as articles, blog posts, and comments) with other users.') . '</p>';
    $output .= '<h3>' . t('Troubleshooting') . '</h3>';
    $output .= '<p>' . t('If you are having trouble with ActivityPub, please check the module documentation and issue queue for known issues and solutions. You can also seek help from the Drupal community.') . '</p>';
    return [
      '#markup' => $output,
    ];
  }
}

/**
 * Implements hook_entity_predelete().
 */
function activitypub_entity_predelete(EntityInterface $entity) {
  \Drupal::service('activitypub.utility')->onEntityDelete($entity);
}

/**
 * Implements hook_form_BASE_FORM_ID_alter() for \Drupal\node\NodeForm.
 *
 * @param $form
 * @param \Drupal\Core\Form\FormStateInterface $form_state
 */
function activitypub_form_node_form_alter(&$form, FormStateInterface $form_state) {
  /** @var \Drupal\Core\Entity\EntityInterface $entity */
  $entity = $form_state->getFormObject()->getEntity();
  if ($entity) {
    \Drupal::service('activitypub.form_alter')->addActivityPubOutboxFormElement($form, $form_state, $entity);
  }
}

/**
 * Implements hook_form_FORM_BASE_ID_alter() for the comments form.
 *
 * @param $form
 * @param \Drupal\Core\Form\FormStateInterface $form_state
 */
function activitypub_form_comment_form_alter(&$form, FormStateInterface $form_state) {
  /** @var \Drupal\Core\Entity\EntityInterface $entity */
  $entity = $form_state->getFormObject()->getEntity();
  if ($entity) {
    \Drupal::service('activitypub.form_alter')->addActivityPubOutboxFormElement($form, $form_state, $entity);
    \Drupal::service('activitypub.form_alter')->alterCommentForm($form, $form_state, $entity);
  }
}

/**
 * Implements hook_nodeinfo_alter().
 *
 * @param $data
 */
function activitypub_nodeinfo_alter(&$data) {
  \Drupal::service('activitypub.utility')->alterNodeInfo($data);
}

/**
 * Implements hook_cron().
 */
function activitypub_cron() {
  if (\Drupal::config('activitypub.settings')->get('process_outbox_handler') == 'cron') {
    \Drupal::service('activitypub.process.client')->prepareOutboxQueue();
  }
  if (\Drupal::config('activitypub.settings')->get('process_outbox_handler') == 'cron') {
    \Drupal::service('activitypub.process.client')->handleOutboxQueue();
  }
  if (\Drupal::config('activitypub.settings')->get('process_inbox_handler') == 'cron') {
    \Drupal::service('activitypub.process.client')->handleInboxQueue();
  }
  if (\Drupal::config('activitypub.settings')->get('inbox_remove_x_days_handler') == 'cron') {
    \Drupal::service('activitypub.process.client')->removeOldActivities();
  }
}

/**
 * Returns a list of available bundles for comment.
 *
 * @return string[]
 *   An array of bundle labels, keyed by the bundle type name.
 */
function activitypub_comment_get_names() {
  $names = &drupal_static(__FUNCTION__);

  if (!isset($names)) {
    $names = [];
    $config_names = \Drupal::configFactory()->listAll('comment.type.');
    foreach ($config_names as $config_name) {
      $id = substr($config_name, strlen('comment.type.'));
      $names[$id] = $id;
    }
  }

  return $names;
}

/**
 * Returns the path where the keys are stored.
 *
 * @return string
 */
function activitypub_keys_path() {
  return Settings::get('activitypub_keys_path', 'private://activitypub/keys');
}

/**
 * Returns the path where the cache is stored.
 *
 * @return string
 */
function activitypub_cache_path() {
  return Settings::get('activitypub_cache_path', 'private://activitypub/cache');
}

/**
 * Returns the cache ttl.
 *
 * @return int
 */
function activitypub_cache_ttl() {
  return Settings::get('activitypub_cache_ttl', 604800);
}

/**
 * Implements hook_reader_channels().
 */
function activitypub_reader_channels() {
  return \Drupal::service('activitypub.reader')->getChannels();
}

/**
 * Implements hook_reader_sources().
 */
function activitypub_reader_sources($op) {
  return \Drupal::service('activitypub.reader')->getSourcesPage($op);
}

/**
 * Implements hook_reader_timeline().
 */
function activitypub_reader_timeline($id, $search = NULL) {
  return \Drupal::service('activitypub.reader')->getTimeline($id, $search);
}

/**
 * Implements hook_reader_timeline_actions().
 */
function activitypub_reader_timeline_actions($id) {
  return \Drupal::service('activitypub.reader')->getTimelineActions($id);
}

/**
 * Implements hook_reader_do_timeline_action().
 */
function activitypub_reader_do_timeline_action($action, $id) {
  return \Drupal::service('activitypub.reader')->doTimelineAction($action, $id);
}

/**
 * Implements hook_reader_post_actions().
 */
function activitypub_reader_post_actions($id, $item) {
  return \Drupal::service('activitypub.reader')->getPostActions($id, $item);
}

/**
 * Implements hook_reader_do_timeline_action().
 */
function activitypub_reader_do_post_action($action, $id, $items) {
  return \Drupal::service('activitypub.reader')->doPostAction($action, $id, $items);
}

/**
 * Implements hook_theme().
 */
function activitypub_theme($existing, $type, $theme, $path) {
  return [
    'activitypub_remote_activity' => [
      'variables' => [
        'activity' => NULL,
      ],
    ],
    'activitypub_remote_person' => [
      'variables' => [
        'person' => NULL,
      ],
    ],
  ];
}

/**
 * Implements hook_node_view().
 *
 * Adds a HTTP header and a <link> tag which give the activitypub link URL.
 */
function activitypub_node_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
  if ($view_mode == 'full' && empty($entity->in_preview)) {
    $activitypub_type_query = \Drupal::entityTypeManager()->getStorage('activitypub_type')->getQuery()->accessCheck();
    $activitypub_type_query
      ->condition('status', 1)
      ->condition('plugin.configuration.target_entity_type_id', $entity->getEntityType()->id())
      ->condition('plugin.configuration.target_bundle', $entity->bundle());
    $enabled_activitypub_types = $activitypub_type_query->execute();

    if ($enabled_activitypub_types) {

      $activitypub_url = Url::fromRoute('activitypub.node.json', ['node' => $entity->id()], ['absolute' => TRUE, 'query' => [ '_format' => 'activity_json'] ])->toString();
      $activitypub_link = [
        '#type' => 'html_tag',
        '#tag' => 'link',
        '#attributes' => [
          'rel' => 'alternate',
          'href' => $activitypub_url,
          'type' => "application/activity+json"
        ],
      ];
      $build['#attached']['html_head'][] = [$activitypub_link, 'activitypub'];
    }
  }
}

if (!function_exists('str_contains')) {
  function str_contains(string $haystack, string $needle) {
    return empty($needle) || strpos($haystack, $needle) !== false;
  }
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc