]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/TwitterBridge/TwitterBridgePlugin.php
Added more checked type-hints.
[quix0rs-gnu-social.git] / plugins / TwitterBridge / TwitterBridgePlugin.php
index b37d01274d9989170608b732f1f8f317cafda05a..d4bb74e93559ee8ecb80e1ad613677ea6c94a4bf 100644 (file)
@@ -30,13 +30,15 @@ if (!defined('STATUSNET')) {
     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 +49,7 @@ require_once INSTALLDIR . '/plugins/TwitterBridge/twitter.php';
  */
 class TwitterBridgePlugin extends Plugin
 {
-    const VERSION = STATUSNET_VERSION;
+    const VERSION = GNUSOCIAL_VERSION;
     public $adminImportControl = false; // Should the 'import' checkbox be exposed in the admin panel?
 
     /**
@@ -105,11 +107,11 @@ 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'));
 
@@ -183,45 +185,6 @@ class TwitterBridgePlugin extends Plugin
         return true;
     }
 
-    /**
-     * Automatically load the actions and libraries used by the Twitter bridge
-     *
-     * @param Class $cls the class
-     *
-     * @return boolean hook return
-     *
-     */
-    function onAutoload($cls)
-    {
-        $dir = dirname(__FILE__);
-
-        switch ($cls) {
-        case 'TwittersettingsAction':
-        case 'TwitterauthorizationAction':
-        case 'TwitterloginAction':
-        case 'TwitteradminpanelAction':
-            include_once $dir . '/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
-            return false;
-        case 'TwitterOAuthClient':
-        case 'TwitterQueueHandler':
-        case 'TwitterImport':
-        case 'JsonStreamReader':
-        case 'TwitterStreamReader':
-            include_once $dir . '/' . strtolower($cls) . '.php';
-            return false;
-        case 'TwitterSiteStream':
-        case 'TwitterUserStream':
-            include_once $dir . '/twitterstreamreader.php';
-            return false;
-        case 'Notice_to_status':
-        case 'Twitter_synch_status':
-            include_once $dir . '/' . $cls . '.php';
-            return false;
-        default:
-            return true;
-        }
-    }
-
     /**
      * Add a Twitter queue item for each notice
      *
@@ -230,7 +193,7 @@ class TwitterBridgePlugin extends Plugin
      *
      * @return boolean hook return
      */
-    function onStartEnqueueNotice($notice, &$transports)
+    function onStartEnqueueNotice(Notice $notice, array &$transports)
     {
         if (self::hasKeys() && $notice->isLocal() && $notice->inScope(null)) {
             // Avoid a possible loop
@@ -248,7 +211,7 @@ class TwitterBridgePlugin extends Plugin
      *
      * @return boolean hook return
      */
-    function onGetValidDaemons($daemons)
+    function onGetValidDaemons(array &$daemons)
     {
         if (self::hasKeys()) {
             array_push(
@@ -307,7 +270,7 @@ class TwitterBridgePlugin extends Plugin
      * @return boolean hook value
      */
 
-    function onEndAdminPanelNav($nav)
+    function onEndAdminPanelNav(Menu $nav)
     {
         if (AdminPanelAction::canAdmin('twitter')) {
 
@@ -334,12 +297,12 @@ class TwitterBridgePlugin extends Plugin
      *
      * @return boolean hook value
      */
-    function onPluginVersion(&$versions)
+    function onPluginVersion(array &$versions)
     {
         $versions[] = array(
             'name' => 'TwitterBridge',
             'version' => self::VERSION,
-            'author' => 'Zach Copley, Julien C',
+            'author' => 'Zach Copley, Julien C, Jean Baptiste Favre',
             'homepage' => 'http://status.net/wiki/Plugin:TwitterBridge',
             // TRANS: Plugin description.
             'rawdescription' => _m('The Twitter "bridge" plugin allows integration ' .
@@ -403,28 +366,10 @@ class TwitterBridgePlugin extends Plugin
 
         // For saving the last-synched status of various timelines
         // home_timeline, messages (in), messages (out), ...
-
-        $schema->ensureTable('twitter_synch_status',
-                             array(new ColumnDef('foreign_id', 'bigint', null,
-                                                 false, 'PRI'),
-                                   new ColumnDef('timeline', 'varchar', 255,
-                                                 false, 'PRI'),
-                                   new ColumnDef('last_id', 'bigint', null, // XXX: check for PostgreSQL
-                                                 false),
-                                   new ColumnDef('created', 'datetime', null,
-                                                 false),
-                                   new ColumnDef('modified', 'datetime', null,
-                                                 false)));
+        $schema->ensureTable('twitter_synch_status', Twitter_synch_status::schemaDef());
 
         // For storing user-submitted flags on profiles
-
-        $schema->ensureTable('notice_to_status',
-                             array(new ColumnDef('notice_id', 'integer', null,
-                                                 false, 'PRI'),
-                                   new ColumnDef('status_id', 'bigint', null, // XXX: check for PostgreSQL
-                                                 false, 'UNI'),
-                                   new ColumnDef('created', 'datetime', null,
-                                                 false)));
+        $schema->ensureTable('notice_to_status', Notice_to_status::schemaDef());
 
         return true;
     }
@@ -440,7 +385,7 @@ class TwitterBridgePlugin extends Plugin
      */
     function onStartDeleteOwnNotice(User $user, Notice $notice)
     {
-        $n2s = Notice_to_status::staticGet('notice_id', $notice->id);
+        $n2s = Notice_to_status::getKV('notice_id', $notice->id);
 
         if (!empty($n2s)) {
 
@@ -557,4 +502,85 @@ class TwitterBridgePlugin extends Plugin
         }
         return true;
     }
+
+    /**
+     * Add links in the user's profile block to their Twitter profile URL.
+     *
+     * @param Profile $profile The profile being shown
+     * @param Array   &$links  Writeable array of arrays (href, text, image).
+     *
+     * @return boolean hook value (true)
+     */
+
+    function onOtherAccountProfiles($profile, &$links)
+    {
+        $fuser = null;
+
+        $flink = Foreign_link::getByUserID($profile->id, TWITTER_SERVICE);
+
+        if (!empty($flink)) {
+            $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"));
+            }
+        }
+
+        return true;
+    }
+
+    public function onEndShowHeadElements(Action $action)
+    {
+        if (!($action instanceof AttachmentAction)) {
+            return true;
+        }
+
+        /* Twitter card support. See https://dev.twitter.com/docs/cards */
+        /* @fixme: should we display twitter cards only for attachments posted
+         *         by local users ? Seems mandatory to display twitter:creator
+         *
+         * Author: jbfavre
+         */
+        switch ($action->attachment->mimetype) {
+            case 'image/pjpeg':
+            case 'image/jpeg':
+            case 'image/jpg':
+            case 'image/png':
+            case 'image/gif':
+                $action->element('meta', array('name'    => 'twitter:card',
+                                             'content' => 'photo'),
+                                       null);
+                $action->element('meta', array('name'    => 'twitter:url',
+                                             'content' => common_local_url('attachment',
+                                                              array('attachment' => $action->attachment->id))),
+                                       null );
+                $action->element('meta', array('name'    => 'twitter:image',
+                                             'content' => $action->attachment->url));
+                $action->element('meta', array('name'    => 'twitter:title',
+                                             'content' => $action->attachment->title));
+
+                $ns = new AttachmentNoticeSection($action);
+                $notices = $ns->getNotices();
+                $noticeArray = $notices->fetchAll();
+
+                // Should not have more than 1 notice for this attachment.
+                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
+                    $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));
+                    }
+                }
+                break;
+            default: break;
+        }
+
+        return true;
+    }
 }