X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;ds=sidebyside;f=plugins%2FTwitterBridge%2Ftwitter.php;h=b8e5457402b46018b001b9834ac610b3f81f815a;hb=1bf4972cc055f013c0a96df7d2d44b55b5e0a12d;hp=de30d9ebf1d8999fcc7a23967e3e091ca3aaded6;hpb=70abea3ac4034cbbfc68cdc8288fc7e2d1cea17c;p=quix0rs-gnu-social.git diff --git a/plugins/TwitterBridge/twitter.php b/plugins/TwitterBridge/twitter.php index de30d9ebf1..b8e5457402 100644 --- a/plugins/TwitterBridge/twitter.php +++ b/plugins/TwitterBridge/twitter.php @@ -1,7 +1,7 @@ uri = $new_uri; - $luser->service = TWITTER_SERVICE; - $result = $luser->delete(); + $luser = Foreign_user::getForeignUser($twitter_id, TWITTER_SERVICE); - if (empty($result)) { - common_log(LOG_WARNING, - "Twitter bridge - removed invalid Twitter user squatting on uri: $new_uri"); + if (!empty($luser)) { + $result = $luser->delete(); + if ($result != false) { + common_log( + LOG_INFO, + "Twitter bridge - removed old Twitter user: $screen_name ($twitter_id)." + ); + } } - $luser->free(); - unset($luser); - - // Otherwise, create a new Twitter user - $fuser = new Foreign_user(); $fuser->nickname = $screen_name; @@ -64,7 +57,8 @@ function add_twitter_user($twitter_id, $screen_name) "Twitter bridge - failed to add new Twitter user: $twitter_id - $screen_name."); common_log_db_error($fuser, 'INSERT', __FILE__); } else { - common_debug("Twitter bridge - Added new Twitter user: $screen_name ($twitter_id)."); + common_log(LOG_INFO, + "Twitter bridge - Added new Twitter user: $screen_name ($twitter_id)."); } return $result; @@ -80,8 +74,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) { @@ -94,8 +86,27 @@ function save_twitter_user($twitter_id, $screen_name) $oldname)); } - return add_twitter_user($twitter_id, $screen_name); + } 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); } function is_twitter_bound($notice, $flink) { @@ -103,9 +114,12 @@ function is_twitter_bound($notice, $flink) { // Check to see if notice should go to Twitter if (!empty($flink) && ($flink->noticesync & FOREIGN_NOTICE_SEND)) { - // If it's not a Twitter-style reply, or if the user WANTS to send replies. + // If it's not a Twitter-style reply, or if the user WANTS to send replies, + // or if it's in reply to a twitter notice + if (!preg_match('/^@[a-zA-Z0-9_]{1,15}\b/u', $notice->content) || - ($flink->noticesync & FOREIGN_NOTICE_SEND_REPLY)) { + ($flink->noticesync & FOREIGN_NOTICE_SEND_REPLY) || + is_twitter_notice($notice->reply_to)) { return true; } } @@ -113,100 +127,113 @@ function is_twitter_bound($notice, $flink) { return false; } +function is_twitter_notice($id) +{ + $notice = Notice::staticGet('id', $id); + + if (empty($notice)) { + // it's not any kind of notice, so it's definitely not a Twitter notice. + return false; + } + + return ($notice->source == 'twitter'); +} + function broadcast_twitter($notice) { $flink = Foreign_link::getByUserID($notice->profile_id, TWITTER_SERVICE); - if (is_twitter_bound($notice, $flink)) { - if (TwitterOAuthClient::isPackedToken($flink->credentials)) { + // Don't bother with basic auth, since it's no longer allowed + + if (!empty($flink) && TwitterOAuthClient::isPackedToken($flink->credentials)) { + if (!empty($notice->repeat_of) && is_twitter_notice($notice->repeat_of)) { + return retweet_notice($flink, Notice::staticGet('id', $notice->repeat_of)); + } else if (is_twitter_bound($notice, $flink)) { return broadcast_oauth($notice, $flink); - } else { - return broadcast_basicauth($notice, $flink); } } return true; } -function broadcast_oauth($notice, $flink) { - $user = $flink->getUser(); - $statustxt = format_status($notice); +function retweet_notice($flink, $notice) +{ $token = TwitterOAuthClient::unpackToken($flink->credentials); $client = new TwitterOAuthClient($token->key, $token->secret); - $status = null; + + $id = twitter_status_id($notice); try { - $status = $client->statusesUpdate($statustxt); + $status = $client->statusesRetweet($id); } catch (OAuthClientException $e) { return process_error($e, $flink, $notice); } +} - if (empty($status)) { - - // This could represent a failure posting, - // or the Twitter API might just be behaving flakey. - - $errmsg = sprintf('Twitter bridge - No data returned by Twitter API when ' . - 'trying to post notice %d for User %s (user id %d).', - $notice->id, - $user->nickname, - $user->id); - - common_log(LOG_WARNING, $errmsg); - - return false; +function twitter_status_id($notice) +{ + if ($notice->source == 'twitter' && + preg_match('#^http://twitter.com/[\w_.]+/status/(\d+)$#', $notice->uri, $match)) { + return $match[1]; } - // Notice crossed the great divide - - $msg = sprintf('Twitter bridge - posted notice %d to Twitter using ' . - 'OAuth for User %s (user id %d).', - $notice->id, - $user->nickname, - $user->id); - - common_log(LOG_INFO, $msg); - - return true; + return null; } -function broadcast_basicauth($notice, $flink) +/** + * 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) { - $user = $flink->getUser(); + $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); - $client = new TwitterBasicAuthClient($flink); + $token = TwitterOAuthClient::unpackToken($flink->credentials); + $client = new TwitterOAuthClient($token->key, $token->secret); $status = null; try { - $status = $client->statusesUpdate($statustxt); - } catch (BasicAuthException $e) { + $status = $client->statusesUpdate($statustxt, $params); + } catch (OAuthClientException $e) { return process_error($e, $flink, $notice); } if (empty($status)) { + // This could represent a failure posting, + // or the Twitter API might just be behaving flakey. + $errmsg = sprintf('Twitter bridge - No data returned by Twitter API when ' . - 'trying to post notice %d for %s (user id %d).', + 'trying to post notice %d for User %s (user id %d).', $notice->id, $user->nickname, $user->id); common_log(LOG_WARNING, $errmsg); - $errmsg = sprintf('No data returned by Twitter API when ' . - 'trying to post notice %d for %s (user id %d).', - $notice->id, - $user->nickname, - $user->id); - common_log(LOG_WARNING, $errmsg); return false; } + // Notice crossed the great divide + $msg = sprintf('Twitter bridge - posted notice %d to Twitter using ' . - 'HTTP basic auth for User %s (user id %d).', + 'OAuth for User %s (user id %d).', $notice->id, $user->nickname, $user->id); @@ -257,8 +284,17 @@ function format_status($notice) $statustxt = preg_replace('/^@/', ' @', $notice->content); // Convert !groups to #hashes + + // XXX: Make this an optional setting? + $statustxt = preg_replace('/(^|\s)!([A-Za-z0-9]{1,64})/', "\\1#\\2", $statustxt); + if (mb_strlen($statustxt) > 140) { + $noticeUrl = common_shorten_url($notice->uri); + $urlLen = mb_strlen($noticeUrl); + $statustxt = mb_substr($statustxt, 0, 140 - ($urlLen + 3)) . ' … ' . $noticeUrl; + } + return $statustxt; } @@ -269,7 +305,7 @@ function remove_twitter_link($flink) common_log(LOG_INFO, 'Removing Twitter bridge Foreign link for ' . "user $user->nickname (user id: $user->id)."); - $result = $flink->delete(); + $result = $flink->safeDelete(); if (empty($result)) { common_log(LOG_ERR, 'Could not remove Twitter bridge ' . @@ -309,10 +345,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'); @@ -328,7 +364,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); }