diff --git a/_darcs/inventory b/_darcs/inventory index e7100599a4..8c94c559c8 100644 --- a/_darcs/inventory +++ b/_darcs/inventory @@ -112,4 +112,6 @@ Evan Prodromou **20081230211444] [better serialization of arrays and booleans in config output Evan Prodromou **20081230211957] [wrapper element for config.xml -Evan Prodromou **20081230212202] \ No newline at end of file +Evan Prodromou **20081230212202] +[trac750 Automatically update linked Facebook users' statuses +Zach Copley **20090105010407] \ No newline at end of file diff --git a/_darcs/patches/20090105010407-7b5ce-559da13720b18e3c570e730326f5e5ef2b2dc1ab.gz b/_darcs/patches/20090105010407-7b5ce-559da13720b18e3c570e730326f5e5ef2b2dc1ab.gz new file mode 100644 index 0000000000..5eb98e9e0e Binary files /dev/null and b/_darcs/patches/20090105010407-7b5ce-559da13720b18e3c570e730326f5e5ef2b2dc1ab.gz differ diff --git a/_darcs/pristine/actions/facebookhome.php b/_darcs/pristine/actions/facebookhome.php index dcdb102e65..a23d1aa07f 100644 --- a/_darcs/pristine/actions/facebookhome.php +++ b/_darcs/pristine/actions/facebookhome.php @@ -36,7 +36,7 @@ class FacebookhomeAction extends FacebookAction $user = null; - $facebook = $this->get_facebook(); + $facebook = get_facebook(); $fbuid = $facebook->require_login(); # check to see whether there's already a Facebook link for this user diff --git a/_darcs/pristine/actions/facebookinvite.php b/_darcs/pristine/actions/facebookinvite.php index 48a820e44b..00efa654b1 100644 --- a/_darcs/pristine/actions/facebookinvite.php +++ b/_darcs/pristine/actions/facebookinvite.php @@ -34,7 +34,7 @@ class FacebookinviteAction extends FacebookAction function display() { - $facebook = $this->get_facebook(); + $facebook = get_facebook(); $fbuid = $facebook->require_login(); diff --git a/_darcs/pristine/actions/facebooksettings.php b/_darcs/pristine/actions/facebooksettings.php index 38542d4b6f..17812850c0 100644 --- a/_darcs/pristine/actions/facebooksettings.php +++ b/_darcs/pristine/actions/facebooksettings.php @@ -34,7 +34,7 @@ class FacebooksettingsAction extends FacebookAction function display() { - $facebook = $this->get_facebook(); + $facebook = get_facebook(); $fbuid = $facebook->require_login(); @@ -44,6 +44,8 @@ class FacebooksettingsAction extends FacebookAction .''; + $fbml .= '

Allow Identi.ca to update my Facebook status

'; + $this->show_header('Settings'); echo $fbml; diff --git a/_darcs/pristine/classes/Foreign_link.php b/_darcs/pristine/classes/Foreign_link.php index 9027ab9015..c89124d19c 100644 --- a/_darcs/pristine/classes/Foreign_link.php +++ b/_darcs/pristine/classes/Foreign_link.php @@ -4,7 +4,7 @@ */ require_once INSTALLDIR.'/classes/Memcached_DataObject.php'; -class Foreign_link extends Memcached_DataObject +class Foreign_link extends Memcached_DataObject { ###START_AUTOCODE /* the code below is auto generated do not remove the above tag */ @@ -13,7 +13,7 @@ class Foreign_link extends Memcached_DataObject public $user_id; // int(4) primary_key not_null public $foreign_id; // int(4) primary_key not_null public $service; // int(4) primary_key not_null - public $credentials; // varchar(255) + public $credentials; // varchar(255) public $noticesync; // tinyint(1) not_null default_1 public $friendsync; // tinyint(1) not_null default_2 public $profilesync; // tinyint(1) not_null default_1 @@ -29,7 +29,8 @@ class Foreign_link extends Memcached_DataObject // XXX: This only returns a 1->1 single obj mapping. Change? Or make // a getForeignUsers() that returns more than one? --Zach - static function getByUserID($user_id, $service) { + static function getByUserID($user_id, $service) + { $flink = new Foreign_link(); $flink->service = $service; $flink->user_id = $user_id; @@ -39,10 +40,11 @@ class Foreign_link extends Memcached_DataObject return $flink; } - return null; + return null; } - - static function getByForeignID($foreign_id, $service) { + + static function getByForeignID($foreign_id, $service) + { $flink = new Foreign_link(); $flink->service = $service; $flink->foreign_id = $foreign_id; @@ -52,28 +54,28 @@ class Foreign_link extends Memcached_DataObject return $flink; } - return null; + return null; } - + # Convenience methods function getForeignUser() - { + { $fuser = new Foreign_user(); $fuser->service = $this->service; $fuser->id = $this->foreign_id; - + $fuser->limit(1); - + if ($fuser->find(true)) { return $fuser; } - - return null; + + return null; } - + function getUser() { return User::staticGet($this->user_id); } - + } diff --git a/_darcs/pristine/lib/facebookaction.php b/_darcs/pristine/lib/facebookaction.php index d5b7ed0fb8..ee9f783c40 100644 --- a/_darcs/pristine/lib/facebookaction.php +++ b/_darcs/pristine/lib/facebookaction.php @@ -19,7 +19,8 @@ if (!defined('LACONICA')) { exit(1); } -require_once(INSTALLDIR.'/extlib/facebook/facebook.php'); +require_once(INSTALLDIR.'/lib/facebookutil.php'); + class FacebookAction extends Action { @@ -29,13 +30,6 @@ class FacebookAction extends Action parent::handle($args); } - function get_facebook() - { - $apikey = common_config('facebook', 'apikey'); - $secret = common_config('facebook', 'secret'); - return new Facebook($apikey, $secret); - } - function update_profile_box($facebook, $fbuid, $user) { diff --git a/_darcs/pristine/lib/facebookutil.php b/_darcs/pristine/lib/facebookutil.php new file mode 100644 index 0000000000..fc0e41e5bf --- /dev/null +++ b/_darcs/pristine/lib/facebookutil.php @@ -0,0 +1,40 @@ +. + */ + +require_once(INSTALLDIR.'/extlib/facebook/facebook.php'); + +// Gets all the notices from users with a Facebook link since a given ID +function get_facebook_notices($since) +{ + $qry = 'SELECT notice.* ' . + 'FROM notice ' . + 'JOIN foreign_link ' . + 'WHERE notice.profile_id = foreign_link.user_id ' . + 'AND foreign_link.service = 2'; + + // XXX: What should the limit be? + return Notice::getStreamDirect($qry, 0, 100, 0, 0, null, $since); +} + +function get_facebook() +{ + $apikey = common_config('facebook', 'apikey'); + $secret = common_config('facebook', 'secret'); + return new Facebook($apikey, $secret); +} diff --git a/_darcs/pristine/scripts/update_facebook.php b/_darcs/pristine/scripts/update_facebook.php new file mode 100644 index 0000000000..d2440b163c --- /dev/null +++ b/_darcs/pristine/scripts/update_facebook.php @@ -0,0 +1,102 @@ +#!/usr/bin/env php +. + */ + +# Abort if called from a web server +if (isset($_SERVER) && array_key_exists('REQUEST_METHOD', $_SERVER)) { + print "This script must be run from the command line\n"; + exit(); +} + +define('INSTALLDIR', realpath(dirname(__FILE__) . '/..')); +define('LACONICA', true); + +require_once(INSTALLDIR . '/lib/common.php'); +require_once(INSTALLDIR . '/lib/facebookutil.php'); + +// For storing the last run date-time +$last_updated_file = "/home/zach/laconica/scripts/facebook_last_updated"; + +// Lock file name +$tmp_file = "/tmp/update_facebook.lock"; + +// Make sure only one copy of the script is running at a time +if (!($tmp_file = @fopen($tmp_file, "w"))) +{ + die("Can't open lock file. Script already running?"); +} + +$facebook = get_facebook(); + +$current_time = time(); + +$notice = get_facebook_notices(get_last_updated()); + +while($notice->fetch()) { + + $flink = Foreign_link::getByUserID($notice->profile_id, 2); + $fbuid = $flink->foreign_id; + + update_status($fbuid, $notice); + +} + +update_last_updated($current_time); + +exit(0); + + + +function update_status($fbuid, $notice) { + global $facebook; + + try { + + $result = $facebook->api_client->users_setStatus($notice->content, $fbuid, false, true); + + } catch(FacebookRestClientException $e){ + + print_r($e); + } + +} + +function get_last_updated(){ + global $last_updated_file, $current_time; + + $file = fopen($last_updated_file, 'r'); + + if ($file) { + $last = fgets($file); + } else { + print "Unable to read $last_updated_file. Using current time.\n"; + return $current_time; + } + + fclose($file); + + return $last; +} + +function update_last_updated($time){ + global $last_updated_file; + $file = fopen($last_updated_file, 'w') or die("Can't open $last_updated_file for writing!"); + fwrite($file, $time); + fclose($file); +} diff --git a/_darcs/tentative_pristine b/_darcs/tentative_pristine index 6a29f612c1..15afe3e099 100644 --- a/_darcs/tentative_pristine +++ b/_darcs/tentative_pristine @@ -1,4 +1,215 @@ -hunk ./actions/twitapilaconica.php 118 -+ common_element_start('config'); -hunk ./actions/twitapilaconica.php 135 -+ common_element_end('config'); +hunk ./actions/facebookhome.php 39 +- $facebook = $this->get_facebook(); ++ $facebook = get_facebook(); +hunk ./actions/facebookinvite.php 37 +- $facebook = $this->get_facebook(); ++ $facebook = get_facebook(); +hunk ./actions/facebooksettings.php 37 +- $facebook = $this->get_facebook(); ++ $facebook = get_facebook(); +hunk ./actions/facebooksettings.php 47 ++ $fbml .= '

Allow Identi.ca to update my Facebook status

'; ++ +hunk ./classes/Foreign_link.php 7 +-class Foreign_link extends Memcached_DataObject ++class Foreign_link extends Memcached_DataObject +hunk ./classes/Foreign_link.php 16 +- public $credentials; // varchar(255) ++ public $credentials; // varchar(255) +hunk ./classes/Foreign_link.php 32 +- static function getByUserID($user_id, $service) { ++ static function getByUserID($user_id, $service) ++ { +hunk ./classes/Foreign_link.php 43 +- return null; ++ return null; +hunk ./classes/Foreign_link.php 45 +- +- static function getByForeignID($foreign_id, $service) { ++ ++ static function getByForeignID($foreign_id, $service) ++ { +hunk ./classes/Foreign_link.php 57 +- return null; ++ return null; +hunk ./classes/Foreign_link.php 59 +- ++ +hunk ./classes/Foreign_link.php 62 +- { ++ { +hunk ./classes/Foreign_link.php 66 +- ++ +hunk ./classes/Foreign_link.php 68 +- ++ +hunk ./classes/Foreign_link.php 72 +- +- return null; ++ ++ return null; +hunk ./classes/Foreign_link.php 75 +- ++ +hunk ./classes/Foreign_link.php 80 +- ++ +hunk ./lib/facebookaction.php 22 +-require_once(INSTALLDIR.'/extlib/facebook/facebook.php'); ++require_once(INSTALLDIR.'/lib/facebookutil.php'); ++ +hunk ./lib/facebookaction.php 32 +- +- function get_facebook() +- { +- $apikey = common_config('facebook', 'apikey'); +- $secret = common_config('facebook', 'secret'); +- return new Facebook($apikey, $secret); +- } +addfile ./lib/facebookutil.php +hunk ./lib/facebookutil.php 1 ++. ++ */ ++ ++require_once(INSTALLDIR.'/extlib/facebook/facebook.php'); ++ ++// Gets all the notices from users with a Facebook link since a given ID ++function get_facebook_notices($since) ++{ ++ $qry = 'SELECT notice.* ' . ++ 'FROM notice ' . ++ 'JOIN foreign_link ' . ++ 'WHERE notice.profile_id = foreign_link.user_id ' . ++ 'AND foreign_link.service = 2'; ++ ++ // XXX: What should the limit be? ++ return Notice::getStreamDirect($qry, 0, 100, 0, 0, null, $since); ++} ++ ++function get_facebook() ++{ ++ $apikey = common_config('facebook', 'apikey'); ++ $secret = common_config('facebook', 'secret'); ++ return new Facebook($apikey, $secret); ++} +addfile ./scripts/update_facebook.php +hunk ./scripts/update_facebook.php 1 ++#!/usr/bin/env php ++. ++ */ ++ ++# Abort if called from a web server ++if (isset($_SERVER) && array_key_exists('REQUEST_METHOD', $_SERVER)) { ++ print "This script must be run from the command line\n"; ++ exit(); ++} ++ ++define('INSTALLDIR', realpath(dirname(__FILE__) . '/..')); ++define('LACONICA', true); ++ ++require_once(INSTALLDIR . '/lib/common.php'); ++require_once(INSTALLDIR . '/lib/facebookutil.php'); ++ ++// For storing the last run date-time ++$last_updated_file = "/home/zach/laconica/scripts/facebook_last_updated"; ++ ++// Lock file name ++$tmp_file = "/tmp/update_facebook.lock"; ++ ++// Make sure only one copy of the script is running at a time ++if (!($tmp_file = @fopen($tmp_file, "w"))) ++{ ++ die("Can't open lock file. Script already running?"); ++} ++ ++$facebook = get_facebook(); ++ ++$current_time = time(); ++ ++$notice = get_facebook_notices(get_last_updated()); ++ ++while($notice->fetch()) { ++ ++ $flink = Foreign_link::getByUserID($notice->profile_id, 2); ++ $fbuid = $flink->foreign_id; ++ ++ update_status($fbuid, $notice); ++ ++} ++ ++update_last_updated($current_time); ++ ++exit(0); ++ ++ ++ ++function update_status($fbuid, $notice) { ++ global $facebook; ++ ++ try { ++ ++ $result = $facebook->api_client->users_setStatus($notice->content, $fbuid, false, true); ++ ++ } catch(FacebookRestClientException $e){ ++ ++ print_r($e); ++ } ++ ++} ++ ++function get_last_updated(){ ++ global $last_updated_file, $current_time; ++ ++ $file = fopen($last_updated_file, 'r'); ++ ++ if ($file) { ++ $last = fgets($file); ++ } else { ++ print "Unable to read $last_updated_file. Using current time.\n"; ++ return $current_time; ++ } ++ ++ fclose($file); ++ ++ return $last; ++} ++ ++function update_last_updated($time){ ++ global $last_updated_file; ++ $file = fopen($last_updated_file, 'w') or die("Can't open $last_updated_file for writing!"); ++ fwrite($file, $time); ++ fclose($file); ++} diff --git a/actions/facebookhome.php b/actions/facebookhome.php index dcdb102e65..a23d1aa07f 100644 --- a/actions/facebookhome.php +++ b/actions/facebookhome.php @@ -36,7 +36,7 @@ class FacebookhomeAction extends FacebookAction $user = null; - $facebook = $this->get_facebook(); + $facebook = get_facebook(); $fbuid = $facebook->require_login(); # check to see whether there's already a Facebook link for this user diff --git a/actions/facebookinvite.php b/actions/facebookinvite.php index 48a820e44b..00efa654b1 100644 --- a/actions/facebookinvite.php +++ b/actions/facebookinvite.php @@ -34,7 +34,7 @@ class FacebookinviteAction extends FacebookAction function display() { - $facebook = $this->get_facebook(); + $facebook = get_facebook(); $fbuid = $facebook->require_login(); diff --git a/actions/facebooksettings.php b/actions/facebooksettings.php index 38542d4b6f..17812850c0 100644 --- a/actions/facebooksettings.php +++ b/actions/facebooksettings.php @@ -34,7 +34,7 @@ class FacebooksettingsAction extends FacebookAction function display() { - $facebook = $this->get_facebook(); + $facebook = get_facebook(); $fbuid = $facebook->require_login(); @@ -44,6 +44,8 @@ class FacebooksettingsAction extends FacebookAction .''; + $fbml .= '

Allow Identi.ca to update my Facebook status

'; + $this->show_header('Settings'); echo $fbml; diff --git a/classes/Foreign_link.php b/classes/Foreign_link.php index 9027ab9015..c89124d19c 100644 --- a/classes/Foreign_link.php +++ b/classes/Foreign_link.php @@ -4,7 +4,7 @@ */ require_once INSTALLDIR.'/classes/Memcached_DataObject.php'; -class Foreign_link extends Memcached_DataObject +class Foreign_link extends Memcached_DataObject { ###START_AUTOCODE /* the code below is auto generated do not remove the above tag */ @@ -13,7 +13,7 @@ class Foreign_link extends Memcached_DataObject public $user_id; // int(4) primary_key not_null public $foreign_id; // int(4) primary_key not_null public $service; // int(4) primary_key not_null - public $credentials; // varchar(255) + public $credentials; // varchar(255) public $noticesync; // tinyint(1) not_null default_1 public $friendsync; // tinyint(1) not_null default_2 public $profilesync; // tinyint(1) not_null default_1 @@ -29,7 +29,8 @@ class Foreign_link extends Memcached_DataObject // XXX: This only returns a 1->1 single obj mapping. Change? Or make // a getForeignUsers() that returns more than one? --Zach - static function getByUserID($user_id, $service) { + static function getByUserID($user_id, $service) + { $flink = new Foreign_link(); $flink->service = $service; $flink->user_id = $user_id; @@ -39,10 +40,11 @@ class Foreign_link extends Memcached_DataObject return $flink; } - return null; + return null; } - - static function getByForeignID($foreign_id, $service) { + + static function getByForeignID($foreign_id, $service) + { $flink = new Foreign_link(); $flink->service = $service; $flink->foreign_id = $foreign_id; @@ -52,28 +54,28 @@ class Foreign_link extends Memcached_DataObject return $flink; } - return null; + return null; } - + # Convenience methods function getForeignUser() - { + { $fuser = new Foreign_user(); $fuser->service = $this->service; $fuser->id = $this->foreign_id; - + $fuser->limit(1); - + if ($fuser->find(true)) { return $fuser; } - - return null; + + return null; } - + function getUser() { return User::staticGet($this->user_id); } - + } diff --git a/lib/facebookaction.php b/lib/facebookaction.php index d5b7ed0fb8..ee9f783c40 100644 --- a/lib/facebookaction.php +++ b/lib/facebookaction.php @@ -19,7 +19,8 @@ if (!defined('LACONICA')) { exit(1); } -require_once(INSTALLDIR.'/extlib/facebook/facebook.php'); +require_once(INSTALLDIR.'/lib/facebookutil.php'); + class FacebookAction extends Action { @@ -29,13 +30,6 @@ class FacebookAction extends Action parent::handle($args); } - function get_facebook() - { - $apikey = common_config('facebook', 'apikey'); - $secret = common_config('facebook', 'secret'); - return new Facebook($apikey, $secret); - } - function update_profile_box($facebook, $fbuid, $user) { diff --git a/lib/facebookutil.php b/lib/facebookutil.php new file mode 100644 index 0000000000..fc0e41e5bf --- /dev/null +++ b/lib/facebookutil.php @@ -0,0 +1,40 @@ +. + */ + +require_once(INSTALLDIR.'/extlib/facebook/facebook.php'); + +// Gets all the notices from users with a Facebook link since a given ID +function get_facebook_notices($since) +{ + $qry = 'SELECT notice.* ' . + 'FROM notice ' . + 'JOIN foreign_link ' . + 'WHERE notice.profile_id = foreign_link.user_id ' . + 'AND foreign_link.service = 2'; + + // XXX: What should the limit be? + return Notice::getStreamDirect($qry, 0, 100, 0, 0, null, $since); +} + +function get_facebook() +{ + $apikey = common_config('facebook', 'apikey'); + $secret = common_config('facebook', 'secret'); + return new Facebook($apikey, $secret); +} diff --git a/scripts/update_facebook.php b/scripts/update_facebook.php new file mode 100755 index 0000000000..d2440b163c --- /dev/null +++ b/scripts/update_facebook.php @@ -0,0 +1,102 @@ +#!/usr/bin/env php +. + */ + +# Abort if called from a web server +if (isset($_SERVER) && array_key_exists('REQUEST_METHOD', $_SERVER)) { + print "This script must be run from the command line\n"; + exit(); +} + +define('INSTALLDIR', realpath(dirname(__FILE__) . '/..')); +define('LACONICA', true); + +require_once(INSTALLDIR . '/lib/common.php'); +require_once(INSTALLDIR . '/lib/facebookutil.php'); + +// For storing the last run date-time +$last_updated_file = "/home/zach/laconica/scripts/facebook_last_updated"; + +// Lock file name +$tmp_file = "/tmp/update_facebook.lock"; + +// Make sure only one copy of the script is running at a time +if (!($tmp_file = @fopen($tmp_file, "w"))) +{ + die("Can't open lock file. Script already running?"); +} + +$facebook = get_facebook(); + +$current_time = time(); + +$notice = get_facebook_notices(get_last_updated()); + +while($notice->fetch()) { + + $flink = Foreign_link::getByUserID($notice->profile_id, 2); + $fbuid = $flink->foreign_id; + + update_status($fbuid, $notice); + +} + +update_last_updated($current_time); + +exit(0); + + + +function update_status($fbuid, $notice) { + global $facebook; + + try { + + $result = $facebook->api_client->users_setStatus($notice->content, $fbuid, false, true); + + } catch(FacebookRestClientException $e){ + + print_r($e); + } + +} + +function get_last_updated(){ + global $last_updated_file, $current_time; + + $file = fopen($last_updated_file, 'r'); + + if ($file) { + $last = fgets($file); + } else { + print "Unable to read $last_updated_file. Using current time.\n"; + return $current_time; + } + + fclose($file); + + return $last; +} + +function update_last_updated($time){ + global $last_updated_file; + $file = fopen($last_updated_file, 'w') or die("Can't open $last_updated_file for writing!"); + fwrite($file, $time); + fclose($file); +}