2014-02-15 21:19:03 +00:00
module JamRuby
class Feed < ActiveRecord :: Base
2014-02-16 13:10:00 +00:00
belongs_to :recording , class_name : " JamRuby::Recording " , inverse_of : :feed , foreign_key : 'recording_id'
2014-05-06 13:34:38 +00:00
belongs_to :music_session , class_name : " JamRuby::MusicSession " , inverse_of : :feed , foreign_key : 'music_session_id'
2014-02-15 21:19:03 +00:00
2014-02-18 18:53:16 +00:00
FIXNUM_MAX = ( 2 ** ( 0 . size * 8 - 2 ) - 1 )
SORT_TYPES = [ 'date' , 'plays' , 'likes' ]
2014-02-20 02:22:41 +00:00
TIME_RANGES = { " today " = > 1 , " week " = > 7 , " month " = > 30 , " all " = > 0 }
2014-05-06 13:34:38 +00:00
TYPE_FILTERS = [ 'music_session' , 'recording' , 'all' ]
2014-02-17 23:26:17 +00:00
2014-02-20 03:23:38 +00:00
def self . index ( user , params = { } )
2014-02-17 23:26:17 +00:00
limit = params [ :limit ]
limit || = 20
2014-02-18 20:12:50 +00:00
limit = limit . to_i
2014-02-17 23:26:17 +00:00
# validate sort
sort = params [ :sort ]
sort || = 'date'
2014-02-18 18:53:16 +00:00
raise " not valid sort #{ sort } " unless SORT_TYPES . include? ( sort )
2014-02-20 05:18:11 +00:00
start = params [ :start ] . presence
2014-02-18 18:53:16 +00:00
if sort == 'date'
start || = FIXNUM_MAX
else
start || = 0
end
2014-02-18 20:12:50 +00:00
start = start . to_i
time_range = params [ :time_range ]
time_range || = 'month'
2014-02-20 02:22:41 +00:00
raise " not valid time_range #{ time_range } " unless TIME_RANGES . has_key? ( time_range )
2014-02-18 20:12:50 +00:00
type_filter = params [ :type ]
type_filter || = 'all'
raise " not valid type #{ type_filter } " unless TYPE_FILTERS . include? ( type_filter )
2014-02-17 23:26:17 +00:00
2014-02-20 07:45:51 +00:00
target_user = params [ :user ]
target_band = params [ :band ]
2014-02-20 03:23:38 +00:00
2014-05-06 13:34:38 +00:00
#query = Feed.includes([:recording]).includes([:music_session]).limit(limit)
2014-02-20 07:45:51 +00:00
query = Feed . joins ( " LEFT OUTER JOIN recordings ON recordings.id = feeds.recording_id " )
. joins ( " LEFT OUTER JOIN music_sessions_history ON music_sessions_history.id = feeds.music_session_id " )
. limit ( limit )
2014-02-18 13:34:51 +00:00
2014-02-18 20:12:50 +00:00
# handle sort
2014-02-17 23:26:17 +00:00
if sort == 'date'
2014-02-20 07:45:51 +00:00
query = query . where ( " feeds.id < #{ start } " )
query = query . order ( 'feeds.id DESC' )
2014-02-18 03:42:42 +00:00
elsif sort == 'plays'
2014-02-18 18:53:16 +00:00
query = query . offset ( start )
2014-02-18 13:34:51 +00:00
query = query . order ( " COALESCE(recordings.play_count, music_sessions_history.play_count) DESC " )
elsif sort == 'likes'
2014-02-18 18:53:16 +00:00
query = query . offset ( start )
2014-02-18 13:34:51 +00:00
query = query . order ( " COALESCE(recordings.like_count, music_sessions_history.like_count) DESC " )
else
raise " sort not implemented: #{ sort } "
2014-02-17 23:26:17 +00:00
end
2014-02-18 20:12:50 +00:00
# handle time range
2014-02-20 02:22:41 +00:00
days = TIME_RANGES [ time_range ]
if days > 0
query = query . where ( " feeds.created_at > NOW() - ' #{ days } day'::INTERVAL " )
end
2014-02-18 20:12:50 +00:00
# handle type filters
2014-05-06 13:34:38 +00:00
if type_filter == 'music_session'
2014-02-20 07:45:51 +00:00
query = query . where ( 'feeds.music_session_id is not NULL' )
2014-02-18 20:12:50 +00:00
elsif type_filter == 'recording'
2014-02-20 07:45:51 +00:00
query = query . where ( 'feeds.recording_id is not NULL' )
2014-02-18 20:12:50 +00:00
end
2014-02-18 18:53:16 +00:00
2014-02-20 03:23:38 +00:00
2014-02-20 07:45:51 +00:00
if target_user
if target_user != user . id
require_public_recordings = " claimed_recordings.is_public = TRUE AND "
require_public_sessions = " music_sessions_history.fan_access = TRUE AND "
end
query = query . joins ( " LEFT OUTER JOIN claimed_recordings ON recordings.id = claimed_recordings.recording_id AND #{ require_public_recordings } (claimed_recordings.user_id = ' #{ target_user } ' OR (recordings.band_id IN (SELECT band_id FROM bands_musicians where user_id=' #{ target_user } '))) " )
query = query . joins ( " LEFT OUTER JOIN music_sessions_user_history ON music_sessions_history.id = music_sessions_user_history.music_session_id AND #{ require_public_sessions } music_sessions_user_history.user_id = ' #{ target_user } ' " )
query = query . group ( " feeds.id, feeds.recording_id, feeds.music_session_id, feeds.created_at, feeds.updated_at, recordings.id, music_sessions_history.id " )
if sort == 'plays'
query = query . group ( " COALESCE(recordings.play_count, music_sessions_history.play_count) " )
elsif sort == 'likes'
query = query . group ( " COALESCE(recordings.like_count, music_sessions_history.like_count) " )
end
query = query . where ( 'recordings.id is NULL OR claimed_recordings.id IS NOT NULL' )
query = query . where ( 'music_sessions_history.id is NULL OR music_sessions_user_history.id IS NOT NULL' )
elsif target_band
unless Band . find ( target_band ) . users . include? ( user )
require_public_recordings = " claimed_recordings.is_public = TRUE AND "
require_public_sessions = " music_sessions_history.fan_access = TRUE AND "
end
2014-02-20 03:23:38 +00:00
2014-02-20 07:45:51 +00:00
query = query . joins ( " LEFT OUTER JOIN claimed_recordings ON recordings.id = claimed_recordings.recording_id AND #{ require_public_recordings } recordings.band_id = ' #{ target_band } ' " )
query = query . where ( " music_sessions_history IS NULL OR #{ require_public_sessions } music_sessions_history.band_id = ' #{ target_band } ' " )
query = query . group ( " feeds.id, feeds.recording_id, feeds.music_session_id, feeds.created_at, feeds.updated_at, recordings.id, music_sessions_history.id " )
if sort == 'plays'
query = query . group ( " COALESCE(recordings.play_count, music_sessions_history.play_count) " )
elsif sort == 'likes'
query = query . group ( " COALESCE(recordings.like_count, music_sessions_history.like_count) " )
end
query = query . where ( 'recordings.id is NULL OR claimed_recordings.id IS NOT NULL' )
#query = query.where('music_sessions_history.id is NULL OR music_sessions_user_history.id IS NOT NULL')
else
query = query . joins ( 'LEFT OUTER JOIN claimed_recordings ON recordings.id = claimed_recordings.recording_id AND claimed_recordings.is_public = TRUE' )
query = query . joins ( " LEFT OUTER JOIN music_sessions_user_history ON music_sessions_history.id = music_sessions_user_history.music_session_id AND music_sessions_history.fan_access = TRUE " )
query = query . group ( " feeds.id, feeds.recording_id, feeds.music_session_id, feeds.created_at, feeds.updated_at, recordings.id, music_sessions_history.id " )
if sort == 'plays'
query = query . group ( " COALESCE(recordings.play_count, music_sessions_history.play_count) " )
elsif sort == 'likes'
query = query . group ( " COALESCE(recordings.like_count, music_sessions_history.like_count) " )
end
query = query . where ( 'recordings.id is NULL OR claimed_recordings.is_public = TRUE' )
query = query . where ( 'music_sessions_history.id is NULL OR music_sessions_user_history.id IS NOT NULL' )
2014-02-20 03:23:38 +00:00
end
2014-02-20 07:45:51 +00:00
2014-03-11 06:09:44 +00:00
if params [ :hash ]
if query . length == 0
{ query : query , next : nil }
elsif query . length < limit
{ query : query , next : nil }
else
if sort == 'date'
{ query : query , next : query . last . id }
else
{ query : query , next : start + limit }
end
end
2014-02-18 18:53:16 +00:00
else
2014-03-11 06:09:44 +00:00
if query . length == 0
[ query , nil ]
elsif query . length < limit
[ query , nil ]
2014-02-18 18:53:16 +00:00
else
2014-03-11 06:09:44 +00:00
if sort == 'date'
[ query , query . last . id ]
else
[ query , start + limit ]
end
2014-02-18 18:53:16 +00:00
end
end
2014-02-15 21:19:03 +00:00
end
end
end