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.

Filed under: Socialmod

Hello World!

30
May
2

We’ve just released Socialmod, a moderation tool that Tim’s dubbed ‘the Basecamp of moderation‘.

User Generate Content (UGC) is such a large part of the internet, and, if it’s to be associated with a brand, it really needs to be moderated.

A lot of companies develop their own moderation systems in house – a classic case of reinventing the wheel. Socialmod was developed so companies don’t have to put the time and money into developing their own.

Socialmod accepts images, videos and text. Additionally it’s got Twitter integration and can publish a ‘clean feed’ – great for award ceremonies and news events (like the UK Budget).

There are two main ways you can use Socialmod:

  • Use your own team (or a moderation company) to moderate your content
  • Use Mechanical Turk – a crowd-sourcing service

Both have their advantages and disadvantages, and are appropriate for different situations.

socialmod - ugc moderation

As for this blog, I hope to mirror it, in some ways, after the  37 Signal’s Signal vs. Noise blog – but with more of an emphasis on the business and development side of things.

Some of the blog posts coming up are:

  • Adding pre-moderation to a Rails site
  • AWS Elastic Load Balancing tutorial
  • US Companies for non-residents
  • LLC’s and Articles of Organization
  • Merchant Accounts

Subscribe to the RSS!

Filed under: Socialmod