cybercog / laravel-love

Add Social Reactions to Laravel Eloquent Models. It lets people express how they feel about the content. Fully customizable Weighted Reaction System & Reaction Type System with Like, Dislike and any other custom emotion types. Do you react?
https://komarev.com/sources/laravel-love
MIT License
1.16k stars 71 forks source link

{Increment/Decrement}Aggregates implements ShouldQueue #139

Closed volkv closed 4 years ago

volkv commented 4 years ago

Hi @antonkomarev You've hardcoded Queue behavior. I can make a PR with another non-queued listeners and a new config option. Is it ok? Or there is a better way to do it? Because I want to use queues globally, but reactions should be sync to get changes in JS response. Thanks

antonkomarev commented 4 years ago

Hi @volkv we're always open to the new ideas! I think we could add getConnection method and by default it will return null that means it will use default connection from the .env file. But it might be modified by user then.

antonkomarev commented 4 years ago

@volkv if you are making calls with JS can't you send this changes to frontend with websockets? What is the reason to have real data in response when you just could add +1 to reaction if 201 response was returned?

antonkomarev commented 4 years ago

There are 2 implementations #141 #142 have been done so far by @volkv. I'm thinking about another one approach. Will try to describe it tomorrow evening. In short - we could encapsulate all logic from Listener's handle method in separate job. Then Listener will become synchronous and Job will be queued.

alvinwhite commented 4 years ago

if you are making calls with JS can't you send this changes to frontend with websockets? What is the reason to have real data in response when you just could add +1 to reaction if 201 response was returned?

@antonkomarev there could be non-trivial logic after user reacted to something, which will affect the result of the overall reaction count. For example, when user adds +1 to a reaction it causes other reaction to subtract -1.

To replicate this on both frontend and backend is kind of a pain.

Websockets can help solve this problem, but we don't use them at all in our project anywhere else, so we were looking for an AJAX solution.

antonkomarev commented 4 years ago

@volkv @alvinwhite I've found a solution which I hope will fit all the requirements and will be easy to use. https://github.com/cybercog/laravel-love/issues/145#issuecomment-583730266

  1. You will need to opt-out package discovery:

    "extra": {
    "laravel": {
        "dont-discover": [
            "cybercog/laravel-love"
        ]
    }
    },
  2. Register only core LoveServiceProvider provider in your application's AppServiceProvider:

    $this->app->register(\Cog\Laravel\Love\LoveServiceProvider::class);
  3. Finally, register any custom event listeners in your EventServiceProvider. To not reinvent the wheel you could just make a copy of stock Love's listeners and dispatch jobs introduced in #146 on sync connection:

    IncrementReactionAggregatesJob::dispatch()->onConnection('sync');
antonkomarev commented 4 years ago

First part is ready to be merged: #146

antonkomarev commented 4 years ago

Last part is ready to be merged right after testing in real application: #147

antonkomarev commented 4 years ago

New solution is working for me! You could try the master branch:

composer require cybercog/laravel-love:dev-master

It will be released as part of Laravel Love v8.3 soon.