Commit fda3ca5a authored by swentel's avatar swentel

Issue #3167975: default image

parent 1e38d3a4
......@@ -3,9 +3,16 @@
## Public and Private keys
Public and private keys are saved in private://activitypub/keys.
The default path can be overridden in settings.php via config:
The default path can be overridden in settings.php via settings:
```
$config['activitypub.settings']['keys_path'] = '/your/path/';
$settings['activitypub_keys_path'] = '/your/path/';
```
## Default avatar
The default avatar path can be overridden in settings.php via settings:
```
$settings['activitypub_default_avatar_path'] = '/default/image.png';
```
......@@ -9,7 +9,7 @@ function activitypub_requirements($phase) {
$requirements = [];
if ($phase == 'runtime') {
$directory = \Drupal::config('activitypub.settings')->get('keys_path');
$directory = activitypub_keys_path();
if (\Drupal::hasService('stream_wrapper.private') && !is_dir($directory)) {
\Drupal::service('file_system')->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS);
}
......
......@@ -6,6 +6,7 @@
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Site\Settings;
/**
* Implements hook_entity_extra_field_info().
......@@ -26,6 +27,9 @@ function activitypub_entity_extra_field_info() {
* Implements hook_form_FORM_ID_alter().
*
* Add the ActivityPub form element to the user edit page.
*
* @param $form
* @param \Drupal\Core\Form\FormStateInterface $form_state
*/
function activitypub_form_user_form_alter(&$form, FormStateInterface $form_state) {
/** @var \Drupal\user\UserInterface $account */
......@@ -37,7 +41,27 @@ function activitypub_form_user_form_alter(&$form, FormStateInterface $form_state
/**
* Implements hook_nodeinfo_alter().
*
* @param $data
*/
function activitypub_nodeinfo_alter(&$data) {
\Drupal::service('activitypub.utility')->alterNodeInfo($data);
}
/**
* Returns the path where the keys are stored.
*
* @return mixed|null
*/
function activitypub_keys_path() {
return Settings::get('activitypub_keys_path', 'private://activitypub/keys');
}
/**
* Returns the default avatar path.
*
* @return mixed|null
*/
function activitypub_default_avatar_path() {
return Settings::get('activitypub_default_avatar_path', 'assets/avatar.png');
}
......@@ -10,7 +10,7 @@ services:
lazy: true
activitypub.keys:
class: Drupal\activitypub\Services\ActivityPubKeys
arguments: ['@config.factory', '@file_system', '@logger.channel.activitypub']
arguments: ['@file_system', '@logger.channel.activitypub']
activitypub.utility:
class: Drupal\activitypub\Services\ActivityPubUtility
arguments: ['@entity_type.manager', '@request_stack', '@config.factory']
......
avatar_style: 'thumbnail'
avatar_user_style: 'thumbnail'
avatar_user_field: 'user_picture'
keys_path: 'private://activitypub/keys'
......@@ -3,12 +3,9 @@ activitypub.settings:
type: config_object
label: 'ActivityPub'
mapping:
avatar_style:
avatar_user_style:
type: string
label: 'Avatar style'
label: 'Avatar user style'
avatar_user_field:
type: string
label: 'Avatar user field'
keys_path:
type: string
label: 'Path where the public and private keys are stored'
......@@ -75,10 +75,10 @@ class ActivityPubSettingsForm extends ConfigFormBase {
$config = $this->config('activitypub.settings');
$form['avatar_style'] = [
$form['avatar_user_style'] = [
'#title' => $this->t('Avatar image style'),
'#description' => $this->t('This image style will be used for the avatar for local and remote users.'),
'#default_value' => $config->get('avatar_style'),
'#default_value' => $config->get('avatar_user_style'),
'#type' => 'select',
'#options' => image_style_options(FALSE),
];
......@@ -108,7 +108,7 @@ class ActivityPubSettingsForm extends ConfigFormBase {
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->config('activitypub.settings')
->set('avatar_style', $form_state->getValue('avatar_style'))
->set('avatar_user_style', $form_state->getValue('avatar_user_style'))
->set('avatar_user_field', $form_state->getValue('avatar_user_field'))
->save();
parent::submitForm($form, $form_state);
......
......@@ -2,20 +2,12 @@
namespace Drupal\activitypub\Services;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\File\FileSystemInterface;
use phpseclib\Crypt\RSA;
use Psr\Log\LoggerInterface;
class ActivityPubKeys implements ActivityPubKeysInterface {
/**
* The system theme config object.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* The file system service.
*
......@@ -33,14 +25,11 @@ class ActivityPubKeys implements ActivityPubKeysInterface {
/**
* Constructs a ActivityPubKeys object.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory.
* @param \Drupal\Core\File\FileSystemInterface $file_system
* @param \Psr\Log\LoggerInterface $logger
* A logger
*/
public function __construct(ConfigFactoryInterface $config_factory, FileSystemInterface $file_system, LoggerInterface $logger) {
$this->configFactory = $config_factory;
public function __construct(FileSystemInterface $file_system, LoggerInterface $logger) {
$this->fileSystem = $file_system;
$this->logger = $logger;
}
......@@ -49,7 +38,7 @@ class ActivityPubKeys implements ActivityPubKeysInterface {
* {@inheritdoc}
*/
public function getPublicKey($path) {
$key_path = rtrim($this->configFactory->get('activitypub.settings')->get('keys_path'), '/') . '/' . $path . '/public.pem';
$key_path = rtrim(activitypub_keys_path(), '/') . '/' . $path . '/public.pem';
return file_get_contents($key_path);
}
......@@ -57,7 +46,7 @@ class ActivityPubKeys implements ActivityPubKeysInterface {
* {@inheritdoc}
*/
public function getPrivateKey($path) {
$key_path = rtrim($this->configFactory->get('activitypub.settings')->get('keys_path'), '/') . '/' . $path . '/private.pem';
$key_path = rtrim(activitypub_keys_path(), '/') . '/' . $path . '/private.pem';
return file_get_contents($key_path);
}
......@@ -89,7 +78,7 @@ class ActivityPubKeys implements ActivityPubKeysInterface {
$rsa = new RSA();
$keys = $rsa->createKey(4096);
$dir_path = rtrim($this->configFactory->get('activitypub.settings')->get('keys_path'), '/') . '/' . $path;
$dir_path = rtrim(activitypub_keys_path(), '/') . '/' . $path;
$this->fileSystem->prepareDirectory($dir_path, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS);
foreach (['public' => 'publickey', 'private' => 'privatekey'] as $filename => $rsakey) {
......
......@@ -70,18 +70,23 @@ class ActivityPubUtility implements ActivityPubUtilityInterface {
* @return string
*/
public function getActivityPubUserAvatar(UserInterface $user) {
$image_url = '';
$config = $this->configFactory->get('activitypub.settings');
$avatar_user_field = $config->get('avatar_user_field');
/** @var \Drupal\file\FileInterface $image */
if (!empty($avatar_user_field) && $user->hasField($avatar_user_field) && ($image = $user->get($avatar_user_field)->entity)) {
/** @var \Drupal\image\ImageStyleInterface $image_style */
$image_style = $this->imageStyleStorage->load($config->get('avatar_style'));
$image_style = $this->imageStyleStorage->load($config->get('avatar_user_style'));
$image_url = $image_style->buildUrl($image->getFileUri());
}
else {
$avatar_path = activitypub_default_avatar_path();
if ($avatar_path == 'assets/avatar.png') {
$avatar_path = drupal_get_path('module', 'activitypub') . '/' . $avatar_path;
}
$image_url = file_create_url($avatar_path);
}
return $image_url;
}
......
......@@ -18,6 +18,7 @@ class ActorTest extends ActivityPubTestBase {
*/
public function testOptInAndOptOut() {
$assert_session = $this->assertSession();
$page = $this->getSession()->getPage();
$this->enableActivityPub($assert_session);
$assert_session->responseContains('ActivityPub is enabled.');
......@@ -43,6 +44,11 @@ class ActorTest extends ActivityPubTestBase {
// By default we'll get back a 400, that's ok as an assertion :)
$assert_session->statusCodeEquals(400);
$this->drupalGet(Url::fromRoute('activitypub.stream.user', ['user' => $this->authenticatedUserOne->id()])->toString());
$assert_session->statusCodeEquals(200);
$content = json_decode($page->getContent());
self::assertTrue(strpos($content->image[0]->url, 'assets/avatar.png') !== FALSE);
$this->drupalLogin($this->authenticatedUserOne);
$this->drupalGet('activitypub/actor/delete/unknown');
$assert_session->statusCodeEquals(404);
......
......@@ -15,7 +15,6 @@ class OutboxTest extends ActivityPubTestBase {
/**
* Test outbox responses.
*
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws \Behat\Mink\Exception\ExpectationException
*/
public function testOutboxResponse() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment