Class: Hobix::Facets::Comments

Inherits:
BaseFacet show all
Defined in:
lib/hobix/facets/comments.rb

Overview

The Comments plugin adds a remote API for adding comments. Basically, to add comments to your site, ensure the plugin is loaded within your hobix.yaml ‘requires’ list:

requires:
- hobix/facets/comments

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseFacet

not_found, #protect

Methods inherited from BasePlugin

inherited, start

Constructor Details

#initialize(weblog, defaults = {}) ⇒ Comments

Returns a new instance of Comments.



34
35
36
# File 'lib/hobix/facets/comments.rb', line 34

def initialize( weblog, defaults = {} )
    @weblog = weblog
end

Class Method Details

.comment_classObject



32
# File 'lib/hobix/facets/comments.rb', line 32

def self.comment_class; Hobix::Comment end

.comment_fieldsObject



31
# File 'lib/hobix/facets/comments.rb', line 31

def self.comment_fields; ['author', 'content']; end

.form_field(name) ⇒ Object



30
# File 'lib/hobix/facets/comments.rb', line 30

def self.form_field( name ); "hobix_comment:#{ name }" end

Instance Method Details

#get(app) ⇒ Object



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
# File 'lib/hobix/facets/comments.rb', line 37

def get app
    if app.respond_to? :action_uri
        action, entry_id = app.action_uri.split( '/', 2 )
        case action
        when "comment"
            # Create the comment entry
            on_entry = @weblog.storage.load_entry( entry_id )
            comment = Comments.comment_class.new do |c|
                Comments.comment_fields.each do |cf|
                    getf = Comments.form_field cf
                    if app._POST[getf].to_s.empty?
                        app.puts "Missing field `#{ getf }'.  Please back up and try again."
                        return true
                    end
                    c.method( "#{ cf }=" ).call( app._POST[getf] )
                end
                c.created = Time.now
                c.ipaddress = app.remote_addr
            end

           # A quick hack to try akismet content spam checking
           if @weblog.requires.detect{ |i| i['hobix/plugin/akismet'] }         
               @akismet = Akismet.new(@weblog.link, AkismetKey.key)
               if @akismet.verifyAPIKey
                   if @akismet.commentCheck(
                           app.remote_addr,                            # remote IP
                           app.get_request_header('User-Agent'),       # user agent
                           app.get_request_header('Referer'),          # http referer
                           '',                                         # permalink
                           'comment',                                  # comment type
                           app._POST['hobix_comment:author'].to_s,     # author name
                           '',                                         # author email
                           '',                                         # author url
                           app._POST['hobix_comment:comment'].to_s,    # comment text
                           {})                                         # other
                       app.puts( "Sorry, that smelled like spam. If wasn't meant to, go back and try again" )
                       return true
                   end
               else
                   # If the key does not verify, post the comment
                   # but note the failure in the apache error logs.
                   $stderr.puts( "Hobix: Akismet API key did not verify." )
               end
           end
               
           # Save the comment, upgen
           @weblog.storage.append_to_attachment( entry_id, 'comments', comment )
           @weblog.regenerate :update
           
           # Redirect
            link = @weblog.output_entry_map[entry_id]
            app.setup_redirection( 302, @weblog.expand_path( link[:page].link ) )
            return true
        when "preview"
            app.puts RedCloth.new( app._POST[Comments.form_field('content')] ).to_html
            return true
        end
    end
end