diff --git a/db/up/like_follower_poly_assoc.sql b/db/up/like_follower_poly_assoc.sql index bfae7b284..ade837a30 100644 --- a/db/up/like_follower_poly_assoc.sql +++ b/db/up/like_follower_poly_assoc.sql @@ -2,8 +2,6 @@ drop table users_followers; drop table users_likers; drop table bands_followers; drop table bands_likers; -drop table music_sessions_likers; -drop table recordings_likers; alter table music_sessions_history add constraint music_sessions_history_pkey PRIMARY KEY (id); diff --git a/db/up/music_session_constraints.sql b/db/up/music_session_constraints.sql index f070bc571..c3cc58a19 100644 --- a/db/up/music_session_constraints.sql +++ b/db/up/music_session_constraints.sql @@ -1,6 +1,6 @@ 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 +references music_sessions_history(id) match simple ON UPDATE NO ACTION ON DELETE CASCADE; alter table music_sessions_likers drop constraint music_sessions_likers_music_session_id_fkey; diff --git a/ruby/lib/jam_ruby.rb b/ruby/lib/jam_ruby.rb index d051ee683..db1b6a3d7 100755 --- a/ruby/lib/jam_ruby.rb +++ b/ruby/lib/jam_ruby.rb @@ -73,6 +73,7 @@ require "jam_ruby/models/friendship" require "jam_ruby/models/music_session" require "jam_ruby/models/music_session_comment" require "jam_ruby/models/music_session_history" +require "jam_ruby/models/music_session_liker" require "jam_ruby/models/music_session_user_history" require "jam_ruby/models/music_session_perf_data" require "jam_ruby/models/invitation" @@ -87,6 +88,7 @@ require "jam_ruby/models/track" 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" diff --git a/ruby/lib/jam_ruby/models/follow.rb b/ruby/lib/jam_ruby/models/follow.rb index 768b7589d..0a8437597 100644 --- a/ruby/lib/jam_ruby/models/follow.rb +++ b/ruby/lib/jam_ruby/models/follow.rb @@ -2,7 +2,12 @@ module JamRuby class Follow < ActiveRecord::Base belongs_to :user, :class_name => "JamRuby::User", :foreign_key => "user_id" - belongs_to :likable, :polymorphic => true + belongs_to :followable, :polymorphic => true + + def type + type = self.followable_type.gsub("JamRuby::", "").downcase + type + end end end \ No newline at end of file diff --git a/ruby/lib/jam_ruby/models/music_session_history.rb b/ruby/lib/jam_ruby/models/music_session_history.rb index 1fae0c97b..9c40b9add 100644 --- a/ruby/lib/jam_ruby/models/music_session_history.rb +++ b/ruby/lib/jam_ruby/models/music_session_history.rb @@ -21,7 +21,7 @@ module JamRuby has_many :music_session_user_histories, :class_name => "JamRuby::MusicSessionUserHistory", :foreign_key => "music_session_id" has_many :comments, :class_name => "JamRuby::MusicSessionComment", :foreign_key => "music_session_id" - has_many :likes, :as => :likable, :class_name => "JamRuby::Like", :dependent => :destroy + has_many :likes, :class_name => "JamRuby::MusicSessionLiker", :foreign_key => "session_id" has_one :share_token, :class_name => "JamRuby::ShareToken", :inverse_of => :shareable, :foreign_key => 'shareable_id' before_create :generate_share_token diff --git a/ruby/lib/jam_ruby/models/music_session_liker.rb b/ruby/lib/jam_ruby/models/music_session_liker.rb new file mode 100644 index 000000000..538822c6e --- /dev/null +++ b/ruby/lib/jam_ruby/models/music_session_liker.rb @@ -0,0 +1,17 @@ +module JamRuby + class MusicSessionLiker < ActiveRecord::Base + + self.table_name = "music_sessions_likers" + + self.primary_key = 'id' + + belongs_to(:music_session_history, + :class_name => "JamRuby::MusicSessionHistory", + :foreign_key => "music_session_id") + + belongs_to(:user, + :class_name => "JamRuby::User", + :foreign_key => "liker_id") + + end +end \ No newline at end of file diff --git a/ruby/lib/jam_ruby/models/notification.rb b/ruby/lib/jam_ruby/models/notification.rb index 5ed469eca..48c363a1b 100644 --- a/ruby/lib/jam_ruby/models/notification.rb +++ b/ruby/lib/jam_ruby/models/notification.rb @@ -64,7 +64,7 @@ module JamRuby def retrieve_user_followers(connection, user_id) follower_ids = [] - connection.exec("SELECT uf.follower_id as friend_id FROM users_followers uf WHERE uf.user_id = $1", [user_id]) do |follower_results| + connection.exec("SELECT u.user_id as follower_id FROM follows f WHERE f.followable_id = $1", [user_id]) do |follower_results| follower_results.each do |follower_result| follower_ids.push(follower_result['follower_id']) end @@ -460,11 +460,11 @@ module JamRuby if music_session.musician_access || music_session.fan_access friends = Friendship.where(:friend_id => user.id) - user_followers = UserFollower.where(:user_id => user.id) + user_followers = user.followers # construct an array of User objects representing friends and followers friend_users = friends.map { |fu| fu.user } - follower_users = user_followers.map { |uf| uf.follower } + follower_users = user_followers.map { |uf| uf.user } friends_and_followers = friend_users.concat(follower_users).uniq # remove anyone in the session @@ -509,22 +509,21 @@ module JamRuby # if the session is private, don't send any notifications if music_session.musician_access || music_session.fan_access - band_followers = BandFollower.where(:band_id => band.id) - notifications, online_followers, offline_followers = [], [], [] notification_msg = format_msg(NotificationTypes::BAND_SESSION_JOIN, nil, band) - band_followers.each do |bf| - if (bf.follower.musician && music_session.musician_access) || (!bf.follower.musician && music_session.fan_access) + band.followers.each do |bf| + follower = bf.user + if (follower.musician && music_session.musician_access) || (!follower.musician && music_session.fan_access) notification = Notification.new notification.band_id = band.id notification.description = NotificationTypes::BAND_SESSION_JOIN - notification.target_user_id = bf.follower.id + notification.target_user_id = follower.id notification.save - if bf.follower.online + if follower.online msg = @@message_factory.band_session_join( - bf.follower.id, + follower.id, music_session.id, band.photo_url, notification_msg, @@ -532,9 +531,9 @@ module JamRuby notification.created_at.to_s ) - @@mq_router.publish_to_user(bf.follower.id, msg) + @@mq_router.publish_to_user(follower.id, msg) else - offline_followers << bf.follower + offline_followers << follower end end end @@ -551,11 +550,11 @@ module JamRuby user = recording.owner friends = Friendship.where(:friend_id => user.id) - user_followers = UserFollower.where(:user_id => user.id) + user_followers = user.followers # construct an array of User objects representing friends and followers friend_users = friends.map { |fu| fu.friend } - follower_users = user_followers.map { |uf| uf.follower } + follower_users = user_followers.map { |uf| uf.user } friends_and_followers = friend_users.concat(follower_users).uniq notifications, online_ff, offline_ff = [], [], [] @@ -592,20 +591,20 @@ module JamRuby def send_band_recording_saved(recording) - band_followers = BandFollower.where(:band_id => band.id) notification_msg = format_msg(NotificationTypes::BAND_RECORDING_SAVED, nil, recording.band) - band_followers.each do |bf| + band.followers.each do |bf| + follower = bf.user notification = Notification.new notification.description = NotificationTypes::BAND_RECORDING_SAVED notification.band_id = band.id - notification.target_user_id = bf.follower.id + notification.target_user_id = follower.id notification.recording_id = recording.id notification.save - if bf.follower.online + if follower.online msg = @@message_factory.band_recording_saved( - bf.follower.id, + follower.id, recording.id, band.photo_url, notification_msg, @@ -615,7 +614,7 @@ module JamRuby @@mq_router.publish_to_user(of.id, notification_msg) else - offline_followers << bf.follower + offline_followers << follower end end diff --git a/ruby/lib/jam_ruby/models/recording.rb b/ruby/lib/jam_ruby/models/recording.rb index 37a4f17ef..631431928 100644 --- a/ruby/lib/jam_ruby/models/recording.rb +++ b/ruby/lib/jam_ruby/models/recording.rb @@ -10,8 +10,7 @@ module JamRuby has_many :mixes, :class_name => "JamRuby::Mix", :inverse_of => :recording, :foreign_key => 'recording_id', :dependent => :destroy has_many :recorded_tracks, :class_name => "JamRuby::RecordedTrack", :foreign_key => :recording_id, :dependent => :destroy has_many :comments, :class_name => "JamRuby::RecordingComment", :foreign_key => "recording_id" - # has_many :likes, :class_name => "JamRuby::RecordingLiker", :foreign_key => "recording_id" - has_many :likes, :as => :likable, :class_name => "JamRuby::Like", :dependent => :destroy + 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, :foreign_key => 'owner_id' diff --git a/ruby/lib/jam_ruby/models/recording_liker.rb b/ruby/lib/jam_ruby/models/recording_liker.rb new file mode 100644 index 000000000..041465d2b --- /dev/null +++ b/ruby/lib/jam_ruby/models/recording_liker.rb @@ -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 \ No newline at end of file diff --git a/ruby/lib/jam_ruby/models/search.rb b/ruby/lib/jam_ruby/models/search.rb index db86dfc82..324b1a9f0 100644 --- a/ruby/lib/jam_ruby/models/search.rb +++ b/ruby/lib/jam_ruby/models/search.rb @@ -124,7 +124,7 @@ module JamRuby .order("play_count DESC, users.created_at DESC") when :followed sel_str = "COUNT(follows) AS search_follow_count, #{sel_str}" - rel = rel.joins("LEFT JOIN users_followers AS follows ON follows.user_id = users.id") + rel = rel.joins("LEFT JOIN follows ON follows.followable_id = users.id") .group("users.id") .order("COUNT(follows) DESC, users.created_at DESC") when :playing @@ -170,7 +170,7 @@ module JamRuby @results.each do |uu| counters = { } counters[COUNT_FRIEND] = Friendship.where(:user_id => uu.id).count - counters[COUNT_FOLLOW] = UserFollowing.where(:user_id => uu.id).count + counters[COUNT_FOLLOW] = Follow.where(:followable_id => uu.id).count counters[COUNT_RECORD] = ClaimedRecording.where(:user_id => uu.id).count counters[COUNT_SESSION] = MusicSession.where(:user_id => uu.id).count @user_counters[uu.id] << counters @@ -179,8 +179,8 @@ module JamRuby # this section determines follow/like/friend status for each search result # so that action links can be activated or not rel = User.select("users.id AS uid") - rel = rel.joins("LEFT JOIN users_followers AS follows ON follows.follower_id = '#{user.id}'") - rel = rel.where(["users.id IN (#{mids}) AND follows.user_id = users.id"]) + rel = rel.joins("LEFT JOIN follows ON follows.user_id = '#{user.id}'") + rel = rel.where(["users.id IN (#{mids}) AND follows.followable_id = users.id"]) rel.all.each { |val| @user_counters[val.uid] << RESULT_FOLLOW } rel = User.select("users.id AS uid") @@ -288,7 +288,7 @@ module JamRuby .order("play_count DESC, bands.created_at DESC") when :followed sel_str = "COUNT(follows) AS search_follow_count, #{sel_str}" - rel = rel.joins("LEFT JOIN bands_followers AS follows ON follows.band_id = bands.id") + rel = rel.joins("LEFT JOIN follows ON follows.followable_id = bands.id") .group("bands.id") .order("COUNT(follows) DESC, bands.created_at DESC") when :playing @@ -317,7 +317,7 @@ module JamRuby # this gets counts for each search result @results.each do |bb| counters = { } - counters[COUNT_FOLLOW] = BandFollowing.where(:band_id => bb.id).count + counters[COUNT_FOLLOW] = Follow.where(:followable_id => bb.id).count counters[COUNT_RECORD] = Recording.where(:band_id => bb.id).count counters[COUNT_SESSION] = MusicSession.where(:band_id => bb.id).count @user_counters[bb.id] << counters @@ -327,8 +327,8 @@ module JamRuby # so that action links can be activated or not rel = Band.select("bands.id AS bid") - rel = rel.joins("LEFT JOIN bands_followers AS follows ON follows.follower_id = '#{user.id}'") - rel = rel.where(["bands.id IN (#{mids}) AND follows.band_id = bands.id"]) + rel = rel.joins("LEFT JOIN follows ON follows.user_id = '#{user.id}'") + rel = rel.where(["bands.id IN (#{mids}) AND follows.followable_id = bands.id"]) rel.all.each { |val| @user_counters[val.bid] << RESULT_FOLLOW } else diff --git a/ruby/lib/jam_ruby/models/user.rb b/ruby/lib/jam_ruby/models/user.rb index 83bb2e8fb..24d273571 100644 --- a/ruby/lib/jam_ruby/models/user.rb +++ b/ruby/lib/jam_ruby/models/user.rb @@ -238,7 +238,7 @@ module JamRuby # check if "this user" likes entity def likes?(entity) - self.likings.where(:user_id => self.id).where(:likable_id => entity.id).size > 0 + self.likings.where(:likable_id => entity.id).size > 0 end def liking_count @@ -251,7 +251,7 @@ module JamRuby # check if "this user" follows entity def following?(entity) - self.followings.where(:user_id => self.id).where(:likable_id => entity.id).size > 0 + self.followings.where(:followable_id => entity.id).size > 0 end def following_count @@ -547,8 +547,7 @@ module JamRuby end def create_user_following(targetUserId) - targetUser = User.new - targetUser.id = targetUserId + targetUser = User.find(targetUserId) follow = Follow.new follow.followable = targetUser @@ -556,14 +555,12 @@ module JamRuby follow.save # TODO: make this async - user = User.find(targetUserId) - Notification.send_new_user_follower(self, user) + Notification.send_new_user_follower(self, targetUser) end def create_band_following(targetBandId) - targetBand= Band.new - targetBand.id = targetBandId + targetBand= Band.find(targetBandId) follow = Follow.new follow.followable = targetBand @@ -571,17 +568,15 @@ module JamRuby follow.save # TODO: make this async - band = Band.find(targetBandId) - Notification.send_new_band_follower(self, band) + Notification.send_new_band_follower(self, targetBand) end - def self.delete_following(likerId, targetEntityId) - Follow.delete_all "(user_id = '#{liker_id}' AND followable_id = '#{targetEntityId}')" + def self.delete_following(followerId, targetEntityId) + Follow.delete_all "(user_id = '#{followerId}' AND followable_id = '#{targetEntityId}')" end def create_user_like(targetUserId) - targetUser = User.new - targetUser.id = targetUserId + targetUser = User.find(targetUserId) like = Like.new like.likable = targetUser @@ -590,8 +585,7 @@ module JamRuby end def create_band_like(targetBandId) - targetBand = Band.new - targetBand.id = targetBandId + targetBand = Band.find(targetBandId) like = Like.new like.likable = targetBand @@ -599,6 +593,24 @@ module JamRuby like.save end + def create_session_like(targetSessionId) + targetSession = MusicSessionHistory.find(targetSessionId) + + like = Like.new + like.likable = targetSession + like.user = self + like.save + end + + def create_recording_like(targetRecordingId) + targetRecording = Recording.find(targetRecordingId) + + like = Like.new + like.likable = targetRecording + like.user = self + like.save + end + def self.delete_like(likerId, targetEntityId) Like.delete_all "(user_id = '#{liker_id}' AND likable_id = '#{targetEntityId}')" end @@ -1075,8 +1087,8 @@ module JamRuby end def top_followings - @topf ||= User.joins("INNER JOIN users_followers AS follows ON follows.user_id = users.id") - .where(['follows.follower_id = ?',self.id]) + @topf ||= User.joins("INNER JOIN follows ON follows.followable_id = users.id") + .where(['follows.user_id = ?',self.id]) .order('follows.created_at DESC') .limit(3) end diff --git a/ruby/spec/jam_ruby/models/band_filter_search_spec.rb b/ruby/spec/jam_ruby/models/band_filter_search_spec.rb index 1f5ec1eba..d1fca2b11 100644 --- a/ruby/spec/jam_ruby/models/band_filter_search_spec.rb +++ b/ruby/spec/jam_ruby/models/band_filter_search_spec.rb @@ -31,7 +31,7 @@ describe 'Band search' do it "finds bands with proper ordering" do # the ordering should be create_at since no followers exist - expect(BandFollower.count).to eq(0) + expect(Follow.count).to eq(0) results = Search.band_filter({ :per_page => Band.count }) results.results.each_with_index do |uu, idx| expect(uu.id).to eq(@bands.reverse[idx].id) @@ -42,17 +42,47 @@ describe 'Band search' do users = [] 4.downto(1) { |nn| users << FactoryGirl.create(:user) } + users.each_with_index do |u, index| + if index != 0 + f1 = Follow.new + f1.user = u + f1.followable = @band4 + f1.save + end + end + + users.each_with_index do |u, index| + if index != 0 + f1 = Follow.new + f1.user = u + f1.followable = @band3 + f1.save + end + end + + f1 = Follow.new + f1.user = users.first + f1.followable = @band2 + f1.save + # establish sorting order - @band4.followers.concat(users[1..-1]) - @band3.followers.concat(users[1..3]) - @band2.followers.concat(users[0]) + # @band4.followers.concat(users[1..-1]) + # @band3.followers.concat(users[1..3]) + # @band2.followers.concat(users[0]) @bands.map(&:reload) expect(@band4.followers.count).to be 3 - expect(BandFollower.count).to be 7 + expect(Follow.count).to be 7 # refresh the order to ensure it works right - @band2.followers.concat(users[1..-1]) + users.each_with_index do |u, index| + f1 = Follow.new + f1.user = u + f1.followable = @band2 + f1.save + end + + # @band2.followers.concat(users[1..-1]) results = Search.band_filter({ :per_page => @bands.size }, users[0]) expect(results.results[0].id).to eq(@band2.id) @@ -85,8 +115,15 @@ describe 'Band search' do users = [] 2.downto(1) { |nn| users << FactoryGirl.create(:user) } + users.each do |u| + f1 = Follow.new + f1.user = u + f1.followable = @band1 + f1.save + end + # establish sorting order - @band1.followers.concat(users) + # @band1.followers.concat(users) results = Search.band_filter({},@band1) uu = results.results.detect { |mm| mm.id == @band1.id } expect(uu).to_not be_nil diff --git a/ruby/spec/jam_ruby/models/musician_search_spec.rb b/ruby/spec/jam_ruby/models/musician_search_spec.rb index b9043be98..5f0b0dc62 100644 --- a/ruby/spec/jam_ruby/models/musician_search_spec.rb +++ b/ruby/spec/jam_ruby/models/musician_search_spec.rb @@ -24,7 +24,7 @@ describe 'Musician search' do it "finds musicians with proper ordering" do # the ordering should be create_at since no followers exist - expect(UserFollower.count).to eq(0) + expect(Follow.count).to eq(0) results = Search.musician_filter({ :per_page => User.musicians.count }) results.results.each_with_index do |uu, idx| expect(uu.id).to eq(@users.reverse[idx].id) @@ -33,14 +33,63 @@ describe 'Musician search' do it "sorts musicians by followers" do # establish sorting order - @user4.followers.concat([@user2, @user3, @user4]) - @user3.followers.concat([@user3, @user4]) - @user2.followers.concat([@user1]) + + # @user4 + f1 = Follow.new + f1.user = @user2 + f1.followable = @user4 + f1.save + + f2 = Follow.new + f2.user = @user3 + f2.followable = @user4 + f2.save + + f3 = Follow.new + f3.user = @user4 + f3.followable = @user4 + f3.save + + # @user3 + f4 = Follow.new + f4.user = @user3 + f4.followable = @user3 + f4.save + + f5 = Follow.new + f5.user = @user4 + f5.followable = @user3 + f5.save + + # @user2 + f6 = Follow.new + f6.user = @user1 + f6.followable = @user2 + f6.save + + # @user4.followers.concat([@user2, @user3, @user4]) + # @user3.followers.concat([@user3, @user4]) + # @user2.followers.concat([@user1]) expect(@user4.followers.count).to be 3 - expect(UserFollower.count).to be 6 + expect(Follow.count).to be 6 # refresh the order to ensure it works right - @user2.followers.concat([@user3, @user4, @user2]) + f1 = Follow.new + f1.user = @user3 + f1.followable = @user2 + f1.save + + f2 = Follow.new + f2.user = @user4 + f2.followable = @user2 + f2.save + + f3 = Follow.new + f3.user = @user2 + f3.followable = @user2 + f3.save + + # @user2.followers.concat([@user3, @user4, @user2]) results = Search.musician_filter({ :per_page => @users.size }, @user3) expect(results.results[0].id).to eq(@user2.id) @@ -85,9 +134,24 @@ describe 'Musician search' do context 'musician stat counters' do it "displays musicians top followings" do - @user4.followers.concat([@user4]) - @user3.followers.concat([@user4]) - @user2.followers.concat([@user4]) + f1 = Follow.new + f1.user = @user4 + f1.followable = @user4 + f1.save + + f2 = Follow.new + f2.user = @user4 + f2.followable = @user3 + f2.save + + f3 = Follow.new + f3.user = @user4 + f3.followable = @user2 + f3.save + + # @user4.followers.concat([@user4]) + # @user3.followers.concat([@user4]) + # @user2.followers.concat([@user4]) expect(@user4.top_followings.count).to be 3 expect(@user4.top_followings.map(&:id)).to match_array((@users - [@user1]).map(&:id)) end diff --git a/web/app/assets/javascripts/bandProfile.js b/web/app/assets/javascripts/bandProfile.js index 723220128..6b2285604 100644 --- a/web/app/assets/javascripts/bandProfile.js +++ b/web/app/assets/javascripts/bandProfile.js @@ -13,7 +13,6 @@ function beforeShow(data) { bandId = data.id; - setIsMember(); } function afterShow(data) { @@ -49,12 +48,14 @@ rest.addFollowing(newFollowing) .done(function() { if (isBand) { + var newCount = parseInt($("#band-profile-follower-stats").text()) + 1; + var text = newCount > 1 || newCount == 0 ? " Followers" : " Follower"; + $('#band-profile-follower-stats').html(newCount + text); configureBandFollowingButton(true); } else { configureMemberFollowingButton(true, id); } - configureFollowingButton(); }) .fail(app.ajaxError); } @@ -67,6 +68,9 @@ .done(function() { renderActive(); // refresh stats if (isBand) { + var newCount = parseInt($("#band-profile-follower-stats").text()) - 1; + var text = newCount > 1 || newCount == 0 ? " Followers" : " Follower"; + $('#band-profile-follower-stats').html(newCount + text); configureBandFollowingButton(false); } else { @@ -100,30 +104,6 @@ return alreadyFollowing; } - function isFollowing() { - var alreadyFollowing = false; - - var url = "/api/users/" + context.JK.currentUserId + "/band_followings/" + bandId; - $.ajax({ - type: "GET", - dataType: "json", - url: url, - async: false, - processData: false, - success: function(response) { - if (response.id !== undefined) { - alreadyFollowing = true; - } - else { - alreadyFollowing = false; - } - }, - error: app.ajaxError - }); - - return alreadyFollowing; - } - function configureBandFollowingButton(following) { $('#btn-follow-band').unbind("click"); @@ -201,45 +181,51 @@ } function bindAbout() { - var url = "/api/bands/" + bandId; - $.ajax({ - type: "GET", - dataType: "json", - url: url, - async: false, - processData:false, - success: function(response) { - band = response; - }, - error: app.ajaxError - }); - if (band) { + rest.getBand(bandId) + .done(function(response) { + band = response; + setIsMember(); + if (band) { + // name + $('#band-profile-name').html(band.name); - // name - $('#band-profile-name').html(band.name); + // avatar + $('#band-profile-avatar').attr('src', context.JK.resolveAvatarUrl(band.photo_url)); - // avatar - $('#band-profile-avatar').attr('src', context.JK.resolveAvatarUrl(band.photo_url)); + // location + $('#band-profile-location').html(band.location); - // location - $('#band-profile-location').html(band.location); + // stats + var text = band.follower_count > 1 || band.follower_count == 0 ? " Followers" : " Follower"; + $('#band-profile-follower-stats').html(band.follower_count + text); - // stats - var text = band.follower_count > 1 || band.follower_count == 0 ? " Followers" : " Follower"; - $('#band-profile-follower-stats').html(band.follower_count + text); + text = band.session_count > 1 || band.session_count == 0 ? " Sessions" : " Session"; + $('#band-profile-session-stats').html(band.session_count + text); - text = band.session_count > 1 || band.session_count == 0 ? " Sessions" : " Session"; - $('#band-profile-session-stats').html(band.session_count + text); + text = band.recording_count > 1 || band.recording_count == 0 ? " Recordings" : " Recording"; + $('#band-profile-recording-stats').html(band.recording_count + text); - text = band.recording_count > 1 || band.recording_count == 0 ? " Recordings" : " Recording"; - $('#band-profile-recording-stats').html(band.recording_count + text); - - $('#band-profile-biography').html(band.biography); - } - else { - logger.debug("No band found with bandId = " + bandId); - } + $('#band-profile-biography').html(band.biography); + + // wire up Follow click + configureBandFollowingButton(band.is_following); + } + else { + logger.debug("No band found with bandId = " + bandId); + } + }) + .fail(function(xhr) { + if(xhr.status >= 500) { + context.JK.fetchUserNetworkOrServerFailure(); + } + else if(xhr.status == 404) { + context.JK.entityNotFound("Band"); + } + else { + context.JK.app.ajaxError(arguments); + } + }); } /****************** SOCIAL TAB *****************/ @@ -270,6 +256,8 @@ $.each(response, function(index, val) { var template = $('#template-band-profile-social').html(); var followerHtml = context.JK.fillTemplate(template, { + userId: val.user_id, + hoverAction: val.musician ? "musician" : "fan", avatar_url: context.JK.resolveAvatarUrl(val.photo_url), userName: val.name, location: val.location @@ -280,6 +268,7 @@ }, error: app.ajaxError }); + context.JK.bindHoverEvents(); } /****************** HISTORY TAB *****************/ @@ -445,10 +434,6 @@ $('#band-profile-members-link').click(renderMembers); $('#band-profile-social-link').click(renderSocial); - // wire up Follow click - var following = isFollowing(); - configureBandFollowingButton(following); - if (isMember) { $("#btn-follow-band").hide(); $("#btn-edit-band-profile").show(); diff --git a/web/app/assets/javascripts/findMusician.js b/web/app/assets/javascripts/findMusician.js index 10afec2f3..5ddc3bae5 100644 --- a/web/app/assets/javascripts/findMusician.js +++ b/web/app/assets/javascripts/findMusician.js @@ -5,6 +5,7 @@ context.JK.FindMusicianScreen = function(app) { var logger = context.JK.logger; + var rest = context.JK.Rest(); var musicians = {}; var musicianList; var instrument_logo_map = context.JK.getInstrumentIconMap24(); @@ -176,7 +177,7 @@ evt.stopPropagation(); var uid = $(this).parent().data('musician-id'); - context.JK.sendFriendRequest(app, uid, friendRequestCallback); + rest.sendFriendRequest(app, uid, friendRequestCallback); } function friendRequestCallback(user_id) { diff --git a/web/app/assets/javascripts/hoverMusician.js b/web/app/assets/javascripts/hoverMusician.js index 2789981b0..63280f2ff 100644 --- a/web/app/assets/javascripts/hoverMusician.js +++ b/web/app/assets/javascripts/hoverMusician.js @@ -31,8 +31,19 @@ followingHtml += ''; } - followingHtml += ''; - followingHtml += '' + val.name + ''; + var avatarUrl, profilePath; + + if (val.type === "band") { + avatarUrl = context.JK.resolveBandAvatarUrl(val.photo_url); + profilePath = "bandProfile" + } + else { + avatarUrl = context.JK.resolveAvatarUrl(val.photo_url); + profilePath = "profile"; + } + + followingHtml += ''; + followingHtml += '' + val.name + ''; if (index % 2 > 0) { followingHtml += ''; @@ -53,6 +64,7 @@ } var musicianHtml = context.JK.fillTemplate(template, { + userId: response.id, avatar_url: context.JK.resolveAvatarUrl(response.photo_url), name: response.name, location: response.location, diff --git a/web/app/assets/javascripts/jam_rest.js b/web/app/assets/javascripts/jam_rest.js index a3317432b..762837179 100644 --- a/web/app/assets/javascripts/jam_rest.js +++ b/web/app/assets/javascripts/jam_rest.js @@ -414,6 +414,26 @@ }); } + /** NOTE: This is only for Musician, Fan, and Band Likes. Recording and + Session Likes have their own section below since unauthenticated users + are allowed to Like these entities. + */ + function addLike(options) { + var id = getId(options); + return $.ajax({ + type: "POST", + dataType: "json", + contentType: 'application/json', + url: "/api/users/" + id + "/likes", + data: JSON.stringify(options), + processData: false + }); + } + + function removeLike(options) { + + } + function addFollowing(options) { var id = getId(options); @@ -462,29 +482,6 @@ }); } - function getBandFollowings(options) { - var userId = getId(options); - - // FOLLOWINGS (BANDS) - return $.ajax({ - type: "GET", - dataType: "json", - url: "/api/users/" + userId + "/band_followings", - processData:false - }); - } - - function getBandFollowing(options) { - var id = getId(options); - var bandId = options["band_id"]; - return $.ajax({ - type: "GET", - dataType: "json", - url: "/api/users/" + id + "/band_followings/" + bandId, - processData: false - }); - } - function getBands(options) { var userId = getId(options); @@ -495,13 +492,6 @@ processData:false }); } - function getMusicianFollowers(userId) { - - } - - function getBandFollowers(bandId) { - - } function getClientDownloads(options) { @@ -562,6 +552,25 @@ }); } + function sendFriendRequest(app, userId, callback) { + var url = "/api/users/" + context.JK.currentUserId + "/friend_requests"; + $.ajax({ + type: "POST", + dataType: "json", + contentType: 'application/json', + url: url, + data: '{"friend_id":"' + userId + '"}', + processData: false, + success: function(response) { + if (callback) { + callback(userId); + } + context.JK.GA.trackFriendConnect(context.JK.GA.FriendConnectTypes.request); + }, + error: app.ajaxError + }); + } + function acceptFriendRequest(options) { var id = getId(options); var friend_request_id = options["friend_request_id"]; @@ -833,12 +842,12 @@ this.getFilepickerPolicy = getFilepickerPolicy; this.getFriends = getFriends; this.removeFriend = removeFriend; + this.addLike = addLike; + this.removeLike = removeLike; this.addFollowing = addFollowing; this.removeFollowing = removeFollowing; this.getFollowings = getFollowings; this.getFollowers = getFollowers; - this.getBandFollowings = getBandFollowings; - this.getBandFollowing = getBandFollowing; this.getBands = getBands; this.updateSession = updateSession; this.getSessionHistory = getSessionHistory; @@ -852,6 +861,7 @@ this.createInvitation = createInvitation; this.postFeedback = postFeedback; this.serverHealthCheck = serverHealthCheck; + this.sendFriendRequest = sendFriendRequest; this.acceptFriendRequest = acceptFriendRequest; this.signout = signout; this.userDownloadedClient = userDownloadedClient; diff --git a/web/app/assets/javascripts/profile.js b/web/app/assets/javascripts/profile.js index ca858360a..04e6f9b2a 100644 --- a/web/app/assets/javascripts/profile.js +++ b/web/app/assets/javascripts/profile.js @@ -164,7 +164,7 @@ evt.stopPropagation(); setFriend(true); // TODO: you aren't a friend yet. just a request to be one really there are 3 states here. sentFriendRequest = true; - context.JK.sendFriendRequest(app, userId, friendRequestCallback); + rest.sendFriendRequest(app, userId, friendRequestCallback); } function removeFriend(evt) { @@ -408,21 +408,6 @@ }) .fail(app.ajaxError); - rest.getBandFollowings({id: userId}) - .done(function(response) { - $.each(response, function(index, val) { - var template = $('#template-profile-social').html(); - var followingHtml = context.JK.fillTemplate(template, { - avatar_url: context.JK.resolveBandAvatarUrl(val.logo_url), - userName: val.name, - location: val.location - }); - - $('#profile-social-followings').append(followingHtml); - }); - }) - .fail(app.ajaxError); - rest.getFollowers({id: userId}) .done(function(response) { $.each(response, function(index, val) { @@ -531,13 +516,7 @@ $('#profile-bands').append(bandHtml); // wire up Band Follow button click handler - // XXX; we should return if you are following the band in the rabl, so we don't - // have to hit the server per band shown - rest.getBandFollowing({band_id: val.id}) - .done(function(response) { - var alreadyFollowing = response.id !== undefined; - configureBandFollowingButton(alreadyFollowing, val.id); - }); + configureBandFollowingButton(val.is_following, val.id); }); if(response.length >= 3) { diff --git a/web/app/assets/javascripts/searchResults.js b/web/app/assets/javascripts/searchResults.js index bf58f118d..884c2dbda 100644 --- a/web/app/assets/javascripts/searchResults.js +++ b/web/app/assets/javascripts/searchResults.js @@ -5,6 +5,7 @@ context.JK = context.JK || {}; context.JK.SearchResultScreen = function(app) { var logger = context.JK.logger; + var rest = context.JK.Rest(); var instrument_logo_map = context.JK.getInstrumentIconMap24(); function initializeSearchNavLinks() { @@ -215,10 +216,10 @@ var userId = $(this).parent().attr('user-id'); if ($(this).closest('#sidebar-search-results')) { - context.JK.sendFriendRequest(app, userId, friendRequestCallbackSidebar); + rest.sendFriendRequest(app, userId, friendRequestCallbackSidebar); } else { - context.JK.sendFriendRequest(app, userId, friendRequestCallbackSearchResults); + rest.sendFriendRequest(app, userId, friendRequestCallbackSearchResults); } } diff --git a/web/app/assets/javascripts/utils.js b/web/app/assets/javascripts/utils.js index 36937668a..22eba1cd4 100644 --- a/web/app/assets/javascripts/utils.js +++ b/web/app/assets/javascripts/utils.js @@ -384,35 +384,18 @@ } context.JK.search = function(query, app, callback) { - logger.debug("search: "+query) - $.ajax({ - type: "GET", - dataType: "json", - contentType: 'application/json', - url: "/api/search?query=" + query, - processData: false, - success: function(response) { - callback(response); - }, - error: app.ajaxError - }); - }; - - context.JK.sendFriendRequest = function(app, userId, callback) { - var url = "/api/users/" + context.JK.currentUserId + "/friend_requests"; - $.ajax({ - type: "POST", - dataType: "json", - contentType: 'application/json', - url: url, - data: '{"friend_id":"' + userId + '"}', - processData: false, - success: function(response) { - callback(userId); - context.JK.GA.trackFriendConnect(context.JK.GA.FriendConnectTypes.request); - }, - error: app.ajaxError - }); + //logger.debug("search: "+ query) + $.ajax({ + type: "GET", + dataType: "json", + contentType: 'application/json', + url: "/api/search?query=" + query, + processData: false, + success: function(response) { + callback(response); + }, + error: app.ajaxError + }); }; /* diff --git a/web/app/controllers/api_users_controller.rb b/web/app/controllers/api_users_controller.rb index 8f9d05ee0..491110426 100644 --- a/web/app/controllers/api_users_controller.rb +++ b/web/app/controllers/api_users_controller.rb @@ -189,19 +189,15 @@ class ApiUsersController < ApiController @user = User.find(params[:id]) end - def band_like_index - @user = User.find(params[:id]) - end - def like_create if !params[:user_id].nil? @user.create_user_like(params[:user_id]) - respond_with @user, responder: ApiResponder, :location => api_user_like_index_url(@user) - + elsif !params[:band_id].nil? @user.create_band_like(params[:band_id]) - respond_with @user, responder: ApiResponder, :location => api_band_like_index_url(@user) end + + respond_with @user, responder: ApiResponder, :location => api_user_like_index_url(@user) end def like_destroy @@ -220,27 +216,15 @@ class ApiUsersController < ApiController @user = User.find(params[:id]) end - # def following_show - # @following = UserFollowing.find_by_user_id_and_follower_id(params[:user_id], params[:id]) - # end - - def band_following_index - @user = User.find(params[:id]) - end - - # def band_following_show - # @following = BandFollowing.find_by_band_id_and_follower_id(params[:band_id], params[:id]) - # end - def following_create if !params[:user_id].nil? @user.create_user_following(params[:user_id]) - respond_with @user, responder: ApiResponder, :location => api_user_following_index_url(@user) elsif !params[:band_id].nil? @user.create_band_following(params[:band_id]) - respond_with @user, responder: ApiResponder, :location => api_band_following_index_url(@user) end + + respond_with @user, responder: ApiResponder, :location => api_user_following_index_url(@user) end def following_destroy diff --git a/web/app/views/api_bands/follower_index.rabl b/web/app/views/api_bands/follower_index.rabl index 5cb70590c..fba0ad9a4 100644 --- a/web/app/views/api_bands/follower_index.rabl +++ b/web/app/views/api_bands/follower_index.rabl @@ -1,21 +1,21 @@ collection @band.followers node :user_id do |follower| - follower.id + follower.user.id end node :name do |follower| - follower.name + follower.user.name end node :location do |follower| - follower.location + follower.user.location end node :musician do |follower| - follower.musician + follower.user.musician end node :photo_url do |follower| - follower.photo_url + follower.user.photo_url end diff --git a/web/app/views/api_bands/show.rabl b/web/app/views/api_bands/show.rabl index ae749ecd3..8bd3ca76a 100644 --- a/web/app/views/api_bands/show.rabl +++ b/web/app/views/api_bands/show.rabl @@ -23,6 +23,6 @@ end if current_user node :is_following do |uu| - current_user.following_band?(@band) + current_user.following?(@band) end end \ No newline at end of file diff --git a/web/app/views/api_users/band_following_index.rabl b/web/app/views/api_users/band_following_index.rabl deleted file mode 100644 index 0e49c2b49..000000000 --- a/web/app/views/api_users/band_following_index.rabl +++ /dev/null @@ -1,21 +0,0 @@ -collection @user.band_followings - -node :band_id do |following| - following.id -end - -node :name do |following| - following.name -end - -node :location do |following| - following.location -end - -node :photo_url do |following| - following.photo_url -end - -node :logo_url do |following| - following.logo_url -end \ No newline at end of file diff --git a/web/app/views/api_users/band_following_show.rabl b/web/app/views/api_users/band_following_show.rabl deleted file mode 100644 index ad7786535..000000000 --- a/web/app/views/api_users/band_following_show.rabl +++ /dev/null @@ -1,3 +0,0 @@ -object @following - -attributes :id, :band_id, :follower_id \ No newline at end of file diff --git a/web/app/views/api_users/band_index.rabl b/web/app/views/api_users/band_index.rabl index 2439ee419..6641afa61 100644 --- a/web/app/views/api_users/band_index.rabl +++ b/web/app/views/api_users/band_index.rabl @@ -31,3 +31,9 @@ end node :biography do |band| band.biography.nil? ? "" : band.biography end + +node :is_following do |band| + if current_user + current_user.following?(band) + end +end \ No newline at end of file diff --git a/web/app/views/api_users/follower_index.rabl b/web/app/views/api_users/follower_index.rabl index 418c61503..d2e84632f 100644 --- a/web/app/views/api_users/follower_index.rabl +++ b/web/app/views/api_users/follower_index.rabl @@ -1,21 +1,21 @@ collection @user.followers node :user_id do |follower| - follower.id + follower.user.id end node :name do |follower| - follower.name + follower.user.name end node :location do |follower| - follower.location + follower.user.location end node :musician do |follower| - follower.musician + follower.user.musician end node :photo_url do |follower| - follower.photo_url + follower.user.photo_url end diff --git a/web/app/views/api_users/following_index.rabl b/web/app/views/api_users/following_index.rabl index 4c7941f2f..a3f46574d 100644 --- a/web/app/views/api_users/following_index.rabl +++ b/web/app/views/api_users/following_index.rabl @@ -1,21 +1,23 @@ collection @user.followings node :user_id do |following| - following.id + following.followable.id end node :name do |following| - following.name + following.followable.name end node :location do |following| - following.location + following.followable.location end node :musician do |following| - following.musician + if following.followable.instance_of?(JamRuby::User) + following.followable.musician + end end node :photo_url do |following| - following.photo_url + following.followable.photo_url end \ No newline at end of file diff --git a/web/app/views/api_users/like_create.rabl b/web/app/views/api_users/like_create.rabl index 426460cc7..53f966c13 100644 --- a/web/app/views/api_users/like_create.rabl +++ b/web/app/views/api_users/like_create.rabl @@ -1,3 +1,3 @@ -object @user.likes +object @user.likings extends "api_users/like_index" \ No newline at end of file diff --git a/web/app/views/api_users/like_index.rabl b/web/app/views/api_users/like_index.rabl index 1976ef29f..daa46f44d 100644 --- a/web/app/views/api_users/like_index.rabl +++ b/web/app/views/api_users/like_index.rabl @@ -1,31 +1,31 @@ -object @user.likes +object @user.likings attributes :user_id -node :first_name do |like| - like.user.first_name +node :first_name do |liking| + liking.user.first_name end -node :last_name do |like| - like.user.last_name +node :last_name do |liking| + liking.user.last_name end -node :city do |like| - like.user.city +node :city do |liking| + liking.user.city end -node :state do |like| - like.user.state +node :state do |liking| + liking.user.state end -node :country do |like| - like.user.country +node :country do |liking| + liking.user.country end -node :musician do |like| - like.user.musician +node :musician do |liking| + liking.user.musician end -node :photo_url do |like| - like.user.photo_url +node :photo_url do |liking| + liking.user.photo_url end \ No newline at end of file diff --git a/web/app/views/api_users/show.rabl b/web/app/views/api_users/show.rabl index b193769d9..9b5c372b6 100644 --- a/web/app/views/api_users/show.rabl +++ b/web/app/views/api_users/show.rabl @@ -1,6 +1,6 @@ object @user -attributes :id, :first_name, :last_name, :name, :city, :state, :country, :location, :online, :photo_url, :musician, :gender, :birth_date, :internet_service_provider, :friend_count, :liker_count, :like_count, :band_like_count, :follower_count, :following_count, :band_following_count, :recording_count, :session_count, :biography, :favorite_count +attributes :id, :first_name, :last_name, :name, :city, :state, :country, :location, :online, :photo_url, :musician, :gender, :birth_date, :internet_service_provider, :friend_count, :liker_count, :like_count, :follower_count, :following_count, :recording_count, :session_count, :biography, :favorite_count if @user.musician? node :location do @user.location end @@ -20,7 +20,7 @@ elsif current_user current_user.following?(@user) end node :is_liking do |uu| - current_user.likes + current_user.likes?(@user) end end @@ -29,7 +29,25 @@ child :friends => :friends do end child :followings => :followings do - attributes :id, :name, :location, :photo_url + + attributes :type + + node :id do |f| + f.followable.id + end + + node :name do |f| + f.followable.name + end + + node :location do |f| + f.followable.location + end + + node :photo_url do |f| + f.followable.photo_url + end + end child :band_musicians => :bands do diff --git a/web/app/views/clients/_bandProfile.html.erb b/web/app/views/clients/_bandProfile.html.erb index c137b14d2..f7a401826 100644 --- a/web/app/views/clients/_bandProfile.html.erb +++ b/web/app/views/clients/_bandProfile.html.erb @@ -119,10 +119,10 @@ \ No newline at end of file diff --git a/web/app/views/clients/_hoverBand.html.erb b/web/app/views/clients/_hoverBand.html.erb index 834983d31..ee8f0c5ff 100644 --- a/web/app/views/clients/_hoverBand.html.erb +++ b/web/app/views/clients/_hoverBand.html.erb @@ -2,6 +2,30 @@ + + + +