Commits

roma milushov  committed 614e935

actions for comments; bug fixes;

  • Participants
  • Parent commits 22bb083

Comments (0)

Files changed (12)

File app/assets/javascripts/backbone/models/vk.js.coffee

     url = "#{@get("url")}api/playlists/#{pid}/comments/#{cid}/delete"
     @ajax url, success, context, false, true
 
+  spamComment: (pid, cid, success, context)  ->
+    return if not pid or not cid
+    url = "#{@get("url")}api/playlists/#{pid}/comments/#{cid}/spam"
+    @ajax url, success, context, false, true
+
   uploadImage: (file) ->
     if not file or not file.type.match /image.*/
       notify 'Выберите изображение!', 'alert'

File app/assets/javascripts/backbone/templates/comments/comment.jst.hamlc

-.comment{id: @_id}
+.comment{id: "cid#{@_id}"}
   .user_photo.fl_l
     %a{href: "/u/#{@user_data.screen_name}"}
       %img{src: @user_data.photo}
-  .content.fl_l
-    .name
-      %a{href: "/u/#{@user_data.screen_name}"}
-        = "#{@user_data.first_name} #{@user_data.last_name}"
-      - if @replied_comment
-        - name = "#{@replied_comment.user_data.first_name} #{@replied_comment.user_data.last_name}"
-        - name_dat = dative name
-        - url = @replied_comment.user_data.screen_name
-        - photo_url = @replied_comment.user_data.photo
-        - created_at = moment(@replied_comment.created_at).format(lite_format)
-        - if @user_data.sex == 2
-          %span
-            ответил
-        - else
-          %span
-            ответила
 
-        - title = $("<div><a href=\"/u/#{url}\" onclick=\"nav(this); return false;\" class=\'popover_user_photo fl_l\'> <img src=\"#{photo_url}\"> </a> <div class=\"fl_l\"> <span class=\"popover_user_name\">#{name}</span> <span class=\"popover_created_at\">#{created_at}</span></div> </div>")
+  .comment_info.fl_l
+    .header.clear
+      .name.fl_l
+        %a{href: "/u/#{@user_data.screen_name}"}
+          = "#{@user_data.first_name} #{@user_data.last_name}"
+        - if @replied_comment
+          
+          - name = "#{@replied_comment.user_data.first_name} #{@replied_comment.user_data.last_name}"
+          - name_dat = dative name
+          - url = @replied_comment.user_data.screen_name
+          - photo_url = @replied_comment.user_data.photo
+          - created_at = moment(@replied_comment.created_at).format(lite_format)
+
+          - if @user_data.sex == 2
+            %span
+              ответил
+          - else
+            %span
+              ответила
+
+          - title = $("<div> <a href=\"/u/#{url}\" onclick=\"nav(this); return false;\" class=\'popover_user_photo fl_l\'> <img src=\"#{photo_url}\"> </a> <div class=\"fl_l\"> <span class=\"popover_user_name\">#{name}</span> <span class=\"popover_created_at\">#{created_at}</span></div> </div>")
+
+          %a.reply_popover{rel: 'popover', data: { original-title: title.html(), content: @replied_comment.content} }
+            = name_dat
+
+      .actions.fl_r
+        - if @user_data.id == my_profile.user.id
+          - if can_update @created_at
+            %a.update_btn.icon-pencil.icon-black{ rel: 'tooltip', data-cid: @_id, data-original-options: '{placement: top}', data-original-title: 'Редактировать' }
+          %a.delete_btn.icon-remove.icon-black{ rel: 'tooltip', data-cid: @_id, data-original-options: '{placement: top}', data-original-title: 'Удалить' }
+        - else
+          %a.spam_btn.icon-flag.icon-black{ rel: 'tooltip', data-cid: @_id, data-original-options: '{placement: top}', data-original-title: 'Спам' }
 
-        %a.reply_popover{rel: 'popover', data: {'original-title': title.html(), content: @replied_comment.content}, onmouseover: 'bind_urls(); //epic fail' }=name_dat
     .content
       = @content
     .date.fl_l
-      %a{rel: 'tooltip', data-original-options: '{placement: right}', data-original-title="#{ moment(@created_at).format(format) }"}
+      %a{rel: 'tooltip', data-original-options: "{placement: 'right'}", data-original-title: moment(@created_at).format(format) } }
         = moment(@created_at).fromNow()
-    %a.reply_btn.btn.btn-mini.fl_r{data: {reply_to: @_id}} Ответить
+    %a.reply_btn.btn.btn-mini.fl_r{ data-reply_to: @_id } Ответить

File app/assets/javascripts/backbone/templates/comments/index.jst.hamlc

         .user_photo.fl_l
           %a{href: "/u/#{my_profile.user.screen_name}"}
             %img{src: my_profile.user.photo}
-        .content.fl_l
-          .name
-            %a{href: "/u/#{my_profile.user.screen_name}"}
-              = "#{my_profile.user.first_name} #{my_profile.user.last_name}"
+        .comment_info.fl_l
+          .header.clear
+            .name.fl_l
+              %a{href: "/u/#{my_profile.user.screen_name}"}
+                = "#{my_profile.user.first_name} #{my_profile.user.last_name}"
           .content
             %textarea
           %a.btn.btn-primary#add_new_comment

File app/assets/javascripts/backbone/templates/tracks/track.jst.hamlc

   .duration.fl_r.clear
     = dur(@duration)
 .lovers.fl_r.clear
-  - if @lovers.length
+  - if @lovers
     - for id, lover of @lovers
       %a.lover{href: "/u/#{lover.screen_name}", rel: 'tooltip', title: "#{lover.first_name}"}
         %img{src: "#{lover.photo}"}

File app/assets/javascripts/backbone/views/comments/comment_view.js.coffee

   template: JST["backbone/templates/comments/comment"]
 
   events:
-    'mouseover' : 'showReplyButton'
-    'mouseout' : 'hideReplyButton'
+    'mouseover' : 'showButtons'
+    'mouseout' : 'hideButtons'
     'click .destroy' : 'destroy'
 
-  showReplyButton: ->
+  showButtons: ->
+    $(@el).find('.actions').show()
     $(@el).find('.reply_btn').show()
 
-  hideReplyButton: ->
+  hideButtons: ->
+    $(@el).find('.actions').hide()
     $(@el).find('.reply_btn').hide()
 
   destroy: () ->
 
   render: ->
     $(@el).html(@template(@model.toJSON() ))
+    content = $(@el).find('.content').html()
+    $(@el).find('.content').html linkify(content)
     return this

File app/assets/javascripts/backbone/views/comments/index_view.js.coffee

 class Playlists.Views.Comments.IndexView extends Backbone.View
   template: JST['backbone/templates/comments/index']
   reply_to: null
+  update_cid: null
 
   events:
     'click #follow' : 'followPlaylist'
     'click #unfollow' : 'unfollowPlaylist'
-    'click #add_new_comment' : 'newComment'
+    'click #add_new_comment' : 'new'
+    'click .update_btn' : 'update'
+    'click .delete_btn' : 'delete'
+    'click .spam_btn' : 'spam'
     'click .reply_btn' : 'reply'
 
   tagName: 'div'
   initialize: () ->
     # @options.comments.bind('reset', @addAll)
 
-  reply: (e) ->
-    @reply_to = $(e.srcElement).data('reply_to')
-    # TODO: show username whom message we raply
-    $.scrollTo top: 1, left: 0,
-      duration: 500,
-      easing:'easeOutExpo'
-    @$('#new_comment textarea').focus()
+  update: (e) ->
+    @update_cid = $(e.srcElement).data 'cid'
+    content = @$("#cid#{@update_cid} .content").text()
+    @$('#new_comment textarea').val content.trim()
+    @scroll()
+
+  delete: (e) ->
+    cid = $(e.srcElement).data 'cid'
+    App.vk.deleteComment @options.id, cid, (data) ->
+      return notify data.error if data.error
+      pid = @options.id
+      App.playlists.getById(pid).comments.where(_id: cid)[0].destroy()
+      @$("#cid#{cid}").remove()
+      $('.tooltip').remove()
+    ,this
+
+  spam: (e) ->
+    cid = $(e.srcElement).data 'cid'
+    App.vk.spamComment @options.id, cid, (data) ->
+      return notify data.error if data.error
+      pid = @options.id
+      console.log App.playlists.getById(pid).comments.where(_id: cid)[0].destroy()
+      @$("#cid#{cid}").remove()
+      $('.tooltip').remove()
+      notify 'Сообщение помечено как спам', 'success'
+    ,this
 
+  reply: (e) ->
+    @reply_to = $(e.srcElement).data 'reply_to'
+    # TODO: show username whom message we reply
+    @scroll()
 
-  newComment: (reply_to = null) ->
+  new: ->
     content = $('.content textarea').val()
     if content.length < 10
-      return notify 'Длина сообщения должная быть больше 10 символов.'
+      return notify 'Длина сообщения должная быть больше 10 символов!'
     
     reply_to = @reply_to
 
-    App.vk.saveNewComment @options.id, content, reply_to, (data) ->
-      return notify data.error if data.error
-      data.comment.user_data = my_profile.user
-      comment = new Playlists.Models.Comment data.comment
-      App.playlists.getById(@options.id).comments.add comment, at: 0
+    if @update_cid
+      App.vk.updateComment @options.id, @update_cid, content, (data) ->
+        return notify data.error if data.error
+        
+
+        data.comment.user_data = my_profile.user
+        comment = new Playlists.Models.Comment data.comment
+        
+        playlist = App.playlists.getById(@options.id)
+        model = playlist.comments.where(_id: @update_cid)[0]
+        i = playlist.comments.models.indexOf model
+        App.playlists.getById(@options.id).comments.add comment, at: i        
+        view = new Playlists.Views.Comments.CommentView model: comment
+        @$("#cid#{@update_cid}").replaceWith view.render().el
+        @$('#new_comment textarea').val('')
+        @update_cid = false
+      ,this
+    else
       @reply_to = null
-      
-      view = new Playlists.Views.Comments.CommentView model: comment
-      @$("#new_comment").after(view.render().el)
-      @$('#new_comment textarea').val('')
-      # App.showComments @options.get 'url'
-    ,this
+      App.vk.saveNewComment @options.id, content, reply_to, (data) ->
+        return notify data.error if data.error
+        data.comment.user_data = my_profile.user
+        comment = new Playlists.Models.Comment data.comment
+        App.playlists.getById(@options.id).comments.add comment, at: 0
+        
+        view = new Playlists.Views.Comments.CommentView model: comment
+        @$("#new_comment").after(view.render().el)
+        @$('#new_comment textarea').val('')
+        # App.showComments @options.get 'url'
+      ,this
+
+  scroll: ->
+    $.scrollTo top: 1, left: 0,
+      duration: 500,
+      easing:'easeOutExpo'
+    @$('#new_comment textarea').focus()
 
   followPlaylist: ->
     App.vk.follow 'playlist', @options.get 'url'

File app/assets/javascripts/common.js.coffee

   noty(
     text: message,
     theme:'noty_theme_twitter',
-    layout:'top',
+    layout: if type == 'error' then 'top' else 'bottomRight',
     type: type,
     animateOpen:
       height: 'toggle'
     closeButton:true,
     closeOnSelfClick: true,
     closeOnSelfOver: false,
-    modal:true
+    modal: if type == 'error' then true else false
   )
 
 $ ()->
     )
 
   soundManager.ontimeout ()->
-    alert 'Плеер завис, перезагрузите страницу! (F5)'
+    notify 'Плеер завис, перезагрузите страницу! (F5)'
 
   $.ajaxSetup cache: false
 
       if JSON.parse(jqxhr.responseText).error == 'abuse'
         return notify 'Вы слишком часто обращаетесь к серверу, вы случайно не робот? Если да, то мы вас скоро забаним :-)', 'error', true
 
-    notify "Упс. Кажется эта ссылка сейчас не работает. Уже чиним. (#{exception})"
+    notify "Упс. Кажется эта функция сейчас не работает. Уже чиним. (#{exception})"
     #history.back()
 
   $('footer').tooltip
     .replace(/[а-яё]/g, (m,k)->return az[m]) 
     .replace(/[^\w\d\s-_]+/g, '') 
     .replace(/[\s-_]+/g, '-')
-    .replace(/(^-|-$)+/g,'')
+    .replace(/(^-|-$)+/g,'')
+
+window.linkify = (text) ->
+  exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig
+  text.replace(exp,"<a href='$1' data-type='ext' target='blank'>$1</a>")
+
+window.can_update = (time_str) ->
+  return false unless time_str
+  diff = (moment().diff(time_str)/1000).toFixed()
+  max_time = 4*60*60
+  return if diff > max_time then false else true

File app/assets/stylesheets/comments.css.scss

       }
     }
 
-    .content {
+    .comment_info {
       width: 470px;
 
-      .name {
-        a {
+      .header {
+        overflow: hidden;
+        .name {
+          
+          a {
+
+          }
+        }
 
+        .actions {
+          overflow: hidden;
+          display: none;
+          opacity: 0.7;
+          a{
+            cursor: pointer;
+          }
         }
       }
 
+      .content {
+        width: 470px;
+      }
+
       .date {
         a {
           text-decoration: none;
       }
 
       textarea {
-        width: 470px; height: 75px;
+        width: 465px; height: 75px;
       }
 
       .reply_btn {
         display: none;
-        margin-top: -3px;
+        margin-top: -2px;
       }
     }
   }

File app/assets/stylesheets/common.css.scss

 
   .fl_l { float: left; }
   .fl_r { float: right; }
-  .clear:after { clear:both; }
+  .clear:after { clear:both; float: none; }
 
   body {
     overflow-y: scroll;

File app/controllers/comments_controller.rb

   end
 
   def create
-    return error 'content nil or less 10 letters' unless content = params[:content] or content.length < 10
+    if content = params[:content] and not (10..1000).include? content.length
+      return error 'comment less 10 of bigger 1000 letters'
+    end
 
     if params[:reply_to] =~ /[a-f0-9]{24}/
       reply_to = params[:reply_to]
     render json: {status: status, id: comment.id, comment: comment}
   end
 
-  def update
-    return error 'content nil or less 10 letters' unless content = params[:content] or content.length < 10
-    comment = @playlist.find(params[:cid])
+def update
+    if content = params[:content] and not (10..1000).include? content.length
+      return error 'comment less 10 of bigger 1000 letters'
+    end
+    
+    comment = Comment.find params[:cid]
+    
     if comment.created_at < Time.now - 4.hour
       return error 'it is too late, was more than 4 hour'
     end
     comment.content = content
     status = comment.save
-    render json: { status: status, id: comment.id  }
+    render json: { status: status, id: comment.id, comment: comment}
   end
 
   def delete
-    comment = @playlist.find(params[:cid]) 
+    unless comment = Comment.find(params[:cid])
+      return error 'comment not found, probably this comment already deleted'
+    end
+
     unless comment.user_id == session[:user_id].to_i
       return error 'you can not delete comments form others users'
     end
     render json: {status: status, id: comment.id}
   end
 
+  def spam
+    comment = Comment.find params[:cid]
+    status = true
+    #status = @playlist.comments.find(params[:cid]).destroy
+    render json: {status: status, id: comment.id}
+  end
+
 private
   def prepare
     @playlist = Playlist.any_of({url: params[:id]}, {_id: params[:id]}).first

File config/routes.rb

         get 'unfollow'
         get 'comments', to: 'comments#index'
         post 'comments/create', to: 'comments#create'
-        post 'comments/:cid/upadate', to: 'comments#update'
+        post 'comments/:cid/update', to: 'comments#update'
         post 'comments/:cid/delete', to: 'comments#delete'
+        post 'comments/:cid/spam', to: 'comments#spam'
       end
 
       resources :tracks do

File vendor/assets/javascripts/backbone_rails_sync.js

   };
 
   Backbone.sync = function(method, model, options) {
+    return true
     var type = methodMap[method];
 
     // Default JSON-request options.