2015-08-30 14:29:12 +02:00
|
|
|
<?php
|
|
|
|
|
2015-10-25 06:17:45 +01:00
|
|
|
/**
|
|
|
|
* Pony.fm - A community for pony fan music.
|
|
|
|
* Copyright (C) 2015 Peter Deltchev
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2016-01-01 01:12:30 +01:00
|
|
|
namespace Poniverse\Ponyfm\Models;
|
2015-08-31 14:35:47 +02:00
|
|
|
|
|
|
|
use Exception;
|
2015-11-01 17:49:28 +01:00
|
|
|
use Helpers;
|
2015-08-30 14:29:12 +02:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
2015-11-10 05:08:37 +01:00
|
|
|
use Auth;
|
|
|
|
use Cache;
|
2016-01-17 16:16:16 +01:00
|
|
|
use Poniverse\Ponyfm\Contracts\Searchable;
|
2015-12-26 03:30:16 +01:00
|
|
|
use Poniverse\Ponyfm\Exceptions\TrackFileNotFoundException;
|
2016-01-16 05:10:20 +01:00
|
|
|
use Poniverse\Ponyfm\Traits\IndexedInElasticsearchTrait;
|
2015-11-10 06:49:12 +01:00
|
|
|
use Poniverse\Ponyfm\Traits\TrackCollection;
|
2015-10-24 03:22:14 +02:00
|
|
|
use Poniverse\Ponyfm\Traits\SlugTrait;
|
2015-11-24 12:07:43 +01:00
|
|
|
use Venturecraft\Revisionable\RevisionableTrait;
|
2015-08-30 14:29:12 +02:00
|
|
|
|
2016-01-01 01:36:08 +01:00
|
|
|
/**
|
|
|
|
* Poniverse\Ponyfm\Models\Album
|
|
|
|
*
|
|
|
|
* @property integer $id
|
|
|
|
* @property integer $user_id
|
|
|
|
* @property string $title
|
|
|
|
* @property string $slug
|
|
|
|
* @property string $description
|
|
|
|
* @property integer $cover_id
|
|
|
|
* @property integer $track_count
|
|
|
|
* @property integer $view_count
|
|
|
|
* @property integer $download_count
|
|
|
|
* @property integer $favourite_count
|
|
|
|
* @property integer $comment_count
|
|
|
|
* @property \Carbon\Carbon $created_at
|
|
|
|
* @property string $updated_at
|
|
|
|
* @property \Carbon\Carbon $deleted_at
|
|
|
|
* @property-read \Poniverse\Ponyfm\Models\User $user
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection|\Poniverse\Ponyfm\Models\ResourceUser[] $users
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection|\Poniverse\Ponyfm\Models\Favourite[] $favourites
|
|
|
|
* @property-read \Poniverse\Ponyfm\Models\Image $cover
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection|\Poniverse\Ponyfm\Models\Track[] $tracks
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection|\Poniverse\Ponyfm\Models\Comment[] $comments
|
|
|
|
* @property-read mixed $url
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection|\Venturecraft\Revisionable\Revision[] $revisionHistory
|
|
|
|
* @method static \Illuminate\Database\Query\Builder|\Poniverse\Ponyfm\Models\Album userDetails()
|
|
|
|
*/
|
2016-01-17 16:16:16 +01:00
|
|
|
class Album extends Model implements Searchable
|
2015-08-30 14:29:12 +02:00
|
|
|
{
|
2016-01-17 16:16:16 +01:00
|
|
|
use SoftDeletes, SlugTrait, TrackCollection, RevisionableTrait, IndexedInElasticsearchTrait;
|
2016-01-07 19:16:37 +01:00
|
|
|
|
|
|
|
protected $elasticsearchType = 'album';
|
2015-08-30 14:29:12 +02:00
|
|
|
|
|
|
|
protected $dates = ['deleted_at'];
|
2015-12-26 12:40:47 +01:00
|
|
|
protected $fillable = ['user_id', 'title', 'slug'];
|
2015-08-30 14:29:12 +02:00
|
|
|
|
|
|
|
public static function summary()
|
|
|
|
{
|
|
|
|
return self::select('id', 'title', 'user_id', 'slug', 'created_at', 'cover_id', 'comment_count',
|
|
|
|
'download_count', 'view_count', 'favourite_count');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function scopeUserDetails($query)
|
|
|
|
{
|
|
|
|
if (Auth::check()) {
|
|
|
|
$query->with([
|
2016-01-18 02:21:39 +01:00
|
|
|
'users' => function($query) {
|
2015-08-30 14:29:12 +02:00
|
|
|
$query->whereUserId(Auth::user()->id);
|
|
|
|
}
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return !$query;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected $table = 'albums';
|
|
|
|
|
|
|
|
public function user()
|
|
|
|
{
|
2016-01-01 01:12:30 +01:00
|
|
|
return $this->belongsTo('Poniverse\Ponyfm\Models\User');
|
2015-08-30 14:29:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function users()
|
|
|
|
{
|
2016-01-01 01:12:30 +01:00
|
|
|
return $this->hasMany('Poniverse\Ponyfm\Models\ResourceUser');
|
2015-08-30 14:29:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function favourites()
|
|
|
|
{
|
2016-01-01 01:12:30 +01:00
|
|
|
return $this->hasMany('Poniverse\Ponyfm\Models\Favourite');
|
2015-08-30 14:29:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function cover()
|
|
|
|
{
|
2016-01-01 01:12:30 +01:00
|
|
|
return $this->belongsTo('Poniverse\Ponyfm\Models\Image');
|
2015-08-30 14:29:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function tracks()
|
|
|
|
{
|
2016-01-01 01:12:30 +01:00
|
|
|
return $this->hasMany('Poniverse\Ponyfm\Models\Track')->orderBy('track_number', 'asc');
|
2015-08-30 14:29:12 +02:00
|
|
|
}
|
|
|
|
|
2015-11-10 06:49:12 +01:00
|
|
|
public function trackFiles() {
|
|
|
|
return $this->hasManyThrough(TrackFile::class, Track::class, 'album_id', 'track_id');
|
|
|
|
}
|
|
|
|
|
2015-08-30 14:29:12 +02:00
|
|
|
public function comments()
|
|
|
|
{
|
2016-01-01 01:12:30 +01:00
|
|
|
return $this->hasMany('Poniverse\Ponyfm\Models\Comment')->orderBy('created_at', 'desc');
|
2015-08-30 14:29:12 +02:00
|
|
|
}
|
|
|
|
|
2015-11-10 05:08:37 +01:00
|
|
|
public static function mapPublicAlbumShow(Album $album)
|
2015-08-30 14:29:12 +02:00
|
|
|
{
|
|
|
|
$tracks = [];
|
|
|
|
foreach ($album->tracks as $track) {
|
|
|
|
$tracks[] = Track::mapPublicTrackSummary($track);
|
|
|
|
}
|
|
|
|
|
|
|
|
$formats = [];
|
|
|
|
foreach (Track::$Formats as $name => $format) {
|
|
|
|
$formats[] = [
|
|
|
|
'name' => $name,
|
|
|
|
'extension' => $format['extension'],
|
|
|
|
'url' => $album->getDownloadUrl($name),
|
2015-10-29 15:36:13 +01:00
|
|
|
'size' => Helpers::formatBytes($album->getFilesize($name)),
|
2015-11-01 17:49:28 +01:00
|
|
|
'isCacheable' => (in_array($name, Track::$CacheableFormats) ? true : false)
|
2015-08-30 14:29:12 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
$comments = [];
|
|
|
|
foreach ($album->comments as $comment) {
|
|
|
|
$comments[] = Comment::mapPublic($comment);
|
|
|
|
}
|
|
|
|
|
|
|
|
$is_downloadable = 0;
|
|
|
|
foreach ($album->tracks as $track) {
|
|
|
|
if ($track->is_downloadable == 1) {
|
|
|
|
$is_downloadable = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$data = self::mapPublicAlbumSummary($album);
|
|
|
|
$data['tracks'] = $tracks;
|
|
|
|
$data['comments'] = $comments;
|
|
|
|
$data['formats'] = $formats;
|
|
|
|
$data['description'] = $album->description;
|
|
|
|
$data['is_downloadable'] = $is_downloadable;
|
|
|
|
$data['share'] = [
|
2015-12-20 16:07:36 +01:00
|
|
|
'url' => action('AlbumsController@getShortlink', ['id' => $album->id]),
|
2016-01-18 02:21:39 +01:00
|
|
|
'tumblrUrl' => 'http://www.tumblr.com/share/link?url='.urlencode($album->url).'&name='.urlencode($album->title).'&description='.urlencode($album->description),
|
|
|
|
'twitterUrl' => 'https://platform.twitter.com/widgets/tweet_button.html?text='.$album->title.' by '.$album->user->display_name.' on Pony.fm'
|
2015-08-30 14:29:12 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2015-11-10 05:08:37 +01:00
|
|
|
public static function mapPublicAlbumSummary(Album $album)
|
2015-08-30 14:29:12 +02:00
|
|
|
{
|
|
|
|
$userData = [
|
|
|
|
'stats' => [
|
|
|
|
'views' => 0,
|
|
|
|
'downloads' => 0
|
|
|
|
],
|
|
|
|
'is_favourited' => false
|
|
|
|
];
|
|
|
|
|
|
|
|
if (Auth::check() && $album->users->count()) {
|
|
|
|
$userRow = $album->users[0];
|
|
|
|
$userData = [
|
|
|
|
'stats' => [
|
2016-01-18 02:21:39 +01:00
|
|
|
'views' => (int) $userRow->view_count,
|
|
|
|
'downloads' => (int) $userRow->download_count,
|
2015-08-30 14:29:12 +02:00
|
|
|
],
|
2016-01-18 02:21:39 +01:00
|
|
|
'is_favourited' => (bool) $userRow->is_favourited
|
2015-08-30 14:29:12 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
2016-01-18 02:21:39 +01:00
|
|
|
'id' => (int) $album->id,
|
|
|
|
'track_count' => (int) $album->track_count,
|
2015-08-30 14:29:12 +02:00
|
|
|
'title' => $album->title,
|
|
|
|
'slug' => $album->slug,
|
2015-12-26 16:20:31 +01:00
|
|
|
'created_at' => $album->created_at->format('c'),
|
2015-08-30 14:29:12 +02:00
|
|
|
'stats' => [
|
2016-01-18 02:21:39 +01:00
|
|
|
'views' => (int) $album->view_count,
|
|
|
|
'downloads' => (int) $album->download_count,
|
|
|
|
'comments' => (int) $album->comment_count,
|
|
|
|
'favourites' => (int) $album->favourite_count
|
2015-08-30 14:29:12 +02:00
|
|
|
],
|
|
|
|
'covers' => [
|
|
|
|
'small' => $album->getCoverUrl(Image::SMALL),
|
2015-12-13 14:42:37 +01:00
|
|
|
'normal' => $album->getCoverUrl(Image::NORMAL),
|
|
|
|
'original' => $album->getCoverUrl(Image::ORIGINAL)
|
2015-08-30 14:29:12 +02:00
|
|
|
],
|
|
|
|
'url' => $album->url,
|
|
|
|
'user' => [
|
2016-01-18 02:21:39 +01:00
|
|
|
'id' => (int) $album->user->id,
|
2015-08-30 14:29:12 +02:00
|
|
|
'name' => $album->user->display_name,
|
2016-03-19 09:28:35 +01:00
|
|
|
'slug' => $album->user->slug,
|
2015-08-30 14:29:12 +02:00
|
|
|
'url' => $album->user->url,
|
|
|
|
],
|
|
|
|
'user_data' => $userData,
|
|
|
|
'permissions' => [
|
|
|
|
'delete' => Auth::check() && Auth::user()->id == $album->user_id,
|
|
|
|
'edit' => Auth::check() && Auth::user()->id == $album->user_id
|
|
|
|
]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function hasCover()
|
|
|
|
{
|
|
|
|
return $this->cover_id != null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getUrlAttribute()
|
|
|
|
{
|
2015-12-20 16:07:36 +01:00
|
|
|
return action('AlbumsController@getShow', ['id' => $this->id, 'slug' => $this->slug]);
|
2015-08-30 14:29:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getDownloadUrl($format)
|
|
|
|
{
|
2015-12-20 16:07:36 +01:00
|
|
|
return action('AlbumsController@getDownload', ['id' => $this->id, 'extension' => Track::$Formats[$format]['extension']]);
|
2015-08-30 14:29:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getFilesize($format)
|
|
|
|
{
|
|
|
|
$tracks = $this->tracks;
|
|
|
|
if (!count($tracks)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-01-18 02:21:39 +01:00
|
|
|
return Cache::remember($this->getCacheKey('filesize-'.$format), 1440, function() use ($tracks, $format) {
|
2015-08-30 14:29:12 +02:00
|
|
|
$size = 0;
|
2015-11-10 05:08:37 +01:00
|
|
|
|
2015-08-30 14:29:12 +02:00
|
|
|
foreach ($tracks as $track) {
|
2015-11-10 05:08:37 +01:00
|
|
|
/** @var $track Track */
|
|
|
|
|
2015-08-30 14:29:12 +02:00
|
|
|
// Ensure that only downloadable tracks are added onto the file size
|
|
|
|
if ($track->is_downloadable == 1) {
|
2015-11-10 05:08:37 +01:00
|
|
|
try {
|
|
|
|
$size += $track->getFilesize($format);
|
|
|
|
|
|
|
|
} catch (TrackFileNotFoundException $e) {
|
|
|
|
// do nothing - this track won't be included in the download
|
|
|
|
}
|
2015-08-30 14:29:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $size;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getCoverUrl($type = Image::NORMAL)
|
|
|
|
{
|
|
|
|
if (!$this->hasCover()) {
|
|
|
|
return $this->user->getAvatarUrl($type);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->cover->getUrl($type);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getDirectory()
|
|
|
|
{
|
2016-01-18 02:21:39 +01:00
|
|
|
$dir = (string) (floor($this->id / 100) * 100);
|
2015-08-30 14:29:12 +02:00
|
|
|
|
2016-01-18 02:21:39 +01:00
|
|
|
return \Config::get('ponyfm.files_directory').'/tracks/'.$dir;
|
2015-08-30 14:29:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getDates()
|
|
|
|
{
|
|
|
|
return ['created_at', 'deleted_at', 'published_at'];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getFilenameFor($format)
|
|
|
|
{
|
|
|
|
if (!isset(Track::$Formats[$format])) {
|
|
|
|
throw new Exception("$format is not a valid format!");
|
|
|
|
}
|
|
|
|
|
|
|
|
$format = Track::$Formats[$format];
|
|
|
|
|
|
|
|
return "{$this->id}.{$format['extension']}.zip";
|
|
|
|
}
|
|
|
|
|
|
|
|
public function updateTrackNumbers()
|
|
|
|
{
|
|
|
|
$tracks = Track::whereAlbumId($this->id)->get();
|
|
|
|
$index = 1;
|
|
|
|
|
|
|
|
foreach ($tracks as $track) {
|
2015-11-10 05:08:37 +01:00
|
|
|
/** @var $track Track */
|
|
|
|
|
2015-08-30 14:29:12 +02:00
|
|
|
$track->track_number = $index;
|
|
|
|
$index++;
|
|
|
|
$track->updateTags();
|
|
|
|
$track->save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function syncTrackIds($trackIds)
|
|
|
|
{
|
|
|
|
$trackIdsInAlbum = [];
|
|
|
|
foreach ($this->tracks as $track) {
|
|
|
|
$trackIdsInAlbum[] = $track->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
$trackIdsCount = count($trackIds);
|
|
|
|
$trackIdsInAlbumCount = count($trackIdsInAlbum);
|
|
|
|
$isSame = true;
|
|
|
|
|
|
|
|
if ($trackIdsInAlbumCount != $trackIdsCount) {
|
|
|
|
$isSame = false;
|
|
|
|
} else {
|
|
|
|
for ($i = 0; $i < $trackIdsInAlbumCount; $i++) {
|
|
|
|
if ($i >= $trackIdsCount || $trackIdsInAlbum[$i] != $trackIds[$i]) {
|
|
|
|
$isSame = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($isSame) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$index = 1;
|
|
|
|
$tracksToRemove = [];
|
|
|
|
$albumsToFix = [];
|
|
|
|
|
|
|
|
foreach ($this->tracks as $track) {
|
|
|
|
$tracksToRemove[$track->id] = $track;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($trackIds as $trackId) {
|
|
|
|
if (!strlen(trim($trackId))) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-11-10 05:08:37 +01:00
|
|
|
/** @var $track Track */
|
2015-08-30 14:29:12 +02:00
|
|
|
$track = Track::find($trackId);
|
2015-11-10 05:08:37 +01:00
|
|
|
|
2015-08-30 14:29:12 +02:00
|
|
|
if ($track->album_id != null && $track->album_id != $this->id) {
|
|
|
|
$albumsToFix[] = $track->album;
|
|
|
|
}
|
|
|
|
|
|
|
|
$track->album_id = $this->id;
|
|
|
|
$track->track_number = $index;
|
|
|
|
$track->updateTags();
|
|
|
|
$track->save();
|
|
|
|
|
|
|
|
unset($tracksToRemove[$track->id]);
|
|
|
|
$index++;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($tracksToRemove as $track) {
|
2015-11-10 05:08:37 +01:00
|
|
|
/** @var $track Track */
|
|
|
|
|
2015-08-30 14:29:12 +02:00
|
|
|
$track->album_id = null;
|
|
|
|
$track->track_number = null;
|
|
|
|
$track->updateTags();
|
|
|
|
$track->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($albumsToFix as $album) {
|
2015-11-10 05:08:37 +01:00
|
|
|
/** @var $album Album */
|
|
|
|
|
2015-08-30 14:29:12 +02:00
|
|
|
$album->updateTrackNumbers();
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (Track::$Formats as $name => $format) {
|
2016-01-18 02:21:39 +01:00
|
|
|
Cache::forget($this->getCacheKey('filesize'.$name));
|
2015-08-30 14:29:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-01 17:49:28 +01:00
|
|
|
public function getCacheKey($key)
|
2015-08-30 14:29:12 +02:00
|
|
|
{
|
2016-01-18 02:21:39 +01:00
|
|
|
return 'album-'.$this->id.'-'.$key;
|
2015-08-30 14:29:12 +02:00
|
|
|
}
|
2016-01-04 17:42:30 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The number of tracks in an album will always be in sync.
|
|
|
|
*
|
|
|
|
* @param array $options
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function save(array $options = []) {
|
|
|
|
$this->recountTracks();
|
|
|
|
return parent::save($options);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function recountTracks() {
|
|
|
|
$this->track_count = $this->tracks->count();
|
|
|
|
}
|
2016-01-07 19:16:37 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns this model in Elasticsearch-friendly form. The array returned by
|
|
|
|
* this method should match the current mapping for this model's ES type.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2016-01-17 16:16:16 +01:00
|
|
|
public function toElasticsearch():array {
|
2016-01-07 19:16:37 +01:00
|
|
|
return [
|
|
|
|
'title' => $this->title,
|
|
|
|
'artist' => $this->user->display_name,
|
|
|
|
'tracks' => $this->tracks->pluck('title'),
|
|
|
|
];
|
|
|
|
}
|
2016-01-17 11:33:58 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
public function shouldBeIndexed():bool {
|
|
|
|
return $this->track_count > 0 && !$this->trashed();
|
|
|
|
}
|
2015-09-12 23:56:09 +02:00
|
|
|
}
|