Commit 977cb652 authored by Nejc Koporec's avatar Nejc Koporec Committed by Robert Ragas
Browse files

Issue #3256100: Drupal 9 compatibility

parent 96731237
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@
  "license": "GPL-2.0+",
  "require": {
    "minishlink/web-push": "^6.0.3",
    "piwik/device-detector": "^3.0"
    "piwik/device-detector": "^3.0",
    "goalgorilla/open_social": "^10.0 || ^11.0"
  }
}
+66 −17
Original line number Diff line number Diff line
@@ -3,13 +3,18 @@
namespace Drupal\activity_send_push\Plugin\ActivitySend;

use Drupal\activity_send\Plugin\ActivitySendBase;
use Drupal\activity_creator\ActivityInterface;
use Drupal\Core\Config\ImmutableConfig;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Url;
use Drupal\message\Entity\Message;
use Drupal\social_pwa\WebPushManagerInterface;
use Drupal\social_pwa\WebPushPayload;
use Drupal\user\Entity\User;
use Drupal\user\UserInterface;
use Minishlink\WebPush\WebPush;
use Drupal\file\Entity\File;
use Psr\Log\LoggerInterface;

/**
 * Provides a 'PushActivitySend' activity action.
@@ -19,14 +24,63 @@ use Drupal\file\Entity\File;
 *  label = @Translation("Action that is triggered when a entity is created"),
 * )
 */
class PushActivitySend extends ActivitySendBase {
class PushActivitySend extends ActivitySendBase implements ContainerFactoryPluginInterface {

  /**
   * The web push manager.
   */
  protected WebPushManagerInterface $webPushManager;

  /**
   * The social PWA settings.
   */
  protected ImmutableConfig $settings;

  /**
   * The logger for this module.
   */
  protected LoggerInterface $logger;

  /**
   * {@inheritdoc}
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, WebPushManagerInterface $web_push_manager, ImmutableConfig $settings, LoggerInterface $logger) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->webPushManager = $web_push_manager;
    $this->settings = $settings;
    $this->logger = $logger;
  }

  /**
   * {@inheritdoc}
   *
   * Arguments have been adjusted for backwards compatibility with Open Social.
   */
  public function create($activity) {
  public static function create($container_or_activity, array $configuration = [], $plugin_id = NULL, $plugin_definition = NULL) {
    // Open Social 10 backwards compatibility layer.
    if ($container_or_activity instanceof ActivityInterface) {
      if (isset($this)) {
        $this->process($container_or_activity);
      }
      return;
    }

    return new static(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $container_or_activity->get('social_pwa.web_push_manager'),
      $container_or_activity->get('config.factory')->get('social_pwa.settings'),
      $container_or_activity->get('logger.factory')->get('activity_send_push')
    );
  }

  /**
   * {@inheritdoc}
   */
  public function process(ActivityInterface $activity): void {
    // If push is disabled then we don't do anything.
    $push_enabled = \Drupal::config('social_pwa.settings')->get('status.all');
    $push_enabled = $this->settings->get('status.all');
    if (!$push_enabled) {
      return;
    }
@@ -39,20 +93,17 @@ class PushActivitySend extends ActivitySendBase {
      // We log this as a debug message since it doesn't make sense to use push
      // notifications for things that don't affect users. Detecting these
      // scenarios helps us improve the notification system.
      \Drupal::logger('activity_send_push')->debug('Tried to send push notification for activity from message (%mid) that did not have a user recipient.', ['%mid' => $activity->field_activity_message->target_id]);
      $this->logger->debug('Tried to send push notification for activity from message (%mid) that did not have a user recipient.', ['%mid' => $activity->field_activity_message->target_id]);
      return;
    }

    $user = User::load($uid);
    if (!$user instanceof UserInterface) {
      \Drupal::logger('activity_send_push')->debug('Ignored push notification for non-existing user.');
      $this->logger->debug('Ignored push notification for non-existing user.');
      return;
    }

    /** @var \Drupal\social_pwa\WebPushManagerInterface $web_push_manager */
    $web_push_manager = \Drupal::service('social_pwa.web_push_manager');

    $user_subscriptions = $web_push_manager->getSubscriptionsForUser($user);
    $user_subscriptions = $this->webPushManager->getSubscriptionsForUser($user);

    // If the user has no push subscriptions then we can stop early.
    if (empty($user_subscriptions)) {
@@ -63,7 +114,7 @@ class PushActivitySend extends ActivitySendBase {
    $message_loaded = Message::load($activity->field_activity_message->target_id);
    $message = $message_loaded->getText();
    if (empty($message[0])) {
      \Drupal::logger('activity_send_push')->error('Tried to send an empty push notification for mid: %mid', ['%mid' => $activity->field_activity_message->target_id]);
      $this->logger->error('Tried to send an empty push notification for mid: %mid', ['%mid' => $activity->field_activity_message->target_id]);
      return;
    }
    $message_to_send = $message[0];
@@ -73,21 +124,19 @@ class PushActivitySend extends ActivitySendBase {
    // anywhere for the user to go to when they click the push notification so
    // we shouldn't send a notification at all.
    if (!($url instanceof Url)) {
      \Drupal::logger('activity_send_push')->error("Tried to send push notification for mid: %mid but the target entity doesn't have a canonical url", ['%mid' => $activity->field_activity_message->target_id]);
      $this->logger->error("Tried to send push notification for mid: %mid but the target entity doesn't have a canonical url", ['%mid' => $activity->field_activity_message->target_id]);
      return;
    }

    $pwa_settings = \Drupal::config('social_pwa.settings');

    // Set fields for payload.
    $message_to_send = html_entity_decode($message_to_send);
    $push_data = [
      'message' => strip_tags($message_to_send),
      'site_name' => $pwa_settings->get('name'),
      'site_name' => $this->settings->get('name'),
      'url' => $url->toString(),
    ];

    $icon = $pwa_settings->get('icons.icon');
    $icon = $this->settings->get('icons.icon');
    if (!empty($icon)) {
      // Get the file id and path.
      $fid = $icon[0];
@@ -101,7 +150,7 @@ class PushActivitySend extends ActivitySendBase {
    // Encode payload.
    $serialized_payload = (new WebPushPayload('legacy', $push_data))->toJson();

    $auth = $web_push_manager->getAuth();
    $auth = $this->webPushManager->getAuth();
    $webPush = new WebPush($auth);

    foreach ($user_subscriptions as $subscription) {
@@ -120,7 +169,7 @@ class PushActivitySend extends ActivitySendBase {
    }

    if (!empty($outdated_subscriptions)) {
      $web_push_manager->removeSubscriptionsForUser($user, $outdated_subscriptions);
      $this->webPushManager->removeSubscriptionsForUser($user, $outdated_subscriptions);
    }
  }

+0 −1
Original line number Diff line number Diff line
name: 'Social PWA'
description: 'Makes the website "Installable" like an App on smartphones and enhances it with Service Workers for the use of Push Notifications and an Offline User Experience.'
type: module
core: 8.x
core_version_requirement: ^8 || ^9
package: Social
configure: social_pwa.settings
+4 −4
Original line number Diff line number Diff line
@@ -7,7 +7,7 @@ use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RequestContext;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Path\PathValidatorInterface;
use Drupal\Core\Path\AliasManagerInterface;
use Drupal\path_alias\AliasManagerInterface;
use Drupal\file\Entity\File;
use Symfony\Component\DependencyInjection\ContainerInterface;

@@ -19,7 +19,7 @@ class ManifestSettingsForm extends ConfigFormBase {
  /**
   * The path alias manager.
   *
   * @var \Drupal\Core\Path\AliasManagerInterface
   * @var \Drupal\path_alias\AliasManagerInterface
   */
  protected $aliasManager;

@@ -42,7 +42,7 @@ class ManifestSettingsForm extends ConfigFormBase {
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The factory for configuration objects.
   * @param \Drupal\Core\Path\AliasManagerInterface $alias_manager
   * @param \Drupal\path_alias\AliasManagerInterface $alias_manager
   *   The path alias manager.
   * @param \Drupal\Core\Path\PathValidatorInterface $path_validator
   *   The path validator.
@@ -62,7 +62,7 @@ class ManifestSettingsForm extends ConfigFormBase {
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('config.factory'),
      $container->get('path.alias_manager'),
      $container->get('path_alias.manager'),
      $container->get('path.validator'),
      $container->get('router.request_context')
    );