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.15k stars 71 forks source link

How do I narrow down the reactor's reaction? #237

Closed Ajay-Bacancy closed 1 year ago

Ajay-Bacancy commented 1 year ago
        $user = Auth::user();
        $post = Post::find($requestData['post_id']);
        $reacterFacade = $user->viaLoveReacter();
        $reactantFacade = $post->viaLoveReactant();

Here is my setup. for adding reaction on the post. Now I want like which reaction is added by the user on perticular post.

$reactions = $reacterFacade->getReactions()

is giving all the reaction made by the user but I want to get reaction with perticular post. Also I want to get the name of the reaction but there is no relationship function is given with ReactionType with reacterFacade. I tried to debug the package but did not found anything so asking here.

Also How can I stop Queue of Reaction Counter And Reaction Total Count. [NOTE] I want to update data immediately.

antonkomarev commented 1 year ago

Yeah it seems like you should use low-level API to fit your needs.

$reacter = $user->getLoveReacter();
$reactant = $post->getLoveReactant();
$userReactionsOnPost = $reacter->reactions()->where('reactant_id', $reactant->getId())->get();
antonkomarev commented 1 year ago

@Ajay-Bacancy here is an issue related to your question about stop queue of reaction counter:

antonkomarev commented 1 year ago

I wrote a new page in documentation with example how to make jobs queue sync: https://laravel-love.readme.io/docs/custom-jobs-dispatching

Ajay-Bacancy commented 1 year ago

Ohhh @antonkomarev I have done the same thing. image

Maybe in enhancement of we can do something like that where we can pass the reactant in getReactions facade like this $reacterFacade->getReactions($reactant) to get reactors reaction on specific reactant.

But anyways I'm closing the issue you can lable it as question and Thank you so much for the custom job dispatching guide really appreciated. Also very nice library for reaction. I have integrated few days back. Thanks Buddy 🤗

image

antonkomarev commented 1 year ago

Looks good! Thanks for sharing! I will think about your proposal

antonkomarev commented 1 year ago

@Ajay-Bacancy FYI this code block may work pretty slow in cases when reacter has many reactions, on each new reaction it will become slower:

$userreactions  = $reacterFacade->getReactions()->where('reactant_id', $reactant->id)->values()->pluck('reaction_type_id');

Here breakdown with explanation:

$allUserRactions  = $reacterFacade->getReactions(); // Here you collecting all the reactions of the user from the database
$filteredUserReactions = $allUserReactions->where('reactant_id', $reactant->id); // We are iterate through the collection and filter out items
$reactionTypeIds = $filteredUserReactions->values()->pluck('reaction_type_id');

As you can see, filtering operation is doing on the PHP side, not on database side, and this may be critical over time.

Instead of getting reactions using ReacterFacade use low-level API and get Reacter model.

$reacter = $user->getLoveReacter();
$reactant = $post->getLoveReactant();
$userReactionsOnPost = $reacter->reactions()->where('reactant_id', $reactant->getId())->pluck('reaction_type_id');

Then reactions() method will return Query builder, not all the reactions collection, and where method will do the filtering on the database side.

Ajay-Bacancy commented 1 year ago

@antonkomarev yeh got your point. Thank you so much buddy 😌❤️

antonkomarev commented 1 year ago

@Ajay-Bacancy I've created discussion how to implement this feature on high-level API. Will be glad to receive a feedback there:

antonkomarev commented 1 year ago

And the first proof of concept:

antonkomarev commented 1 year ago

@Ajay-Bacancy I've introduced new method to fetch concrete user reactions. It's breaking change since API has been changed. Will be added in next major version release.

Ajay-Bacancy commented 1 year ago

I wrote a new page in documentation with example how to make jobs queue sync: https://laravel-love.readme.io/docs/custom-jobs-dispatching

Hey @antonkomarev I have one doubt regarding custom job dispatching. IncrementReactionAggregatesJob::dispatch()->onConnection('sync'); DecrementReactionAggregatesJob::dispatch()->onConnection('sync'); required instance of $reactant, $reaction I'm doing it this way.

if ($isNotReacted) {
    $reacterFacade->reactTo($post, $requestData['reaction_type'], 1.0);
    $reaction = $reacterFacade->getReactions()->last();
    IncrementReactionAggregatesJob::dispatch($reactant, $reaction)->onConnection('sync');
} else {
     $reacterFacade->unreactTo($post, $requestData['reaction_type']);
     DecrementReactionAggregatesJob::dispatch($reactant)->onConnection('sync');
}

getting all reaction and passing last added reaction. This will works fine with Increment job but how do I pass reaction instance in decrement job?

antonkomarev commented 1 year ago

One of the solutions: use events and write your own event listener.

Event::listen(
    ReactionHasBeenAdded::class,
    IncrementAggregates::class,
);