summaryrefslogtreecommitdiff
path: root/lib/twitter.php
diff options
context:
space:
mode:
Diffstat (limited to 'lib/twitter.php')
-rw-r--r--lib/twitter.php117
1 files changed, 44 insertions, 73 deletions
diff --git a/lib/twitter.php b/lib/twitter.php
index 47af32e61..2369ac267 100644
--- a/lib/twitter.php
+++ b/lib/twitter.php
@@ -360,104 +360,72 @@ function is_twitter_bound($notice, $flink) {
function broadcast_twitter($notice)
{
-
$flink = Foreign_link::getByUserID($notice->profile_id,
TWITTER_SERVICE);
if (is_twitter_bound($notice, $flink)) {
- $fuser = $flink->getForeignUser();
- $twitter_user = $fuser->nickname;
- $twitter_password = $flink->credentials;
- $uri = 'http://www.twitter.com/statuses/update.json';
+ $user = $flink->getUser();
// XXX: Hack to get around PHP cURL's use of @ being a a meta character
$statustxt = preg_replace('/^@/', ' @', $notice->content);
- $options = array(
- CURLOPT_USERPWD => "$twitter_user:$twitter_password",
- CURLOPT_POST => true,
- CURLOPT_POSTFIELDS =>
- array(
- 'status' => $statustxt,
- 'source' => common_config('integration', 'source')
- ),
- CURLOPT_RETURNTRANSFER => true,
- CURLOPT_FAILONERROR => true,
- CURLOPT_HEADER => false,
- CURLOPT_FOLLOWLOCATION => true,
- CURLOPT_USERAGENT => "Laconica",
- CURLOPT_CONNECTTIMEOUT => 120, // XXX: How long should this be?
- CURLOPT_TIMEOUT => 120,
-
- # Twitter is strict about accepting invalid "Expect" headers
- CURLOPT_HTTPHEADER => array('Expect:')
- );
-
- $ch = curl_init($uri);
- curl_setopt_array($ch, $options);
- $data = curl_exec($ch);
- $errmsg = curl_error($ch);
- $errno = curl_errno($ch);
+ $client = new TwitterOAuthClient($flink->token, $flink->credentials);
- if (!empty($errmsg)) {
- common_debug("cURL error ($errno): $errmsg - " .
- "trying to send notice for $twitter_user.",
- __FILE__);
+ $status = null;
- $user = $flink->getUser();
+ try {
+ $status = $client->statuses_update($statustxt);
+ } catch (OAuthClientCurlException $e) {
- if ($errmsg == 'The requested URL returned error: 401') {
- common_debug(sprintf('User %s (user id: %s) ' .
- 'has bad Twitter credentials!',
- $user->nickname, $user->id));
+ if ($e->getMessage() == 'The requested URL returned error: 401') {
- // Bad credentials we need to delete the foreign_link
- // to Twitter and inform the user.
+ $errmsg = sprintf('User %1$s (user id: %2$s) has an invalid ' .
+ 'Twitter OAuth access token.',
+ $user->nickname, $user->id);
+ common_log(LOG_WARNING, $errmsg);
- remove_twitter_link($flink);
+ // Bad auth token! We need to delete the foreign_link
+ // to Twitter and inform the user.
- return true;
+ remove_twitter_link($flink);
+ return true;
- } else {
+ } else {
- // Some other error happened, so we should try to
- // send again later
+ // Some other error happened, so we should probably
+ // try to send again later.
- return false;
- }
+ $errmsg = sprintf('cURL error trying to send notice to Twitter ' .
+ 'for user %1$s (user id: %2$s) - ' .
+ 'code: %3$s message: $4$s.',
+ $user->nickname, $user->id,
+ $e->getCode(), $e->getMessage());
+ common_log(LOG_WARNING, $errmsg);
+ return false;
}
+ }
- curl_close($ch);
-
- if (empty($data)) {
- common_debug("No data returned by Twitter's " .
- "API trying to send update for $twitter_user",
- __FILE__);
+ if (empty($status)) {
- // XXX: Not sure this represents a failure to send, but it
- // probably does
+ // This could represent a failure posting,
+ // or the Twitter API might just be behaving flakey.
- return false;
+ $errmsg = sprint('No data returned by Twitter API when ' .
+ 'trying to send update for %1$s (user id %2$s).',
+ $user->nickname, $user->id);
+ common_log(LOG_WARNING, $errmsg);
- } else {
-
- // Twitter should return a status
- $status = json_decode($data);
+ return false;
+ }
- if (empty($status)) {
- common_debug("Unexpected data returned by Twitter " .
- " API trying to send update for $twitter_user",
- __FILE__);
+ // Notice crossed the great divide
- // XXX: Again, this could represent a failure posting
- // or the Twitter API might just be behaving flakey.
- // We're treating it as a failure to post.
+ $msg = sprintf('Twitter bridge posted notice %s to Twitter.',
+ $notice->id);
+ common_log(LOG_INFO, $msg);
- return false;
- }
- }
}
return true;
@@ -480,17 +448,20 @@ function remove_twitter_link($flink)
// Notify the user that her Twitter bridge is down
+ if (isset($user->email)) {
+
$result = mail_twitter_bridge_removed($user);
if (!$result) {
$msg = 'Unable to send email to notify ' .
- "$user->nickname (user id: $user->id) " .
- 'that their Twitter bridge link was ' .
+ "$user->nickname (user id: $user->id) " .
+ 'that their Twitter bridge link was ' .
'removed!';
common_log(LOG_WARNING, $msg);
}
+ }
}