Skip to content

DevGuide PerlObjectsJunkable

Violet edited this page Nov 2, 2010 · 2 revisions

MDG: Creating "Junkable" Objects

Junkable objects are MT::Objects that have associated with them a set of properties and methods that can be used by developers to filter them through Melody's spam filtering system, and for that system to score and log the results of passing through them.

This document describes the Junkable interface for developers and provides examples of how it can be used.

About MT::Junkable

The MT::Junkable class is an abstract class that defines a common set of properties and behaviors for all junkable objects. It is abstract because it requires the developer to implement some functionality on an object-by-object basis. In other words, it is not a complete implementation in and of itself, and simply inheriting from it alone is not sufficient and can produce errors.

Inheriting from MT::Junkable

Objects that pass through junk filters must be junkable. To be junkable means that certain fields are associated with the object in order for the system to track and assign an object's junk status properly. Here is how a developer would declare an object as junkable:

package MT::MyObject;
use base qw( MT::Object MT::Junkable );

This defines a new object called MT::MyObject, which inherits the properties of MT::Object (the Melody database abstraction layer) and of course, MT::Junkable.

Developers must then implement the can_be_junked_by($user) method like so:

sub can_be_junked_by {
  my $obj = shift;
  my ($user) = @_;
  return ($obj->parent->author_id == $user->id);
}

The subroutine is responsible for returning true or false depending upon whether the user passed in as a parameter has permission to junk the object currently in context.

MT::Junkable Properties

  • junk_status - A boolean value indicating whether the object is junk or not.

  • junk_score - A floating point number ranging between -1 (junk) and 1 (not junk) representing how "junky" an object is.

  • junk_log - A string value holding a record of all the ways in which an object is scored or rated as junk.

Methods

  • $class->is_junk - This method returns true if the object is junk, and false otherwise.

  • $class->is_not_junk - This method returns true if the object is NOT junk, and false otherwise.

  • $class->junk - This method marks an object as junk.

  • $class->can_be_junked_by($user) - This is an abstract method. All Objects that inherit from MT::Junkable must implement this method. This method returns true if the MT::Author record passed to it has permission to junk the current object. This is necessary as permission to junk an object is often not directly associated with the object itself. For example, permission to junk a comment depends upon the comment's author.

  • $class->log - This methods adds a string/message to the object's junk log.

More on creating a Junk Filter

Continue Reading

## 
Questions, comments, can't find something? Let us know at our community outpost on Get Satisfaction.

Credits

  • Author: Byrne Reese
  • Edited by: Violet Bliss Dietz
Clone this wiki locally