Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Ajay-Bacancy opened this issue Mar 28, 2023 · 12 comments
Closed

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

Ajay-Bacancy opened this issue Mar 28, 2023 · 12 comments

Comments

@Ajay-Bacancy
Copy link

Ajay-Bacancy commented Mar 28, 2023

        $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
Copy link
Member

antonkomarev commented Mar 31, 2023

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
Copy link
Member

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

@antonkomarev
Copy link
Member

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
Copy link
Author

Ajay-Bacancy commented Mar 31, 2023

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
Copy link
Member

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

@antonkomarev
Copy link
Member

antonkomarev commented Apr 2, 2023

@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
Copy link
Author

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

@antonkomarev
Copy link
Member

antonkomarev commented Apr 2, 2023

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

@antonkomarev
Copy link
Member

And the first proof of concept:

@antonkomarev
Copy link
Member

@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
Copy link
Author

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
Copy link
Member

antonkomarev commented Apr 11, 2023

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants