summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZach Copley <zach@controlyourself.ca>2009-01-04 20:04:07 -0500
committerZach Copley <zach@controlyourself.ca>2009-01-04 20:04:07 -0500
commitfd6f9b9d7622f280f92810770deb217eb7301c14 (patch)
tree63cdbd1aeff69b33a9740360d97cdc4ffda404dd
parentd57c06b4171fbed9f059c53d66f6b313d9303d6d (diff)
trac750 Automatically update linked Facebook users' statuses
darcs-hash:20090105010407-7b5ce-559da13720b18e3c570e730326f5e5ef2b2dc1ab.gz
-rw-r--r--_darcs/inventory4
-rw-r--r--_darcs/patches/20090105010407-7b5ce-559da13720b18e3c570e730326f5e5ef2b2dc1ab.gzbin0 -> 2082 bytes
-rw-r--r--_darcs/pristine/actions/facebookhome.php2
-rw-r--r--_darcs/pristine/actions/facebookinvite.php2
-rw-r--r--_darcs/pristine/actions/facebooksettings.php4
-rw-r--r--_darcs/pristine/classes/Foreign_link.php32
-rw-r--r--_darcs/pristine/lib/facebookaction.php10
-rw-r--r--_darcs/pristine/lib/facebookutil.php40
-rw-r--r--_darcs/pristine/scripts/update_facebook.php102
-rw-r--r--_darcs/tentative_pristine219
-rw-r--r--actions/facebookhome.php2
-rw-r--r--actions/facebookinvite.php2
-rw-r--r--actions/facebooksettings.php4
-rw-r--r--classes/Foreign_link.php32
-rw-r--r--lib/facebookaction.php10
-rw-r--r--lib/facebookutil.php40
-rwxr-xr-xscripts/update_facebook.php102
17 files changed, 550 insertions, 57 deletions
diff --git a/_darcs/inventory b/_darcs/inventory
index e7100599a..8c94c559c 100644
--- a/_darcs/inventory
+++ b/_darcs/inventory
@@ -112,4 +112,6 @@ Evan Prodromou <evan@prodromou.name>**20081230211444]
[better serialization of arrays and booleans in config output
Evan Prodromou <evan@prodromou.name>**20081230211957]
[wrapper element for config.xml
-Evan Prodromou <evan@prodromou.name>**20081230212202] \ No newline at end of file
+Evan Prodromou <evan@prodromou.name>**20081230212202]
+[trac750 Automatically update linked Facebook users' statuses
+Zach Copley <zach@controlyourself.ca>**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 000000000..5eb98e9e0
--- /dev/null
+++ b/_darcs/patches/20090105010407-7b5ce-559da13720b18e3c570e730326f5e5ef2b2dc1ab.gz
Binary files differ
diff --git a/_darcs/pristine/actions/facebookhome.php b/_darcs/pristine/actions/facebookhome.php
index dcdb102e6..a23d1aa07 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 48a820e44..00efa654b 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 38542d4b6..17812850c 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
.'</fb:if-section-not-added>';
+ $fbml .= '<fb:prompt-permission perms="status_update"><h2>Allow Identi.ca to update my Facebook status</h2></fb:prompt-permission>';
+
$this->show_header('Settings');
echo $fbml;
diff --git a/_darcs/pristine/classes/Foreign_link.php b/_darcs/pristine/classes/Foreign_link.php
index 9027ab901..c89124d19 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 d5b7ed0fb..ee9f783c4 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 000000000..fc0e41e5b
--- /dev/null
+++ b/_darcs/pristine/lib/facebookutil.php
@@ -0,0 +1,40 @@
+<?php
+/*
+ * Laconica - a distributed open-source microblogging tool
+ * Copyright (C) 2008, Controlez-Vous, Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+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 000000000..d2440b163
--- /dev/null
+++ b/_darcs/pristine/scripts/update_facebook.php
@@ -0,0 +1,102 @@
+#!/usr/bin/env php
+<?php
+/*
+ * Laconica - a distributed open-source microblogging tool
+ * Copyright (C) 2008, Controlez-Vous, Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+# 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 6a29f612c..15afe3e09 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 .= '<fb:prompt-permission perms="status_update"><h2>Allow Identi.ca to update my Facebook status</h2></fb:prompt-permission>';
++
+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
++<?php
++/*
++ * Laconica - a distributed open-source microblogging tool
++ * Copyright (C) 2008, Controlez-Vous, Inc.
++ *
++ * This program is free software: you can redistribute it and/or modify
++ * it under the terms of the GNU Affero General Public License as published by
++ * the Free Software Foundation, either version 3 of the License, or
++ * (at your option) any later version.
++ *
++ * This program is distributed in the hope that it will be useful,
++ * but WITHOUT ANY WARRANTY; without even the implied warranty of
++ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
++ * GNU Affero General Public License for more details.
++ *
++ * You should have received a copy of the GNU Affero General Public License
++ * along with this program. If not, see <http://www.gnu.org/licenses/>.
++ */
++
++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
++<?php
++/*
++ * Laconica - a distributed open-source microblogging tool
++ * Copyright (C) 2008, Controlez-Vous, Inc.
++ *
++ * This program is free software: you can redistribute it and/or modify
++ * it under the terms of the GNU Affero General Public License as published by
++ * the Free Software Foundation, either version 3 of the License, or
++ * (at your option) any later version.
++ *
++ * This program is distributed in the hope that it will be useful,
++ * but WITHOUT ANY WARRANTY; without even the implied warranty of
++ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
++ * GNU Affero General Public License for more details.
++ *
++ * You should have received a copy of the GNU Affero General Public License
++ * along with this program. If not, see <http://www.gnu.org/licenses/>.
++ */
++
++# 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 dcdb102e6..a23d1aa07 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 48a820e44..00efa654b 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 38542d4b6..17812850c 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
.'</fb:if-section-not-added>';
+ $fbml .= '<fb:prompt-permission perms="status_update"><h2>Allow Identi.ca to update my Facebook status</h2></fb:prompt-permission>';
+
$this->show_header('Settings');
echo $fbml;
diff --git a/classes/Foreign_link.php b/classes/Foreign_link.php
index 9027ab901..c89124d19 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 d5b7ed0fb..ee9f783c4 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 000000000..fc0e41e5b
--- /dev/null
+++ b/lib/facebookutil.php
@@ -0,0 +1,40 @@
+<?php
+/*
+ * Laconica - a distributed open-source microblogging tool
+ * Copyright (C) 2008, Controlez-Vous, Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+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 000000000..d2440b163
--- /dev/null
+++ b/scripts/update_facebook.php
@@ -0,0 +1,102 @@
+#!/usr/bin/env php
+<?php
+/*
+ * Laconica - a distributed open-source microblogging tool
+ * Copyright (C) 2008, Controlez-Vous, Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+# 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);
+}