aboutsummaryrefslogtreecommitdiffstats
path: root/app/controllers/comment_controller.rb
blob: 8ac6d3850c69e123e6d1be772ebb9b862bcd77bb (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
# app/controllers/comment_controller.rb:
# Show annotations upon a request or other object.
#
# Copyright (c) 2008 UK Citizens Online Democracy. All rights reserved.
# Email: francis@mysociety.org; WWW: http://www.mysociety.org/
#
# $Id: comment_controller.rb,v 1.5 2008-08-29 12:03:40 francis Exp $

class CommentController < ApplicationController
    
    def new
        if params[:type] == 'request'
            @info_request = InfoRequest.find_by_url_title(params[:url_title])
            @track_thing = TrackThing.create_track_for_request(@info_request)
            @comment = Comment.new(params[:comment].merge({
                :comment_type => 'request', 
                :user => @user
            }))
        else
            raise "Unknown type " + params[:type]
        end

        # XXX this check should theoretically be a validation rule in the model
        @existing_comment = Comment.find_by_existing_comment(@info_request.id, params[:comment][:body])
        
        # See if values were valid or not
        if !@existing_comment.nil? || !@comment.valid? || params[:reedit]
            render :action => 'new'
            return
        end

        # Show preview page, if it is a preview
        if params[:preview].to_i == 1
            render :action => 'preview'
            return
        end

        if authenticated?(
                :web => "To post your annotation",
                :email => "Then your annotation to " + @info_request.title + " will be posted.",
                :email_subject => "Confirm your annotation to " + @info_request.title
            )
            @comment = @info_request.add_comment(params[:comment][:body], authenticated_user)
            # This automatically saves dependent objects in the same transaction
            @info_request.save!
            flash[:notice] = "Thank you for making an annotation!"

            # Also subscribe to track for this request, so they get updates
            if params[:subscribe_to_request]
                @track_thing = TrackThing.create_track_for_request(@info_request)
                @existing_track = TrackThing.find_by_existing_track(@user, @track_thing)
                if not @existing_track
                    @track_thing.track_medium = 'email_daily'
                    @track_thing.tracking_user_id = @user.id
                    @track_thing.save!
                    flash[:notice] += " You will also be emailed updates about the request."
                else
                    flash[:notice] += " You are already being emailed updates about the request."
                end
            end

            # we don't use comment_url here, as then you don't see the flash at top of page
            redirect_to request_url(@info_request)
        else
            # do nothing - as "authenticated?" has done the redirect to signin page for us
        end
    end

end