summaryrefslogtreecommitdiff
path: root/app/controllers/messages_controller.rb
blob: e597d945c6a9e8d8ad29cc08705d8b12484050f7 (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
class MessagesController < ApplicationController

  before_filter :check_permission, only: [:destroy]

  def index
    if !current_user
      flash[:alert] = "Please log in to see your private messages."
      redirect_to blogposts_path
    end
    @messages = Message.where(user_target: current_user).page(params[:page])
  end

  def destroy
    if @message.user_target.is?(current_user)
      if @message.destroy
        flash[:notice] = "Message deleted!"
      else
        flash[:alert] = "There was a problem while deleting this message"
      end
    else
      flash[:alert] = "You are not allowed to delete this message"
    end
    redirect_to messages_path
  end

  def create
    if !message_params[:user_target_id]
      flash[:alert] = "Please enter a valid IGN before sending."
      redirect_to new_message_path
      return
    end
    if message_params[:text] == ""
      flash[:alert] = "Please write a message before sending."
      redirect_to new_message_path
      return
    end
    @message = Message.new(message_params)
    if @message.save
      flash[:notice] = "Message sent!"
      redirect_to messages_path
      return
    else
      flash[:alert] = "Something went wrong while creating your message."
      render action: "new"
      return
    end
  end

  def new
    if !current_user
      flash[:alert] = "Please log in to send a private message."
      redirect_to blogposts_path
      return
    end
    @message = Message.new
  end

  def message_params(add = [])
    params[:message][:user_target_id] = User.find_by(ign: params[:message][:user_target].gsub(/[@ ]/,"")).try(:id)
    params[:message][:user_sender_id] = User.find_by(ign: params[:message][:user_sender]).id

    params.require(:message).permit([:text, :user_target_id, :user_sender_id])
  end

  private

  def check_permission
    @message = Message.find(params[:id])
    unless @message.user_target == current_user
      flash[:alert] = "You are not allowed to view this message"
      redirect_to home_statics_path
    end
  end
end