koel/app/Models/Interaction.php

139 lines
2.9 KiB
PHP
Raw Normal View History

2015-12-13 04:42:28 +00:00
<?php
namespace App\Models;
2015-12-21 13:49:00 +00:00
use App\Events\SongLikeToggled;
2015-12-13 04:42:28 +00:00
use App\Traits\CanFilterByUser;
2015-12-14 13:22:39 +00:00
use Illuminate\Database\Eloquent\Model;
2015-12-13 04:42:28 +00:00
/**
* @property bool liked
* @property int play_count
*/
class Interaction extends Model
{
use CanFilterByUser;
protected $casts = [
'liked' => 'boolean',
'play_count' => 'integer',
];
protected $guarded = ['id'];
protected $hidden = ['id', 'user_id', 'created_at', 'updated_at'];
2015-12-13 04:42:28 +00:00
public function user()
{
return $this->belongsTo(User::class);
}
public function song()
{
return $this->belongsTo(Song::class);
}
/**
* Increase the number of times a song is played by a user.
*
2015-12-21 13:50:26 +00:00
* @param string $songId
* @param User $user
2015-12-13 04:42:28 +00:00
*
* @return Interaction
*/
2015-12-21 04:32:15 +00:00
public static function increasePlayCount($songId, User $user)
2015-12-13 04:42:28 +00:00
{
$interaction = self::firstOrCreate([
'song_id' => $songId,
2015-12-21 04:32:15 +00:00
'user_id' => $user->id,
2015-12-13 04:42:28 +00:00
]);
if (!$interaction->exists) {
$interaction->liked = false;
}
++$interaction->play_count;
$interaction->save();
return $interaction;
}
/**
* Like or unlike a song on behalf of a user.
*
2015-12-21 13:50:26 +00:00
* @param string $songId
* @param User $user
2015-12-13 04:42:28 +00:00
*
* @return Interaction
*/
2015-12-21 04:32:15 +00:00
public static function toggleLike($songId, User $user)
2015-12-13 04:42:28 +00:00
{
$interaction = self::firstOrCreate([
'song_id' => $songId,
2015-12-21 04:32:15 +00:00
'user_id' => $user->id,
2015-12-13 04:42:28 +00:00
]);
if (!$interaction->exists) {
$interaction->play_count = 0;
}
$interaction->liked = !$interaction->liked;
$interaction->save();
2015-12-21 13:49:00 +00:00
event(new SongLikeToggled($interaction));
2015-12-13 04:42:28 +00:00
return $interaction;
}
/**
* Like several songs at once.
*
2015-12-21 13:50:26 +00:00
* @param array $songIds
* @param User $user
2015-12-13 04:42:28 +00:00
*
* @return array
*/
2015-12-21 04:32:15 +00:00
public static function batchLike(array $songIds, User $user)
2015-12-13 04:42:28 +00:00
{
$result = [];
foreach ($songIds as $songId) {
$interaction = self::firstOrCreate([
'song_id' => $songId,
2015-12-21 04:32:15 +00:00
'user_id' => $user->id,
2015-12-13 04:42:28 +00:00
]);
if (!$interaction->exists) {
$interaction->play_count = 0;
}
$interaction->liked = true;
$interaction->save();
2015-12-21 13:49:00 +00:00
event(new SongLikeToggled($interaction));
2015-12-13 04:42:28 +00:00
$result[] = $interaction;
}
return $result;
}
/**
* Unlike several songs at once.
*
2015-12-21 13:50:26 +00:00
* @param array $songIds
* @param User $user
2015-12-13 04:42:28 +00:00
*
* @return int
*/
2015-12-21 04:32:15 +00:00
public static function batchUnlike(array $songIds, User $user)
2015-12-13 04:42:28 +00:00
{
2015-12-21 13:50:26 +00:00
foreach (self::whereIn('song_id', $songIds)->whereUserId($user->id)->get() as $interaction) {
2015-12-21 13:49:00 +00:00
$interaction->liked = false;
$interaction->save();
event(new SongLikeToggled($interaction));
}
2015-12-13 04:42:28 +00:00
}
}