Adding moderation to your Rails site in 5 minutes

31
May
0

This short tutorial will show you how to integrate Socialmod into Rails in a matter of minutes. This time we’re focussing on text moderation – future tutorials will tackle image and video moderation.

The general process goes likes this:

  • Comment (i.e. UGC text content) is created
  • A POST request is sent to Socialmod, with the text to be moderated and the Comment’s id
  • Moderator moderates text
  • A POST request is sent from Socialmod to your designated callback url with the moderators verdict
  • You then choose to hide/show that item depending on whether you’re doing pre/post moderation, and obviously the verdict.

Firstly, install the gem:

sudo gem install socialmod

Generate your Rails app, and add the gem to config/enviroment.rb;

config.gem "socialmod"

We need to configure the Socialmod library to use your api key.

config/initializers/socialmod.rb

Socialmod::Base.api_key = '00000000-0000-0000-0000-000000000000'

Now we’re going to generate the scaffolding for the Comment model, and install the state machine plugin:

script/generate scaffold comment body:text state:string
script/plugin install git://github.com/pluginaweek/state_machine.git
rake db:migrate

Now to implement the pending, passed and failed states on the Comment model:

app/model/comment.rb

class Comment < ActiveRecord::Base
  state_machine :initial => :pending do
    event :passed do
      transition [:pending, :failed] => :passed
    end

    event :failed do
      transition [:pending, :passed] => :failed
    end

    state :pending, :passed, :failed
  end
  named_scope :passed, :conditions => {:state => "passed"}
end

And now we’re going to add the Socialmod specifics to the Comment model.
After the comment is created, the comment text is sent to Socialmod.

app/model/comment.rb

after_create :create_on_socialmod
def create_on_socialmod
  Socialmod::Item::Text.create(
    :data => body,
    :custom_id => id
  )
end

Now generate the callback controller. Below you can see we’re finding the comment, and passing or failing it based on the callback’s verdict.

You’ll also notice we’re skipping the verify_authenticity_token method. We don’t need XSS protection here (since we’re checking the signature of the callback – verifying its identity).

app/controllers/callback_controller.rb

class CallbackController < ApplicationController
  skip_before_filter :verify_authenticity_token, :only => :index

  def index
    callback = Socialmod::Callback.new(params)
    unless callback.valid?
      return head(403)
    end

    comment = Comment.find(callback.custom_id)
    if callback.passed?
      comment.passed!
    else
      comment.failed!
    end
    head 200
  end
end

How does Socialmod know which callback url to use?
Well you need to specify it in the settings:

callback-url-settings

Unfortunately the callback aspect of it makes testing locally difficult – that’s something that should be tested in the staging environment.

And that’s really all there is to it. You can choose whether or not you want pre-moderation by showing/hiding comments in the pending state.
One nice touch you could add is to only show pending items to the users that created them, so they can get some instant feedback.

The next blog post will show you how to use images with Socialmod.

Enjoy this article?

Consider subscribing to our RSS feed!

Filed under: Socialmod

No Comments

No comments yet.

Leave a comment

RSS feed for comments on this post