2018-08-18 12:27:26 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
2024-01-10 21:37:24 +00:00
|
|
|
use App\Events\MultipleSongsLiked;
|
|
|
|
use App\Events\MultipleSongsUnliked;
|
2018-08-18 12:27:26 +00:00
|
|
|
use App\Events\SongLikeToggled;
|
|
|
|
use App\Models\Interaction;
|
2024-06-04 13:35:00 +00:00
|
|
|
use App\Models\Song as Playable;
|
2018-08-18 12:27:26 +00:00
|
|
|
use App\Models\User;
|
2021-06-04 15:19:33 +00:00
|
|
|
use Illuminate\Support\Collection;
|
2018-08-18 12:27:26 +00:00
|
|
|
|
|
|
|
class InteractionService
|
|
|
|
{
|
2024-06-04 13:35:00 +00:00
|
|
|
public function increasePlayCount(Playable $playable, User $user): Interaction
|
2018-08-18 12:27:26 +00:00
|
|
|
{
|
2022-08-09 18:45:11 +00:00
|
|
|
return tap(Interaction::query()->firstOrCreate([
|
2024-06-04 13:35:00 +00:00
|
|
|
'song_id' => $playable->id,
|
2018-08-18 12:27:26 +00:00
|
|
|
'user_id' => $user->id,
|
2018-08-24 15:27:19 +00:00
|
|
|
]), static function (Interaction $interaction): void {
|
2018-08-18 12:27:26 +00:00
|
|
|
if (!$interaction->exists) {
|
|
|
|
$interaction->liked = false;
|
|
|
|
}
|
|
|
|
|
2022-11-08 17:38:28 +00:00
|
|
|
$interaction->last_played_at = now();
|
|
|
|
|
2020-09-06 21:20:42 +00:00
|
|
|
++$interaction->play_count;
|
2018-08-18 12:27:26 +00:00
|
|
|
$interaction->save();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-06-04 13:35:00 +00:00
|
|
|
* Like or unlike a song/episode as a user.
|
2018-08-18 12:27:26 +00:00
|
|
|
*
|
2024-01-10 21:37:24 +00:00
|
|
|
* @return Interaction The affected Interaction object
|
2018-08-18 12:27:26 +00:00
|
|
|
*/
|
2024-06-04 13:35:00 +00:00
|
|
|
public function toggleLike(Playable $playable, User $user): Interaction
|
2018-08-18 12:27:26 +00:00
|
|
|
{
|
2022-08-09 18:45:11 +00:00
|
|
|
return tap(Interaction::query()->firstOrCreate([
|
2024-06-04 13:35:00 +00:00
|
|
|
'song_id' => $playable->id,
|
2018-08-18 12:27:26 +00:00
|
|
|
'user_id' => $user->id,
|
2018-09-04 06:25:24 +00:00
|
|
|
]), static function (Interaction $interaction): void {
|
2018-08-18 12:27:26 +00:00
|
|
|
$interaction->liked = !$interaction->liked;
|
|
|
|
$interaction->save();
|
|
|
|
|
2018-09-04 06:25:24 +00:00
|
|
|
event(new SongLikeToggled($interaction));
|
2018-08-18 12:27:26 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-06-04 13:35:00 +00:00
|
|
|
* Like several songs/episodes at once as a user.
|
2018-08-18 12:27:26 +00:00
|
|
|
*
|
2024-06-04 13:35:00 +00:00
|
|
|
* @param Collection<array-key, Playable> $playables
|
2018-08-18 12:27:26 +00:00
|
|
|
*
|
2024-04-18 11:27:07 +00:00
|
|
|
* @return Collection<array-key, Interaction> The array of Interaction objects
|
2018-08-18 12:27:26 +00:00
|
|
|
*/
|
2024-06-04 13:35:00 +00:00
|
|
|
public function likeMany(Collection $playables, User $user): Collection
|
2018-08-18 12:27:26 +00:00
|
|
|
{
|
2024-06-04 13:35:00 +00:00
|
|
|
$interactions = $playables->map(static function (Playable $playable) use ($user): Interaction {
|
2022-08-09 18:45:11 +00:00
|
|
|
return tap(Interaction::query()->firstOrCreate([
|
2024-06-04 13:35:00 +00:00
|
|
|
'song_id' => $playable->id,
|
2022-08-09 18:45:11 +00:00
|
|
|
'user_id' => $user->id,
|
|
|
|
]), static function (Interaction $interaction): void {
|
|
|
|
$interaction->play_count ??= 0;
|
|
|
|
$interaction->liked = true;
|
|
|
|
$interaction->save();
|
|
|
|
});
|
|
|
|
});
|
2021-06-04 15:19:33 +00:00
|
|
|
|
2024-06-04 13:35:00 +00:00
|
|
|
event(new MultipleSongsLiked($playables, $user));
|
2021-06-04 15:19:33 +00:00
|
|
|
|
|
|
|
return $interactions;
|
2018-08-18 12:27:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-06-04 13:35:00 +00:00
|
|
|
* Unlike several songs/episodes at once.
|
2018-08-18 12:27:26 +00:00
|
|
|
*
|
2024-06-04 13:35:00 +00:00
|
|
|
* @param array<array-key, Playable>|Collection $playables
|
2018-08-18 12:27:26 +00:00
|
|
|
*/
|
2024-06-04 13:35:00 +00:00
|
|
|
public function unlikeMany(Collection $playables, User $user): void
|
2018-08-18 12:27:26 +00:00
|
|
|
{
|
2022-08-09 18:45:11 +00:00
|
|
|
Interaction::query()
|
2024-06-04 13:35:00 +00:00
|
|
|
->whereIn('song_id', $playables->pluck('id')->all())
|
2018-08-18 12:27:26 +00:00
|
|
|
->where('user_id', $user->id)
|
2021-06-04 15:19:33 +00:00
|
|
|
->update(['liked' => false]);
|
2018-08-18 12:27:26 +00:00
|
|
|
|
2024-06-04 13:35:00 +00:00
|
|
|
event(new MultipleSongsUnliked($playables, $user));
|
2018-08-18 12:27:26 +00:00
|
|
|
}
|
|
|
|
}
|