Advertisement
Guest User

Untitled

a guest
Jan 8th, 2014
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 5.90 KB | None | 0 0
  1.     # the callback
  2.     my $r = $self->routes->bridge('/')->to(cb => sub {
  3.         my $self = shift;
  4.         return $self->init_auth();
  5.     });
  6.  
  7.     # the helper called above
  8.     $app->helper(init_auth => sub {
  9.         my $self   = shift; # controller object
  10.  
  11.         if(my $o = $self->session('openid')) {
  12.             $self->debug('have openid: ', $o);
  13.             if($o =~ /https:\/\/(.*?)\..*\/id\/(\d+)-(.*)\//) {
  14.                 $self->debug('openid matches regex');
  15.                 my $server = $1;
  16.                 my $pname = $3;
  17.  
  18.                 $server = 'sea' if(lc($server) eq 'asia'); # fuck WG and renaming endpoints
  19.  
  20.                 my $user_id   = sprintf('%s-%s', lc($server), lc($pname));
  21.  
  22.                 if($self->req->is_xhr) {
  23.                     $self->debug('request is XHR, loading user');
  24.                     $self->model('wot-replays.accounts')->find_one({ _id => $user_id } => sub {
  25.                         my ($coll, $err, $user) = (@_);
  26.                         $self->stash(current_user => $user);
  27.                         $self->stash(current_player_name => $user->{player_name});
  28.                         $self->stash(current_player_server => uc($user->{player_server}));
  29.                         $self->debug('user loaded, continue');
  30.                         $self->continue;
  31.                     });
  32.                     $self->debug('post-load');
  33.                     return undef;
  34.                 } else {
  35.                     $self->debug('request is not XHR');
  36.                     my $last_seen = Mango::BSON::bson_time;
  37.  
  38.                     $self->debug('updating user with last seen');
  39.                     $self->model('wot-replays.accounts')->update({
  40.                         _id => $user_id
  41.                     }, {
  42.                         '$set' => {
  43.                             player_name     => $pname,
  44.                             player_server   => lc($server),
  45.                             last_seen       => $last_seen,
  46.                         },
  47.                     }, {
  48.                         upsert => 1,
  49.                     },
  50.                     sub {
  51.                         my ($coll, $err, $oid) = (@_);
  52.                         $self->debug('user updated');
  53.                         $self->debug('loading user');
  54.                         $self->model('wot-replays.accounts')->find_one({ _id => $user_id } => sub {
  55.                             my ($coll, $err, $user) = (@_);
  56.  
  57.                             $self->debug('user loaded');
  58.                             $self->stash(current_user           => $user);
  59.                             $self->stash(current_player_name    => $user->{player_name});
  60.                             $self->stash(current_player_server  => uc($user->{player_server}));
  61.  
  62.                             $self->current_user->{last_clan_check} ||= 0;
  63.  
  64.                             if($self->current_user->{last_clan_check} < Mango::BSON::bson_time( (time() - 86400) * 1000)) {
  65.                                 $self->debug('clan check needed');
  66.                                 # we need to re-check the users' clan settings, we do that by yoinking statterbox for it
  67.                                 my $url = sprintf('http://statterbox.com/api/v1/%s/clan?server=%s&player=%s',
  68.                                     '5299a074907e1337e0010000', # yes it's a hardcoded API token :P
  69.                                     lc($self->current_user->{player_server}),
  70.                                     lc($self->current_user->{player_name}),
  71.                                     );
  72.                                 $self->debug('fetching from ', $url);
  73.                                 $self->ua->get($url => sub {
  74.                                     my ($ua, $tx) = (@_);
  75.                                     my $clan = undef;
  76.  
  77.                                     $self->debug('url fetched');
  78.                                    
  79.                                     if(my $res = $tx->success) {
  80.                                         if($res->json->{ok} == 1) {
  81.                                             $self->debug('have clan data');
  82.                                             $clan = $res->json->{data}->{lc($self->current_user->{player_name})};
  83.                                         } else {
  84.                                             $self->debug('no clan data');
  85.                                             $clan = undef;
  86.                                         }
  87.                                     } else {
  88.                                         $self->debug('request failed');
  89.                                         $clan = undef;
  90.                                     }
  91.                                     $self->current_user->{clan} = $clan;
  92.                                     $self->debug('updating user for clan');
  93.                                     $self->model('wot-replays.accounts')->update({ _id => $user_id }, { '$set' => {
  94.                                         'last_clan_check' => Mango::BSON::bson_time,
  95.                                         'clan'            => $clan,
  96.                                     }} => sub {
  97.                                         $self->debug('clan check results saved, continue');
  98.                                         $self->continue;
  99.                                     });
  100.                                 });
  101.                             } else {
  102.                                 $self->debug('no clan check required, continue');
  103.                                 $self->continue;
  104.                             }
  105.                         });
  106.                     });
  107.                     $self->debug('post last-seen update');
  108.                     return undef;
  109.                 }
  110.             } else {
  111.                 $self->debug('openid does not match regex');
  112.                 return 1;
  113.             }
  114.         } else {
  115.             $self->debug('no openid in session');
  116.             return 1;
  117.         }
  118.     });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement