fu: Close-up of Fu, bringing a scoop of water to her mouth (Default)
fu ([personal profile] fu) wrote in [site community profile] changelog2010-09-21 02:20 pm

[dw-free] cleaning up userpics code

[commit: http://hg.dwscoalition.org/dw-free/rev/f1f9d6f349bc]

http://bugs.dwscoalition.org/show_bug.cgi?id=513

Remove LJ::Talk::load_userpics from Apache/LiveJournal.pm. Use methods for
more things to maintain consistency. Add option which controls whether we
also want to load expunged userpics or not.

Patch by [personal profile] kareila.

Files modified:
  • cgi-bin/Apache/LiveJournal.pm
  • cgi-bin/LJ/Userpic.pm
--------------------------------------------------------------------------------
diff -r 0c0b0efdf409 -r f1f9d6f349bc cgi-bin/Apache/LiveJournal.pm
--- a/cgi-bin/Apache/LiveJournal.pm	Tue Sep 21 21:18:16 2010 +0800
+++ b/cgi-bin/Apache/LiveJournal.pm	Tue Sep 21 22:20:43 2010 +0800
@@ -39,7 +39,6 @@ use LJ::URI;
 use LJ::URI;
 use DW::Routing;
 use DW::Template;
-use LJ::Talk;
 
 BEGIN {
     $LJ::OPTMOD_ZLIB = eval "use Compress::Zlib (); 1;";
@@ -1136,22 +1135,16 @@ sub userpic_content
 
     # Load the user object and pic and make sure the picture is viewable
     my $u = LJ::load_userid($userid);
-    return NOT_FOUND unless $u && ! ( $u->is_expunged || $u->is_suspended );
-
-    my %upics;
-    LJ::Talk::load_userpics(\%upics, [ $u, $picid ]);
-    my $pic = $upics{$picid} or return NOT_FOUND;
-    return NOT_FOUND if $pic->{'userid'} != $userid || $pic->{state} eq 'X';
+    my $pic = LJ::Userpic->get( $u, $picid, { no_expunged => 1 } )
+        or return NOT_FOUND;
 
     # Read the mimetype from the pichash if dversion 7
-    $mime = { 'G' => 'image/gif',
-              'J' => 'image/jpeg',
-              'P' => 'image/png', }->{$pic->{fmt}};
+    $mime = $pic->mimetype;
 
     ### Handle reproxyable requests
 
     # For dversion 7+ and mogilefs userpics, follow this path
-    if ($pic->{location} eq 'M' ) {  # 'M' for mogilefs
+    if ( $pic->in_mogile ) {
         my $key = $u->mogfs_userpic_key( $picid );
 
         if ( !$LJ::REPROXY_DISABLE{userpics} &&
@@ -1219,12 +1212,12 @@ sub userpic_content
 
     # else, get it from db.
     unless ($data) {
-        $lastmod = $pic->{'picdate'};
+        $lastmod = $pic->picdate;
 
         my $dbb = LJ::get_cluster_reader( $u );
         return SERVER_ERROR unless $dbb;
         $data = $dbb->selectrow_array( "SELECT imagedata FROM userpicblob2 WHERE " .
-                                       "userid=$pic->{userid} AND picid=$picid" );
+                                       "userid=$userid AND picid=$picid" );
     }
 
     return NOT_FOUND unless $data;
@@ -1233,7 +1226,7 @@ sub userpic_content
         # make $realfile /userpic-userid, and $file /userpic
         my $realfile = $file;
         unless ($file =~ s/-\d+$//) {
-            $realfile .= "-$pic->{'userid'}";
+            $realfile .= "-$userid";
         }
 
         # delete short file on Unix if it exists
diff -r 0c0b0efdf409 -r f1f9d6f349bc cgi-bin/LJ/Userpic.pm
--- a/cgi-bin/LJ/Userpic.pm	Tue Sep 21 21:18:16 2010 +0800
+++ b/cgi-bin/LJ/Userpic.pm	Tue Sep 21 22:20:43 2010 +0800
@@ -83,9 +83,9 @@ sub instance {
 # undef if userpic doesn't exist in the db.
 # TODO: add in lazy peer loading here?
 sub get {
-    my ( $class, $u, $picid ) = @_;
+    my ( $class, $u, $picid, $opts ) = @_;
     return unless LJ::isu( $u );
-    return if $u->is_expunged;
+    return if $u->is_expunged || $u->is_suspended;
 
     my $obj = ref $class ? $class : $class->new( $u, $picid );
     my @cache = $class->load_user_userpics( $u );
@@ -96,6 +96,7 @@ sub get {
 
     # check the database directly (for expunged userpics,
     # which aren't included in load_user_userpics)
+    return undef if $opts && $opts->{no_expunged};
     my $row = $u->selectrow_hashref( "SELECT userid, picid, width, height, state, " .
                                      "fmt, comment, description, location, url, " .
                                      "UNIX_TIMESTAMP(picdate) AS 'pictime', flags, md5base64 " .
@@ -245,6 +246,11 @@ sub height {
     return undef unless @dims;
     return $dims[1];
 }
+
+sub picdate {
+    return LJ::mysql_time( $_[0]->pictime );
+}
+
 sub pictime {
     return $_[0]->{pictime};
 }
@@ -255,6 +261,13 @@ sub flags {
 
 sub md5base64 {
     return $_[0]->{md5base64};
+}
+
+sub mimetype {
+    my $self = $_[0];
+    return { gif => 'image/gif',
+             jpg => 'image/jpeg',
+             png => 'image/png' }->{ $self->extension };
 }
 
 sub extension {
--------------------------------------------------------------------------------