diff --git a/web/app/assets/javascripts/web/recordings.js b/web/app/assets/javascripts/web/recordings.js index 1793b2609..58700b1dd 100644 --- a/web/app/assets/javascripts/web/recordings.js +++ b/web/app/assets/javascripts/web/recordings.js @@ -51,10 +51,10 @@ function like() { rest.addRecordingLike(recordingId, claimedRecordingId, JK.currentUserId) - .done(function(response) { - $("#spnLikeCount").html(parseInt($("#spnLikeCount").text()) + 1); - $("#btnLike", $scope).unbind("click"); - }); + .done(function(response) { + $("#spnLikeCount").html(parseInt($("#spnLikeCount").text()) + 1); + $("#btnLike", $scope).unbind("click"); + }); } function play() { @@ -68,22 +68,35 @@ var comment = $("#txtRecordingComment").val(); if ($.trim(comment).length > 0) { rest.addRecordingComment(recordingId, JK.currentUserId, comment) - .done(function(response) { - $("#spnCommentCount", $scope).html(parseInt($("#spnCommentCount").text()) + 1); - - var template = $('#template-landing-comment').html(); - var commentHtml = context.JK.fillTemplate(template, { - avatar_url: context.JK.currentUserAvatarUrl, - name: context.JK.currentUserName, - comment: comment, - timeago: $.timeago(Date.now()) - }); - - $(".landing-comment-scroller").prepend(commentHtml); - }); + .done(function(response) { + $("#spnCommentCount", $scope).html(parseInt($("#spnCommentCount").text()) + 1); + renderComment(comment, context.JK.currentUserId, context.JK.currentUserName, + context.JK.currentUserAvatarUrl, $.timeago(Date.now()), context.JK.currentUserMusician, false); + }); } } + function renderComment(comment, userId, userName, userAvatarUrl, timeago, musician, append) { + var template = $('#template-landing-comment').html(); + var commentHtml = context.JK.fillTemplate(template, { + avatar_url: userAvatarUrl, + user_id: userId, + hoverAction: musician ? "musician" : "fan", + name: userName, + comment: comment, + timeago: timeago + }); + + if (append) { + $(".landing-comment-scroller").append(commentHtml); + } + else { + $(".landing-comment-scroller").prepend(commentHtml); + } + + context.JK.bindHoverEvents(); + } + function initialize(_claimedRecordingId, _recordingId) { recordingId = _recordingId; claimedRecordingId = _claimedRecordingId; @@ -116,6 +129,28 @@ $("#btnLike").click(like); $("#btnPlay").click(play); + + pollForUpdates(claimedRecordingId); + } + + function pollForUpdates(claimedRecordingId) { + $(".landing-comment-scroller").empty(); + rest.getClaimedRecording(claimedRecordingId) + .done(function(response) { + if (response.recording && response.recording.comments) { + $("#spnCommentCount", $scope).html(response.recording.comment_count); + $.each(response.recording.comments, function(index, val) { + renderComment(val.comment, val.creator.id, val.creator.name, + context.JK.resolveAvatarUrl(val.creator.photo_url), $.timeago(val.created_at), val.creator.musician, true); + }); + setTimeout(function() { + pollForUpdates(claimedRecordingId); + }, 60000); + } + }) + .fail(function(xhr) { + + }); } this.initialize = initialize; diff --git a/web/app/assets/javascripts/web/sessions.js b/web/app/assets/javascripts/web/sessions.js index 070333da5..54669d67c 100644 --- a/web/app/assets/javascripts/web/sessions.js +++ b/web/app/assets/javascripts/web/sessions.js @@ -10,32 +10,45 @@ function like() { rest.addSessionLike(sessionId, JK.currentUserId) - .done(function(response) { - $("#spnLikeCount").html(parseInt($("#spnLikeCount").text()) + 1); - $("#btnLike").unbind("click"); - }); + .done(function(response) { + $("#spnLikeCount").html(parseInt($("#spnLikeCount").text()) + 1); + $("#btnLike").unbind("click"); + }); } function addComment() { var comment = $("#txtSessionComment").val(); if ($.trim(comment).length > 0) { - rest.addSessionComment(sessionId, JK.currentUserId, comment) - .done(function(response) { - $("#spnCommentCount").html(parseInt($("#spnCommentCount").text()) + 1); - - var template = $('#template-landing-comment').html(); - var commentHtml = context.JK.fillTemplate(template, { - avatar_url: context.JK.currentUserAvatarUrl, - name: context.JK.currentUserName, - comment: comment, - timeago: $.timeago(Date.now()) - }); - - $(".landing-comment-scroller").prepend(commentHtml); - }); + rest.addSessionComment(sessionId, JK.currentUserId, comment) + .done(function(response) { + $("#spnCommentCount").html(parseInt($("#spnCommentCount").text()) + 1); + renderComment(comment, context.JK.currentUserId, context.JK.currentUserName, + context.JK.currentUserAvatarUrl, $.timeago(Date.now()), context.JK.currentUserMusician, false); + }); } } + function renderComment(comment, userId, userName, userAvatarUrl, timeago, musician, append) { + var template = $('#template-landing-comment').html(); + var commentHtml = context.JK.fillTemplate(template, { + avatar_url: userAvatarUrl, + user_id: userId, + hoverAction: musician ? "musician" : "fan", + name: userName, + comment: comment, + timeago: timeago + }); + + if (append) { + $(".landing-comment-scroller").append(commentHtml); + } + else { + $(".landing-comment-scroller").prepend(commentHtml); + } + + context.JK.bindHoverEvents(); + } + function stateChange(e, data) { if(data.displayText) { @@ -94,28 +107,49 @@ sessionId = musicSessionId; if (JK.currentUserId) { - var shareDialog = new JK.ShareDialog(context.JK.app, sessionId, "session"); - shareDialog.initialize(JK.FacebookHelperInstance); + var shareDialog = new JK.ShareDialog(context.JK.app, sessionId, "session"); + shareDialog.initialize(JK.FacebookHelperInstance); - $("#btnShare").click(function(e) { - shareDialog.showDialog(); - }); + $("#btnShare").click(function(e) { + shareDialog.showDialog(); + }); - $("#btnPostComment").click(function(e) { - if ($.trim($("#txtSessionComment").val()).length > 0) { - addComment(); - $("#txtSessionComment").val(''); - $("#txtSessionComment").blur(); - } - }); + $("#btnPostComment").click(function(e) { + if ($.trim($("#txtSessionComment").val()).length > 0) { + addComment(); + $("#txtSessionComment").val(''); + $("#txtSessionComment").blur(); + } + }); } else { - $("#txtSessionComment").attr("disabled", "disabled"); - $("#txtSessionComment").val("You must be logged in to add a comment."); + $("#txtSessionComment").attr("disabled", "disabled"); + $("#txtSessionComment").val("You must be logged in to add a comment."); } $("#btnLike").click(like); + pollForUpdates(musicSessionId); + } + + function pollForUpdates(musicSessionId) { + $(".landing-comment-scroller").empty(); + rest.getSessionHistory(musicSessionId) + .done(function(response) { + if (response && response.comments) { + $("#spnCommentCount", $scope).html(response.comment_count); + $.each(response.comments, function(index, val) { + renderComment(val.comment, val.creator.id, val.creator.name, + context.JK.resolveAvatarUrl(val.creator.photo_url), $.timeago(val.created_at), val.creator.musician, true); + }); + setTimeout(function() { + pollForUpdates(musicSessionId); + }, 60000); + } + }) + .fail(function(xhr) { + + }); } this.initialize = initialize; diff --git a/web/app/views/api_claimed_recordings/show.rabl b/web/app/views/api_claimed_recordings/show.rabl index 683c1bed9..5210fcbc9 100644 --- a/web/app/views/api_claimed_recordings/show.rabl +++ b/web/app/views/api_claimed_recordings/show.rabl @@ -35,7 +35,7 @@ child(:recording => :recording) { attributes :comment, :created_at child(:user => :creator) { - attributes :id, :first_name, :last_name, :photo_url + attributes :id, :first_name, :last_name, :name, :photo_url, :musician } } } diff --git a/web/app/views/api_music_sessions/history_show.rabl b/web/app/views/api_music_sessions/history_show.rabl index 841e427d5..d29de5af3 100644 --- a/web/app/views/api_music_sessions/history_show.rabl +++ b/web/app/views/api_music_sessions/history_show.rabl @@ -22,4 +22,12 @@ child(:music_session_user_histories => :users) { child(:user => :user) { attributes :name, :photo_url } +} + +child(:comments => :comments) { + attributes :comment, :created_at + + child(:user => :creator) { + attributes :id, :first_name, :last_name, :name, :photo_url, :musician + } } \ No newline at end of file diff --git a/web/app/views/layouts/web.html.erb b/web/app/views/layouts/web.html.erb index 8765fe050..532ee9b06 100644 --- a/web/app/views/layouts/web.html.erb +++ b/web/app/views/layouts/web.html.erb @@ -92,10 +92,12 @@ JK.currentUserId = '<%= current_user.id %>'; JK.currentUserAvatarUrl = JK.resolveAvatarUrl('<%= current_user.photo_url %>'); JK.currentUserName = '<%= current_user.name %>'; + JK.currentUserMusician = '<%= current_user.musician %>'; <% else %> JK.currentUserId = null; JK.currentUserAvatarUrl = null; JK.currentUserName = null; + JK.currentUserMusician = null; <% end %> JK.app = JK.JamKazam(); diff --git a/web/app/views/shared/_comments.html.erb b/web/app/views/shared/_comments.html.erb index 851e8bd81..0dba9f65e 100644 --- a/web/app/views/shared/_comments.html.erb +++ b/web/app/views/shared/_comments.html.erb @@ -12,33 +12,18 @@
- <% comments.each do |c| %> - <% hoverAction = c.user.musician ? "musician" : "fan" %> -
- <% unless c.user.photo_url.blank? %> - <%= image_tag "#{c.user.photo_url}", {:alt => ""} %> - <% else %> - <%= image_tag "shared/avatar_generic.png", {:alt => ""} %> - <% end %> -
-
- <%= c.user.name %> <%= c.comment %> -
-
<%= timeago(c.created_at) %>
-
-
- <% end %> +
\ No newline at end of file