From 554725095e0f3191289d7eb29f53b79777bc1c61 Thu, 11 Aug 2011 18:44:41 -0700
From: Geoff Appleby <gapple@490940.no-reply.drupal.org>
Date: Thu, 11 Aug 2011 18:43:58 -0700
Subject: [PATCH] Allow configuring inclusion of replies and retweets in user timelines

diff --git a/twitter_pull.class.inc b/twitter_pull.class.inc
index 20bebed..8a98c00 100644
--- a/twitter_pull.class.inc
+++ b/twitter_pull.class.inc
@@ -10,6 +10,8 @@
   var $twitkey;
   var $num_items;
   var $tweets;
+  var $filter_reply = FALSE;
+  var $include_retweets = TRUE;
 
   /**
   * @param $twitkey
@@ -54,7 +56,13 @@
     // if the first character is @, then consider the key a username
     elseif ($prefix == "@") {
       $key = drupal_substr($this->twitkey, 1);
-      $url = 'http://api.twitter.com/1/statuses/user_timeline.json?screen_name='. urlencode($key) .'&include_rts=true&count='. $num;   
+      $url = 'http://api.twitter.com/1/statuses/user_timeline.json?screen_name='. urlencode($key) .'&count='. $num;
+      if ($this->include_retweets) {
+        $url .= '&include_rts=true';
+      }
+      if ($this->filter_reply) {
+        $url .= '&exclude_replies=true';
+      }
     }
     // otherwise, use the key as a search term
     else {
@@ -98,12 +106,12 @@
         $obj->userphoto = (isset($item->user) && !empty($item->user->profile_image_url)) ? $item->user->profile_image_url : $item->profile_image_url;
           $obj->userphoto = check_plain($obj->userphoto);
         $obj->text = filter_xss($item->text);
-          
+
         //-- Convert date to unix timestamp so themers can easily work with it.
         //-- We need to suppress possible warnings (in PHP 5.3+) related to non-set timezones because
         //-- Drupal6 does not save proper timezones (just offsets) and there's no good way to fix this.
         $obj->timestamp = @strtotime($item->created_at);
-        $obj->time_ago =  t('!time ago.', array('!time' => format_interval(time() - $obj->timestamp)));        
+        $obj->time_ago =  t('!time ago.', array('!time' => format_interval(time() - $obj->timestamp)));
         $tweets[] = $obj;
       }
     }
diff --git a/twitter_pull.module b/twitter_pull.module
index 496b329..2a11bc8 100644
--- a/twitter_pull.module
+++ b/twitter_pull.module
@@ -24,6 +24,13 @@
   return variable_get('twitter_pull_empty_message', TWITTER_PULL_EMPTY_MESSAGE);
 }
 
+function twitter_pull_filter_reply() {
+  return variable_get('twitter_pull_filter_reply', FALSE);
+}
+function twitter_pull_include_retweets() {
+  return variable_get('twitter_pull_include_retweets', TRUE);
+}
+
 /**
  * Implementation of hook_flush_caches().
  */
@@ -102,6 +109,9 @@
   else {
     try {
       $puller = new twitter_puller($twitkey, $num_items);
+      $puller->filter_reply = twitter_pull_filter_reply();
+      $puller->include_retweets = twitter_pull_include_retweets();
+
       $puller->get_items();
       $tweets = $puller->tweets;
     }
@@ -133,7 +143,7 @@
 
   $pattern = '/[#]+([A-Za-z0-9-_]+)/';
   $repl = '#<a href="http://twitter.com/#!/search?q=%23$1" title="#$1" rel="nofollow">$1</a>';
-  $text = preg_replace($pattern, $repl, $text);  
+  $text = preg_replace($pattern, $repl, $text);
 
   return $text;
 }
@@ -152,10 +162,10 @@
 
 /**
  * Implementation of hook_block()
- * Note we are getting info about blocks from the 
- * twitter_pull_block_data function 
+ * Note we are getting info about blocks from the
+ * twitter_pull_block_data function
  * if at some time this data is store in the db (currently
- * it is just in code) then we would want to add the config 
+ * it is just in code) then we would want to add the config
  * options in a $op = configure
  */
 function twitter_pull_block($op = 'list', $delta = 0, $edit = array()) {
@@ -173,8 +183,8 @@
 }
 
 
-/* 
- * data hook for twitter_pull blocks.  
+/*
+ * data hook for twitter_pull blocks.
  * RETURN an array of data object with the following properties
  *   delta (unique for all blocks)
  *   name
@@ -185,26 +195,26 @@
  */
 function twitter_pull_block_data() {
   static $data;
-  
+
   //-- Static cache;
   if (!empty($data)) return $data;
-  
+
   $data = module_invoke_all('twitter_pull_blocks');
   drupal_alter('twitter_pull_data', $data);
-  
+
   //-- Do some cleanup
   if (!empty($data) && is_array($data)) {
     foreach ($data as &$block) {
       //-- assign defaults
-      $block->title = (empty($block->title)) ? $block->name : $block->title;     
-      $block->number_of_items = (empty($block->number_of_items)) ? 5 : $block->number_of_items;      
-      $block->theme_key = (empty($block->theme_key)) ? 'twitter_pull_listing' : $block->theme_key;            
-    }  
+      $block->title = (empty($block->title)) ? $block->name : $block->title;
+      $block->number_of_items = (empty($block->number_of_items)) ? 5 : $block->number_of_items;
+      $block->theme_key = (empty($block->theme_key)) ? 'twitter_pull_listing' : $block->theme_key;
+    }
   }
   else {
     $data = array();
   }
-  
+
   return $data;
 }
 
