X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=plugins%2FTwitterBridge%2FTwitterBridgePlugin.php;h=72c28d4fa45eb4dfc4eefbaa1528cc357bf9055e;hb=95d415257a43d5d4338ce22089463c1820b6bf8b;hp=b37d01274d9989170608b732f1f8f317cafda05a;hpb=e04ce18a939d632ab26b96f996fafd6f4e7a6f7e;p=quix0rs-gnu-social.git diff --git a/plugins/TwitterBridge/TwitterBridgePlugin.php b/plugins/TwitterBridge/TwitterBridgePlugin.php index b37d01274d..72c28d4fa4 100644 --- a/plugins/TwitterBridge/TwitterBridgePlugin.php +++ b/plugins/TwitterBridge/TwitterBridgePlugin.php @@ -26,17 +26,17 @@ * @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 @@ -47,7 +47,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 +105,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 +183,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 * @@ -248,7 +209,7 @@ class TwitterBridgePlugin extends Plugin * * @return boolean hook return */ - function onGetValidDaemons($daemons) + function onGetValidDaemons(&$daemons) { if (self::hasKeys()) { array_push( @@ -334,12 +295,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 +364,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,14 +383,13 @@ class TwitterBridgePlugin extends Plugin */ function onStartDeleteOwnNotice(User $user, Notice $notice) { - $n2s = Notice_to_status::staticGet('notice_id', $notice->id); - - if (!empty($n2s)) { + $n2s = Notice_to_status::getKV('notice_id', $notice->id); - $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; } @@ -479,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; } @@ -519,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; } @@ -557,4 +497,102 @@ 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; + + try { + $flink = Foreign_link::getByUserID($profile->id, TWITTER_SERVICE); + $fuser = $flink->getForeignUser(); + + $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; + } + + 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; + } + + /* 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]; + + try { + $flink = Foreign_link::getByUserID($post->profile_id, TWITTER_SERVICE); + $fuser = Foreign_user::getForeignUser($flink->foreign_id, TWITTER_SERVICE); + $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; + } + + return true; + } }