Merge pull request #31581 from nextcloud/refactor/carl/comment-app

Modernize comments app
pull/31487/head
Carl Schwan 4 years ago committed by GitHub
commit 92a63e6189
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 25
      apps/comments/lib/Activity/Listener.php
  2. 70
      apps/comments/lib/Activity/Provider.php
  3. 7
      apps/comments/lib/AppInfo/Application.php
  4. 16
      apps/comments/lib/Collaboration/CommentersSorter.php
  5. 37
      apps/comments/lib/Controller/Notifications.php
  6. 3
      apps/comments/lib/Listener/LoadSidebarScripts.php
  7. 9
      apps/comments/lib/Notification/Listener.php
  8. 19
      apps/comments/lib/Notification/Notifier.php
  9. 15
      apps/comments/lib/Search/CommentsSearchProvider.php
  10. 3
      apps/comments/lib/Search/LegacyProvider.php
  11. 4
      apps/comments/tests/Unit/AppInfo/ApplicationTest.php

@ -35,28 +35,15 @@ use OCP\IUserSession;
use OCP\Share\IShareHelper;
class Listener {
/** @var IManager */
protected $activityManager;
/** @var IUserSession */
protected $session;
/** @var \OCP\App\IAppManager */
protected $appManager;
/** @var \OCP\Files\Config\IMountProviderCollection */
protected $mountCollection;
/** @var \OCP\Files\IRootFolder */
protected $rootFolder;
/** @var IShareHelper */
protected $shareHelper;
protected IManager $activityManager;
protected IUserSession $session;
protected IAppManager $appManager;
protected IMountProviderCollection $mountCollection;
protected IRootFolder $rootFolder;
protected IShareHelper $shareHelper;
/**
* Listener constructor.
*
* @param IManager $activityManager
* @param IUserSession $session
* @param IAppManager $appManager
* @param IMountProviderCollection $mountCollection
* @param IRootFolder $rootFolder
* @param IShareHelper $shareHelper
*/
public function __construct(IManager $activityManager,
IUserSession $session,

@ -37,34 +37,15 @@ use OCP\L10N\IFactory;
class Provider implements IProvider {
/** @var IFactory */
protected $languageFactory;
/** @var IL10N */
protected $l;
/** @var IURLGenerator */
protected $url;
/** @var ICommentsManager */
protected $commentsManager;
/** @var IUserManager */
protected $userManager;
/** @var IManager */
protected $activityManager;
protected IFactory $languageFactory;
protected ?IL10N $l = null;
protected IUrlGenerator $url;
protected ICommentsManager $commentsManager;
protected IUserManager $userManager;
protected IManager $activityManager;
/** @var string[] */
protected $displayNames = [];
protected array $displayNames = [];
/**
* @param IFactory $languageFactory
* @param IURLGenerator $url
* @param ICommentsManager $commentsManager
* @param IUserManager $userManager
* @param IManager $activityManager
*/
public function __construct(IFactory $languageFactory, IURLGenerator $url, ICommentsManager $commentsManager, IUserManager $userManager, IManager $activityManager) {
$this->languageFactory = $languageFactory;
$this->url = $url;
@ -111,11 +92,9 @@ class Provider implements IProvider {
}
/**
* @param IEvent $event
* @return IEvent
* @throws \InvalidArgumentException
*/
protected function parseShortVersion(IEvent $event) {
protected function parseShortVersion(IEvent $event): IEvent {
$subjectParameters = $this->getSubjectParameters($event);
if ($event->getSubject() === 'add_comment_subject') {
@ -137,11 +116,9 @@ class Provider implements IProvider {
}
/**
* @param IEvent $event
* @return IEvent
* @throws \InvalidArgumentException
*/
protected function parseLongVersion(IEvent $event) {
protected function parseLongVersion(IEvent $event): IEvent {
$subjectParameters = $this->getSubjectParameters($event);
if ($event->getSubject() === 'add_comment_subject') {
@ -170,7 +147,7 @@ class Provider implements IProvider {
return $event;
}
protected function getSubjectParameters(IEvent $event) {
protected function getSubjectParameters(IEvent $event): array {
$subjectParameters = $event->getSubjectParameters();
if (isset($subjectParameters['fileId'])) {
return $subjectParameters;
@ -190,10 +167,7 @@ class Provider implements IProvider {
];
}
/**
* @param IEvent $event
*/
protected function parseMessage(IEvent $event) {
protected function parseMessage(IEvent $event): void {
$messageParameters = $event->getMessageParameters();
if (empty($messageParameters)) {
// Email
@ -228,12 +202,7 @@ class Provider implements IProvider {
}
}
/**
* @param int $id
* @param string $path
* @return array
*/
protected function generateFileParameter($id, $path) {
protected function generateFileParameter(int $id, string $path): array {
return [
'type' => 'file',
'id' => $id,
@ -243,11 +212,7 @@ class Provider implements IProvider {
];
}
/**
* @param string $uid
* @return array
*/
protected function generateUserParameter($uid) {
protected function generateUserParameter(string $uid): array {
if (!isset($this->displayNames[$uid])) {
$this->displayNames[$uid] = $this->getDisplayName($uid);
}
@ -259,16 +224,11 @@ class Provider implements IProvider {
];
}
/**
* @param string $uid
* @return string
*/
protected function getDisplayName($uid) {
protected function getDisplayName(string $uid): string {
$user = $this->userManager->get($uid);
if ($user instanceof IUser) {
return $user->getDisplayName();
} else {
return $uid;
}
return $uid;
}
}

@ -46,6 +46,7 @@ use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\Comments\CommentsEntityEvent;
use OCP\ISearch;
use OCP\IServerContainer;
use OCP\Comments\ICommentsManager;
class Application extends App implements IBootstrap {
public const APP_ID = 'comments';
@ -84,9 +85,9 @@ class Application extends App implements IBootstrap {
$context->getServerContainer()->get(ISearch::class)->registerProvider(LegacyProvider::class, ['apps' => ['files']]);
}
protected function registerCommentsEventHandler(IServerContainer $container) {
$container->getCommentsManager()->registerEventHandler(function () {
return $this->getContainer()->query(EventHandler::class);
protected function registerCommentsEventHandler(IServerContainer $container): void {
$container->get(ICommentsManager::class)->registerEventHandler(function (): EventHandler {
return $this->getContainer()->get(EventHandler::class);
});
}
}

@ -28,8 +28,7 @@ use OCP\Comments\ICommentsManager;
class CommentersSorter implements ISorter {
/** @var ICommentsManager */
private $commentsManager;
private ICommentsManager $commentsManager;
public function __construct(ICommentsManager $commentsManager) {
$this->commentsManager = $commentsManager;
@ -77,12 +76,7 @@ class CommentersSorter implements ISorter {
}
}
/**
* @param $type
* @param $id
* @return array
*/
protected function retrieveCommentsInformation($type, $id) {
protected function retrieveCommentsInformation(string $type, string $id): array {
$comments = $this->commentsManager->getForObject($type, $id);
if (count($comments) === 0) {
return [];
@ -102,12 +96,12 @@ class CommentersSorter implements ISorter {
return $actors;
}
protected function compare(array $a, array $b, array $commenters) {
protected function compare(array $a, array $b, array $commenters): int {
$a = $a['value']['shareWith'];
$b = $b['value']['shareWith'];
$valueA = isset($commenters[$a]) ? $commenters[$a] : 0;
$valueB = isset($commenters[$b]) ? $commenters[$b] : 0;
$valueA = $commenters[$a] ?? 0;
$valueB = $commenters[$b] ?? 0;
return $valueB - $valueA;
}

@ -43,34 +43,18 @@ use OCP\Notification\IManager;
* @package OCA\Comments\Controller
*/
class Notifications extends Controller {
/** @var IRootFolder */
protected $rootFolder;
/** @var ICommentsManager */
protected $commentsManager;
/** @var IURLGenerator */
protected $urlGenerator;
/** @var IManager */
protected $notificationManager;
/** @var IUserSession */
protected $userSession;
protected IRootFolder $rootFolder;
protected ICommentsManager $commentsManager;
protected IURLGenerator $urlGenerator;
protected IManager $notificationManager;
protected IUserSession $userSession;
/**
* Notifications constructor.
*
* @param string $appName
* @param IRequest $request
* @param ICommentsManager $commentsManager
* @param IRootFolder $rootFolder
* @param IURLGenerator $urlGenerator
* @param IManager $notificationManager
* @param IUserSession $userSession
*/
public function __construct(
$appName,
string $appName,
IRequest $request,
ICommentsManager $commentsManager,
IRootFolder $rootFolder,
@ -89,11 +73,8 @@ class Notifications extends Controller {
/**
* @PublicPage
* @NoCSRFRequired
*
* @param string $id the comment ID
* @return Response
*/
public function view($id) {
public function view(string $id): Response {
$currentUser = $this->userSession->getUser();
if (!$currentUser instanceof IUser) {
return new RedirectResponse(
@ -133,10 +114,8 @@ class Notifications extends Controller {
/**
* Marks the notification about a comment as processed
* @param IComment $comment
* @param IUser $currentUser
*/
protected function markProcessed(IComment $comment, IUser $currentUser) {
protected function markProcessed(IComment $comment, IUser $currentUser): void {
$notification = $this->notificationManager->createNotification();
$notification->setApp('comments')
->setObject('comment', $comment->getId())

@ -35,8 +35,7 @@ use OCP\Util;
class LoadSidebarScripts implements IEventListener {
/** @var ICommentsManager */
private $commentsManager;
private ICommentsManager $commentsManager;
public function __construct(ICommentsManager $commentsManager) {
$this->commentsManager = $commentsManager;

@ -29,17 +29,12 @@ use OCP\IUserManager;
use OCP\Notification\IManager;
class Listener {
/** @var IManager */
protected $notificationManager;
/** @var IUserManager */
protected $userManager;
protected IManager $notificationManager;
protected IUserManager $userManager;
/**
* Listener constructor.
*
* @param IManager $notificationManager
* @param IUserManager $userManager
*/
public function __construct(
IManager $notificationManager,

@ -38,20 +38,11 @@ use OCP\Notification\INotifier;
class Notifier implements INotifier {
/** @var IFactory */
protected $l10nFactory;
/** @var IRootFolder */
protected $rootFolder;
/** @var ICommentsManager */
protected $commentsManager;
/** @var IURLGenerator */
protected $url;
/** @var IUserManager */
protected $userManager;
protected IFactory $l10nFactory;
protected IRootFolder $rootFolder;
protected ICommentsManager $commentsManager;
protected IURLGenerator $url;
protected IUserManager $userManager;
public function __construct(
IFactory $l10nFactory,

@ -40,17 +40,10 @@ use function pathinfo;
class CommentsSearchProvider implements IProvider {
/** @var IUserManager */
private $userManager;
/** @var IL10N */
private $l10n;
/** @var IURLGenerator */
private $urlGenerator;
/** @var LegacyProvider */
private $legacyProvider;
private IUserManager $userManager;
private IL10N $l10n;
private IURLGenerator $urlGenerator;
private LegacyProvider $legacyProvider;
public function __construct(IUserManager $userManager,
IL10N $l10n,

@ -31,6 +31,7 @@ use OCP\Files\Node;
use OCP\Files\NotFoundException;
use OCP\IUser;
use OCP\Search\Provider;
use OCP\Comments\ICommentsManager;
use function count;
class LegacyProvider extends Provider {
@ -43,7 +44,7 @@ class LegacyProvider extends Provider {
* @since 7.0.0
*/
public function search($query): array {
$cm = \OC::$server->getCommentsManager();
$cm = \OC::$server->get(ICommentsManager::class);
$us = \OC::$server->getUserSession();
$user = $us->getUser();

@ -52,7 +52,7 @@ class ApplicationTest extends TestCase {
$c = $app->getContainer();
// assert service instances in the container are properly setup
$s = $c->query('NotificationsController');
$s = $c->get('NotificationsController');
$this->assertInstanceOf('OCA\Comments\Controller\Notifications', $s);
$services = [
@ -65,7 +65,7 @@ class ApplicationTest extends TestCase {
];
foreach ($services as $service) {
$s = $c->query($service);
$s = $c->get($service);
$this->assertInstanceOf($service, $s);
}
}

Loading…
Cancel
Save