Андрей (andy) wrote in changelog,
Андрей
andy
changelog

[ljcom] r11181: LJSUP-10400 (reduce the number of twitte...

Committer: ailyin
LJSUP-10400 (reduce the number of twitter-related warnings in the error logs)
U   trunk/cgi-bin/LJ/Client/Twitter.pm
U   trunk/cgi-bin/LJ/Worker/Repost/CommentToTwitter.pm
U   trunk/cgi-bin/LJ/Worker/Repost/EntryToTwitter.pm
Modified: trunk/cgi-bin/LJ/Client/Twitter.pm
===================================================================
--- trunk/cgi-bin/LJ/Client/Twitter.pm	2011-11-15 04:11:21 UTC (rev 11180)
+++ trunk/cgi-bin/LJ/Client/Twitter.pm	2011-11-15 04:40:48 UTC (rev 11181)
@@ -189,6 +189,19 @@
     return $token;
 }
 
+sub get_user_access_token {
+    my ( $class, $u ) = @_;
+
+    my $token = {
+        'public' => $u->prop('twitter_access_token') || undef,
+        'secret' => $u->prop('twitter_access_token_secret') || undef,
+    };
+
+    return unless $token->{'public'};
+
+    return $token;
+}
+
 sub call {
     my ($class, %opts) = @_;
 
@@ -200,18 +213,14 @@
     my $u = $opts{user};
 
     if ($u) {
-        $opts{'access_token'} = {
-            'public' => $u->prop('twitter_access_token') || undef,
-            'secret' => $u->prop('twitter_access_token_secret') || undef,
-        };
+        $opts{'access_token'} = $class->get_user_access_token($u);
     }
 
     my $access_token = $opts{'access_token'};
     die 'access token not provided' unless $access_token;
 
-    unless ($access_token->{'public'}) {
-        warn "User " . $u->username . " has no twitter access token\n";
-        return undef;
+    unless ($access_token) {
+        die "User " . $u->username . " has no twitter access token\n";
     }
 
     my $http_method = $opts{'http_method'} || 'GET';
@@ -301,6 +310,14 @@
                 subject  => LJ::Lang::get_text($lang, 'twitter.disconnect_letter.subject', undef),
                 body     => LJ::Lang::get_text($lang, 'twitter.disconnect_letter.body', undef, { user => $u->username }),
             } ) or warn "Unable to send_mail";
+            return;
+        } elsif ( $res->code >= 500 && $res->code < 600 ) {
+            # 50x codes (which indicate a server error) only get
+            # printed if we have an environment flag on
+            if ( $ENV{'LJ_DEBUG_TWITTER_CONNECTIVITY'} ) {
+                die "twitter connectivity error: " . $res->status_line;
+            }
+            return;
         } else {
             die "twitter connectivity error: " . $res->status_line;
         }
@@ -431,6 +448,11 @@
 sub user_last_tweets {
     my ($class, $u) = @_;
 
+    unless ( LJ::Client::Twitter->get_user_access_token($u) ) {
+        # if they don't have a token, return an empty result
+        return [];
+    }
+
     my $res = LJ::Client::Twitter->call(
         'api_method' => 'statuses/user_timeline',
         'user' => $u,
@@ -447,10 +469,20 @@
 sub get_userinfo {
     my ($class, %opts) = @_;
 
+    my $token = $opts{'access_token'};
+    my $u     = $opts{'user'};
+
+    if ($u) {
+        unless ( LJ::Client::Twitter->get_user_access_token($u) ) {
+            # if they don't have a token, return undef for no info
+            return;
+        }
+    }
+
     my $userdata = $class->call(
         'api_method'    => 'account/verify_credentials',
-        'access_token'  => $opts{'access_token'},
-        'user'          => $opts{'user'},
+        'access_token'  => $token,
+        'user'          => $u,
     );
 
     my $twu = LJ::Client::Twitter::User->from_hash($userdata);

Modified: trunk/cgi-bin/LJ/Worker/Repost/CommentToTwitter.pm
===================================================================
--- trunk/cgi-bin/LJ/Worker/Repost/CommentToTwitter.pm	2011-11-15 04:11:21 UTC (rev 11180)
+++ trunk/cgi-bin/LJ/Worker/Repost/CommentToTwitter.pm	2011-11-15 04:40:48 UTC (rev 11181)
@@ -16,6 +16,12 @@
     my $journal = LJ::load_userid($arg->{'journalid'});
     my $comment = LJ::Comment->new($journal, 'jtalkid' => $arg->{'jtalkid'});
 
+    unless ( LJ::Client::Twitter->get_user_access_token($poster) ) {
+        # if they don't have a token, pretend we reposted that without
+        # actually doing it
+        return $job->completed;
+    }
+
     # if it has somehow disappeared in the meantime, pretend everything
     # is fine and don't even attempt to repost
     return $job->completed unless $comment->valid;

Modified: trunk/cgi-bin/LJ/Worker/Repost/EntryToTwitter.pm
===================================================================
--- trunk/cgi-bin/LJ/Worker/Repost/EntryToTwitter.pm	2011-11-15 04:11:21 UTC (rev 11180)
+++ trunk/cgi-bin/LJ/Worker/Repost/EntryToTwitter.pm	2011-11-15 04:40:48 UTC (rev 11181)
@@ -14,6 +14,12 @@
     my $journal = LJ::load_userid($arg->{'journalid'});
     my $entry   = LJ::Entry->new($journal, 'jitemid' => $arg->{'jitemid'});
 
+    unless ( LJ::Client::Twitter->get_user_access_token($poster) ) {
+        # if they don't have a token, pretend we reposted that without
+        # actually doing it
+        return $job->completed;
+    }
+
     # if it has somehow disappeared in the meantime, pretend everything
     # is fine and don't even attempt to repost
     return $job->completed unless $entry->valid;

Tags: ailyin, andy, ljcom, pm
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments