aboutsummaryrefslogtreecommitdiffstats
path: root/app/models/outgoing_message.rb
blob: 6cc8b23685286384f6e4cedce5e55732b1cbbfac (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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# == Schema Information
# Schema version: 56
#
# Table name: outgoing_messages
#
#  id                           :integer         not null, primary key
#  info_request_id              :integer         not null
#  body                         :text            not null
#  status                       :string(255)     not null
#  message_type                 :string(255)     not null
#  created_at                   :datetime        not null
#  updated_at                   :datetime        not null
#  last_sent_at                 :datetime        
#  incoming_message_followup_id :integer         
#

# models/outgoing_message.rb:
# A message, associated with a request, from the user of the site to somebody
# else. e.g. An initial request for information, or a complaint.
#
# Copyright (c) 2007 UK Citizens Online Democracy. All rights reserved.
# Email: francis@mysociety.org; WWW: http://www.mysociety.org/
#
# $Id: outgoing_message.rb,v 1.54 2008-07-10 08:26:08 francis Exp $

class OutgoingMessage < ActiveRecord::Base
    belongs_to :info_request
    validates_presence_of :info_request

    validates_inclusion_of :status, :in => ['ready', 'sent', 'failed']
    validates_inclusion_of :message_type, :in => ['initial_request', 'followup' ] #, 'complaint']

    belongs_to :incoming_message_followup, :foreign_key => 'incoming_message_followup_id', :class_name => 'IncomingMessage'

    # How the default letter starts and ends
    def get_salutation
        ret = "Dear "
        if self.message_type == 'followup' && !self.incoming_message_followup.nil? && !self.incoming_message_followup.safe_mail_from.nil?
            ret = ret + self.incoming_message_followup.safe_mail_from
        else
            ret = ret + "Sir or Madam"
        end
        return ret + ","
    end
    def get_signoff
        if self.message_type == 'followup'
            return "Yours sincerely,"
        else
            return "Yours faithfully,"
        end
    end

    def body
        ret = read_attribute(:body)
        if ret.nil?
            return ret
        end
        ret.strip!
        ret = ret.gsub(/(?:\n\s*){2,}/, "\n\n") # remove excess linebreaks that unnecessarily space it out
        ret
    end

    def body_without_salutation
        ret = self.body
        ret.sub!(/Dear .+,/, "")
        return ret
    end

    # Used to give warnings when writing new messages
    def contains_email?
        MySociety::Validate.email_find_regexp.match(self.body)
    end
    def contains_postcode?
        MySociety::Validate.contains_postcode?(self.body)
    end
 
    # Set default letter
    def after_initialize
        if self.body.nil?
            self.body = get_salutation + "\n\n\n\n" + get_signoff + "\n\n"
        end
    end

    # Check have edited letter
    def validate
        if self.body.empty? || self.body =~ /\A#{get_salutation}\s+#{get_signoff}\s+/
            if self.message_type == 'followup'
                errors.add(:body, "^Please enter your follow up message")
            elsif
                errors.add(:body, "^Please enter your letter requesting information")
            else
                raise "Message id #{self.id} has type '#{self.message_type}' which validate can't handle"
            end
        end
        if self.body =~ /#{get_signoff}\s+\Z/
            errors.add(:body, '^Please sign at the bottom with your name, or alter the "' + get_signoff + '" signature')
        end
    end

    # Deliver outgoing message
    # Note: You can test this from script/console with, say:
    # InfoRequest.find(1).outgoing_messages[0].send_message
    def send_message(log_event_type = 'sent')
        if self.status == 'ready'
            if self.message_type == 'initial_request'
                RequestMailer.deliver_initial_request(self.info_request, self)
                self.last_sent_at = Time.now
                self.status = 'sent'
                self.save!
                self.info_request.log_event(log_event_type, { :email => self.info_request.recipient_name_and_email, :outgoing_message_id => self.id })
            elsif self.message_type == 'followup'
                RequestMailer.deliver_followup(self.info_request, self, self.incoming_message_followup)
                self.last_sent_at = Time.now
                self.status = 'sent'
                self.save!
                self.info_request.log_event('followup_' + log_event_type, { :email => RequestMailer.name_and_email_for_followup(self.info_request, self.incoming_message_followup), :outgoing_message_id => self.id })
                if self.info_request.described_state == 'waiting_clarification'
                    self.info_request.set_described_state('waiting_response')
                end
            else
                raise "Message id #{self.id} has type '#{self.message_type}' which send_message can't handle"
            end
        elsif self.status == 'sent'
            raise "Message id #{self.id} has already been sent"
        else
            raise "Message id #{self.id} not in state for send_message"
        end
    end

    # An admin function
    def resend_message
        if self.message_type == 'initial_request' and self.status == 'sent'
            self.status = 'ready'
            send_message('resent')
        else
            raise "Message id #{self.id} has type '#{self.message_type}' status '#{self.status}' which resend_message can't handle"
        end
    end

    # Returns the text to quote the original message when sending this one
    def quoted_part_to_append_to_email
        if self.message_type == 'followup' && !self.incoming_message_followup.nil?
            return "\n\n-----Original Message-----\n\n" + self.incoming_message_followup.get_body_for_quoting + "\n"
        else
            return ""
        end
    end

    # Return body for display as HTML
    def get_body_for_html_display
        text = self.body.strip
        text = CGI.escapeHTML(text)
        text = MySociety::Format.make_clickable(text, :contract => 1)
        text = text.gsub(/\n/, '<br>')
        return text
    end

    # Return body for display as HTML
    # XXX this is repeating code in a combination of 
    # views/layouts/request_mailer.rhtml and views/request_mailer/initial_request.rhtml
    def get_body_for_html_preview
        text = self.body.strip
        text = MySociety::Format.wrap_email_body(self.body.strip)
        text = CGI.escapeHTML(text)
        text = MySociety::Format.make_clickable(text, :contract => 1)
        text = text.gsub(/\n/, '<br>')

        return text
    end

end