X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=plugins%2FFacebookBridge%2Flib%2Ffacebookclient.php;h=0469ce8aca2aa7d98c9cebc8e2d8135a09630ed7;hb=f25b6e61166be9e27cba473e3f8544ef10b0577c;hp=907c7537e9028c53e7ec0ac266fe1d69aa1ce838;hpb=3ed56730581e78911f5a98ff4a817e7fc2ab3bfc;p=quix0rs-gnu-social.git diff --git a/plugins/FacebookBridge/lib/facebookclient.php b/plugins/FacebookBridge/lib/facebookclient.php index 907c7537e9..0469ce8aca 100644 --- a/plugins/FacebookBridge/lib/facebookclient.php +++ b/plugins/FacebookBridge/lib/facebookclient.php @@ -48,17 +48,32 @@ class Facebookclient protected $notice = null; // The user's notice protected $user = null; // Sender of the notice - function __construct($notice) + /** + * + * @param Notice $notice the notice to manipulate + * @param Profile $profile local user to act as; if left empty, the notice's poster will be used. + */ + function __construct($notice, $profile=null) { $this->facebook = self::getFacebook(); - $this->notice = $notice; + if (empty($this->facebook)) { + throw new FacebookApiException( + "Could not create Facebook client! Bad application ID or secret?" + ); + } + + $this->notice = $notice; + + $profile_id = $profile ? $profile->id : $notice->profile_id; $this->flink = Foreign_link::getByUserID( - $notice->profile_id, + $profile_id, FACEBOOK_SERVICE ); - $this->user = $this->flink->getUser(); + if (!empty($this->flink)) { + $this->user = $this->flink->getUser(); + } } /* @@ -87,6 +102,22 @@ class Facebookclient $secret = common_config('facebook', 'global_secret'); } + if (empty($appId)) { + common_log( + LOG_WARNING, + "Couldn't find Facebook application ID!", + __FILE__ + ); + } + + if (empty($secret)) { + common_log( + LOG_WARNING, + "Couldn't find Facebook application ID!", + __FILE__ + ); + } + return new Facebook( array( 'appId' => $appId, @@ -113,14 +144,7 @@ class Facebookclient function isFacebookBound() { if (empty($this->flink)) { - common_log( - LOG_WARN, - sprintf( - "No Foreign_link to Facebook for the author of notice %d.", - $this->notice->id - ), - __FILE__ - ); + // User hasn't setup bridging return false; } @@ -178,16 +202,10 @@ class Facebookclient // Otherwise we most likely have an access token return $this->sendGraph(); } - - } else { - common_debug( - sprintf( - "Skipping notice %d - not bound for Facebook", - $this->notice->id, - __FILE__ - ) - ); } + + // dequeue + return true; } /* @@ -322,7 +340,8 @@ class Facebookclient function checkPermission($permission) { if (!in_array($permission, array('publish_stream', 'status_update'))) { - throw new ServerException("No such permission!"); + // TRANS: Server exception thrown when permission check fails. + throw new ServerException(_('No such permission!')); } $fbuid = $this->flink->foreign_id; @@ -379,7 +398,6 @@ class Facebookclient ); return false; - } } @@ -424,12 +442,6 @@ class Facebookclient ); return true; break; - - // @fixme: Facebook returns these 2xx permission errors sometimes - // FOR NO GOOD REASON AT ALL! It would be better to retry a few times - // over an extended period of time to instead of immediately - // disconnecting. - case 200: // Permissions error case 250: // Updating status requires the extended permission status_update $this->disconnect(); @@ -449,7 +461,7 @@ class Facebookclient ), __FILE__ ); - // @fixme: We want to rety at a later time when the throttling has expired + // @todo FIXME: We want to rety at a later time when the throttling has expired // instead of just giving up. return true; break; @@ -570,7 +582,6 @@ class Facebookclient ); if (!empty($result)) { // result will contain the item ID - // Save a mapping Notice_to_item::saveNew($this->notice->id, $result); @@ -586,7 +597,6 @@ class Facebookclient ), __FILE__ ); - } else { $msg = sprintf( @@ -728,7 +738,6 @@ class Facebookclient $result = $this->mailFacebookDisconnect(); if (!$result) { - $msg = 'Unable to send email to notify %s (%d), fbuid %d ' . 'about his/her Facebook link being removed.'; @@ -743,9 +752,7 @@ class Facebookclient __FILE__ ); } - } else { - $msg = 'Unable to send email to notify %s (%d), fbuid %d ' . 'about his/her Facebook link being removed because the ' . 'user has not set an email address.'; @@ -777,26 +784,23 @@ class Facebookclient common_switch_locale($this->user->language); + // TRANS: E-mail subject. $subject = _m('Your Facebook connection has been removed'); - $msg = <<user->nickname, $siteName ); @@ -836,22 +840,21 @@ BODY; common_switch_locale($user->language); + // TRANS: E-mail subject. %s is the StatusNet sitename. $subject = _m('Contact the %s administrator to retrieve your account'); - $msg = <<nickname, $siteName, $siteEmail @@ -978,41 +981,45 @@ BODY; $n2i = Notice_to_item::staticGet('notice_id', $this->notice->id); if (!empty($this->flink) && !empty($n2i)) { - - $result = $this->facebook->api( - array( - 'method' => 'stream.remove', - 'post_id' => $n2i->item_id, - 'uid' => $this->flink->foreign_id - ) - ); - - if (!empty($result) && result == true) { - - common_log( - LOG_INFO, - sprintf( - 'Deleted Facebook item: %s for %s (%d), fbuid %d', - $n2i->item_id, - $this->user->nickname, - $this->user->id, - $this->flink->foreign_id - ), - __FILE__ + try { + $result = $this->facebook->api( + array( + 'method' => 'stream.remove', + 'post_id' => $n2i->item_id, + 'uid' => $this->flink->foreign_id + ) ); - $n2i->delete(); - - } else { - + if (!empty($result) && result == true) { + common_log( + LOG_INFO, + sprintf( + 'Deleted Facebook item: %s for %s (%d), fbuid %d', + $n2i->item_id, + $this->user->nickname, + $this->user->id, + $this->flink->foreign_id + ), + __FILE__ + ); + + $n2i->delete(); + + } else { + throw new FaceboookApiException(var_export($result, true)); + } + } catch (FacebookApiException $e) { common_log( LOG_WARNING, sprintf( - 'Could not deleted Facebook item: %s for %s (%d), fbuid %d', + 'Could not deleted Facebook item: %s for %s (%d), ' + . 'fbuid %d - (API error: %s) item already deleted ' + . 'on Facebook? ', $n2i->item_id, $this->user->nickname, $this->user->id, - $this->flink->foreign_id + $this->flink->foreign_id, + $e ), __FILE__ ); @@ -1029,39 +1036,41 @@ BODY; $n2i = Notice_to_item::staticGet('notice_id', $this->notice->id); if (!empty($this->flink) && !empty($n2i)) { - - $result = $this->facebook->api( - array( - 'method' => 'stream.addlike', - 'post_id' => $n2i->item_id, - 'uid' => $this->flink->foreign_id - ) - ); - - if (!empty($result) && result == true) { - - common_log( - LOG_INFO, - sprintf( - 'Added like for item: %s for %s (%d), fbuid %d', - $n2i->item_id, - $this->user->nickname, - $this->user->id, - $this->flink->foreign_id - ), - __FILE__ + try { + $result = $this->facebook->api( + array( + 'method' => 'stream.addlike', + 'post_id' => $n2i->item_id, + 'uid' => $this->flink->foreign_id + ) ); - } else { - + if (!empty($result) && result == true) { + common_log( + LOG_INFO, + sprintf( + 'Added like for item: %s for %s (%d), fbuid %d', + $n2i->item_id, + $this->user->nickname, + $this->user->id, + $this->flink->foreign_id + ), + __FILE__ + ); + } else { + throw new FacebookApiException(var_export($result, true)); + } + } catch (FacebookApiException $e) { common_log( LOG_WARNING, sprintf( - 'Could not like Facebook item: %s for %s (%d), fbuid %d', + 'Could not like Facebook item: %s for %s (%d), ' + . 'fbuid %d (API error: %s)', $n2i->item_id, $this->user->nickname, $this->user->id, - $this->flink->foreign_id + $this->flink->foreign_id, + $e ), __FILE__ ); @@ -1078,44 +1087,46 @@ BODY; $n2i = Notice_to_item::staticGet('notice_id', $this->notice->id); if (!empty($this->flink) && !empty($n2i)) { - - $result = $this->facebook->api( - array( - 'method' => 'stream.removeLike', - 'post_id' => $n2i->item_id, - 'uid' => $this->flink->foreign_id - ) - ); - - if (!empty($result) && result == true) { - - common_log( - LOG_INFO, - sprintf( - 'Removed like for item: %s for %s (%d), fbuid %d', - $n2i->item_id, - $this->user->nickname, - $this->user->id, - $this->flink->foreign_id - ), - __FILE__ + try { + $result = $this->facebook->api( + array( + 'method' => 'stream.removeLike', + 'post_id' => $n2i->item_id, + 'uid' => $this->flink->foreign_id + ) ); - } else { - - common_log( + if (!empty($result) && result == true) { + common_log( + LOG_INFO, + sprintf( + 'Removed like for item: %s for %s (%d), fbuid %d', + $n2i->item_id, + $this->user->nickname, + $this->user->id, + $this->flink->foreign_id + ), + __FILE__ + ); + + } else { + throw new FacebookApiException(var_export($result, true)); + } + } catch (FacebookApiException $e) { + common_log( LOG_WARNING, sprintf( - 'Could not remove like for Facebook item: %s for %s (%d), fbuid %d', + 'Could not remove like for Facebook item: %s for %s ' + . '(%d), fbuid %d (API error: %s)', $n2i->item_id, $this->user->nickname, $this->user->id, - $this->flink->foreign_id + $this->flink->foreign_id, + $e ), __FILE__ ); } } } - }