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;
|
2017-08-05 18:55:02 +00:00
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
2015-12-13 04:42:28 +00:00
|
|
|
|
|
|
|
/**
|
2016-08-03 10:42:11 +00:00
|
|
|
* @property bool liked
|
|
|
|
* @property int play_count
|
|
|
|
* @property Song song
|
|
|
|
* @property User user
|
2017-06-24 20:46:55 +00:00
|
|
|
* @property int id
|
2015-12-13 04:42:28 +00:00
|
|
|
*/
|
|
|
|
class Interaction extends Model
|
|
|
|
{
|
|
|
|
use CanFilterByUser;
|
|
|
|
|
|
|
|
protected $casts = [
|
|
|
|
'liked' => 'boolean',
|
|
|
|
'play_count' => 'integer',
|
|
|
|
];
|
|
|
|
|
|
|
|
protected $guarded = ['id'];
|
|
|
|
|
2016-02-13 03:21:24 +00:00
|
|
|
protected $hidden = ['id', 'user_id', 'created_at', 'updated_at'];
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2017-08-05 18:55:02 +00:00
|
|
|
/**
|
|
|
|
* An interaction belongs to a user.
|
|
|
|
*
|
|
|
|
* @return BelongsTo
|
|
|
|
*/
|
2015-12-13 04:42:28 +00:00
|
|
|
public function user()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(User::class);
|
|
|
|
}
|
|
|
|
|
2017-08-05 18:55:02 +00:00
|
|
|
/**
|
|
|
|
* An interaction is associated with a song.
|
|
|
|
*
|
|
|
|
* @return BelongsTo
|
|
|
|
*/
|
2015-12-13 04:42:28 +00:00
|
|
|
public function song()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Song::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Increase the number of times a song is played by a user.
|
2016-02-13 03:21:24 +00:00
|
|
|
*
|
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
|
|
|
{
|
2017-06-03 23:21:50 +00:00
|
|
|
return tap(self::firstOrCreate([
|
2015-12-13 04:42:28 +00:00
|
|
|
'song_id' => $songId,
|
2015-12-21 04:32:15 +00:00
|
|
|
'user_id' => $user->id,
|
2017-06-03 23:21:50 +00:00
|
|
|
]), function (Interaction $interaction) {
|
|
|
|
if (!$interaction->exists) {
|
|
|
|
$interaction->liked = false;
|
|
|
|
}
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2017-11-08 13:11:45 +00:00
|
|
|
$interaction->play_count++;
|
2017-06-03 23:21:50 +00:00
|
|
|
$interaction->save();
|
|
|
|
});
|
2015-12-13 04:42:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Like or unlike a song on behalf of a user.
|
2016-02-13 03:21:24 +00:00
|
|
|
*
|
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
|
|
|
{
|
2017-06-03 23:21:50 +00:00
|
|
|
return tap(self::firstOrCreate([
|
2015-12-13 04:42:28 +00:00
|
|
|
'song_id' => $songId,
|
2015-12-21 04:32:15 +00:00
|
|
|
'user_id' => $user->id,
|
2017-06-03 23:21:50 +00:00
|
|
|
]), function (Interaction $interaction) {
|
|
|
|
$interaction->liked = !$interaction->liked;
|
|
|
|
$interaction->save();
|
2015-12-21 13:49:00 +00:00
|
|
|
|
2017-06-03 23:21:50 +00:00
|
|
|
event(new SongLikeToggled($interaction));
|
|
|
|
});
|
2015-12-13 04:42:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
{
|
2017-06-03 23:21:50 +00:00
|
|
|
return collect($songIds)->map(function ($songId) use ($user) {
|
|
|
|
return tap(self::firstOrCreate([
|
2015-12-13 04:42:28 +00:00
|
|
|
'song_id' => $songId,
|
2015-12-21 04:32:15 +00:00
|
|
|
'user_id' => $user->id,
|
2017-06-03 23:21:50 +00:00
|
|
|
]), function (Interaction $interaction) {
|
|
|
|
if (!$interaction->exists) {
|
|
|
|
$interaction->play_count = 0;
|
|
|
|
}
|
2015-12-21 13:49:00 +00:00
|
|
|
|
2017-06-03 23:21:50 +00:00
|
|
|
$interaction->liked = true;
|
|
|
|
$interaction->save();
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2017-06-03 23:21:50 +00:00
|
|
|
event(new SongLikeToggled($interaction));
|
|
|
|
});
|
|
|
|
})->all();
|
2015-12-13 04:42:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
{
|
2017-06-03 23:21:50 +00:00
|
|
|
self::whereIn('song_id', $songIds)->whereUserId($user->id)->get()->each(function (Interaction $interaction) {
|
2015-12-21 13:49:00 +00:00
|
|
|
$interaction->liked = false;
|
|
|
|
$interaction->save();
|
|
|
|
|
|
|
|
event(new SongLikeToggled($interaction));
|
2017-05-01 17:36:42 +00:00
|
|
|
});
|
2015-12-13 04:42:28 +00:00
|
|
|
}
|
|
|
|
}
|