diff options
author | Brion Vibber <brion@status.net> | 2010-08-10 15:01:29 -0700 |
---|---|---|
committer | Brion Vibber <brion@status.net> | 2010-08-10 15:01:29 -0700 |
commit | 819d33210d298de74b64dc7ead79e9d9b223b12e (patch) | |
tree | 902d42087e633b96e12bef699f6c80e7342c9312 /plugins/TwitterBridge | |
parent | 8f071b2818e8321ea910df612016175f65093402 (diff) | |
parent | 08fc6053ec55e911b842fd05dafc5e0c99c4e992 (diff) |
Merge branch '0.9.x' into tinymce
Diffstat (limited to 'plugins/TwitterBridge')
-rw-r--r-- | plugins/TwitterBridge/TwitterBridgePlugin.php | 15 | ||||
-rwxr-xr-x | plugins/TwitterBridge/daemons/twitterstatusfetcher.php | 141 | ||||
-rw-r--r-- | plugins/TwitterBridge/locale/TwitterBridge.pot (renamed from plugins/TwitterBridge/locale/TwitterBridge.po) | 93 | ||||
-rw-r--r-- | plugins/TwitterBridge/twitter.php | 53 | ||||
-rw-r--r-- | plugins/TwitterBridge/twitteradminpanel.php | 29 | ||||
-rw-r--r-- | plugins/TwitterBridge/twitterauthorization.php | 18 | ||||
-rw-r--r-- | plugins/TwitterBridge/twitterbasicauthclient.php | 15 | ||||
-rw-r--r-- | plugins/TwitterBridge/twitteroauthclient.php | 17 |
8 files changed, 289 insertions, 92 deletions
diff --git a/plugins/TwitterBridge/TwitterBridgePlugin.php b/plugins/TwitterBridge/TwitterBridgePlugin.php index 1a0a69682..0505a328f 100644 --- a/plugins/TwitterBridge/TwitterBridgePlugin.php +++ b/plugins/TwitterBridge/TwitterBridgePlugin.php @@ -50,6 +50,7 @@ class TwitterBridgePlugin extends Plugin { const VERSION = STATUSNET_VERSION; + public $adminImportControl = false; // Should the 'import' checkbox be exposed in the admin panel? /** * Initializer for the plugin. @@ -221,7 +222,7 @@ class TwitterBridgePlugin extends Plugin */ function onStartEnqueueNotice($notice, &$transports) { - if (self::hasKeys()) { + if (self::hasKeys() && $notice->isLocal()) { // Avoid a possible loop if ($notice->source != 'twitter') { array_push($transports, 'twitter'); @@ -322,5 +323,17 @@ class TwitterBridgePlugin extends Plugin return true; } + /** + * Expose the adminImportControl setting to the administration panel code. + * This allows us to disable the import bridge enabling checkbox for administrators, + * since on a bulk farm site we can't yet automate the import daemon setup. + * + * @return boolean hook value; + */ + function onTwitterBridgeAdminImportControl() + { + return (bool)$this->adminImportControl; + } + } diff --git a/plugins/TwitterBridge/daemons/twitterstatusfetcher.php b/plugins/TwitterBridge/daemons/twitterstatusfetcher.php index bff657eb6..7c624fdb3 100755 --- a/plugins/TwitterBridge/daemons/twitterstatusfetcher.php +++ b/plugins/TwitterBridge/daemons/twitterstatusfetcher.php @@ -44,10 +44,17 @@ require_once INSTALLDIR . '/plugins/TwitterBridge/twitterbasicauthclient.php'; require_once INSTALLDIR . '/plugins/TwitterBridge/twitteroauthclient.php'; /** - * Fetcher for statuses from Twitter + * Fetch statuses from Twitter * - * Fetches statuses from Twitter and inserts them as notices in local - * system. + * Fetches statuses from Twitter and inserts them as notices + * + * NOTE: an Avatar path MUST be set in config.php for this + * script to work, e.g.: + * $config['avatar']['path'] = $config['site']['path'] . '/avatar/'; + * + * @todo @fixme @gar Fix the above. For some reason $_path is always empty when + * this script is run, so the default avatar path is always set wrong in + * default.php. Therefore it must be set explicitly in config.php. --Z * * @category Twitter * @package StatusNet @@ -57,9 +64,6 @@ require_once INSTALLDIR . '/plugins/TwitterBridge/twitteroauthclient.php'; * @link http://status.net/ */ -// NOTE: an Avatar path MUST be set in config.php for this -// script to work: e.g.: $config['avatar']['path'] = '/statusnet/avatar'; - class TwitterStatusFetcher extends ParallelizingDaemon { /** @@ -195,6 +199,8 @@ class TwitterStatusFetcher extends ParallelizingDaemon return; } + common_debug(LOG_INFO, $this->name() . ' - Retrieved ' . sizeof($timeline) . ' statuses from Twitter.'); + // Reverse to preserve order foreach (array_reverse($timeline) as $status) { @@ -209,13 +215,7 @@ class TwitterStatusFetcher extends ParallelizingDaemon continue; } - $notice = null; - - $notice = $this->saveStatus($status, $flink); - - if (!empty($notice)) { - common_broadcast_notice($notice); - } + $this->saveStatus($status, $flink); } // Okay, record the time we synced with Twitter for posterity @@ -226,50 +226,77 @@ class TwitterStatusFetcher extends ParallelizingDaemon function saveStatus($status, $flink) { - $id = $this->ensureProfile($status->user); - - $profile = Profile::staticGet($id); + $profile = $this->ensureProfile($status->user); if (empty($profile)) { common_log(LOG_ERR, $this->name() . ' - Problem saving notice. No associated Profile.'); - return null; + return; } - // XXX: change of screen name? - - $uri = 'http://twitter.com/' . $status->user->screen_name . - '/status/' . $status->id; + $statusUri = 'http://twitter.com/' + . $status->user->screen_name + . '/status/' + . $status->id; // check to see if we've already imported the status - $notice = Notice::staticGet('uri', $uri); + $dupe = $this->checkDupe($profile, $statusUri); + + if (!empty($dupe)) { + common_log( + LOG_INFO, + $this->name() . + " - Ignoring duplicate import: $statusUri" + ); + return; + } + + $notice = new Notice(); - if (empty($notice)) { + $notice->profile_id = $profile->id; + $notice->uri = $statusUri; + $notice->url = $statusUri; + $notice->created = strftime( + '%Y-%m-%d %H:%M:%S', + strtotime($status->created_at) + ); - // XXX: transaction here? + $notice->source = 'twitter'; + $notice->reply_to = null; + $notice->is_local = Notice::GATEWAY; - $notice = new Notice(); + $notice->content = common_shorten_links($status->text); + $notice->rendered = common_render_content( + $notice->content, + $notice + ); - $notice->profile_id = $id; - $notice->uri = $uri; - $notice->created = strftime('%Y-%m-%d %H:%M:%S', - strtotime($status->created_at)); - $notice->content = common_shorten_links($status->text); // XXX - $notice->rendered = common_render_content($notice->content, $notice); - $notice->source = 'twitter'; - $notice->reply_to = null; // XXX: lookup reply - $notice->is_local = Notice::GATEWAY; + if (Event::handle('StartNoticeSave', array(&$notice))) { - if (Event::handle('StartNoticeSave', array(&$notice))) { - $notice->insert(); - Event::handle('EndNoticeSave', array($notice)); + $id = $notice->insert(); + + if (!$id) { + common_log_db_error($notice, 'INSERT', __FILE__); + common_log(LOG_ERR, $this->name() . + ' - Problem saving notice.'); } + Event::handle('EndNoticeSave', array($notice)); } - Inbox::insertNotice($flink->user_id, $notice->id); + $orig = clone($notice); + $conv = Conversation::create(); + + $notice->conversation = $conv->id; + + if (!$notice->update($orig)) { + common_log_db_error($notice, 'UPDATE', __FILE__); + common_log(LOG_ERR, $this->name() . + ' - Problem saving notice.'); + } + Inbox::insertNotice($flink->user_id, $notice->id); $notice->blowOnInsert(); return $notice; @@ -279,9 +306,10 @@ class TwitterStatusFetcher extends ParallelizingDaemon * Look up a Profile by profileurl field. Profile::staticGet() was * not working consistently. * - * @param string $url the profile url + * @param string $nickname local nickname of the Twitter user + * @param string $profileurl the profile url * - * @return mixed the first profile with that url, or null + * @return mixed value the first Profile with that url, or null */ function getProfileByUrl($nickname, $profileurl) @@ -299,6 +327,30 @@ class TwitterStatusFetcher extends ParallelizingDaemon return null; } + /** + * Check to see if this Twitter status has already been imported + * + * @param Profile $profile Twitter user's local profile + * @param string $statusUri URI of the status on Twitter + * + * @return mixed value a matching Notice or null + */ + + function checkDupe($profile, $statusUri) + { + $notice = new Notice(); + $notice->uri = $statusUri; + $notice->profile_id = $profile->id; + $notice->limit(1); + + if ($notice->find()) { + $notice->fetch(); + return $notice; + } + + return null; + } + function ensureProfile($user) { // check to see if there's already a profile for this user @@ -313,7 +365,7 @@ class TwitterStatusFetcher extends ParallelizingDaemon // Check to see if the user's Avatar has changed $this->checkAvatar($user, $profile); - return $profile->id; + return $profile; } else { @@ -372,7 +424,7 @@ class TwitterStatusFetcher extends ParallelizingDaemon $this->saveAvatars($user, $id); - return $id; + return $profile; } } @@ -403,7 +455,6 @@ class TwitterStatusFetcher extends ParallelizingDaemon $this->updateAvatars($twitter_user, $profile); } - } function updateAvatars($twitter_user, $profile) { @@ -428,17 +479,13 @@ class TwitterStatusFetcher extends ParallelizingDaemon } function missingAvatarFile($profile) { - foreach (array(24, 48, 73) as $size) { - $filename = $profile->getAvatar($size)->filename; $avatarpath = Avatar::path($filename); - if (file_exists($avatarpath) == FALSE) { return true; } } - return false; } diff --git a/plugins/TwitterBridge/locale/TwitterBridge.po b/plugins/TwitterBridge/locale/TwitterBridge.pot index eff125579..c7ac8053c 100644 --- a/plugins/TwitterBridge/locale/TwitterBridge.po +++ b/plugins/TwitterBridge/locale/TwitterBridge.pot @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2010-03-01 14:58-0800\n" +"POT-Creation-Date: 2010-04-29 23:39+0000\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" "Language-Team: LANGUAGE <LL@li.org>\n" @@ -16,11 +16,11 @@ msgstr "" "Content-Type: text/plain; charset=CHARSET\n" "Content-Transfer-Encoding: 8bit\n" -#: twitter.php:320 +#: twitter.php:342 msgid "Your Twitter bridge has been disabled." msgstr "" -#: twitter.php:324 +#: twitter.php:346 #, php-format msgid "" "Hi, %1$s. We're sorry to inform you that your link to Twitter has been " @@ -36,28 +36,97 @@ msgid "" "%3$s\n" msgstr "" -#: twitterauthorization.php:181 twitterauthorization.php:229 -msgid "Couldn't link your Twitter account." +#: TwitterBridgePlugin.php:155 TwitterBridgePlugin.php:178 +#: TwitterBridgePlugin.php:291 twitteradminpanel.php:54 +msgid "Twitter" msgstr "" -#: twitterauthorization.php:201 -msgid "Couldn't link your Twitter account: oauth_token mismatch." +#: TwitterBridgePlugin.php:156 +msgid "Login or register using Twitter" msgstr "" -#: TwitterBridgePlugin.php:114 -msgid "Twitter" +#: TwitterBridgePlugin.php:179 +msgid "Twitter integration options" msgstr "" -#: TwitterBridgePlugin.php:115 -msgid "Twitter integration options" +#: TwitterBridgePlugin.php:292 +msgid "Twitter bridge configuration" msgstr "" -#: TwitterBridgePlugin.php:207 +#: TwitterBridgePlugin.php:317 msgid "" "The Twitter \"bridge\" plugin allows you to integrate your StatusNet " "instance with <a href=\"http://twitter.com/\">Twitter</a>." msgstr "" +#: twitteradminpanel.php:65 +msgid "Twitter bridge settings" +msgstr "" + +#: twitteradminpanel.php:148 +msgid "Invalid consumer key. Max length is 255 characters." +msgstr "" + +#: twitteradminpanel.php:154 +msgid "Invalid consumer secret. Max length is 255 characters." +msgstr "" + +#: twitteradminpanel.php:207 +msgid "Twitter application settings" +msgstr "" + +#: twitteradminpanel.php:213 +msgid "Consumer key" +msgstr "" + +#: twitteradminpanel.php:214 +msgid "Consumer key assigned by Twitter" +msgstr "" + +#: twitteradminpanel.php:222 +msgid "Consumer secret" +msgstr "" + +#: twitteradminpanel.php:223 +msgid "Consumer secret assigned by Twitter" +msgstr "" + +#: twitteradminpanel.php:240 +msgid "Integration source" +msgstr "" + +#: twitteradminpanel.php:241 +msgid "Name of your Twitter application" +msgstr "" + +#: twitteradminpanel.php:253 +msgid "Options" +msgstr "" + +#: twitteradminpanel.php:260 +msgid "Enable \"Sign-in with Twitter\"" +msgstr "" + +#: twitteradminpanel.php:262 +msgid "Allow users to login with their Twitter credentials" +msgstr "" + +#: twitteradminpanel.php:268 +msgid "Enable Twitter import" +msgstr "" + +#: twitteradminpanel.php:270 +msgid "Allow users to import their Twitter friends' timelines" +msgstr "" + +#: twitterauthorization.php:181 twitterauthorization.php:229 +msgid "Couldn't link your Twitter account." +msgstr "" + +#: twitterauthorization.php:201 +msgid "Couldn't link your Twitter account: oauth_token mismatch." +msgstr "" + #: twittersettings.php:59 msgid "Twitter settings" msgstr "" diff --git a/plugins/TwitterBridge/twitter.php b/plugins/TwitterBridge/twitter.php index 2805b3ab5..306ba2442 100644 --- a/plugins/TwitterBridge/twitter.php +++ b/plugins/TwitterBridge/twitter.php @@ -75,8 +75,6 @@ function save_twitter_user($twitter_id, $screen_name) if (!empty($fuser)) { - $result = true; - // Delete old record if Twitter user changed screen name if ($fuser->nickname != $screen_name) { @@ -88,6 +86,25 @@ function save_twitter_user($twitter_id, $screen_name) $screen_name, $oldname)); } + + } else { + + // Kill any old, invalid records for this screen name + + $fuser = Foreign_user::getByNickname($screen_name, TWITTER_SERVICE); + + if (!empty($fuser)) { + $fuser->delete(); + common_log( + LOG_INFO, + sprintf( + 'Twitter bridge - deteted old record for Twitter ' . + 'screen name "%s" belonging to Twitter ID %d.', + $screen_name, + $fuser->id + ) + ); + } } return add_twitter_user($twitter_id, $screen_name); @@ -124,15 +141,36 @@ function broadcast_twitter($notice) return true; } +/** + * Pull any extra information from a notice that we should transfer over + * to Twitter beyond the notice text itself. + * + * @param Notice $notice + * @return array of key-value pairs for Twitter update submission + * @access private + */ +function twitter_update_params($notice) +{ + $params = array(); + if ($notice->lat || $notice->lon) { + $params['lat'] = $notice->lat; + $params['long'] = $notice->lon; + } + return $params; +} + + function broadcast_oauth($notice, $flink) { $user = $flink->getUser(); $statustxt = format_status($notice); + $params = twitter_update_params($notice); + $token = TwitterOAuthClient::unpackToken($flink->credentials); $client = new TwitterOAuthClient($token->key, $token->secret); $status = null; try { - $status = $client->statusesUpdate($statustxt); + $status = $client->statusesUpdate($statustxt, $params); } catch (OAuthClientException $e) { return process_error($e, $flink, $notice); } @@ -171,12 +209,13 @@ function broadcast_basicauth($notice, $flink) $user = $flink->getUser(); $statustxt = format_status($notice); + $params = twitter_update_params($notice); $client = new TwitterBasicAuthClient($flink); $status = null; try { - $status = $client->statusesUpdate($statustxt); + $status = $client->statusesUpdate($statustxt, $params); } catch (BasicAuthException $e) { return process_error($e, $flink, $notice); } @@ -313,10 +352,10 @@ function remove_twitter_link($flink) function mail_twitter_bridge_removed($user) { - common_init_locale($user->language); - $profile = $user->getProfile(); + common_switch_locale($user->language); + $subject = sprintf(_m('Your Twitter bridge has been disabled.')); $site_name = common_config('site', 'name'); @@ -332,7 +371,7 @@ function mail_twitter_bridge_removed($user) common_local_url('twittersettings'), common_config('site', 'name')); - common_init_locale(); + common_switch_locale(); return mail_to_user($user, $subject, $body); } diff --git a/plugins/TwitterBridge/twitteradminpanel.php b/plugins/TwitterBridge/twitteradminpanel.php index a78a92c66..69f8da078 100644 --- a/plugins/TwitterBridge/twitteradminpanel.php +++ b/plugins/TwitterBridge/twitteradminpanel.php @@ -92,9 +92,11 @@ class TwitteradminpanelAction extends AdminPanelAction ); static $booleans = array( - 'twitter' => array('signin'), - 'twitterimport' => array('enabled') + 'twitter' => array('signin') ); + if (Event::handle('TwitterBridgeAdminImportControl')) { + $booleans['twitterimport'] = array('enabled'); + } $values = array(); @@ -155,6 +157,13 @@ class TwitteradminpanelAction extends AdminPanelAction ); } } + + function isImportEnabled() + { + // Since daemon setup isn't automated yet... + // @todo: if merged into main queues, detect presence of daemon config + return true; + } } class TwitterAdminPanelForm extends AdminForm @@ -263,13 +272,15 @@ class TwitterAdminPanelForm extends AdminForm ); $this->unli(); - $this->li(); - $this->out->checkbox( - 'enabled', _m('Enable Twitter import'), - (bool) $this->value('enabled', 'twitterimport'), - _m('Allow users to import their Twitter friends\' timelines') - ); - $this->unli(); + if (Event::handle('TwitterBridgeAdminImportControl')) { + $this->li(); + $this->out->checkbox( + 'enabled', _m('Enable Twitter import'), + (bool) $this->value('enabled', 'twitterimport'), + _m('Allow users to import their Twitter friends\' timelines. Requires daemons to be manually configured.') + ); + $this->unli(); + } $this->out->elementEnd('ul'); diff --git a/plugins/TwitterBridge/twitterauthorization.php b/plugins/TwitterBridge/twitterauthorization.php index bc004cb95..7a896e168 100644 --- a/plugins/TwitterBridge/twitterauthorization.php +++ b/plugins/TwitterBridge/twitterauthorization.php @@ -332,6 +332,11 @@ class TwitterauthorizationAction extends Action parent::showPage(); } + /** + * @fixme much of this duplicates core code, which is very fragile. + * Should probably be replaced with an extensible mini version of + * the core registration form. + */ function showContent() { if (!empty($this->message_text)) { @@ -353,10 +358,15 @@ class TwitterauthorizationAction extends Action 'name' => 'license', 'value' => 'true')); $this->elementStart('label', array('class' => 'checkbox', 'for' => 'license')); - $this->text(_('My text and files are available under ')); - $this->element('a', array('href' => common_config('license', 'url')), - common_config('license', 'title')); - $this->text(_(' except this private data: password, email address, IM address, phone number.')); + $message = _('My text and files are available under %s ' . + 'except this private data: password, ' . + 'email address, IM address, and phone number.'); + $link = '<a href="' . + htmlspecialchars(common_config('license', 'url')) . + '">' . + htmlspecialchars(common_config('license', 'title')) . + '</a>'; + $this->raw(sprintf(htmlspecialchars($message), $link)); $this->elementEnd('label'); $this->elementEnd('li'); $this->elementEnd('ul'); diff --git a/plugins/TwitterBridge/twitterbasicauthclient.php b/plugins/TwitterBridge/twitterbasicauthclient.php index fd26293f9..2c18c9469 100644 --- a/plugins/TwitterBridge/twitterbasicauthclient.php +++ b/plugins/TwitterBridge/twitterbasicauthclient.php @@ -76,18 +76,21 @@ class TwitterBasicAuthClient /** * Calls Twitter's /statuses/update API method * - * @param string $status text of the status - * @param int $in_reply_to_status_id optional id of the status it's - * a reply to + * @param string $status text of the status + * @param mixed $params optional other parameters to pass to Twitter, + * as defined. For back-compatibility, if an int + * is passed we'll consider it a reply-to ID. * * @return mixed the status */ function statusesUpdate($status, $in_reply_to_status_id = null) { $url = 'https://twitter.com/statuses/update.json'; - $params = array('status' => $status, - 'source' => common_config('integration', 'source'), - 'in_reply_to_status_id' => $in_reply_to_status_id); + if (is_numeric($params)) { + $params = array('in_reply_to_status_id' => intval($params)); + } + $params['status'] = $status; + $params['source'] = common_config('integration', 'source'); $response = $this->httpRequest($url, $params); $status = json_decode($response); return $status; diff --git a/plugins/TwitterBridge/twitteroauthclient.php b/plugins/TwitterBridge/twitteroauthclient.php index 93f6aadd1..d895d8c73 100644 --- a/plugins/TwitterBridge/twitteroauthclient.php +++ b/plugins/TwitterBridge/twitteroauthclient.php @@ -166,17 +166,22 @@ class TwitterOAuthClient extends OAuthClient /** * Calls Twitter's /statuses/update API method * - * @param string $status text of the status - * @param int $in_reply_to_status_id optional id of the status it's - * a reply to + * @param string $status text of the status + * @param mixed $params optional other parameters to pass to Twitter, + * as defined. For back-compatibility, if an int + * is passed we'll consider it a reply-to ID. * * @return mixed the status */ - function statusesUpdate($status, $in_reply_to_status_id = null) + function statusesUpdate($status, $params=array()) { $url = 'https://twitter.com/statuses/update.json'; - $params = array('status' => $status, - 'in_reply_to_status_id' => $in_reply_to_status_id); + if (is_numeric($params)) { + $params = array('in_reply_to_status_id' => intval($params)); + } + $params['status'] = $status; + // We don't have to pass 'source' as the oauth key is tied to an app. + $response = $this->oAuthPost($url, $params); $status = json_decode($response); return $status; |