]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/FacebookBridge/lib/facebookclient.php
File::processNew can return -1 which was not true for empty()
[quix0rs-gnu-social.git] / plugins / FacebookBridge / lib / facebookclient.php
index 907c7537e9028c53e7ec0ac266fe1d69aa1ce838..5b512dfdff6cb5ca70ea660601cb38582dab08ac 100644 (file)
@@ -23,7 +23,7 @@
  * @package   StatusNet
  * @author    Craig Andrews <candrews@integralblue.com>
  * @author    Zach Copley <zach@status.net>
- * @copyright 2009-2010 StatusNet, Inc.
+ * @copyright 2009-2011 StatusNet, Inc.
  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  * @link      http://status.net/
  */
@@ -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;
         }
 
@@ -152,7 +176,8 @@ class Facebookclient
 
         // If it's not a reply, or if the user WANTS to send @-replies,
         // then, yeah, it can go to Facebook.
-        if (!preg_match('/@[a-zA-Z0-9_]{1,15}\b/u', $this->notice->content) ||
+
+        if (empty($this->notice->reply_to) ||
             ($this->flink->noticesync & FOREIGN_NOTICE_SEND_REPLY)) {
             return true;
         }
@@ -178,16 +203,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 +341,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 +399,6 @@ class Facebookclient
             );
 
             return false;
-
         }
     }
 
@@ -424,12 +443,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 +462,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 +583,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 +598,6 @@ class Facebookclient
                 ),
                 __FILE__
             );
-
         } else {
 
             $msg = sprintf(
@@ -617,7 +628,7 @@ class Facebookclient
 
         // Facebook has a 420-char hardcoded max.
         if (mb_strlen($statustxt) > 420) {
-            $noticeUrl = common_shorten_url($this->notice->uri);
+            $noticeUrl = common_shorten_url($this->notice->getUrl());
             $urlLen = mb_strlen($noticeUrl);
             $txt = mb_substr($statustxt, 0, 420 - ($urlLen + 3)) . ' … ' . $noticeUrl;
         }
@@ -640,9 +651,10 @@ class Facebookclient
 
         foreach($attachments as $attachment)
         {
-            if($enclosure = $attachment->getEnclosure()){
+            try {
+                $enclosure = $attachment->getEnclosure();
                 $fbmedia = $this->getFacebookMedia($enclosure);
-            }else{
+            } catch (ServerException $e) {
                 $fbmedia = $this->getFacebookMedia($attachment);
             }
             if($fbmedia){
@@ -728,7 +740,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 +754,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 +786,23 @@ class Facebookclient
 
         common_switch_locale($this->user->language);
 
+        // TRANS: E-mail subject.
         $subject = _m('Your Facebook connection has been removed');
 
-        $msg = <<<BODY
-Hi %1$s,
+        // TRANS: E-mail body. %1$s is a username, %2$s is the StatusNet sitename.
+        $msg = _m("Hi %1\$s,\n\n".
+                  "We are sorry to inform you we are unable to publish your notice to\n".
+                  "Facebook, and have removed the connection between your %2\$s account and\n".
+                  "Facebook.\n\n".
+                  "This may have happened because you have removed permission for %2\$s\n".
+                  "to post on your behalf, or perhaps you have deactivated your Facebook\n".
+                  "account. You can reconnect your %2\$s account to Facebook at any time by\n".
+                  "logging in with Facebook again.\n\n".
+                  "Sincerely,\n\n".
+                  "%2\$s\n");
 
-We're sorry to inform you we are unable to publish your notice to
-Facebook, and have removed the connection between your %2$s account and
-Facebook.
-
-This may have happened because you have removed permission for %2$s
-to post on your behalf, or perhaps you have deactivated your Facebook
-account. You can reconnect your %s account to Facebook at any time by
-logging in with Facebook again.
-
-Sincerely,
-
-%2$s
-BODY;
         $body = sprintf(
-            _m($msg),
+            $msg,
             $this->user->nickname,
             $siteName
         );
@@ -836,22 +842,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 = <<<BODY
-Hi %1$s,
+        // TRANS: E-mail body. %1$s is a username,
+        // TRANS: %2$s is the StatusNet sitename, %3$s is the site contact e-mail address.
+        $msg = _m("Hi %1\$s,\n\n".
+                  "We have noticed you have deauthorized the Facebook connection for your\n".
+                  "%2\$s account.  You have not set a password for your %2\$s account yet, so\n".
+                  "you will not be able to login. If you wish to continue using your %2\$s\n".
+                  "account, please contact the site administrator (%3\$s) to set a password.\n\n".
+                  "Sincerely,\n\n".
+                  "%2\$s\n");
 
-We've noticed you have deauthorized the Facebook connection for your
-%2$s account.  You have not set a password for your %2$s account yet, so
-you will not be able to login. If you wish to continue using your %2$s
-account, please contact the site administrator (%3$s) to set a password.
-
-Sincerely,
-
-%2$s
-BODY;
         $body = sprintf(
-            _m($msg),
+            $msg,
             $user->nickname,
             $siteName,
             $siteEmail
@@ -893,7 +898,7 @@ BODY;
      */
     static function facebookStatusId($notice)
     {
-        $n2i = Notice_to_item::staticGet('notice_id', $notice->id);
+        $n2i = Notice_to_item::getKV('notice_id', $notice->id);
 
         if (empty($n2i)) {
             return null;
@@ -913,7 +918,7 @@ BODY;
     static function addFacebookUser($fbuser)
     {
         // remove any existing, possibly outdated, record
-        $luser = Foreign_user::getForeignUser($fbuser['id'], FACEBOOK_SERVICE);
+        $luser = Foreign_user::getForeignUser($fbuser->id, FACEBOOK_SERVICE);
 
         if (!empty($luser)) {
 
@@ -924,8 +929,8 @@ BODY;
                     LOG_INFO,
                     sprintf(
                         'Removed old Facebook user: %s, fbuid %d',
-                        $fbuid['name'],
-                        $fbuid['id']
+                        $fbuid->name,
+                        $fbuid->id
                     ),
                     __FILE__
                 );
@@ -934,9 +939,9 @@ BODY;
 
         $fuser = new Foreign_user();
 
-        $fuser->nickname = $fbuser['name'];
-        $fuser->uri      = $fbuser['link'];
-        $fuser->id       = $fbuser['id'];
+        $fuser->nickname = $fbuser->username;
+        $fuser->uri      = $fbuser->link;
+        $fuser->id       = $fbuser->id;
         $fuser->service  = FACEBOOK_SERVICE;
         $fuser->created  = common_sql_now();
 
@@ -947,8 +952,8 @@ BODY;
                 LOG_WARNING,
                     sprintf(
                         'Failed to add new Facebook user: %s, fbuid %d',
-                        $fbuser['name'],
-                        $fbuser['id']
+                        $fbuser->username,
+                        $fbuser->id
                     ),
                     __FILE__
             );
@@ -959,8 +964,8 @@ BODY;
                 LOG_INFO,
                 sprintf(
                     'Added new Facebook user: %s, fbuid %d',
-                    $fbuser['name'],
-                    $fbuser['id']
+                    $fbuser->name,
+                    $fbuser->id
                 ),
                 __FILE__
             );
@@ -975,44 +980,48 @@ BODY;
      */
     function streamRemove()
     {
-        $n2i = Notice_to_item::staticGet('notice_id', $this->notice->id);
+        $n2i = Notice_to_item::getKV('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__
                 );
@@ -1026,42 +1035,44 @@ BODY;
      */
     function like()
     {
-        $n2i = Notice_to_item::staticGet('notice_id', $this->notice->id);
+        $n2i = Notice_to_item::getKV('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__
                 );
@@ -1075,47 +1086,49 @@ BODY;
      */
     function unLike()
     {
-        $n2i = Notice_to_item::staticGet('notice_id', $this->notice->id);
+        $n2i = Notice_to_item::getKV('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__
                 );
             }
         }
     }
-
 }