]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/TwitterBridge/TwitterBridgePlugin.php
[ROUTES] Allow accept-header specification during router creation
[quix0rs-gnu-social.git] / plugins / TwitterBridge / TwitterBridgePlugin.php
index 03d4374547c61d62e658781771582fa4c684cb0e..79d705c2555cffc1cc9cff82a3881f4d812886b8 100644 (file)
  * @link      http://status.net/
  */
 
-if (!defined('STATUSNET')) {
-    exit(1);
-}
+if (!defined('GNUSOCIAL')) { exit(1); }
 
-require_once INSTALLDIR . '/plugins/TwitterBridge/twitter.php';
+require_once __DIR__ . '/twitter.php';
 
 /**
  * Plugin for sending and importing Twitter statuses
  *
  * This class allows users to link their Twitter accounts
  *
+ * Depends on Favorite plugin.
+ *
  * @category Plugin
  * @package  StatusNet
  * @author   Zach Copley <zach@status.net>
@@ -47,7 +47,7 @@ require_once INSTALLDIR . '/plugins/TwitterBridge/twitter.php';
  */
 class TwitterBridgePlugin extends Plugin
 {
-    const VERSION = GNUSOCIAL_VERSION;
+    const PLUGIN_VERSION = '2.0.0';
     public $adminImportControl = false; // Should the 'import' checkbox be exposed in the admin panel?
 
     /**
@@ -105,29 +105,23 @@ class TwitterBridgePlugin extends Plugin
      *
      * Hook for RouterInitialized event.
      *
-     * @param Net_URL_Mapper $m path-to-action mapper
+     * @param URLMapper $m path-to-action mapper
      *
      * @return boolean hook return
      */
-    function onRouterInitialized($m)
+    public function onRouterInitialized(URLMapper $m)
     {
-        $m->connect('panel/twitter', array('action' => 'twitteradminpanel'));
+        $m->connect('panel/twitter', ['action' => 'twitteradminpanel']);
 
         if (self::hasKeys()) {
-            $m->connect(
-                'twitter/authorization',
-                array('action' => 'twitterauthorization')
-            );
-            $m->connect(
-                'settings/twitter', array(
-                    'action' => 'twittersettings'
-                    )
-                );
+            $m->connect('twitter/authorization',
+                        ['action' => 'twitterauthorization']);
+            $m->connect('settings/twitter',
+                        ['action' => 'twittersettings']);
+
             if (common_config('twitter', 'signin')) {
-                $m->connect(
-                    'main/twitterlogin',
-                    array('action' => 'twitterlogin')
-                );
+                $m->connect('main/twitterlogin',
+                            ['action' => 'twitterlogin']);
             }
         }
 
@@ -295,13 +289,13 @@ class TwitterBridgePlugin extends Plugin
      *
      * @return boolean hook value
      */
-    function onPluginVersion(&$versions)
+    function onPluginVersion(array &$versions)
     {
         $versions[] = array(
             'name' => 'TwitterBridge',
-            'version' => self::VERSION,
+            'version' => self::PLUGIN_VERSION,
             'author' => 'Zach Copley, Julien C, Jean Baptiste Favre',
-            'homepage' => 'http://status.net/wiki/Plugin:TwitterBridge',
+            'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/TwitterBridge',
             // TRANS: Plugin description.
             'rawdescription' => _m('The Twitter "bridge" plugin allows integration ' .
                 'of a StatusNet instance with ' .
@@ -323,31 +317,6 @@ class TwitterBridgePlugin extends Plugin
         return (bool)$this->adminImportControl;
     }
 
-    /**
-     * When the site is set to ssl=sometimes mode, we should make sure our
-     * various auth-related pages are on SSL to keep things looking happy.
-     * Although we're not submitting passwords directly, we do link out to
-     * an authentication source and it's a lot happier if we've got some
-     * protection against MitM.
-     *
-     * @param string $action name
-     * @param boolean $ssl outval to force SSL
-     * @return mixed hook return value
-     */
-    function onSensitiveAction($action, &$ssl)
-    {
-        $sensitive = array('twitteradminpanel',
-                           'twittersettings',
-                           'twitterauthorization',
-                           'twitterlogin');
-        if (in_array($action, $sensitive)) {
-            $ssl = true;
-            return false;
-        } else {
-            return true;
-        }
-    }
-
     /**
      * Database schema setup
      *
@@ -385,12 +354,11 @@ class TwitterBridgePlugin extends Plugin
     {
         $n2s = Notice_to_status::getKV('notice_id', $notice->id);
 
-        if (!empty($n2s)) {
+        if ($n2s instanceof Notice_to_status) {
 
-            $flink = Foreign_link::getByUserID($notice->profile_id,
-                                               TWITTER_SERVICE); // twitter service
-
-            if (empty($flink)) {
+            try {
+                $flink = Foreign_link::getByUserID($notice->profile_id, TWITTER_SERVICE); // twitter service
+            } catch (NoResultException $e) {
                 return true;
             }
 
@@ -422,15 +390,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;
         }
 
@@ -462,10 +429,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;
         }
 
@@ -514,16 +480,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;
@@ -531,6 +496,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;
         }
@@ -559,7 +541,7 @@ class TwitterBridgePlugin extends Plugin
                 $action->element('meta', array('name'    => 'twitter:title',
                                              'content' => $action->attachment->title));
 
-                $ns = new AttachmentNoticeSection($this);
+                $ns = new AttachmentNoticeSection($action);
                 $notices = $ns->getNotices();
                 $noticeArray = $notices->fetchAll();
 
@@ -567,18 +549,43 @@ 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;
     }
+    
+    /**
+     * Set the object_type field of previously imported Twitter notices to
+     * ActivityObject::NOTE if they are unset. Null object_type caused a notice
+     * not to show on the timeline.
+     */
+    public function onEndUpgrade()
+    {
+       printfnq("Ensuring all Twitter notices have an object_type...");
+       
+       $notice = new Notice();
+       $notice->whereAdd("source='twitter'");
+       $notice->whereAdd('object_type IS NULL');
+       
+       if ($notice->find()) {
+               while ($notice->fetch()) {
+                       $orig = Notice::getKV('id', $notice->id);
+                       $notice->object_type = ActivityObject::NOTE;
+                       $notice->update($orig);
+               }
+       }
+       
+       printfnq("DONE.\n");
+    }
 }