Alexander Nazarov (nfokz) wrote in changelog,
Alexander Nazarov
nfokz
changelog

[livejournal] r21855: LJSUP-11985: Change LJ::ljuser() impleme...

Committer: anazarov
LJSUP-11985: Change LJ::ljuser() implementation
U   trunk/cgi-bin/LJ/User.pm
Modified: trunk/cgi-bin/LJ/User.pm
===================================================================
--- trunk/cgi-bin/LJ/User.pm	2012-04-28 09:56:17 UTC (rev 21854)
+++ trunk/cgi-bin/LJ/User.pm	2012-04-28 13:16:45 UTC (rev 21855)
@@ -7990,14 +7990,14 @@
 }
 
 my $ljuser_tmpl_path = join('/', $ENV{'LJHOME'}, 'templates', 'User');
+my $ljuser_cache     = {};
 
 sub ljuser2 {
     my ($user, $opts) = @_;
     my ($u, $username, $journal_url, $striked);
     my ($journal_name, $journal, $userhead);
-    my ($attrs, $color);
-    my $profile_url = $opts->{'profile_url'};
-    my $side_alias  = $opts->{'side_alias'};
+    my ($attrs, $color, $user_alias, %user);
+    my $profile_url;
 
     if ( isu($user) ) {
         $u = $user;
@@ -8007,101 +8007,116 @@
         $username = $user;
     }
 
-    if ( $u and LJ::isu($u) ) {
-        # Traverse the renames to the final journal
-        unless ( $opts->{'no_follow'} ) {
-            $u = $u->get_renamed_user;
-            $username = $u->username;
-        }
+    {
+        if ( $u and LJ::isu($u) ) {
+            # Traverse the renames to the final journal
+            unless ( $opts->{'no_follow'} ) {
+                $u = $u->get_renamed_user;
+                $username = $u->username;
+            }
 
-        unless ( $profile_url ) {
-            $profile_url = $u->profile_url;
-            $profile_url .= '?mode=full' if $opts->{'full'};
-        }
+            last if $ljuser_cache->{$username};
 
-        # Mark accounts as deleted that aren't visible, memorial, locked, or
-        # read-only
-        if ( $u->statusvis !~ m![VMLO]! ) {
-           $striked = 1; 
-        }
+            # Mark accounts as deleted that aren't visible, memorial, locked, or
+            # read-only
+            if ( $u->statusvis !~ m![VMLO]! ) {
+               $striked = 1; 
+            }
 
-        $journal_name = $username;
-        $journal_url  = $u->journal_base . "/";
-        ($userhead)   = $u->userhead($opts);
+            $journal_name = $username;
+            $journal_url  = $u->journal_base . "/";
+            ($userhead)   = $u->userhead($opts);
 
-        # Identity
-        if ( $u->is_identity ) {
-            my $params = $u->identity->ljuser_display_params($u, $opts);
-            $profile_url  = $params->{'profile_url'}  || $profile_url;
-            $journal_url  = $params->{'journal_url'}  || $journal_url;
-            $journal_name = $params->{'journal_name'} || $journal_name;
-        }
-    } else {
-        $username      = LJ::canonical_username($username);
-        $journal_url   = join('', $LJ::SITEROOT, '/userinfo.bml?user=', $username);
-        $profile_url ||= $journal_url;
-        $userhead      = 'userinfo.gif';
-    }
+            # Identity
+            if ( $u->is_identity ) {
+                my $params = $u->identity->ljuser_display_params($u, $opts);
+                $profile_url  = $params->{'profile_url'};
+                $journal_url  = $params->{'journal_url'}  || $journal_url;
+                $journal_name = $params->{'journal_name'} || $journal_name;
+            }
 
-    my $user_alias = LJ::ljuser_alias($username);
-    my $alias      = ($user_alias and not $side_alias)? 1 : 0;
+            $profile_url  = $u->profile_url();
+        } else {
+            $username      = LJ::canonical_username($username);
 
-    # FIXME: try to remove this
-    if ( $opts->{'in_journal'} ) {
-        my $cu = LJ::load_user($opts->{'in_journal'});
-        if ( $cu ) {
-            $attrs = join('"', 'data-journal=', $cu->journal_base, '');
+            last if $ljuser_cache->{$username};
+
+            $journal_url   = join('', $LJ::SITEROOT, '/userinfo.bml?user=', $username);
+            $profile_url ||= $journal_url;
+            $userhead      = 'userinfo.gif';
         }
     }
 
-    # Userhead
-    unless ( $userhead =~ m!^https?:\/\/! ) {
-        $userhead   = join('',
-            $opts->{'imgroot'} || $LJ::IMGPREFIX,
-            '/', $userhead,
-            '?v=', $LJ::CURRENT_VERSION
-        );
-    }
-
     if ( $color = $opts->{'link_color'} ) {
         unless ( $color =~ /^#(?:[a-f0-9]{3}|[a-f0-9]{6})$/i ) {
             undef $color;
         }
     }
 
-    $opts->{'bold'} = 1 unless exists $opts->{'bold'};
-
-    my $params     = {
-        alias          => $alias,
+    my %user = %{ $ljuser_cache->{$username} ||= {
         attrs          => $attrs,
-        bold           => $opts->{'bold'}? 1 : 0,
-        color          => $color,
-        user_alias     => LJ::ehtml($user_alias),
-        side_alias     => $user_alias? $side_alias : undef,
-        target         => $opts->{'target'},
+        bold           => 0,
+        side_alias     => 0,
+        inline_css     => 0,
         username       => $username,
-        journal        => $opts->{'title'} || $journal_name,
+        journal        => $journal_name,
         striked        => $striked,
         journal_url    => $journal_url,
         profile_url    => $profile_url,
         userhead_url   => $userhead,
-    };
+    }};
 
-    if ( $opts->{'json'} ) {
-        return LJ::JSON->to_json($params);
-    } elsif ( $opts->{'raw'} ) {
-        return $params;
+    $user{'bold'}         = 1                      if $opts->{'bold'} or not exists $opts->{'bold'};
+    $user{'inline_css'}   = 1                      if $opts->{'inline_css'};
+    $user{'journal'}      = $opts->{'title'}       if $opts->{'title'};
+    $user{'target'}       = $opts->{'target'}      if $opts->{'target'};
+    $user{'profile_url'} .= '?mode=full'           if $opts->{'full'};
+    $user{'profile_url'}  = $opts->{'profile_url'} if $opts->{'profile_url'};
+    $user{'user_alias'}   = LJ::ehtml(LJ::ljuser_alias($username));
+    $user{'alias'}        = $user{'user_alias'}?   1 : 0;
+    $user{'color'}        = $color;
+
+    if ( $opts->{'side_alias'} and $user{'alias'} ) {
+        $user{'side_alias'} = 1;
+        $user{'alias'}      = 1;
+    }
+
+    # Userhead
+    unless ( $user{'userhead_url'} =~ m!^https?:\/\/! ) {
+        $user{'userhead_url'} = join('',
+            $opts->{'imgroot'} || $LJ::IMGPREFIX,
+            '/', $user{'userhead_url'},
+            '?v=', $LJ::CURRENT_VERSION
+        );
+    }
+
+    # FIXME: try to remove this
+    if ( $opts->{'in_journal'} ) {
+        my $cu = LJ::load_user($opts->{'in_journal'});
+        if ( $cu ) {
+            $user{'attrs'} = join('"', 'data-journal=', $cu->journal_base, '');
+        }
+    }
+
+    if ( $opts->{'raw'} ) {
+        return \%user;
     } else {
         return LJ::Response::CachedTemplate->new(
             path   => $ljuser_tmpl_path,
             file   => 'Display.tmpl',
-            params => $params,
+            params => \%user,
         )->raw_output();
     }
 } # ljuser2
 
-unless ( $LJ::DISABLED{'ljuser_templates'} ) {
-    *ljuser = \&ljuser2;
+if ( not $LJ::DISABLED{'ljuser_templates'} ) {
+    if ( $ENV{'MOD_PERL'} ) {
+        # Sometimes we need style inlined
+        # In emails, for example
+        *ljuser = sub { ljuser2($_[0], { %{ $_[1] || {} }, inline_css => 1 }) };
+    } else {
+        *ljuser = \&ljuser2;
+    }
 }
 
 sub set_email {

Tags: anazarov, livejournal, nfokz, 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