VRFS-987 backend support for session/recording likes, plays, and comments

This commit is contained in:
Brian Smith 2014-01-12 12:51:06 -05:00
parent 660e452996
commit 6d42e42c18
15 changed files with 228 additions and 5 deletions

View File

@ -86,4 +86,5 @@ music_sessions_have_claimed_recording.sql
discardable_recorded_tracks2.sql
icecast.sql
home_page_promos.sql
mix_job_watch.sql
mix_job_watch.sql
music_session_constraints.sql

View File

@ -0,0 +1,9 @@
alter table music_sessions_comments drop constraint music_sessions_comments_music_session_id_fkey;
alter table music_sessions_comments add constraint ms_comments_ms_history_fkey foreign key (music_session_id)
references music_sessions_history(music_session_id) match simple
ON UPDATE NO ACTION ON DELETE CASCADE;
alter table music_sessions_likers drop constraint music_sessions_likers_music_session_id_fkey;
alter table music_sessions_likers add constraint ms_likers_ms_history_fkey foreign key (music_session_id)
references music_sessions_history(music_session_id) match simple
ON UPDATE NO ACTION ON DELETE CASCADE;

View File

@ -64,6 +64,8 @@ require "jam_ruby/models/band_musician"
require "jam_ruby/models/connection"
require "jam_ruby/models/friendship"
require "jam_ruby/models/music_session"
require "jam_ruby/models/music_session_comment"
require "jam_ruby/models/music_session_liker"
require "jam_ruby/models/music_session_history"
require "jam_ruby/models/music_session_user_history"
require "jam_ruby/models/music_session_perf_data"
@ -80,6 +82,9 @@ require "jam_ruby/models/user_follower"
require "jam_ruby/models/user_following"
require "jam_ruby/models/search"
require "jam_ruby/models/recording"
require "jam_ruby/models/recording_comment"
require "jam_ruby/models/recording_liker"
require "jam_ruby/models/recording_play"
require "jam_ruby/models/recorded_track"
require "jam_ruby/models/recorded_track_observer"
require "jam_ruby/models/mix"

View File

@ -37,6 +37,7 @@ module JamRuby
validate :creator_is_musician
validate :no_new_playback_while_playing
def creator_is_musician
unless creator.musician?
errors.add(:creator, ValidationMessages::MUST_BE_A_MUSICIAN)

View File

@ -0,0 +1,12 @@
module JamRuby
class MusicSessionComment < ActiveRecord::Base
self.table_name = "music_sessions_comments"
self.primary_key = 'id'
belongs_to :music_session, :class_name => "JamRuby::MusicSessionHistory", :foreign_key => "music_session_id"
belongs_to :user, :class_name => "JamRuby::User", :foreign_key => "creator_id"
end
end

View File

@ -15,8 +15,19 @@ module JamRuby
:foreign_key => :band_id,
:inverse_of => :music_session_history)
has_many :comments, :class_name => "JamRuby::MusicSessionComment", :foreign_key => "music_session_id"
has_many :likes, :class_name => "JamRuby::MusicSessionLiker", :foreign_key => "music_session_id"
GENRE_SEPARATOR = '|'
def comment_count
self.comments.size
end
def like_count
self.likes.size
end
def self.index(current_user, user_id, band_id = nil, genre = nil)
hide_private = false
if current_user.id != user_id

View File

@ -0,0 +1,12 @@
module JamRuby
class MusicSessionLiker < ActiveRecord::Base
self.table_name = "music_sessions_likers"
self.primary_key = 'id'
belongs_to :music_session, :class_name => "JamRuby::MusicSessionHistory", :foreign_key => "music_session_id"
belongs_to :user, :class_name => "JamRuby::User", :foreign_key => "liker_id"
end
end

View File

@ -7,17 +7,34 @@ module JamRuby
has_many :claimed_recordings, :class_name => "JamRuby::ClaimedRecording", :inverse_of => :recording
has_many :users, :through => :recorded_tracks, :class_name => "JamRuby::User"
has_many :mixes, :class_name => "JamRuby::Mix", :inverse_of => :recording
has_many :recorded_tracks, :class_name => "JamRuby::RecordedTrack", :foreign_key => :recording_id
has_many :comments, :class_name => "JamRuby::RecordingComment", :foreign_key => "recording_id"
has_many :likes, :class_name => "JamRuby::RecordingLiker", :foreign_key => "recording_id"
has_many :plays, :class_name => "JamRuby::RecordingPlay", :foreign_key => "recording_id"
belongs_to :owner, :class_name => "JamRuby::User", :inverse_of => :owned_recordings
belongs_to :band, :class_name => "JamRuby::Band", :inverse_of => :recordings
belongs_to :music_session, :class_name => "JamRuby::MusicSession", :inverse_of => :recordings
has_many :mixes, :class_name => "JamRuby::Mix", :inverse_of => :recording
has_many :recorded_tracks, :class_name => "JamRuby::RecordedTrack", :foreign_key => :recording_id
validates :music_session, :presence => true
validate :not_already_recording, :on => :create
validate :not_still_finalizing_previous, :on => :create
validate :not_playback_recording, :on => :create
validate :already_stopped_recording
def comment_count
self.comments.size
end
def like_count
self.likes.size
end
def play_count
self.plays.size
end
def not_already_recording
if music_session.is_recording?
errors.add(:music_session, ValidationMessages::ALREADY_BEING_RECORDED)

View File

@ -0,0 +1,12 @@
module JamRuby
class RecordingComment < ActiveRecord::Base
self.table_name = "recordings_comments"
self.primary_key = 'id'
belongs_to :recording, :class_name => "JamRuby::Recording", :foreign_key => "recording_id"
belongs_to :user, :class_name => "JamRuby::User", :foreign_key => "creator_id"
end
end

View File

@ -0,0 +1,12 @@
module JamRuby
class RecordingLiker < ActiveRecord::Base
self.table_name = "recordings_likers"
self.primary_key = 'id'
belongs_to :recording, :class_name => "JamRuby::Recording", :foreign_key => "recording_id"
belongs_to :user, :class_name => "JamRuby::User", :foreign_key => "liker_id"
end
end

View File

@ -0,0 +1,12 @@
module JamRuby
class RecordingPlay < ActiveRecord::Base
self.table_name = "recordings_plays"
self.primary_key = 'id'
belongs_to :recording, :class_name => "JamRuby::Recording", :foreign_key => "recording_id"
belongs_to :user, :class_name => "JamRuby::User", :foreign_key => "player_id"
end
end

View File

@ -234,6 +234,52 @@ class ApiMusicSessionsController < ApiController
end
end
def add_comment
if params[:id].blank?
render :json => { :message => "Session ID is required" }, :status => 400
end
if params[:user_id].blank?
render :json => { :message => "User ID is required" }, :status => 400
end
if params[:comment].blank?
render :json => { :message => "Comment is required" }, :status => 400
end
comment = MusicSessionComment.new
comment.music_session_id = params[:id]
comment.creator_id = params[:user_id]
comment.comment = params[:comment]
comment.save
if comment.errors.any?
render :json => { :message => "Unexpected error occurred" }, :status => 500
else
render :json => {}, :status => 201
end
end
def add_like
if params[:id].blank?
render :json => { :message => "Session ID is required" }, :status => 400
end
if params[:user_id].blank?
render :json => { :message => "User ID is required" }, :status => 400
end
liker = MusicSessionLiker.new
liker.music_session_id = params[:id]
liker.liker_id = params[:user_id]
liker.save
if liker.errors.any?
render :json => { :message => "Unexpected error occurred" }, :status => 500
else
render :json => {}, :status => 201
end
end
def claimed_recording_start
@music_session.claimed_recording_start(current_user, ClaimedRecording.find(params[:claimed_recording_id]))

View File

@ -80,6 +80,74 @@ class ApiRecordingsController < ApiController
end
end
def add_comment
if params[:id].blank?
render :json => { :message => "Recording ID is required" }, :status => 400
end
if params[:user_id].blank?
render :json => { :message => "User ID is required" }, :status => 400
end
if params[:comment].blank?
render :json => { :message => "Comment is required" }, :status => 400
end
comment = RecordingComment.new
comment.recording_id = params[:id]
comment.creator_id = params[:user_id]
comment.comment = params[:comment]
comment.save
if comment.errors.any?
render :json => { :message => "Unexpected error occurred" }, :status => 500
else
render :json => {}, :status => 201
end
end
def add_like
if params[:id].blank?
render :json => { :message => "Recording ID is required" }, :status => 400
end
if params[:user_id].blank?
render :json => { :message => "User ID is required" }, :status => 400
end
liker = RecordingLiker.new
liker.recording_id = params[:id]
liker.liker_id = params[:user_id]
liker.save
if liker.errors.any?
render :json => { :message => "Unexpected error occurred" }, :status => 500
else
render :json => {} :status => 201
end
end
def add_play
if params[:id].blank?
render :json => { :message => "Recording ID is required" }, :status => 400
end
if params[:user_id].blank?
render :json => { :message => "User ID is required" }, :status => 400
end
play = RecordingPlay.new
play.recording_id = params[:id]
play.player_id = params[:user_id]
play.save
if play.errors.any?
render :json => { :message => "Unexpected error occurred" }, :status => 500
else
render :json => {}, :status => 201
end
end
# discard will tell the server the user has no interest in the recording
def discard
@recording.discard(current_user)

View File

@ -1,6 +1,6 @@
object @recording
attributes :id, :band, :created_at, :duration
attributes :id, :band, :created_at, :duration, :comment_count, :like_count, :play_count
child(:recorded_tracks => :recorded_tracks) {
attributes :id, :fully_uploaded, :url, :client_track_id, :client_id, :instrument_id

View File

@ -89,7 +89,9 @@ SampleApp::Application.routes.draw do
match '/sessions' => 'api_music_sessions#index', :via => :get
match '/sessions' => 'api_music_sessions#create', :via => :post
match '/sessions/:id/perf' => 'api_music_sessions#perf_upload', :via => :put
match '/sessions/:id/comments' => 'api_music_sessions#add_comment', :via => :post
match '/sessions/:id/likes' => 'api_music_sessions#add_like', :via => :post
# music session tracks
match '/sessions/:id/tracks' => 'api_music_sessions#track_create', :via => :post
match '/sessions/:id/tracks' => 'api_music_sessions#track_sync', :via => :put
@ -278,6 +280,9 @@ SampleApp::Application.routes.draw do
match '/recordings/:id' => 'api_recordings#show', :via => :get, :as => 'api_recordings_detail'
match '/recordings/:id/stop' => 'api_recordings#stop', :via => :post, :as => 'api_recordings_stop'
match '/recordings/:id/claim' => 'api_recordings#claim', :via => :post, :as => 'api_recordings_claim'
match '/recordings/:id/comments' => 'api_recordings#add_comment', :via => :post, :as => 'api_recordings_add_comment'
match '/recordings/:id/likes' => 'api_recordings#add_like', :via => :post, :as => 'api_recordings_add_like'
match '/recordings/:id/plays' => 'api_recordings#add_play', :via => :post, :as => 'api_recordings_add_play'
match '/recordings/:id/discard' => 'api_recordings#discard', :via => :post, :as => 'api_recordings_discard'
match '/recordings/:id/tracks/:track_id/download' => 'api_recordings#download', :via => :get, :as => 'api_recordings_download'
match '/recordings/:id/tracks/:track_id/upload_next_part' => 'api_recordings#upload_next_part', :via => :get