forked from friendica/friendica-addons
Use batch requests when syncing friends; this reduces the time for synchronization a lot
This commit is contained in:
parent
8a3643e8db
commit
1df6f59127
79
facebook/facebook.php
Executable file → Normal file
79
facebook/facebook.php
Executable file → Normal file
|
@ -46,7 +46,7 @@
|
|||
* in a future release.
|
||||
*/
|
||||
|
||||
/** TODO
|
||||
/** TODO
|
||||
* - Implement a method for the administrator to delete all configuration data the plugin has created,
|
||||
* e.g. the app_access_token
|
||||
*/
|
||||
|
@ -197,7 +197,6 @@ function facebook_init(&$a) {
|
|||
$auth_code = (x($_GET, 'code') ? $_GET['code'] : '');
|
||||
$error = (x($_GET, 'error_description') ? $_GET['error_description'] : '');
|
||||
|
||||
|
||||
if($error)
|
||||
logger('facebook_init: Error: ' . $error);
|
||||
|
||||
|
@ -243,7 +242,9 @@ function fb_get_self($uid) {
|
|||
}
|
||||
}
|
||||
|
||||
function fb_get_friends_sync_new($uid, $access_token, $person) {
|
||||
function fb_get_friends_sync_new($uid, $access_token, $persons) {
|
||||
$persons_todo = array();
|
||||
foreach ($persons as $person) {
|
||||
$link = 'http://facebook.com/profile.php?id=' . $person->id;
|
||||
|
||||
$r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `url` = '%s' LIMIT 1",
|
||||
|
@ -253,30 +254,25 @@ function fb_get_friends_sync_new($uid, $access_token, $person) {
|
|||
|
||||
if (count($r) == 0) {
|
||||
logger('fb_get_friends: new contact found: ' . $link, LOGGER_DEBUG);
|
||||
$persons_todo[] = $person;
|
||||
}
|
||||
|
||||
fb_get_friends_sync_full($uid, $access_token, $person);
|
||||
if (count($persons_todo) > 0) fb_get_friends_sync_full($uid, $access_token, $persons_todo);
|
||||
}
|
||||
}
|
||||
|
||||
function fb_get_friends_sync_full($uid, $access_token, $person) {
|
||||
$s = fetch_url('https://graph.facebook.com/' . $person->id . '?access_token=' . $access_token);
|
||||
if($s) {
|
||||
$jp = json_decode($s);
|
||||
logger('fb_get_friends: info: ' . print_r($jp,true), LOGGER_DATA);
|
||||
|
||||
// always use numeric link for consistency
|
||||
|
||||
$jp->link = 'http://facebook.com/profile.php?id=' . $person->id;
|
||||
function fb_get_friends_sync_parsecontact($uid, $contact) {
|
||||
$contact->link = 'http://facebook.com/profile.php?id=' . $contact->id;
|
||||
|
||||
// If its a page then set the first name from the username
|
||||
if (!$jp->first_name and $jp->username)
|
||||
$jp->first_name = $jp->username;
|
||||
if (!$contact->first_name and $contact->username)
|
||||
$contact->first_name = $contact->username;
|
||||
|
||||
// check if we already have a contact
|
||||
|
||||
$r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `url` = '%s' LIMIT 1",
|
||||
intval($uid),
|
||||
dbesc($jp->link)
|
||||
dbesc($contact->link)
|
||||
);
|
||||
|
||||
if(count($r)) {
|
||||
|
@ -286,7 +282,7 @@ function fb_get_friends_sync_full($uid, $access_token, $person) {
|
|||
if((! $r[0]['photo']) || (! $r[0]['thumb']) || (! $r[0]['micro'])) {
|
||||
require_once("Photo.php");
|
||||
|
||||
$photos = import_profile_photo('https://graph.facebook.com/' . $jp->id . '/picture', $uid, $r[0]['id']);
|
||||
$photos = import_profile_photo('https://graph.facebook.com/' . $contact->id . '/picture', $uid, $r[0]['id']);
|
||||
|
||||
$r = q("UPDATE `contact` SET `photo` = '%s',
|
||||
`thumb` = '%s',
|
||||
|
@ -316,15 +312,15 @@ function fb_get_friends_sync_full($uid, $access_token, $person) {
|
|||
VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, %d, 0, 0, 0 ) ",
|
||||
intval($uid),
|
||||
dbesc(datetime_convert()),
|
||||
dbesc($jp->link),
|
||||
dbesc(normalise_link($jp->link)),
|
||||
dbesc($contact->link),
|
||||
dbesc(normalise_link($contact->link)),
|
||||
dbesc(''),
|
||||
dbesc(''),
|
||||
dbesc($jp->id),
|
||||
dbesc('facebook ' . $jp->id),
|
||||
dbesc($jp->name),
|
||||
dbesc(($jp->nickname) ? $jp->nickname : strtolower($jp->first_name)),
|
||||
dbesc('https://graph.facebook.com/' . $jp->id . '/picture'),
|
||||
dbesc($contact->id),
|
||||
dbesc('facebook ' . $contact->id),
|
||||
dbesc($contact->name),
|
||||
dbesc(($contact->nickname) ? $contact->nickname : strtolower($contact->first_name)),
|
||||
dbesc('https://graph.facebook.com/' . $contact->id . '/picture'),
|
||||
dbesc(NETWORK_FACEBOOK),
|
||||
intval(CONTACT_IS_FRIEND),
|
||||
intval(1),
|
||||
|
@ -333,7 +329,7 @@ function fb_get_friends_sync_full($uid, $access_token, $person) {
|
|||
}
|
||||
|
||||
$r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
|
||||
dbesc($jp->link),
|
||||
dbesc($contact->link),
|
||||
intval($uid)
|
||||
);
|
||||
|
||||
|
@ -364,7 +360,23 @@ function fb_get_friends_sync_full($uid, $access_token, $person) {
|
|||
dbesc(datetime_convert()),
|
||||
intval($contact_id)
|
||||
);
|
||||
}
|
||||
|
||||
function fb_get_friends_sync_full($uid, $access_token, $persons) {
|
||||
if (count($persons) == 0) return;
|
||||
$nums = Ceil(count($persons) / 50);
|
||||
for ($i = 0; $i < $nums; $i++) {
|
||||
$batch_request = array();
|
||||
for ($j = $i * 50; $j < ($i+1) * 50 && $j < count($persons); $j++) $batch_request[] = array('method'=>'GET', 'relative_url'=>$persons[$j]->id);
|
||||
$s = post_url('https://graph.facebook.com/', array('access_token' => $access_token, 'batch' => json_encode($batch_request)));
|
||||
if($s) {
|
||||
$results = json_decode($s);
|
||||
logger('fb_get_friends: info: ' . print_r($results,true), LOGGER_DATA);
|
||||
foreach ($results as $contact) {
|
||||
if ($contact->code != 200) logger('fb_get_friends: not found: ' . print_r($contact,true), LOGGER_DEBUG);
|
||||
else fb_get_friends_sync_parsecontact($uid, json_decode($contact->body));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -393,11 +405,14 @@ function fb_get_friends($uid, $fullsync = true) {
|
|||
logger('facebook: fb_get_friends: json: ' . print_r($j,true), LOGGER_DATA);
|
||||
if(! $j->data)
|
||||
return;
|
||||
foreach($j->data as $person)
|
||||
|
||||
$persons_todo = array();
|
||||
foreach($j->data as $person) $persons_todo[] = $person;
|
||||
|
||||
if ($fullsync)
|
||||
fb_get_friends_sync_full($uid, $access_token, $person);
|
||||
fb_get_friends_sync_full($uid, $access_token, $persons_todo);
|
||||
else
|
||||
fb_get_friends_sync_new($uid, $access_token, $person);
|
||||
fb_get_friends_sync_new($uid, $access_token, $persons_todo);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -477,7 +492,7 @@ function facebook_content(&$a) {
|
|||
if (get_pconfig(local_user(),'facebook','post')) {
|
||||
$access_token = get_pconfig(local_user(),'facebook','access_token');
|
||||
if ($access_token) {
|
||||
$private_wall = intval(get_pconfig($uid,'facebook','private_wall'));
|
||||
$private_wall = intval(get_pconfig(local_user(),'facebook','private_wall'));
|
||||
$s = fetch_url('https://graph.facebook.com/me/feed?access_token=' . $access_token);
|
||||
if($s) {
|
||||
$j = json_decode($s);
|
||||
|
@ -1179,7 +1194,7 @@ function fb_queue_hook(&$a,&$b) {
|
|||
}
|
||||
|
||||
function fb_get_timeline($access_token, &$since) {
|
||||
|
||||
$entries = new stdClass();
|
||||
$entries->data = array();
|
||||
$newest = 0;
|
||||
|
||||
|
@ -1759,7 +1774,7 @@ function facebook_check_realtime_active() {
|
|||
// DELETE-request to $url
|
||||
|
||||
if(! function_exists('facebook_delete_url')) {
|
||||
function facebook_delete_url($url,$headers = null, &$redirects = 0, $timeout = 0) {
|
||||
function facebook_delete_url($url,$headers = null, &$redirects = 0, $timeout = 0) {
|
||||
$a = get_app();
|
||||
$ch = curl_init($url);
|
||||
if(($redirects > 8) || (! $ch))
|
||||
|
@ -1840,4 +1855,4 @@ function facebook_delete_url($url,$headers = null, &$redirects = 0, $timeout = 0
|
|||
|
||||
curl_close($ch);
|
||||
return($body);
|
||||
}}
|
||||
}}
|
||||
|
|
Loading…
Reference in a new issue