UserDeleteHook constructs Application directly: 'not setup via query()' error on every user deletion with debug on

Open Beginner friendly
#4,020 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
72/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
php
Domain
backend

Research direction

Start in lib/Hooks/UserDeleteHook.php around line 36, where new Application() is built, and compare it with how lib/AppInfo/Application.php registers the listener. Switching to constructor-injected services means the hook should no longer call new Application(). Done when occ user:delete with 'debug' => true logs no ERROR entry and the user's items, feeds and folders are still removed in the same order.

Written by the indexing model from the issue text.

Description

IMPORTANT

  • I have read the CONTRIBUTING.md and followed the provided tips
  • I accept that the issue will be closed without comment if I do not check here
  • I accept that the issue will be closed without comment if I do not fill out all items in the issue template.

Explain the Problem

With 'debug' => true, deleting a user makes News log a level-3 (ERROR) entry:

RuntimeException: App class OCA\News\AppInfo\Application is not setup via query() but directly

The entry comes from OCP\AppFramework\App::__construct(). When debug is on, it logs an Application that was constructed with new and not resolved through the container. OCA\News\Hooks\UserDeleteHook::handle() does exactly that:

// lib/Hooks/UserDeleteHook.php:36
$app = new Application();
$container = $app->getContainer();

The error is only logged, not thrown. The listener carries on, and the user's items, feeds and folders are still deleted. So the effect is log noise, but every user deletion on a debug instance produces an ERROR entry with a stack trace, and that hides real errors in log checks.

The same code is still on master (the file was last changed in 2020).

Steps to Reproduce

  1. Set 'debug' => true in config/config.php.
  2. Run occ user:add --password-from-env newsrepro, then occ user:delete newsrepro.
  3. grep 'not setup via query' data/nextcloud.log shows the entry below.

System Information

  • News app version: 29.0.0
  • Nextcloud version: 35.0.2
  • Cron type: system cron (docker nextcloud-cron container)
  • PHP version: 8.5.12
  • Database and version: PostgreSQL 18.4
  • Browser and version: n/a (server-side, reproduced with occ)
  • OS and version: official nextcloud:35-apache docker image
Contents of nextcloud/data/nextcloud.log
level: 3, app: news, version: 35.0.2.2, occ_command: ["occ","user:delete","newsrepro"]
RuntimeException: App class OCA\News\AppInfo\Application is not setup via query() but directly
  custom_apps/news/lib/AppInfo/Application.php:81          OCP\AppFramework\App->__construct
  custom_apps/news/lib/Hooks/UserDeleteHook.php:36         OCA\News\AppInfo\Application->__construct
  lib/private/EventDispatcher/ServiceEventListener.php:57  OCA\News\Hooks\UserDeleteHook->handle
  3rdparty/symfony/event-dispatcher/EventDispatcher.php:206
  3rdparty/symfony/event-dispatcher/EventDispatcher.php:56
  lib/private/EventDispatcher/EventDispatcher.php:74
  lib/private/EventDispatcher/EventDispatcher.php:87
  lib/private/User/User.php:266                            OC\EventDispatcher\EventDispatcher->dispatchTyped
  core/Command/User/Delete.php:46                          OC\User\User->delete
Contents of Browser Error Console
n/a (server-side)

Suggested fix

UserDeleteHook is registered with $context->registerEventListener(BeforeUserDeletedEvent::class, UserDeleteHook::class), so ServiceEventListener already resolves it through the app's DI container. The services can be constructor-injected, and no Application instance is needed:

class UserDeleteHook implements IEventListener
{
    public function __construct(
        private ItemServiceV2 $itemService,
        private FeedServiceV2 $feedService,
        private FolderServiceV2 $folderService,
    ) {
    }

    public function handle(Event $event): void
    {
        if (!$event instanceof BeforeUserDeletedEvent) {
            return;
        }
        $userId = $event->getUser()->getUID();

        // order is important!
        $this->itemService->deleteUser($userId);
        $this->feedService->deleteUser($userId);
        $this->folderService->deleteUser($userId);
    }
}
Dominant language
PHP
Stars
1k
Forks
200
Avg merge
20h 28m
Merged PRs (30d)
32

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from nextcloud/news

All issues in nextcloud/news

Similar issues

More PHP issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.