mirror of
https://github.com/koel/koel
synced 2024-12-22 02:23:14 +00:00
cce534bac9
* chore(deps): bump aws/aws-sdk-php-laravel from 3.4.0 to 3.5.0 Bumps [aws/aws-sdk-php-laravel](https://github.com/aws/aws-sdk-php-laravel) from 3.4.0 to 3.5.0. - [Release notes](https://github.com/aws/aws-sdk-php-laravel/releases) - [Changelog](https://github.com/aws/aws-sdk-php-laravel/blob/master/CHANGELOG.md) - [Commits](https://github.com/aws/aws-sdk-php-laravel/compare/3.4.0...3.5.0) Signed-off-by: dependabot-preview[bot] <support@dependabot.com> * Apply fixes from StyleCI (#1196) Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com> Co-authored-by: Phan An <me@phanan.net>
102 lines
2.8 KiB
PHP
102 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Events\SongLikeToggled;
|
|
use App\Models\Interaction;
|
|
use App\Models\User;
|
|
|
|
class InteractionService
|
|
{
|
|
private $interaction;
|
|
|
|
public function __construct(Interaction $interaction)
|
|
{
|
|
$this->interaction = $interaction;
|
|
}
|
|
|
|
/**
|
|
* Increase the number of times a song is played by a user.
|
|
*
|
|
* @return Interaction The affected Interaction object
|
|
*/
|
|
public function increasePlayCount(string $songId, User $user): Interaction
|
|
{
|
|
return tap($this->interaction->firstOrCreate([
|
|
'song_id' => $songId,
|
|
'user_id' => $user->id,
|
|
]), static function (Interaction $interaction): void {
|
|
if (!$interaction->exists) {
|
|
$interaction->liked = false;
|
|
}
|
|
|
|
$interaction->play_count++;
|
|
$interaction->save();
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Like or unlike a song on behalf of a user.
|
|
*
|
|
* @return Interaction The affected Interaction object.
|
|
*/
|
|
public function toggleLike(string $songId, User $user): Interaction
|
|
{
|
|
return tap($this->interaction->firstOrCreate([
|
|
'song_id' => $songId,
|
|
'user_id' => $user->id,
|
|
]), static function (Interaction $interaction): void {
|
|
$interaction->liked = !$interaction->liked;
|
|
$interaction->save();
|
|
|
|
event(new SongLikeToggled($interaction));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Like several songs at once as a user.
|
|
*
|
|
* @param string[] $songIds
|
|
*
|
|
* @return Interaction[] The array of Interaction objects.
|
|
*/
|
|
public function batchLike(array $songIds, User $user): array
|
|
{
|
|
return collect($songIds)->map(function ($songId) use ($user): Interaction {
|
|
return tap($this->interaction->firstOrCreate([
|
|
'song_id' => $songId,
|
|
'user_id' => $user->id,
|
|
]), static function (Interaction $interaction): void {
|
|
if (!$interaction->exists) {
|
|
$interaction->play_count = 0;
|
|
}
|
|
|
|
$interaction->liked = true;
|
|
$interaction->save();
|
|
|
|
event(new SongLikeToggled($interaction));
|
|
});
|
|
})->all();
|
|
}
|
|
|
|
/**
|
|
* Unlike several songs at once.
|
|
*
|
|
* @param string[] $songIds
|
|
*/
|
|
public function batchUnlike(array $songIds, User $user): void
|
|
{
|
|
$this->interaction
|
|
->whereIn('song_id', $songIds)
|
|
->where('user_id', $user->id)
|
|
->get()
|
|
->each(
|
|
static function (Interaction $interaction): void {
|
|
$interaction->liked = false;
|
|
$interaction->save();
|
|
|
|
event(new SongLikeToggled($interaction));
|
|
}
|
|
);
|
|
}
|
|
}
|