freescout-help-desk / freescout

FreeScout — Free self-hosted help desk & shared mailbox (Zendesk / Help Scout alternative)
https://freescout.net
GNU Affero General Public License v3.0
3.02k stars 494 forks source link

Too few arguments to event handler `conversation.subject_changed` #4209

Closed IchHabeHunger54 closed 1 month ago

IchHabeHunger54 commented 2 months ago

PHP version: 8.2.20 FreeScout version: 1.8.152 Database: MySQL / PostgreSQL Are you using CloudFlare: Yes / No Are you using non-official modules: Yes / No

Hello, plugin dev here. First off, if there's a better place to ask for plugin dev support, feel free to direct me there instead.

I tried making a simple plugin that adds a lineitem thread when the title of a conversation changes (event conversation.subject_changed), using the following code:

//...
class TicketChangelogServiceProvider extends ServiceProvider
{
    //...
    public function hooks(): void
    {
        \Eventy::addAction('conversation.subject_changed', function($conversation, $user, $prev_subject) {
            if ($conversation->getSubject() == $prev_subject) return;
            $thread = new Thread();
            //...
            $thread->save();
        });
    }
}

However, whenever I change the title, I get an error telling me to check my internet connection. What actually happens, though, is the following message appearing in the logs (with more internal calls at the bottom that I removed here, and the email address is censored):

production.ERROR: Type error: Too few arguments to function Modules\TicketChangelog\Providers\TicketChangelogServiceProvider::Modules\TicketChangelog\Providers\{closure}(), 1 passed and exactly 3 expected {"userId":1,"email":"myemail@email.com","exception":"[object] (Symfony\\Component\\Debug\\Exception\\FatalThrowableError(code: 0): Type error: Too few arguments to function Modules\\TicketChangelog\\Providers\\TicketChangelogServiceProvider::Modules\\TicketChangelog\\Providers\\{closure}(), 1 passed and exactly 3 expected at /var/www/html/Modules/TicketChangelog/Providers/TicketChangelogServiceProvider.php:50)
[stacktrace]
#0 [internal function]: Modules\\TicketChangelog\\Providers\\TicketChangelogServiceProvider->Modules\\TicketChangelog\\Providers\\{closure}()
#1 /var/www/html/overrides/tormjens/eventy/src/Action.php(26): call_user_func_array()
#2 /var/www/html/vendor/tormjens/eventy/src/Events.php(139): TorMorten\\Eventy\\Action->fire()
#3 /var/www/html/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php(221): TorMorten\\Eventy\\Events->action()
#4 /var/www/html/app/Conversation.php(1777): Illuminate\\Support\\Facades\\Facade::__callStatic()
#5 /var/www/html/app/Http/Controllers/ConversationsController.php(2298): App\\Conversation->changeSubject()
#6 [internal function]: App\\Http\\Controllers\\ConversationsController->ajax()

I'm not all too familiar with PHP, but for me this looks like my code expects three parameters (which I took from the Conversation class in the source code), however the code only provides one JSON object. What can/should I do here?

Thank you in advance for your response.

sofl88 commented 2 months ago

I think you miss 2 parameters in the method call of addAction. Both are usually optional when you only pass one parameter to the callback but since you need more, you have to add priority and parameter count. Id recommend to checkout the eventy documentation https://github.com/tormjens/eventy. So basicially you could try this

//...
class TicketChangelogServiceProvider extends ServiceProvider
{
    //...
    public function hooks(): void
    {
        \Eventy::addAction('conversation.subject_changed', function($conversation, $user, $prev_subject) {
            if ($conversation->getSubject() == $prev_subject) return;
            $thread = new Thread();
            //...
            $thread->save();
        }, 20, 3); // 20 is base prio and 3 = parameter count
    }
}
freescout-help commented 1 month ago

@sofl88's suggestion is correct.

IchHabeHunger54 commented 1 month ago

Yes, this solved it, thank you very much.