public
Description: The open source social networking platform in Ruby on Rails from the author of RailsSpace
Homepage: http://insoshi.com
Clone URL: git://github.com/insoshi/insoshi.git
Search Repo:
Michael Hartl (author)
Thu May 08 12:36:21 -0700 2008
commit  81389f1c0a1dc1874f2f5c885fb43efa97233e60
tree    226e293aaccd6e1ecf915f90f1a9bc710c853bfc
parent  0177c8084c6d7da1cec4ecaf6f116c1865caae35
insoshi / app / controllers / messages_controller.rb
100644 159 lines (137 sloc) 4.414 kb
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
class MessagesController < ApplicationController
 
  before_filter :login_required, :setup
  before_filter :authenticate_person, :only => [:show]
  before_filter :handle_cancel, :only => [:create]
  before_filter :validate_reply, :only => [:create]
 
  # GET /messages
  def index
    @messages = current_person.received_messages(params[:page])
    respond_to do |format|
      format.html { render :template => "messages/index" }
    end
  end
 
  # GET /messages/sent
  def sent
    @messages = current_person.sent_messages(params[:page])
    respond_to do |format|
      format.html { render :template => "messages/index" }
    end
  end
  
  # GET /messages/trash
  def trash
    @messages = current_person.trashed_messages(params[:page])
    respond_to do |format|
      format.html { render :template => "messages/index" }
    end
  end
 
  def show
    @message.mark_as_read if current_person?(@message.recipient)
    respond_to do |format|
      format.html
    end
  end
 
  def new
    @message = Message.new
    @recipient = Person.find(params[:person_id])
 
    respond_to do |format|
      format.html
    end
  end
 
  def reply
    original_message = Message.find(params[:id])
    @message = Message.new(:parent_id => original_message.id,
                           :subject => original_message.subject,
                           :sender => current_person,
                           :recipient => original_message.sender)
    @recipient = not_current_person(original_message)
    respond_to do |format|
      format.html { render :action => "new" }
    end
  end
 
  def create
    
    if params["commit"] == "Send to all" and current_person.admin?
      # Send messages to all (active) people.
      messages = Person.active.inject([]) do |messages, person|
        message = Message.new(params[:message].merge(:sender => current_person,
                                                     :recipient => person))
        messages.push(message)
      end
      respond_to do |format|
        @message = messages.first
        if @message.save
          messages.each { |m| m.save }
          flash[:success] = 'Message sent to everyone!'
          format.html { redirect_to messages_url }
        else
          format.html { render :action => "new" }
        end
      end
    else
      @message = Message.new(params[:message].merge(:sender => current_person,
                                                    :recipient => @recipient))
    
      respond_to do |format|
        if @message.save
          flash[:success] = 'Message sent!'
          format.html { redirect_to messages_url }
        else
          format.html { render :action => "new" }
        end
      end
    end
  end
 
  def destroy
    @message = Message.find(params[:id])
    if @message.trash(current_person)
      flash[:success] = "Message trashed"
    else
      # This should never happen...
      flash[:error] = "Invalid action"
    end
  
    respond_to do |format|
      format.html { redirect_to messages_url }
    end
  end
  
  def undestroy
    @message = Message.find(params[:id])
    if @message.untrash(current_person)
      flash[:success] = "Message restored to inbox"
    else
      # This should never happen...
      flash[:error] = "Invalid action"
    end
    respond_to do |format|
      format.html { redirect_to messages_url }
    end
  end
 
  private
  
    def setup
      @body = "messages"
    end
  
    def authenticate_person
      @message = Message.find(params[:id])
      unless (current_person == @message.sender or
              current_person == @message.recipient)
        redirect_to login_url
      end
    end
 
    def handle_cancel
      redirect_to messages_url if params[:commit] == "Cancel"
    end
    
    def validate_reply
      @recipient = Person.find(params[:person_id])
      redirect_to home_url if reply? and not valid_reply?(@recipient)
    end
    
    def reply?
      !params[:parent_id].nil?
    end
 
    def valid_reply?(recipient)
      original = Message.find(params[:parent_id])
      original.recipient == current_person and original.sender == recipient
    end
    
    # Return the proper recipient for a message.
    # This should not be the current person in order to allow multiple replies
    # to the same message.
    def not_current_person(message)
      message.sender == current_person ? message.recipient : message.sender
    end
end