Skip to content

Commit

Permalink
Cleanup comments code
Browse files Browse the repository at this point in the history
- Fix various psalm issues
- Add as much typing as possible while preserving stable API

Signed-off-by: Carl Schwan <carl@carlschwan.eu>
  • Loading branch information
CarlSchwan committed Nov 25, 2022
1 parent 5f3585d commit 98eabd0
Show file tree
Hide file tree
Showing 20 changed files with 149 additions and 403 deletions.
38 changes: 8 additions & 30 deletions apps/comments/lib/Activity/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,64 +27,42 @@
use OCP\IURLGenerator;

class Filter implements IFilter {

/** @var IL10N */
protected $l;

/** @var IURLGenerator */
protected $url;
protected IL10N $l;
protected IURLGenerator $url;

public function __construct(IL10N $l, IURLGenerator $url) {
$this->l = $l;
$this->url = $url;
}

/**
* @return string Lowercase a-z only identifier
* @since 11.0.0
*/
public function getIdentifier() {
public function getIdentifier(): string {
return 'comments';
}

/**
* @return string A translated string
* @since 11.0.0
*/
public function getName() {
public function getName(): string {
return $this->l->t('Comments');
}

/**
* @return int
* @since 11.0.0
*/
public function getPriority() {
public function getPriority(): int {
return 40;
}

/**
* @return string Full URL to an icon, empty string when none is given
* @since 11.0.0
*/
public function getIcon() {
public function getIcon(): string {
return $this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/comment.svg'));
}

/**
* @param string[] $types
* @return string[] An array of allowed apps from which activities should be displayed
* @since 11.0.0
*/
public function filterTypes(array $types) {
public function filterTypes(array $types): array {
return $types;
}

/**
* @return string[] An array of allowed apps from which activities should be displayed
* @since 11.0.0
*/
public function allowedApps() {
public function allowedApps(): array {
return ['comments'];
}
}
5 changes: 1 addition & 4 deletions apps/comments/lib/Activity/Listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,7 @@ public function __construct(IManager $activityManager,
$this->shareHelper = $shareHelper;
}

/**
* @param CommentsEvent $event
*/
public function commentEvent(CommentsEvent $event) {
public function commentEvent(CommentsEvent $event): void {
if ($event->getComment()->getObjectType() !== 'files'
|| $event->getEvent() !== CommentsEvent::EVENT_ADD
|| !$this->appManager->isInstalled('activity')) {
Expand Down
2 changes: 1 addition & 1 deletion apps/comments/lib/Activity/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function __construct(IFactory $languageFactory, IURLGenerator $url, IComm
* @throws \InvalidArgumentException
* @since 11.0.0
*/
public function parse($language, IEvent $event, IEvent $previousEvent = null) {
public function parse($language, IEvent $event, IEvent $previousEvent = null): IEvent {
if ($event->getApp() !== 'comments') {
throw new \InvalidArgumentException();
}
Expand Down
51 changes: 8 additions & 43 deletions apps/comments/lib/Activity/Setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,72 +26,37 @@
use OCP\IL10N;

class Setting implements ISetting {
protected IL10N $l;

/** @var IL10N */
protected $l;

/**
* @param IL10N $l
*/
public function __construct(IL10N $l) {
$this->l = $l;
}

/**
* @return string Lowercase a-z and underscore only identifier
* @since 11.0.0
*/
public function getIdentifier() {
public function getIdentifier(): string {
return 'comments';
}

/**
* @return string A translated string
* @since 11.0.0
*/
public function getName() {
public function getName(): string {
return $this->l->t('<strong>Comments</strong> for files');
}

/**
* @return int whether the filter should be rather on the top or bottom of
* the admin section. The filters are arranged in ascending order of the
* priority values. It is required to return a value between 0 and 100.
* @since 11.0.0
*/
public function getPriority() {
public function getPriority(): int {
return 50;
}

/**
* @return bool True when the option can be changed for the stream
* @since 11.0.0
*/
public function canChangeStream() {
public function canChangeStream(): bool {
return true;
}

/**
* @return bool True when the option can be changed for the stream
* @since 11.0.0
*/
public function isDefaultEnabledStream() {
public function isDefaultEnabledStream(): bool {
return true;
}

/**
* @return bool True when the option can be changed for the mail
* @since 11.0.0
*/
public function canChangeMail() {
public function canChangeMail(): bool {
return true;
}

/**
* @return bool True when the option can be changed for the stream
* @since 11.0.0
*/
public function isDefaultEnabledMail() {
public function isDefaultEnabledMail(): bool {
return false;
}
}
10 changes: 6 additions & 4 deletions apps/comments/lib/Collaboration/CommentersSorter.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,24 @@
use OCP\Comments\ICommentsManager;

class CommentersSorter implements ISorter {

private ICommentsManager $commentsManager;

public function __construct(ICommentsManager $commentsManager) {
$this->commentsManager = $commentsManager;
}

public function getId() {
public function getId(): string {
return 'commenters';
}

/**
* Sorts people who commented on the given item atop (descelating) of the
* others
*
* @param array $sortArray
* @param array &$sortArray
* @param array $context
*/
public function sort(array &$sortArray, array $context) {
public function sort(array &$sortArray, array $context): void {
$commenters = $this->retrieveCommentsInformation($context['itemType'], $context['itemId']);
if (count($commenters) === 0) {
return;
Expand Down Expand Up @@ -76,6 +75,9 @@ public function sort(array &$sortArray, array $context) {
}
}

/**
* @return array<string, array<string, int>>
*/
protected function retrieveCommentsInformation(string $type, string $id): array {
$comments = $this->commentsManager->getForObject($type, $id);
if (count($comments) === 0) {
Expand Down
22 changes: 5 additions & 17 deletions apps/comments/lib/EventHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,15 @@
* @package OCA\Comments
*/
class EventHandler implements ICommentsEventHandler {
/** @var ActivityListener */
private $activityListener;

/** @var NotificationListener */
private $notificationListener;
private ActivityListener $activityListener;
private NotificationListener $notificationListener;

public function __construct(ActivityListener $activityListener, NotificationListener $notificationListener) {
$this->activityListener = $activityListener;
$this->notificationListener = $notificationListener;
}

/**
* @param CommentsEvent $event
*/
public function handle(CommentsEvent $event) {
public function handle(CommentsEvent $event): void {
if ($event->getComment()->getObjectType() !== 'files') {
// this is a 'files'-specific Handler
return;
Expand All @@ -73,17 +67,11 @@ public function handle(CommentsEvent $event) {
}
}

/**
* @param CommentsEvent $event
*/
private function activityHandler(CommentsEvent $event) {
private function activityHandler(CommentsEvent $event): void {
$this->activityListener->commentEvent($event);
}

/**
* @param CommentsEvent $event
*/
private function notificationHandler(CommentsEvent $event) {
private function notificationHandler(CommentsEvent $event): void {
$this->notificationListener->evaluate($event);
}
}
13 changes: 11 additions & 2 deletions apps/comments/lib/Listener/CommentsEntityEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,25 @@
use OCP\Comments\CommentsEntityEvent;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\Files\IRootFolder;

class CommentsEntityEventListener implements IEventListener {
private IRootFolder $rootFolder;
private ?string $userId;

public function __construct(IRootFolder $rootFolder, ?string $userId = null) {
$this->rootFolder = $rootFolder;
$this->userId = $userId;
}

public function handle(Event $event): void {
if (!($event instanceof CommentsEntityEvent)) {
// Unrelated
return;
}

$event->addEntityCollection('files', function ($name) {
$nodes = \OC::$server->getUserFolder()->getById((int)$name);
$event->addEntityCollection('files', function ($name): bool {
$nodes = $this->rootFolder->getUserFolder($this->userId)->getById((int)$name);
return !empty($nodes);
});
}
Expand Down
4 changes: 1 addition & 3 deletions apps/comments/lib/MaxAutoCompleteResultsInitialState.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@
use OCP\IConfig;

class MaxAutoCompleteResultsInitialState extends InitialStateProvider {

/** @var IConfig */
private $config;
private IConfig $config;

public function __construct(IConfig $config) {
$this->config = $config;
Expand Down
23 changes: 7 additions & 16 deletions apps/comments/lib/Notification/Listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,12 @@
use OCP\Comments\IComment;
use OCP\IUserManager;
use OCP\Notification\IManager;
use OCP\Notification\INotification;

class Listener {

protected IManager $notificationManager;
protected IUserManager $userManager;

/**
* Listener constructor.
*/
public function __construct(
IManager $notificationManager,
IUserManager $userManager
Expand All @@ -44,10 +41,7 @@ public function __construct(
$this->userManager = $userManager;
}

/**
* @param CommentsEvent $event
*/
public function evaluate(CommentsEvent $event) {
public function evaluate(CommentsEvent $event): void {
$comment = $event->getComment();

$mentions = $this->extractMentions($comment->getMentions());
Expand Down Expand Up @@ -77,12 +71,9 @@ public function evaluate(CommentsEvent $event) {
}

/**
* creates a notification instance and fills it with comment data
*
* @param IComment $comment
* @return \OCP\Notification\INotification
* Creates a notification instance and fills it with comment data
*/
public function instantiateNotification(IComment $comment) {
public function instantiateNotification(IComment $comment): INotification {
$notification = $this->notificationManager->createNotification();
$notification
->setApp('comments')
Expand All @@ -94,12 +85,12 @@ public function instantiateNotification(IComment $comment) {
}

/**
* flattens the mention array returned from comments to a list of user ids.
* Flattens the mention array returned from comments to a list of user ids.
*
* @param array $mentions
* @return string[] containing the mentions, e.g. ['alice', 'bob']
* @return list<string> containing the mentions, e.g. ['alice', 'bob']
*/
public function extractMentions(array $mentions) {
public function extractMentions(array $mentions): array {
if (empty($mentions)) {
return [];
}
Expand Down
13 changes: 0 additions & 13 deletions apps/comments/lib/Search/CommentsSearchProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
use function pathinfo;

class CommentsSearchProvider implements IProvider {

private IUserManager $userManager;
private IL10N $l10n;
private IURLGenerator $urlGenerator;
Expand All @@ -55,23 +54,14 @@ public function __construct(IUserManager $userManager,
$this->legacyProvider = $legacyProvider;
}

/**
* @inheritDoc
*/
public function getId(): string {
return 'comments';
}

/**
* @inheritDoc
*/
public function getName(): string {
return $this->l10n->t('Comments');
}

/**
* @inheritDoc
*/
public function getOrder(string $route, array $routeParameters): int {
if ($route === 'files.View.index') {
// Files first
Expand All @@ -80,9 +70,6 @@ public function getOrder(string $route, array $routeParameters): int {
return 10;
}

/**
* @inheritDoc
*/
public function search(IUser $user, ISearchQuery $query): SearchResult {
return SearchResult::complete(
$this->l10n->t('Comments'),
Expand Down
Loading

0 comments on commit 98eabd0

Please sign in to comment.