Commit 8a9bfa9c authored by Bogdan Konkevich's avatar Bogdan Konkevich Committed by Artem Sylchuk
Browse files

Issue #3179135 by id.conky: Delete Conversations as Admin

parent 15e5ab3e
Loading
Loading
Loading
Loading
+29 −7
Original line number Diff line number Diff line
@@ -48,6 +48,13 @@ function private_message_entity_extra_field_info() {
      'weight' => -100,
      'visible' => TRUE,
    ];

    $fields['private_message_thread'][$bundle]['display']['clear_history_link'] = [
      'label' => t('Clear history link'),
      'description' => t('The link to clear personal messages history in the thread'),
      'weight' => -100,
      'visible' => TRUE,
    ];
  }

  $private_message_bundles = \Drupal::service('entity_type.bundle.info')->getBundleInfo('private_message');
@@ -160,6 +167,7 @@ function private_message_private_message_thread_view(array &$build, EntityInterf

  if ($display->getComponent('delete_link')) {
    $url = Url::fromRoute('entity.private_message_thread.delete_form', ['private_message_thread' => $entity->id()]);
    if ($url->access()) {
      $build['delete_link'] = [
        '#prefix' => '<div class="private_message_thread_delete_link_wrapper">',
        '#suffix' => '</div>',
@@ -170,6 +178,20 @@ function private_message_private_message_thread_view(array &$build, EntityInterf
    }
  }

  if ($display->getComponent('clear_history_link')) {
    $url = Url::fromRoute('entity.private_message_thread.clear_personal_history_form', ['private_message_thread' => $entity->id()]);
    if ($url->access()) {
      $build['clear_history_link'] = [
        '#prefix' => '<div class="private_message_thread_clear_history_link_wrapper">',
        '#suffix' => '</div>',
        '#type' => 'link',
        '#url' => $url,
        '#title' => t('Clear history'),
      ];
    }
  }
}

/**
 * Implements hook_ENTITY_TYPE_view().
 *
+6 −0
Original line number Diff line number Diff line
@@ -2,10 +2,16 @@ use private messaging system:
  title: 'Use private messaging system'
  description: 'Allows users to send and receive private messages'

delete private message thread for all:
  title: 'Delete private message thread for all participants'
  description: 'Allows users to delete thread completely for all it members'

administer private messages:
  title: 'Administer private messages'
  description: 'Allows administrators to administer private messages'
  restrict access: true

administer private message module:
  title: 'Administer private message module'
  description: 'Allows administrators to manage all parts of the One Page Private Message module'
  restrict access: true
+9 −0
Original line number Diff line number Diff line
@@ -27,6 +27,15 @@ entity.private_message_thread.delete_form:
    _entity_access: 'private_message_thread.delete'
    _permission: 'use private messaging system,access user profiles'

entity.private_message_thread.clear_personal_history_form:
  path: '/private-messages/{private_message_thread}/clear-personal-history'
  defaults:
    _entity_form: private_message_thread.clear_personal_history
    _title: 'Clear personal history in thread'
  requirements:
    _entity_access: 'private_message_thread.clear_personal_history'
    _permission: 'use private messaging system,access user profiles'

private_message.private_message_create:
  path: '/private-message/create'
  defaults:
+11 −0
Original line number Diff line number Diff line
@@ -34,6 +34,17 @@ class PrivateMessageThreadAccessControlHandler extends EntityAccessControlHandle
          break;

        case 'delete':
          // Allow delete if we are member of this thread
          // And if we have permission to delete thread for everyone.
          if ($entity->isMember($account->id())
            && $account->hasPermission('delete private message thread for all')) {
            return AccessResult::allowed();
          }

          break;

        case 'clear_personal_history':
          // We can clear personal history only if we are member of this thread.
          if ($entity->isMember($account->id())) {
            return AccessResult::allowed();
          }
+28 −17
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ use Drupal\Core\Session\AccountInterface;
 *     "access" = "Drupal\private_message\Entity\Access\PrivateMessageThreadAccessControlHandler",
 *     "form" = {
 *       "delete" = "Drupal\private_message\Form\PrivateMessageThreadDeleteForm",
 *       "clear_personal_history" = "Drupal\private_message\Form\PrivateMessageThreadClearPersonalHistoryForm",
 *     },
 *   },
 *   base_table = "private_message_threads",
@@ -223,25 +224,35 @@ class PrivateMessageThread extends ContentEntityBase implements PrivateMessageTh
  /**
   * {@inheritdoc}
   */
  public function delete(AccountInterface $account = NULL) {
    if ($account) {
  public function delete() {
    $this->deleteReferencedEntities();
    parent::delete();
    $this->clearCacheTags();
  }

  /**
   * {@inheritDoc}
   */
  public function clearAccountHistory(AccountInterface $account = NULL) {
    if (!$account) {
      $account = \Drupal::currentUser();
    }
    // Update thread deleted time for account.
    $this->updateLastDeleteTime($account);

    // Get timestamp when last message was created.
    $last_creation_timestamp = $this->getNewestMessageCreationTimestamp();

    // Query thread history table to get deleted timestamp.
    $query = \Drupal::database()->select('pm_thread_history', 'pm_thread_history')
      ->condition('thread_id', $this->id());
    $query->addExpression('MIN(delete_timestamp)', 'min_deleted');
    $min_deleted = $query->execute()->fetchField();

    // If no messages have been created after every member has deleted thread.
    if ($min_deleted >= $last_creation_timestamp) {
        $this->deleteReferencedEntities();
        parent::delete();
      $this->delete();
    }
    }
    else {
      $this->deleteReferencedEntities();
      parent::delete();
    }

    $this->clearCacheTags();
  }

Loading