trac750 FB script to update statuses working again with new uiredesign stuff

This commit is contained in:
Zach Copley 2009-01-23 04:23:44 +00:00
parent e59b8c8758
commit 8dad87ea95
3 changed files with 64 additions and 16 deletions

View File

@ -46,13 +46,37 @@ class FacebookAction extends Action
var $app_uri = null; var $app_uri = null;
var $app_name = null; var $app_name = null;
/**
* Constructor
*
* Just wraps the HTMLOutputter constructor.
*
* @param string $output URI to output to, default = stdout
* @param boolean $indent Whether to indent output, default true
*
* @see XMLOutputter::__construct
* @see HTMLOutputter::__construct
*/
function __construct($output='php://output', $indent=true, $facebook=null, $flink=null)
{
parent::__construct($output, $indent);
$this->facebook = $facebook;
$this->flink = $flink;
if ($this->flink) {
$this->fbuid = $flink->foreign_id;
$this->user = $flink->getUser();
}
}
function prepare($argarray) function prepare($argarray)
{ {
parent::prepare($argarray); parent::prepare($argarray);
common_debug("Facebookaction::prepare"); common_debug("Facebookaction::prepare");
$this->facebook = get_facebook(); $this->facebook = getFacebook();
$this->fbuid = $this->facebook->require_login(); $this->fbuid = $this->facebook->require_login();
$this->action = $this->trimmed('action'); $this->action = $this->trimmed('action');

View File

@ -18,6 +18,7 @@
*/ */
require_once INSTALLDIR.'/extlib/facebook/facebook.php'; require_once INSTALLDIR.'/extlib/facebook/facebook.php';
require_once INSTALLDIR.'/lib/facebookaction.php';
require_once INSTALLDIR.'/lib/noticelist.php'; require_once INSTALLDIR.'/lib/noticelist.php';
define("FACEBOOK_SERVICE", 2); // Facebook is foreign_service ID 2 define("FACEBOOK_SERVICE", 2); // Facebook is foreign_service ID 2
@ -25,7 +26,7 @@ define("FACEBOOK_NOTICE_PREFIX", 1);
define("FACEBOOK_PROMPTED_UPDATE_PREF", 2); define("FACEBOOK_PROMPTED_UPDATE_PREF", 2);
// Gets all the notices from users with a Facebook link since a given ID // Gets all the notices from users with a Facebook link since a given ID
function get_facebook_notices($since) function getFacebookNotices($since)
{ {
$qry = 'SELECT notice.* ' . $qry = 'SELECT notice.* ' .
'FROM notice ' . 'FROM notice ' .
@ -37,7 +38,7 @@ function get_facebook_notices($since)
return Notice::getStreamDirect($qry, 0, 100, 0, 0, null, $since); return Notice::getStreamDirect($qry, 0, 100, 0, 0, null, $since);
} }
function get_facebook() function getFacebook()
{ {
$apikey = common_config('facebook', 'apikey'); $apikey = common_config('facebook', 'apikey');
$secret = common_config('facebook', 'secret'); $secret = common_config('facebook', 'secret');
@ -76,3 +77,8 @@ function getFacebookJS() {
return $jsurl; return $jsurl;
} }
function updateProfileBox($facebook, $flink, $notice) {
$fbaction = new FacebookAction($output='php://output', $indent=true, $facebook, $flink);
$fbaction->updateProfileBox($notice);
}

View File

@ -27,8 +27,8 @@ if (isset($_SERVER) && array_key_exists('REQUEST_METHOD', $_SERVER)) {
define('INSTALLDIR', realpath(dirname(__FILE__) . '/..')); define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
define('LACONICA', true); define('LACONICA', true);
require_once(INSTALLDIR . '/lib/common.php'); require_once INSTALLDIR . '/lib/common.php';
require_once(INSTALLDIR . '/lib/facebookutil.php'); require_once INSTALLDIR . '/lib/facebookutil.php';
// For storing the last run date-time // For storing the last run date-time
$last_updated_file = INSTALLDIR . '/scripts/facebook_last_updated'; $last_updated_file = INSTALLDIR . '/scripts/facebook_last_updated';
@ -42,11 +42,11 @@ if (!($tmp_file = @fopen($tmp_file, "w")))
die("Can't open lock file. Script already running?"); die("Can't open lock file. Script already running?");
} }
$facebook = get_facebook(); $facebook = getFacebook();
$current_time = time(); $current_time = time();
$notice = get_facebook_notices(get_last_updated()); $notice = getFacebookNotices(getLastUpdated());
print date('r', $current_time) . " Looking for notices to send to Facebook...\n"; print date('r', $current_time) . " Looking for notices to send to Facebook...\n";
@ -57,11 +57,14 @@ while($notice->fetch()) {
$flink = Foreign_link::getByUserID($notice->profile_id, FACEBOOK_SERVICE); $flink = Foreign_link::getByUserID($notice->profile_id, FACEBOOK_SERVICE);
$user = $flink->getUser(); $user = $flink->getUser();
$fbuid = $flink->foreign_id; $fbuid = $flink->foreign_id;
if (!userCanUpdate($fbuid)) {
continue;
}
$prefix = $facebook->api_client->data_getUserPreference(1, $fbuid); $prefix = $facebook->api_client->data_getUserPreference(FACEBOOK_NOTICE_PREFIX, $fbuid);
$content = "$prefix $notice->content"; $content = "$prefix $notice->content";
if (($flink->noticesync & FOREIGN_NOTICE_SEND) == FOREIGN_NOTICE_SEND) { if (($flink->noticesync & FOREIGN_NOTICE_SEND) == FOREIGN_NOTICE_SEND) {
// If it's not a reply, or if the user WANTS to send replies... // If it's not a reply, or if the user WANTS to send replies...
@ -70,23 +73,38 @@ while($notice->fetch()) {
// Avoid a Loop // Avoid a Loop
if ($notice->source != 'Facebook') { if ($notice->source != 'Facebook') {
update_status($fbuid, $content); updateStatus($fbuid, $content);
update_profile_box($facebook, $fbuid, $user, $notice); updateProfileBox($facebook, $flink, $notice);
$cnt++; $cnt++;
} }
} }
} }
} }
update_last_updated($current_time); updateLastUpdated($current_time);
print "Sent $cnt notices to Facebook.\n"; print "Sent $cnt notices to Facebook.\n";
exit(0); exit(0);
function userCanUpdate($fbuid) {
global $facebook;
function update_status($fbuid, $content) { $result = false;
try {
$result = $facebook->api_client->users_hasAppPermission('status_update', $fbuid);
} catch(FacebookRestClientException $e){
print_r($e);
}
return $result;
}
function updateStatus($fbuid, $content) {
global $facebook; global $facebook;
try { try {
@ -96,7 +114,7 @@ function update_status($fbuid, $content) {
} }
} }
function get_last_updated(){ function getLastUpdated(){
global $last_updated_file, $current_time; global $last_updated_file, $current_time;
$file = fopen($last_updated_file, 'r'); $file = fopen($last_updated_file, 'r');
@ -113,7 +131,7 @@ function get_last_updated(){
return $last; return $last;
} }
function update_last_updated($time){ function updateLastUpdated($time){
global $last_updated_file; global $last_updated_file;
$file = fopen($last_updated_file, 'w') or die("Can't open $last_updated_file for writing!"); $file = fopen($last_updated_file, 'w') or die("Can't open $last_updated_file for writing!");
fwrite($file, $time); fwrite($file, $time);