| 1 | class SongsController < ApplicationController |
| 2 | def index |
| 3 | meta_tags 'songs' |
| 4 | @page = Page |
| 5 | .select(:page_content, :title) |
| 6 | .where(page_slug: 'songs', locale: [I18n.locale, I18n.default_locale]) |
| 7 | .last |
| 8 | |
| 9 | @songs = Song |
| 10 | .select(:id, :name, :description, :updated_at, :album_id, :views_count) |
| 11 | .paginate(page: params[:page], per_page: 10) |
| 12 | |
| 13 | @trending_songs = Song |
| 14 | .select(:id, :name, :description, :album_id, :views_count, :updated_at) |
| 15 | .order(views_count: :desc) |
| 16 | .limit(10) |
| 17 | |
| 18 | @most_loved_songs = Song |
| 19 | .joins(:users) |
| 20 | .select(:id, :name, :description, :updated_at, :album_id, 'COUNT(users.id) as users_count') |
| 21 | .group('songs.id') |
| 22 | .order(users_count: :desc) |
| 23 | .limit(10) |
| 24 | end |
| 25 | |
| 26 | def detail |
| 27 | unless valid_params? |
| 28 | redirect_to root_path |
| 29 | return |
| 30 | end |
| 31 | |
| 32 | @lovers = @song.users |
| 33 | @author = @song.user ||= User.find_by(username: 'nowhereman') # One must have nowhereman |
| 34 | |
| 35 | @page_title = t( |
| 36 | 'song_detail_page_title', |
| 37 | song_name: @song.name |
| 38 | ) |
| 39 | |
| 40 | @meta_title = t( |
| 41 | 'song_detail_page_meta_title', |
| 42 | song_name: @song.name, |
| 43 | song_artist_name: @song.album.artists[0].name, |
| 44 | song_album_name: @song.album.name |
| 45 | ) |
| 46 | |
| 47 | description = ActionController::Base.helpers.strip_tags(@song.description.truncate(400)) |
| 48 | @meta_description = description.gsub('\\n', ' ').gsub(/\r\n?/, ' ') |
| 49 | @meta_url = request.url |
| 50 | end |
| 51 | |
| 52 | def bridge |
| 53 | meta_tags 'songs_bridge' |
| 54 | return redirect_to song_path unless valid_bridge_params? |
| 55 | |
| 56 | @sf_link = "#{ENV['SF_HOST']}/split/#{@song.id}" |
| 57 | @page_title = @song.name |
| 58 | @meta_title = @song.name |
| 59 | @meta_description = @song.description |
| 60 | end |
| 61 | |
| 62 | def chords |
| 63 | unless valid_params? |
| 64 | redirect_to root_path |
| 65 | return |
| 66 | end |
| 67 | |
| 68 | @chords = Chord.where(song_id: @song.id).order(views_count: :desc) |
| 69 | @lovers = @song.users |
| 70 | @author = @song.user ||= User.find_by(username: 'nowhereman') # One must have nowhereman |
| 71 | |
| 72 | @page_title = t( |
| 73 | 'song_chords_page_title', |
| 74 | song_name: @song.name |
| 75 | ) |
| 76 | |
| 77 | @meta_title = @page_title |
| 78 | description = ActionController::Base.helpers.strip_tags(@song.description.truncate(400)) |
| 79 | @meta_description = description.gsub('\\n', ' ').gsub(/\r\n?/, ' ') |
| 80 | @meta_url = request.url |
| 81 | end |
| 82 | |
| 83 | def audio |
| 84 | unless valid_params? |
| 85 | redirect_to root_path |
| 86 | return |
| 87 | end |
| 88 | |
| 89 | @audio_files = AudioFile.where(song_provider_id: @song.id) |
| 90 | @karaoke_files = @audio_files.map(&:karaoke_file) |
| 91 | |
| 92 | @chords = Chord.where(song_id: @song.id).order(views_count: :desc) |
| 93 | @lovers = @song.users |
| 94 | @author = @song.user ||= User.find_by(username: 'nowhereman') # One must have nowhereman |
| 95 | |
| 96 | @page_title = t( |
| 97 | 'song_audio_page_title', |
| 98 | song_name: @song.name |
| 99 | ) |
| 100 | |
| 101 | @meta_title = @page_title |
| 102 | description = ActionController::Base.helpers.strip_tags(@song.description.truncate(400)) |
| 103 | @meta_description = description.gsub('\\n', ' ').gsub(/\r\n?/, ' ') |
| 104 | @meta_url = request.url |
| 105 | end |
| 106 | |
| 107 | def lyrics |
| 108 | unless valid_params? |
| 109 | redirect_to root_path |
| 110 | return |
| 111 | end |
| 112 | |
| 113 | @lyrics = Lyric.where(song_id: @song.id).order(views_count: :desc) |
| 114 | |
| 115 | @page_title = t( |
| 116 | 'song_lyrics_page_title', |
| 117 | song_name: @song.name |
| 118 | ) |
| 119 | |
| 120 | @meta_title = @page_title |
| 121 | description = ActionController::Base.helpers.strip_tags(@song.description.truncate(400)) |
| 122 | @meta_description = description.gsub('\\n', ' ').gsub(/\r\n?/, ' ') |
| 123 | @meta_url = request.url |
| 124 | end |
| 125 | |
| 126 | def karaoke |
| 127 | # Simply redirect to splitfire |
| 128 | redirect_to ENV['SF_HOST'] |
| 129 | end |
| 130 | |
| 131 | def most_loved |
| 132 | @page_title = t('breadcrumb_most_loved_songs') |
| 133 | @most_loved_songs = Song |
| 134 | .joins(:users) |
| 135 | .select(:id, :name, :description, :album_id, 'COUNT(users.id) as users_count') |
| 136 | .group('songs.id') |
| 137 | .order(users_count: :desc) |
| 138 | |
| 139 | @meta_title = @page_title |
| 140 | @meta_description = @page_title |
| 141 | @meta_url = request.url |
| 142 | end |
| 143 | |
| 144 | def loved |
| 145 | redirect_to_login && return unless user_signed_in? |
| 146 | |
| 147 | plea = SongChordPlea.new(song_id: params[:id], user_id: current_user.id) |
| 148 | plea.save |
| 149 | |
| 150 | redirect_back(fallback_location: root_path) |
| 151 | end |
| 152 | |
| 153 | def unloved |
| 154 | redirect_to_login && return unless user_signed_in? |
| 155 | |
| 156 | plea = SongChordPlea.where(song_id: params[:id], user_id: current_user.id) |
| 157 | plea.destroy_all |
| 158 | |
| 159 | redirect_back(fallback_location: root_path) |
| 160 | end |
| 161 | |
| 162 | def bass_backing_track |
| 163 | # Get the song_id |
| 164 | id = params[:slug].split('-')[-1] |
| 165 | @song = Song.find_by id: id |
| 166 | @page_title = t( |
| 167 | 'song_karaoke_page_title', |
| 168 | song_name: @song.name, |
| 169 | song_artist_name: @song.album.artists[0].name, |
| 170 | song_album_name: @song.album.name |
| 171 | ) |
| 172 | |
| 173 | @lovers = nil |
| 174 | @author = nil |
| 175 | @karaoke_file_shown = nil |
| 176 | |
| 177 | if @song.audio_files.count == 0 |
| 178 | @shown_text = t( |
| 179 | 'song_karaoke_unavailable_content', |
| 180 | song_name: @song.name, |
| 181 | song_artist_name: @song.album.artists[0].name |
| 182 | ) |
| 183 | |
| 184 | @meta_description = t( |
| 185 | 'song_karaoke_unavailable_meta_description', |
| 186 | song_name: @song.name, |
| 187 | song_artist_name: @song.album.artists[0].name, |
| 188 | song_album_name: @song.album.name |
| 189 | ) |
| 190 | |
| 191 | else |
| 192 | @meta_description = "#{@page_title}. #{@song.album.name}" |
| 193 | end |
| 194 | |
| 195 | @meta_title = @page_title |
| 196 | end |
| 197 | |
| 198 | def bass_tab |
| 199 | # Simply redirect to splitfire |
| 200 | redirect_to ENV['SF_HOST'] |
| 201 | end |
| 202 | |
| 203 | def drum_backing_track |
| 204 | # Get the song_id |
| 205 | id = params[:slug].split('-')[-1] |
| 206 | @song = Song.find_by id: id |
| 207 | @page_title = t( |
| 208 | 'song_karaoke_page_title', |
| 209 | song_name: @song.name, |
| 210 | song_artist_name: @song.album.artists[0].name, |
| 211 | song_album_name: @song.album.name |
| 212 | ) |
| 213 | |
| 214 | @lovers = nil |
| 215 | @author = nil |
| 216 | @karaoke_file_shown = nil |
| 217 | |
| 218 | if @song.audio_files.count == 0 |
| 219 | @shown_text = t( |
| 220 | 'song_karaoke_unavailable_content', |
| 221 | song_name: @song.name, |
| 222 | song_artist_name: @song.album.artists[0].name |
| 223 | ) |
| 224 | |
| 225 | @meta_description = t( |
| 226 | 'song_karaoke_unavailable_meta_description', |
| 227 | song_name: @song.name, |
| 228 | song_artist_name: @song.album.artists[0].name, |
| 229 | song_album_name: @song.album.name |
| 230 | ) |
| 231 | |
| 232 | else |
| 233 | @meta_description = "#{@page_title}. #{@song.album.name}" |
| 234 | end |
| 235 | |
| 236 | @meta_title = @page_title |
| 237 | end |
| 238 | |
| 239 | private |
| 240 | |
| 241 | def valid_params? |
| 242 | id = params[:slug].split('-')[-1] |
| 243 | @song = Song.find_by(id: id) |
| 244 | if @song.nil? |
| 245 | flash[:danger] = t('not_found') |
| 246 | return false |
| 247 | end |
| 248 | @lovers = @song.users |
| 249 | @author = @song.user ||= User.find_by(username: 'nowhereman') # One must have nowhereman |
| 250 | true |
| 251 | end |
| 252 | |
| 253 | def valid_bridge_params? |
| 254 | id = params[:slug].split('-')[-1] |
| 255 | @song = SongProvider.find_by(id: id) |
| 256 | if @song.nil? |
| 257 | flash[:danger] = t('not_found') |
| 258 | return false |
| 259 | end |
| 260 | @song_in_albums = [] |
| 261 | true |
| 262 | end |
| 263 | |
| 264 | def redirect_to_login |
| 265 | flash[:success] = t('need_to_logged_in') |
| 266 | song = Song.find_by(id: params[:id]) |
| 267 | redirect_to login_path(redirect_to: song_detail_path(song.slug)) |
| 268 | end |
| 269 | |
| 270 | def meta_tags(slug) |
| 271 | meta = Meta |
| 272 | .select(:title, :description) |
| 273 | .where(home: slug, locale: [I18n.locale, I18n.default_locale]) |
| 274 | .last |
| 275 | |
| 276 | @meta_title = meta.title if meta |
| 277 | @meta_description = meta.description if meta |
| 278 | end |
| 279 | end |