213 lines
7.1 KiB
Ruby
213 lines
7.1 KiB
Ruby
module JamRuby
|
|
class MusicSessionHistory < ActiveRecord::Base
|
|
|
|
self.table_name = "music_sessions_history"
|
|
|
|
self.primary_key = 'id'
|
|
|
|
belongs_to(:user,
|
|
:class_name => 'JamRuby::User',
|
|
:foreign_key => :user_id,
|
|
:inverse_of => :music_session_histories)
|
|
|
|
belongs_to(:band,
|
|
:class_name => 'JamRuby::Band',
|
|
:foreign_key => :band_id,
|
|
:inverse_of => :music_session_history)
|
|
|
|
belongs_to(:music_session,
|
|
:class_name => 'JamRuby::MusicSession',
|
|
:foreign_key => 'music_session_id')
|
|
|
|
has_many :music_session_user_histories, :class_name => "JamRuby::MusicSessionUserHistory", :foreign_key => "music_session_id", :dependent => :delete_all
|
|
has_many :comments, :class_name => "JamRuby::MusicSessionComment", :foreign_key => "music_session_id"
|
|
has_many :likes, :class_name => "JamRuby::MusicSessionLiker", :foreign_key => "session_id"
|
|
has_many :plays, :class_name => "JamRuby::PlayablePlay", :as => :playable, :dependent => :destroy
|
|
has_one :share_token, :class_name => "JamRuby::ShareToken", :inverse_of => :shareable, :foreign_key => 'shareable_id'
|
|
has_one :feed, :class_name => "JamRuby::Feed", :inverse_of => :music_session_history, :foreign_key => 'music_session_id', :dependent => :destroy
|
|
|
|
|
|
before_create :generate_share_token
|
|
before_create :add_to_feed
|
|
|
|
SHARE_TOKEN_LENGTH = 8
|
|
|
|
SEPARATOR = '|'
|
|
|
|
def add_to_feed
|
|
feed = Feed.new
|
|
feed.music_session_history = self
|
|
end
|
|
|
|
def comment_count
|
|
self.comments.size
|
|
end
|
|
|
|
def grouped_tracks
|
|
tracks = []
|
|
self.music_session_user_histories.each do |msuh|
|
|
user = User.find(msuh.user_id)
|
|
t = Track.new
|
|
t.musician = user
|
|
t.instrument_ids = []
|
|
# this treats each track as a "user", which has 1 or more instruments in the session
|
|
unless msuh.instruments.blank?
|
|
instruments = msuh.instruments.split(SEPARATOR)
|
|
instruments.each do |instrument|
|
|
if !t.instrument_ids.include? instrument
|
|
t.instrument_ids << instrument
|
|
end
|
|
end
|
|
end
|
|
tracks << t
|
|
end
|
|
tracks
|
|
end
|
|
|
|
def self.index(current_user, user_id, band_id = nil, genre = nil)
|
|
hide_private = false
|
|
if current_user.id != user_id
|
|
hide_private = false # TODO: change to true once public flag exists
|
|
end
|
|
|
|
query = MusicSessionHistory
|
|
.joins(
|
|
%Q{
|
|
LEFT OUTER JOIN
|
|
music_sessions_user_history
|
|
ON
|
|
music_sessions_history.music_session_id = music_sessions_user_history.music_session_id
|
|
}
|
|
)
|
|
.where(
|
|
%Q{
|
|
music_sessions_history.user_id = '#{user_id}'
|
|
}
|
|
)
|
|
|
|
#query = query.where("public = false") unless !hide_private
|
|
query = query.where("music_sessions_history.band_id = '#{band_id}") unless band_id.nil?
|
|
query = query.where("music_sessions_history.genres like '%#{genre}%'") unless genre.nil?
|
|
return query
|
|
end
|
|
|
|
def unique_users
|
|
User
|
|
.joins(:music_session_user_histories)
|
|
.group("users.id")
|
|
.order("users.id")
|
|
.where(%Q{ music_sessions_user_history.music_session_id = '#{music_session_id}'})
|
|
end
|
|
|
|
# returns one user history per user, with instruments all crammed together, and with total duration
|
|
def unique_user_histories
|
|
MusicSessionUserHistory
|
|
.joins(:user)
|
|
.select("STRING_AGG(instruments, '|') AS total_instruments,
|
|
SUM(date_part('epoch', COALESCE(music_sessions_user_history.session_removed_at, music_sessions_user_history.created_at) - music_sessions_user_history.created_at)) AS total_duration,
|
|
music_sessions_user_history.user_id, music_sessions_user_history.music_session_id, users.first_name, users.last_name, users.photo_url")
|
|
.group("music_sessions_user_history.user_id, music_sessions_user_history.music_session_id, users.first_name, users.last_name, users.photo_url")
|
|
.order("music_sessions_user_history.user_id")
|
|
.where(%Q{ music_sessions_user_history.music_session_id = '#{music_session_id}'})
|
|
end
|
|
|
|
def duration_minutes
|
|
end_time = self.session_removed_at || Time.now
|
|
(end_time - self.created_at) / 60.0
|
|
end
|
|
|
|
def music_session_user_histories
|
|
@msuh ||= JamRuby::MusicSessionUserHistory
|
|
.where(:music_session_id => self.music_session_id)
|
|
.order('created_at DESC')
|
|
end
|
|
|
|
def comments
|
|
@comments ||= JamRuby::MusicSessionComment
|
|
.where(:music_session_id => self.music_session_id)
|
|
.order('created_at DESC')
|
|
end
|
|
|
|
def likes
|
|
@likes ||= JamRuby::MusicSessionLiker
|
|
.where(:music_session_id => self.music_session_id)
|
|
end
|
|
|
|
def self.save(music_session)
|
|
session_history = MusicSessionHistory.find_by_music_session_id(music_session.id)
|
|
|
|
if session_history.nil?
|
|
session_history = MusicSessionHistory.new
|
|
end
|
|
|
|
session_history.music_session_id = music_session.id
|
|
session_history.description = music_session.description unless music_session.description.nil?
|
|
session_history.user_id = music_session.creator.id
|
|
session_history.band_id = music_session.band.id unless music_session.band.nil?
|
|
session_history.genres = music_session.genres.map { |g| g.id }.join SEPARATOR if music_session.genres.count > 0
|
|
session_history.fan_access = music_session.fan_access
|
|
session_history.save!
|
|
end
|
|
|
|
def is_over?
|
|
music_session.nil? || !session_removed_at.nil?
|
|
end
|
|
|
|
def has_mount?
|
|
music_session && music_session.mount
|
|
end
|
|
|
|
def recordings
|
|
Recording.where(music_session_id: self.id)
|
|
end
|
|
|
|
def end_history
|
|
self.update_attribute(:session_removed_at, Time.now)
|
|
|
|
|
|
# ensure all user histories are closed
|
|
music_session_user_histories.each do |music_session_user_history|
|
|
music_session_user_history.end_history
|
|
|
|
# then update any users that need their user progress updated
|
|
if music_session_user_history.duration_minutes > 15 && music_session_user_history.max_concurrent_connections >= 3
|
|
music_session_user_history.user.update_progression_field(:first_real_music_session_at)
|
|
end
|
|
end
|
|
|
|
end
|
|
|
|
def self.removed_music_session(session_id)
|
|
hist = self
|
|
.where(:music_session_id => session_id)
|
|
.limit(1)
|
|
.first
|
|
|
|
hist.end_history if hist
|
|
|
|
Notification.send_session_ended(session_id)
|
|
end
|
|
|
|
def remove_non_alpha_num(token)
|
|
token.gsub(/[^0-9A-Za-z]/, '')
|
|
end
|
|
|
|
private
|
|
def generate_share_token
|
|
self.id = music_session.id # unify music_session.id and music_session_history.id
|
|
|
|
token = loop do
|
|
token = SecureRandom.urlsafe_base64(SHARE_TOKEN_LENGTH, false)
|
|
token = remove_non_alpha_num(token)
|
|
token.upcase!
|
|
break token unless ShareToken.exists?(token: token)
|
|
end
|
|
|
|
self.share_token = ShareToken.new
|
|
self.share_token.token = token
|
|
self.share_token.shareable_type = "session"
|
|
end
|
|
|
|
end
|
|
end
|