Guest reactions #23
|
Hello. As i understand there is no guest likes. At the moment i provide user_id=1 . It would be nice to get user_id nullable. But i don't know if it affects other methods |
Replies: 8 comments
|
@MWStudio4 you are right. Current implementation isn't designed for guest likes out of the box. Passing I see 3 possible solutions here:
Just don't forget that if you allows guest to like models there could be issues with counters cheating. |
|
I see thanks. And surely about cheating. To say the truth I doubt about need of guest likes at the moment. About cheating may be there will be good way to implement rate limit |
|
Some more thoughts for guest reactions:
|
|
Nice thoughts. Thanks! |
|
Since v6 any model could act as After authentication you can collect all guest's reactions and change their |
|
Good news! Thank you. |
|
Can someone post example implementation of this? |
|
@vlados it depends from your system requirements and application logic a lot. But simple implementation might look like this one. Add <?php
namespace App;
use Cog\Laravel\Love\Reacterable\Models\Traits\Reacterable;
use Cog\Contracts\Love\Reacterable\Models\Reacterable as ReacterableInterface;
class GuestUser extends Model implements
ReacterableInterface
{
use Reacterable;
protected $table = 'guest_users';
}When user is visiting your page - you are creating $guestUser = GuestUser::create();Then guest user adding some likes: $guestUser->viaLoveReacter()->reactTo($goodArticle, 'like');
$guestUser->viaLoveReacter()->reactTo($badArticle, 'dislike');On successful login you are going though the collection of guest user reactions and adding them to user and then deleting this GuestUser or only his reactions. For this you might store latest GuestUser in User as attribute. $user = $request->user();
$guestUserReacter = $user->getGuestUser()->viaLoveReacter();
$userReacter = $user->viaLoveReacter();
$reactions = $guestUserReacter->getReactions();
foreach ($reactions as $reaction) {
$reactable = $reaction->getReactant()->getReactable();
$reactionTypeName = $reaction->getReactionType()->getName();
$reactionRate = $reaction->getRate();
$userReacter->reactTo($reactable, $reactionTypeName, $reactionRate);
$guestUserReacter->unreactTo($reactable, $reactionTypeName);
} |
Since v6 any model could act as
Reacter. That means you can createGuestEloquent model and make itReacterable.After authentication you can collect all guest's reactions and change their
reacter_idon authenticated user's reacter id. Just don't forget to check if model was already reacted by user's reacter. Counters of affected reactants should be rebuilt.