summaryrefslogtreecommitdiff
path: root/app/controllers/messagereplies_controller.rb
blob: 45c5785e8ddc661409b4bad565a1d51ea2e715ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
class MessagerepliesController < ApplicationController

  def edit
    @reply = Messagereply.find(params[:id])
    if mod? || @reply.author.is?(current_user)
    else
      flash[:alert] = "You are not allowed to edit this reply"
      redirect_to @reply.message
    end
  end

  def create
    message = Message.find(params[:message_id])
    if [message.user_sender, message.user_target].include? current_user
      @reply = Messagereply.new(reply_params)
      @reply.user_author = current_user
      @reply.message = message
      if @reply.save
        if false
          @reply.send_new_message_reply_mail
        end
        Message.find(params[:message_id]).update_attributes(user_hidden: nil, user_unread_id: current_user.id)
        position = message.replies.count - 1
        page     = position / Kaminari.config.default_per_page + 1
        redirect_to message_path(@reply.message, page: page) + "#reply-#{@reply.id}", notice: 'Reply created!'
      else
        flash[:alert] = "Could not create reply."
        redirect_to Message.find(params[:message_id])
      end
    else
      flash[:alert] = "You are not allowed to create replies."
      redirect_to Message.find(params[:message_id])
    end
  end

  def update
    @reply = Messagereply.find(params[:id])
    if mod? || @reply.author.is?(current_user)
      old_content = @reply.text_was
      if @reply.update_attributes(reply_params)
        if false
          @reply.send_new_reply_mail(old_content)
        end
        flash[:notice] = "Reply updated!"
        position = @reply.message.replies.index(@reply)
        page     = position / Kaminari.config.default_per_page + 1
        redirect_to message_path(@reply.message, page: page) + "#reply-#{@reply.id}"
      else
        flash[:alert] = "There was a problem while updating your reply"
        render action: "edit"
      end
    else
      flash[:alert] = "You are not allowed to edit this reply"
      redirect_to @reply.message
    end
  end

  def destroy
    @reply = Messagereply.find(params[:id])
    if mod? || @reply.author.is?(current_user)
      if @reply.destroy
        flash[:notice] = "Reply deleted!"
      else
        flash[:alert] = "There was a problem while deleting this reply"
      end
    else
      flash[:alert] = "You are not allowed to delete this reply"
    end
    redirect_to @reply.message
  end

  private

  def reply_params
    params.require(:messagereply).permit(:text)
  end
end