]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/TwitterBridge/TwitterBridgePlugin.php
Merge remote-tracking branch 'upstream/master' into social-master
[quix0rs-gnu-social.git] / plugins / TwitterBridge / TwitterBridgePlugin.php
index 8b90e7702900a893cd1fb53f4c8f81c8321b80e4..644cd8af75fbc7d22d39c18250d1337a46404736 100644 (file)
@@ -26,9 +26,7 @@
  * @link      http://status.net/
  */
 
-if (!defined('STATUSNET')) {
-    exit(1);
-}
+if (!defined('GNUSOCIAL')) { exit(1); }
 
 require_once __DIR__ . '/twitter.php';
 
@@ -238,7 +236,7 @@ class TwitterBridgePlugin extends Plugin
      *
      * @return boolean hook return
      */
-    function onEndInitializeQueueManager($manager)
+    function onEndInitializeQueueManager(QueueManager $manager)
     {
         if (self::hasKeys()) {
             // Outgoing notices -> twitter
@@ -270,7 +268,7 @@ class TwitterBridgePlugin extends Plugin
      * @return boolean hook value
      */
 
-    function onEndAdminPanelNav($nav)
+    function onEndAdminPanelNav(Menu $nav)
     {
         if (AdminPanelAction::canAdmin('twitter')) {
 
@@ -387,12 +385,11 @@ class TwitterBridgePlugin extends Plugin
     {
         $n2s = Notice_to_status::getKV('notice_id', $notice->id);
 
-        if (!empty($n2s)) {
-
-            $flink = Foreign_link::getByUserID($notice->profile_id,
-                                               TWITTER_SERVICE); // twitter service
+        if ($n2s instanceof Notice_to_status) {
 
-            if (empty($flink)) {
+            try {
+                $flink = Foreign_link::getByUserID($notice->profile_id, TWITTER_SERVICE); // twitter service
+            } catch (NoResultException $e) {
                 return true;
             }
 
@@ -424,15 +421,14 @@ class TwitterBridgePlugin extends Plugin
      */
     function onEndFavorNotice(Profile $profile, Notice $notice)
     {
-        $flink = Foreign_link::getByUserID($profile->id,
-                                           TWITTER_SERVICE); // twitter service
-
-        if (empty($flink)) {
+        try {
+            $flink = Foreign_link::getByUserID($profile->getID(), TWITTER_SERVICE); // twitter service
+        } catch (NoResultException $e) {
             return true;
         }
 
         if (!TwitterOAuthClient::isPackedToken($flink->credentials)) {
-            $this->log(LOG_INFO, "Skipping fave processing for {$profile->id} since link is not OAuth.");
+            $this->log(LOG_INFO, "Skipping fave processing for {$profile->getID()} since link is not OAuth.");
             return true;
         }
 
@@ -464,10 +460,9 @@ class TwitterBridgePlugin extends Plugin
      */
     function onEndDisfavorNotice(Profile $profile, Notice $notice)
     {
-        $flink = Foreign_link::getByUserID($profile->id,
-                                           TWITTER_SERVICE); // twitter service
-
-        if (empty($flink)) {
+        try {
+            $flink = Foreign_link::getByUserID($profile->getID(), TWITTER_SERVICE); // twitter service
+        } catch (NoResultException $e) {
             return true;
         }
 
@@ -516,16 +511,15 @@ class TwitterBridgePlugin extends Plugin
     {
         $fuser = null;
 
-        $flink = Foreign_link::getByUserID($profile->id, TWITTER_SERVICE);
-
-        if (!empty($flink)) {
+        try {
+            $flink = Foreign_link::getByUserID($profile->id, TWITTER_SERVICE);
             $fuser = $flink->getForeignUser();
 
-            if (!empty($fuser)) {
-                $links[] = array("href" => $fuser->uri,
-                                 "text" => sprintf(_("@%s on Twitter"), $fuser->nickname),
-                                 "image" => $this->path("icons/twitter-bird-white-on-blue.png"));
-            }
+            $links[] = array("href" => $fuser->uri,
+                             "text" => sprintf(_("@%s on Twitter"), $fuser->nickname),
+                             "image" => $this->path("icons/twitter-bird-white-on-blue.png"));
+        } catch (NoResultException $e) {
+            // no foreign link and/or user for Twitter on this profile ID
         }
 
         return true;
@@ -533,6 +527,23 @@ class TwitterBridgePlugin extends Plugin
 
     public function onEndShowHeadElements(Action $action)
     {
+        if($action instanceof ShowNoticeAction) { // Showing a notice
+            $notice = Notice::getKV('id', $action->arg('notice'));
+
+            try {
+                $flink = Foreign_link::getByUserID($notice->profile_id, TWITTER_SERVICE);
+                $fuser = Foreign_user::getForeignUser($flink->foreign_id, TWITTER_SERVICE);
+            } catch (NoResultException $e) {
+                return true;
+            }
+
+            $statusId = twitter_status_id($notice);
+            if($notice instanceof Notice && $notice->isLocal() && $statusId) {
+                $tweetUrl = 'https://twitter.com/' . $fuser->nickname . '/status/' . $statusId;
+                $action->element('link', array('rel' => 'syndication', 'href' => $tweetUrl));
+            }
+        }
+
         if (!($action instanceof AttachmentAction)) {
             return true;
         }
@@ -569,16 +580,17 @@ class TwitterBridgePlugin extends Plugin
                 if( count($noticeArray) != 1 ) { break; }
                 $post = $noticeArray[0];
 
-                $flink = Foreign_link::getByUserID($post->profile_id, TWITTER_SERVICE);
-                if( $flink ) { // Our local user has registered Twitter Gateway
+                try {
+                    $flink = Foreign_link::getByUserID($post->profile_id, TWITTER_SERVICE);
                     $fuser = Foreign_user::getForeignUser($flink->foreign_id, TWITTER_SERVICE);
-                    if( $fuser ) { // Got nickname for local user's Twitter account
-                        $action->element('meta', array('name'    => 'twitter:creator',
-                                                     'content' => '@'.$fuser->nickname));
-                    }
+                    $action->element('meta', array('name'    => 'twitter:creator',
+                                                   'content' => '@'.$fuser->nickname));
+                } catch (NoResultException $e) {
+                    // no foreign link and/or user for Twitter on this profile ID
                 }
                 break;
-            default: break;
+            default:
+                break;
         }
 
         return true;