mark: A photo of Mark kneeling on top of the Taal Volcano in the Philippines. It was a long hike. (Default)
Mark Smith ([staff profile] mark) wrote in [site community profile] changelog2009-08-27 05:17 am

[dw-free] Lacks function to return a list of sorted trust groups

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

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

Awesome code cleanup; de-duplicate the security group display code.

Patch by [personal profile] kareila.

Files modified:
  • cgi-bin/LJ/Entry.pm
  • cgi-bin/LJ/User.pm
  • htdocs/preview/entry.bml
--------------------------------------------------------------------------------
diff -r 57744754f564 -r 1bfddd15a0c0 cgi-bin/LJ/Entry.pm
--- a/cgi-bin/LJ/Entry.pm	Wed Aug 26 21:08:37 2009 -0500
+++ b/cgi-bin/LJ/Entry.pm	Thu Aug 27 05:17:36 2009 +0000
@@ -942,27 +942,7 @@ sub group_names {
     my $poster = $self->poster;
     return "" unless $remote && $poster && $poster->equals( $remote );
 
-    my %group_ids = ( map { $_ => 1 } grep { $self->allowmask & ( 1 << $_ ) } 1..60 );
-    return "" unless scalar( keys %group_ids ) > 0;
-
-    my $groups = $poster->trust_groups || {};
-    if ( keys %$groups ) {
-        my @friendgroups = ();
-
-        foreach my $groupid (keys %$groups) {
-            next unless $group_ids{$groupid};
-
-            my $name = LJ::ehtml($groups->{$groupid}->{groupname});
-            my $url = LJ::eurl($poster->journal_base . "/security/group:$name");
-
-            my $group_text = $remote->get_cap("security_filter") || $poster->get_cap("security_filter") ? "<a href='$url'>$name</a>" : $name;
-            push @friendgroups, $group_text;
-        }
-
-        return join(', ', @friendgroups) if @friendgroups;
-    }
-
-    return "";
+    return $poster->security_group_display( $self->allowmask );
 }
 
 sub statusvis {
diff -r 57744754f564 -r 1bfddd15a0c0 cgi-bin/LJ/User.pm
--- a/cgi-bin/LJ/User.pm	Wed Aug 26 21:08:37 2009 -0500
+++ b/cgi-bin/LJ/User.pm	Thu Aug 27 05:17:36 2009 +0000
@@ -3592,6 +3592,37 @@ sub recent_entries {
 }
 
 
+sub security_group_display {
+    my ( $u, $allowmask ) = @_;
+    return '' unless LJ::isu( $u );
+    return '' unless defined $allowmask;
+
+    my $remote = LJ::get_remote() or return '';
+    my $use_urls = $remote->get_cap( "security_filter" ) || $u->get_cap( "security_filter" );
+
+    # see which group ids are in the security mask
+    my %group_ids = ( map { $_ => 1 } grep { $allowmask & ( 1 << $_ ) } 1..60 );
+    return '' unless scalar( keys %group_ids ) > 0;
+
+    my @ret;
+
+    my $groups = $u->trust_groups || {};
+    foreach my $groupid ( keys %$groups ) {
+        next unless $group_ids{$groupid};  # not in mask
+
+        my $name = LJ::ehtml( $groups->{$groupid}->{groupname} );
+        if ( $use_urls ) {
+            my $url = LJ::eurl( $u->journal_base . "/security/group:$name" );
+            push @ret, "<a href='$url'>$name</a>";
+        } else {
+            push @ret, $name;
+        }
+    }
+
+    return join( ', ', @ret ) if @ret;
+}
+
+
 sub set_draft_text {
     my ($u, $draft) = @_;
     my $old = $u->draft_text;
diff -r 57744754f564 -r 1bfddd15a0c0 htdocs/preview/entry.bml
--- a/htdocs/preview/entry.bml	Wed Aug 26 21:08:37 2009 -0500
+++ b/htdocs/preview/entry.bml	Thu Aug 27 05:17:36 2009 +0000
@@ -169,26 +169,8 @@
         }    
 
         # custom friend groups
-        my %group_ids = ( map { $_ => 1 } grep { $req{allowmask} & ( 1 << $_ ) } 1..60 );
-        
-        if ( scalar( keys %group_ids ) > 0 ) {
-            my $groups = $u->trust_groups || {};
-            if ( keys %$groups ) {
-                my @trust_groups = ();
-        
-                foreach my $groupid ( keys %$groups ) {
-                    next unless $group_ids{$groupid};
-        
-                    my $name = LJ::ehtml( $groups->{$groupid}->{groupname} );
-                    my $url = LJ::eurl( $u->journal_base . "/security/group:$name" );
-        
-                    my $group_text = $u->get_cap( "security_filter" ) ? "<a href='$url'>$name</a>" : $name;
-                    push @trust_groups, $group_text;
-                }
-        
-                $current{Groups} = join( ', ', @trust_groups ) if @trust_groups;
-            }        
-        }
+        $current{Groups} = $u->security_group_display( $req{allowmask} );
+        delete $current{Groups} unless $current{Groups};
 
         my @taglist = ();
         LJ::Tags::is_valid_tagstring( $POST{prop_taglist}, \@taglist );
@@ -288,27 +270,7 @@
             foreach ( qw( current_music current_location current_coords current_moodid current_mood ) );
 
         # custom friends groups
-        my $group_names;
-        my %group_ids = ( map { $_ => 1 } grep { $req{allowmask} & ( 1 << $_ ) } 1..60 );
-        
-        if ( scalar( keys %group_ids ) > 0 ) {
-            my $groups = $u->trust_groups || {};
-            if ( keys %$groups ) {
-                my @trust_groups = ();
-        
-                foreach my $groupid ( keys %$groups ) {
-                    next unless $group_ids{$groupid};
-        
-                    my $name = LJ::ehtml( $groups->{$groupid}->{groupname} );
-                    my $url = LJ::eurl( $u->journal_base . "/security/group:$name" );
-        
-                    my $group_text = $u->get_cap( "security_filter" ) ? "<a href='$url'>$name</a>" : $name;
-                    push @trust_groups, $group_text;
-                }
-        
-                $group_names = join( ', ', @trust_groups ) if @trust_groups;
-            }        
-        }
+        my $group_names = $u->security_group_display( $req{allowmask} ) || undef;
 
         # format it
         my $raw_subj = $req{'subject'};
--------------------------------------------------------------------------------