From ec0eb4bb2a4bf0e03c6a0c359a276edcade90735 Mon Sep 17 00:00:00 2001 From: Seth Call Date: Tue, 18 Feb 2014 20:12:50 +0000 Subject: [PATCH] * VRFS-1140 --- .../jam_ruby/app/uploaders/mix_uploader.rb | 5 ++-- .../app/uploaders/recorded_track_uploader.rb | 5 ++-- ruby/lib/jam_ruby/models/feed.rb | 25 ++++++++++++++++--- ruby/spec/jam_ruby/models/feed_spec.rb | 20 +++++++++++++++ web/app/controllers/api_feeds_controller.rb | 3 ++- 5 files changed, 50 insertions(+), 8 deletions(-) diff --git a/ruby/lib/jam_ruby/app/uploaders/mix_uploader.rb b/ruby/lib/jam_ruby/app/uploaders/mix_uploader.rb index a0fe4c2e5..351560280 100644 --- a/ruby/lib/jam_ruby/app/uploaders/mix_uploader.rb +++ b/ruby/lib/jam_ruby/app/uploaders/mix_uploader.rb @@ -41,12 +41,13 @@ class MixUploader < CarrierWave::Uploader::Base # JamRecordingId=438 # JamMixId=438 # JamType=Mix - # secret sauce is -codec copy, and a bunch of -metadata arguments. + # secret sauce is -codec copy (or -acodec), and a bunch of -metadata arguments. # after done, stomp input file with new one input_file = current_path output_file = current_path + '.new.ogg' - ffmpeg_cmd = "#{APP_CONFIG.ffmpeg_path} -i \"#{input_file}\" -codec copy -metadata JamRecordingId=#{model.recording_id} -metadata JamMixId=#{model.id} -metadata JamType=Mix \"#{output_file}\"" + codec_param = RUBY_PLATFORM.include?('darwin') ? 'codec' : 'acodec' + ffmpeg_cmd = "#{APP_CONFIG.ffmpeg_path} -i \"#{input_file}\" -#{codec_param} copy -metadata JamRecordingId=#{model.recording_id} -metadata JamMixId=#{model.id} -metadata JamType=Mix \"#{output_file}\"" system(ffmpeg_cmd) unless $? == 0 diff --git a/ruby/lib/jam_ruby/app/uploaders/recorded_track_uploader.rb b/ruby/lib/jam_ruby/app/uploaders/recorded_track_uploader.rb index 17a80a752..7f8e12a17 100644 --- a/ruby/lib/jam_ruby/app/uploaders/recorded_track_uploader.rb +++ b/ruby/lib/jam_ruby/app/uploaders/recorded_track_uploader.rb @@ -18,12 +18,13 @@ class RecordedTrackUploader < CarrierWave::Uploader::Base #JamClientId=8331bcec-7810-42c1-9f39-a5c129406e85 #JamType=LocalTrack - # secret sauce is -codec copy, and a bunch of -metadata arguments. + # secret sauce is -codec copy (or -acodec), and a bunch of -metadata arguments # after done, stomp input file with new one input_file = current_path output_file = current_path + '.new.ogg' - ffmpeg_cmd = "#{APP_CONFIG.ffmpeg_path} -i \"#{input_file}\" -codec copy -metadata JamRecordingId=#{model.recording_id} -metadata JamTrackId=#{model.client_track_id} -metadata JamClientId=#{model.client_id} -metadata JamType=LocalTrack \"#{output_file}\"" + codec_param = RUBY_PLATFORM.include?('darwin') ? 'codec' : 'acodec' + ffmpeg_cmd = "#{APP_CONFIG.ffmpeg_path} -i \"#{input_file}\" -#{codec_param} copy -metadata JamRecordingId=#{model.recording_id} -metadata JamTrackId=#{model.client_track_id} -metadata JamClientId=#{model.client_id} -metadata JamType=LocalTrack \"#{output_file}\"" system(ffmpeg_cmd) unless $? == 0 diff --git a/ruby/lib/jam_ruby/models/feed.rb b/ruby/lib/jam_ruby/models/feed.rb index 31794ab9f..fd930102f 100644 --- a/ruby/lib/jam_ruby/models/feed.rb +++ b/ruby/lib/jam_ruby/models/feed.rb @@ -7,26 +7,38 @@ module JamRuby FIXNUM_MAX = (2**(0.size * 8 -2) -1) SORT_TYPES = ['date', 'plays', 'likes'] + TIME_RANGES = ['today', 'week', 'month', 'all'] + TYPE_FILTERS = ['session', 'recording', 'all'] - def self.index(params = {limit:20, sort: 'date'}) + def self.index(params = {}) limit = params[:limit] limit ||= 20 + limit = limit.to_i # validate sort sort = params[:sort] sort ||= 'date' raise "not valid sort #{sort}" unless SORT_TYPES.include?(sort) - start = params[:start] if sort == 'date' start ||= FIXNUM_MAX else start ||= 0 end + start = start.to_i + + time_range = params[:time_range] + time_range ||= 'month' + raise "not valid time_range #{time_range}" unless TIME_RANGES.include?(time_range) + + type_filter = params[:type] + type_filter ||= 'all' + raise "not valid type #{type_filter}" unless TYPE_FILTERS.include?(type_filter) query = Feed.includes([:recording]).includes([:music_session_history]).limit(limit) + # handle sort if sort == 'date' query = query.where("id < #{start}") query = query.order('id DESC') @@ -40,7 +52,14 @@ module JamRuby raise "sort not implemented: #{sort}" end - #query = query.order('claimed_recordings.created_at') + # handle time range + + # handle type filters + if type_filter == 'session' + query = query.where('music_session_id is not NULL') + elsif type_filter == 'recording' + query = query.where('recording_id is not NULL') + end if query.length == 0 [query, nil] diff --git a/ruby/spec/jam_ruby/models/feed_spec.rb b/ruby/spec/jam_ruby/models/feed_spec.rb index 8f9ee2359..13cb877b6 100644 --- a/ruby/spec/jam_ruby/models/feed_spec.rb +++ b/ruby/spec/jam_ruby/models/feed_spec.rb @@ -109,6 +109,25 @@ describe Feed do end end + describe "type filters" do + it "returns only sessions" do + # creates both recording and history record in feed + claimed_recording1 = FactoryGirl.create(:claimed_recording) + + feeds, start = Feed.index(:type => 'session') + feeds.length.should == 1 + feeds[0].music_session_history == claimed_recording1.recording.music_session.music_session_history + end + + it "returns only sessions" do + # creates both recording and history record in feed + claimed_recording1 = FactoryGirl.create(:claimed_recording) + + feeds, start = Feed.index(:type => 'session') + feeds.length.should == 1 + feeds[0].music_session_history == claimed_recording1.recording.music_session.music_session_history + end + end describe "pagination" do it "supports date pagination" do claimed_recording = FactoryGirl.create(:claimed_recording) @@ -170,6 +189,7 @@ describe Feed do feeds.length.should == 0 start.should be_nil end + end end diff --git a/web/app/controllers/api_feeds_controller.rb b/web/app/controllers/api_feeds_controller.rb index f65ce3295..a875395b3 100644 --- a/web/app/controllers/api_feeds_controller.rb +++ b/web/app/controllers/api_feeds_controller.rb @@ -6,7 +6,8 @@ class ApiFeedsController < ApiController @feeds, @next = Feed.index(user: current_user, start: params[:since], limit: params[:limit], - sort: params[:sort]) + sort: params[:sort], + time_range: params[:time_range]) render "api_feeds/index", :layout => nil end