Add an interest with one click from the results page, even when no one 
else is interested in it (the interest doesn't exist yet).

         Patch from <user name="yvi"> with some tweaks by <user 
name="kareila">. 

diffs (170 lines):

diff -r a84acdcd9510 -r 8bdc021088c3 cgi-bin/LJ/Keywords.pm
--- a/cgi-bin/LJ/Keywords.pm    Thu Dec 09 14:28:10 2010 +0800
+++ b/cgi-bin/LJ/Keywords.pm    Thu Dec 09 19:12:51 2010 +0800
@@ -100,6 +100,23 @@ sub interest_string_to_list {
 
     # final list is ,-sep
     return grep { length } split (/\s*,\s*/, $intstr);
+}
+
+
+# insert new interest into interest table, return new interest id
+# input: keyword of interest
+sub new_interest_from_kw {
+    my ( $keyword ) = @_;
+    my $intid = LJ::get_sitekeyword_id( $keyword );
+    return undef unless $intid;
+
+    # add zero intcount if this is a new interest
+    my $dbh = LJ::get_db_writer();
+    $dbh->do( "INSERT IGNORE INTO interests (intid, intcount) VALUES (?,?)",
+            undef, $intid, 0 );
+    die $dbh->errstr if $dbh->err;
+
+    return $intid;
 }
 
 
@@ -322,22 +339,15 @@ sub set_interests {
     ### do we have new interests to add?
     my @new_intids = ();  ## existing IDs we'll add for this user
     if ( %int_new ) {
-        $did_mod = 1;
-
-        my $int_in = join(", ", map { $dbh->quote($_); } keys %int_new);
-
-        ## find existing IDs
-        my $sth = $dbh->prepare( "SELECT keyword, kwid FROM sitekeywords " .
-                                 "WHERE keyword IN ($int_in)" );
-        $sth->execute;
-        while (my ($intr, $intid) = $sth->fetchrow_array) {
-            push @new_intids, $intid;       # - we'll add this later.
-            delete $int_new{$intr};         # - so we don't have to make a new 
intid for
-                                            #   this next pass.
+        foreach my $int ( keys %int_new ) {
+            my $intid = LJ::new_interest_from_kw( $int ) ;
+            push @new_intids, $intid if $intid;
         }
 
-        ## do updating en masse for interests that already exist
+        ## do updating en masse for interests
         if ( @new_intids ) {
+            $did_mod = 1;
+
             my $sql = "REPLACE INTO $uitable (userid, intid) VALUES ";
             $sql .= join( ", ", map { "($userid, $_)" } @new_intids );
             $dbh->do( $sql );
@@ -347,26 +357,6 @@ sub set_interests {
         }
     }
 
-    ### do we STILL have interests to add?  (must make new intids)
-    if ( %int_new ) {
-        foreach my $int ( keys %int_new ) {
-            my $intid = LJ::get_sitekeyword_id( $int );
-            next unless $intid;
-
-            my $rows = $dbh->do( "UPDATE interests SET intcount=intcount+1 
WHERE intid=?",
-                                 undef, $intid );
-            if ( $rows eq "0E0") {
-                # newly created
-                $dbh->do( "INSERT INTO interests (intid, intcount) VALUES 
(?,?)",
-                          undef, $intid, 1 );
-            }
-            next if $dbh->err;
-            ## now we can actually insert it into the userinterests table:
-            $dbh->do( "INSERT INTO $uitable (userid, intid) VALUES (?,?)",
-                      undef, $userid, $intid );
-            push @new_intids, $intid;
-        }
-    }
     LJ::Hooks::run_hooks("set_interests", $u, \%int_del, \...@new_intids); # 
interest => intid
 
     # do migrations to clean up userinterests vs comminterests conflicts
diff -r a84acdcd9510 -r 8bdc021088c3 htdocs/interests.bml
--- a/htdocs/interests.bml      Thu Dec 09 14:28:10 2010 +0800
+++ b/htdocs/interests.bml      Thu Dec 09 19:12:51 2010 +0800
@@ -80,16 +80,39 @@ body<=
         return $ret;
     }
 
-    if ((!$did_post && $GET{'mode'} eq "add" && $GET{'intid'}) ||
-        ($did_post && $POST{'mode'} eq "add" && $POST{'intid'})) {
-
-        my $intid = $did_post ? $POST{'intid'}+0 : $GET{'intid'}+0;
+    my $formargs = $did_post ? \%POST : \%GET;
+    if ( $formargs->{mode} &&
+         ( $formargs->{mode} eq "add" && $formargs->{intid} ||
+           $formargs->{mode} eq "addnew" && $formargs->{keyword} ) ) {
 
         my $ret = '';
-        unless ($remote) {
+        unless ( $remote ) {
             $ret .= "<?h1 $ML{'Error'} h1?><?p $ML{'.error.add.mustlogin'} 
p?>";
             return $ret;
         }
+
+        # account for the case where we bypass the POST due to the referer
+        return $ML{'error.invalidform'} unless !$did_post || 
LJ::check_form_auth();
+
+        my $rints = $remote->get_interests();
+        my $count = scalar( @$rints );
+
+        if ($count >= $maxinterests) {
+            $ret .= "<?h1 $ML{'.add.toomany.head'} h1?><?p " .BML::ml( 
".add.toomany.text", { maxinterests => $maxinterests } ) ." p?>";
+            return $ret;
+        }
+
+        my $intid;
+        if ( $formargs->{mode} eq "add" ) {
+            # adding an existing interest, so we have an intid to work with
+            $intid = $formargs->{intid} + 0;
+        } else {
+            # adding a new interest
+            my @validate = LJ::validate_interest_list( $formargs->{keyword} );
+            $intid = LJ::new_interest_from_kw( $validate[0] ) if @validate;
+        }
+
+        return $ML{'error.invalidform'} unless $intid;
 
         # force them to either come from the interests page, or have posted 
the request.
         # if both fail, ask them to confirm with a post form.
@@ -106,17 +129,6 @@ body<=
             $ret .= LJ::html_submit(undef, BML::ml(".add.btn.text", 
{'interest' => $int}));
             $ret .= LJ::form_auth();
             $ret .= "</div></form> p?>";
-            return $ret;
-        }
-
-        my $rints = $remote->get_interests();
-        my $count = scalar(@$rints);
-
-        # account for the case where we bypass the POST due to the referer
-        return $ML{'error.invalidform'} unless !$did_post || 
LJ::check_form_auth();
-
-        if ($count >= $maxinterests) {
-            $ret .= "<?h1 $ML{'.add.toomany.head'} h1?><?p " 
.BML::ml(".add.toomany.text", {'maxinterests' => $maxinterests}) ." p?>";
             return $ret;
         }
 
@@ -429,7 +441,7 @@ body<=
             $ret .= "<?h1 $ML{'.nocomms.header'} h1?><?p ";
             $ret .= BML::ml( '.nocomms.text', { 'int' => $e_int, 'aopts' => 
qq(href="$LJ::SITEROOT/community/create") } );
             $ret .= " p?><?h1 $ML{'.nousers.header'} h1?><?p ";
-            $ret .= BML::ml( '.nousers.text', { 'int' => $e_int, 
'aopts_manage' => "href='$LJ::SITEROOT/manage/profile/'", 'aopts_int' => 
"href='$LJ::SITEROOT/interests'" } );
+            $ret .= BML::ml( '.nousers.text2', { int => $e_int, aopts_add => 
"href='$LJ::SITEROOT/interests?mode=addnew&amp;keyword=$e_int'", aopts_int => 
"href='$LJ::SITEROOT/interests'" } );
             $ret .= " p?>";
             return $ret;
         } 
diff -r a84acdcd9510 -r 8bdc021088c3 htdocs/interests.bml.text
--- a/htdocs/interests.bml.text Thu Dec 09 14:28:10 2010 +0800
+++ b/htdocs/interests.bml.text Thu Dec 09 19:12:51 2010 +0800
@@ -128,7 +128,7 @@ When you're done, click "Save Changes".
 
 .nousers.header=Interested users
 
-.nousers.text=There are no users interested in <b>[[int]]</b>. If you are 
interested in this and would like to be added to this list, <a 
[[aopts_manage]]>click here</a>. More fun stuff can be found on the <a 
[[aopts_int]]>interests page</a>.
+.nousers.text2=There are no users interested in "<b>[[int]]</b>". If you are 
interested in this, <a [[aopts_add]]>click here to add the interest to your 
profile</a>. More fun stuff can be found on the <a [[aopts_int]]>interests 
page</a>.
 
 .popular.disabled=Sorry, the popular interests feature is unavailable.
 
_______________________________________________
dw-changelog mailing list
[email protected]
http://lists.dwscoalition.org/mailman/listinfo/dw-changelog

Reply via email to