/** * @throws \Exception * @return CommandResponse */ public function execute() { $typeId = $this->_resourceType . '_id'; $existing = Favourite::where($typeId, '=', $this->_resourceId)->whereUserId(Auth::user()->id)->first(); $isFavourited = false; if ($existing) { $existing->delete(); } else { $fav = new Favourite(); $fav->{$typeId} = $this->_resourceId; $fav->user_id = Auth::user()->id; $fav->created_at = time(); $fav->save(); $isFavourited = true; } $resourceUser = ResourceUser::get(Auth::user()->id, $this->_resourceType, $this->_resourceId); $resourceUser->is_favourited = $isFavourited; $resourceUser->save(); $resourceTable = $this->_resourceType . 's'; // We do this to prevent a race condition. Sure I could simply increment the count columns and re-save back to the db // but that would require an additional SELECT and the operation would be non-atomic. If two log items are created // for the same resource at the same time, the cached values will still be correct with this method. DB::table($resourceTable)->whereId($this->_resourceId)->update(['favourite_count' => DB::raw('( SELECT COUNT(id) FROM favourites WHERE ' . $typeId . ' = ' . $this->_resourceId . ')')]); return CommandResponse::succeed(['is_favourited' => $isFavourited]); }
public function getPlaylists() { $query = Favourite::whereUserId(Auth::user()->id)->whereNotNull('playlist_id')->with(['playlist' => function ($query) { $query->userDetails(); }, 'playlist.user', 'playlist.user.avatar', 'playlist.tracks', 'playlist.tracks.cover']); $playlists = []; foreach ($query->get() as $fav) { if ($fav->playlist == null) { continue; } $playlists[] = Playlist::mapPublicPlaylistSummary($fav->playlist); } return Response::json(["playlists" => $playlists], 200); }
public function getFavourites($slug) { $user = User::whereSlug($slug)->first(); if (!$user) { App::abort(404); } $favs = Favourite::whereUserId($user->id)->with(['track.genre', 'track.cover', 'track.user', 'album.cover', 'album.user', 'track' => function ($query) { $query->userDetails(); }, 'album' => function ($query) { $query->userDetails(); }])->get(); $tracks = []; $albums = []; foreach ($favs as $fav) { if ($fav->type == 'Poniverse\\Ponyfm\\Models\\Track') { $tracks[] = Track::mapPublicTrackSummary($fav->track); } else { if ($fav->type == 'Poniverse\\Ponyfm\\Models\\Album') { $albums[] = Album::mapPublicAlbumSummary($fav->album); } } } return Response::json(['tracks' => $tracks, 'albums' => $albums], 200); }
/** * Execute the console command. * * @return mixed */ public function handle() { // Get list of affected users $usernames = DB::table('users')->select(['username', DB::raw('COUNT(*) as count')])->whereNull('disabled_at')->whereNotNull('username')->groupBy(DB::raw('LOWER(username)'))->having('count', '>=', 2)->lists('username'); foreach ($usernames as $username) { // Find the relevant accounts // ========================== /** @var Collection $accounts */ $accounts = User::where('username', $username)->orderBy('created_at', 'ASC')->get(); $firstAccount = $accounts[0]; $accounts->forget(0); $accountIds = $accounts->pluck('id'); // Reassign content // ================ // This is done with the less-efficient-than-raw-SQL Eloquent // methods to generate appropriate revision logs. $this->info('Merging duplicates for: ' . $firstAccount->username); DB::transaction(function () use($accounts, $accountIds, $firstAccount) { foreach (Album::whereIn('user_id', $accountIds)->get() as $album) { $album->user_id = $firstAccount->id; $album->save(); } foreach (Comment::whereIn('user_id', $accountIds)->get() as $comment) { $comment->user_id = $firstAccount->id; $comment->save(); } foreach (Favourite::whereIn('user_id', $accountIds)->get() as $favourite) { $favourite->user_id = $firstAccount->id; $favourite->save(); } foreach (Follower::whereIn('artist_id', $accountIds)->get() as $follow) { $follow->artist_id = $firstAccount->id; $follow->save(); } foreach (Image::whereIn('uploaded_by', $accountIds)->get() as $image) { $image->uploaded_by = $firstAccount->id; $image->save(); } foreach (Image::whereIn('uploaded_by', $accountIds)->get() as $image) { $image->uploaded_by = $firstAccount->id; $image->save(); } DB::table('oauth2_tokens')->whereIn('user_id', $accountIds)->update(['user_id' => $firstAccount->id]); foreach (PinnedPlaylist::whereIn('user_id', $accountIds)->get() as $playlist) { $playlist->user_id = $firstAccount->id; $playlist->save(); } foreach (Playlist::whereIn('user_id', $accountIds)->get() as $playlist) { $playlist->user_id = $firstAccount->id; $playlist->save(); } foreach (ResourceLogItem::whereIn('user_id', $accountIds)->get() as $item) { $item->user_id = $firstAccount->id; $item->save(); } foreach (ResourceUser::whereIn('user_id', $accountIds)->get() as $item) { $item->user_id = $firstAccount->id; $item->save(); } foreach (Track::whereIn('user_id', $accountIds)->get() as $track) { $track->user_id = $firstAccount->id; $track->save(); } foreach ($accounts as $account) { $account->disabled_at = Carbon::now(); $account->save(); } }); } }