BigW Consortium Gitlab

notes_controller.rb 1.76 KB
Newer Older
gitlabhq committed
1
class NotesController < ApplicationController
Nihad Abbasov committed
2
  before_filter :project
gitlabhq committed
3 4 5

  # Authorize
  before_filter :add_project_abilities
6 7

  before_filter :authorize_read_note!
Nihad Abbasov committed
8
  before_filter :authorize_write_note!, :only => [:create]
gitlabhq committed
9 10 11

  respond_to :js

12
  def index
13 14
    notes
    respond_with(@notes)
15 16
  end

gitlabhq committed
17 18 19
  def create
    @note = @project.notes.new(params[:note])
    @note.author = current_user
Valery Sizov committed
20
    @note.notify = true if params[:notify] == '1'
21
    @note.notify_author = true if params[:notify_author] == '1'
Valery Sizov committed
22
    @note.save
gitlabhq committed
23 24 25

    respond_to do |format|
      format.html {redirect_to :back}
Nihad Abbasov committed
26
      format.js
gitlabhq committed
27 28 29 30 31
    end
  end

  def destroy
    @note = @project.notes.find(params[:id])
gitlabhq committed
32
    return access_denied! unless can?(current_user, :admin_note, @note)
gitlabhq committed
33 34 35
    @note.destroy

    respond_to do |format|
Nihad Abbasov committed
36
      format.js { render :nothing => true }
gitlabhq committed
37 38 39
    end
  end

40 41
  protected 

42 43 44 45 46 47 48
  def notes
    @notes = case params[:target_type]
             when "commit" 
               then project.commit_notes(project.commit((params[:target_id]))).fresh.limit(20)
             when "snippet"
               then  project.snippets.find(params[:target_id]).notes
             when "wall"
49
               then project.common_notes.order("created_at DESC").fresh.limit(50)
50 51 52 53 54 55 56 57 58 59 60 61 62
             when "issue"
               then project.issues.find(params[:target_id]).notes.inc_author.order("created_at DESC").limit(20)
             when "merge_request"
               then project.merge_requests.find(params[:target_id]).notes.inc_author.order("created_at DESC").limit(20)
             end

    @notes = if params[:last_id]
               @notes.where("id > ?", params[:last_id])
             elsif params[:first_id]
               @notes.where("id < ?", params[:first_id])
             else 
               @notes
             end
63
  end
gitlabhq committed
64
end