Андрей (andy) wrote in changelog,
Андрей
andy
changelog

[ljcom] r9865: LJINT-362 (Comments for side projects): ...

Committer: ailyin
LJINT-362 (Comments for side projects): checkpoint commit; the scheme with redirects/iframes works now
U   trunk/cgi-bin/LJ/ExternalComments.pm
U   trunk/htdocs/identity/callback-facebook.bml
U   trunk/htdocs/identity/callback-twitter.bml
U   trunk/htdocs/identity/facebook-interstitial.bml
U   trunk/htdocs/identity/twitter-interstitial.bml
U   trunk/htdocs/tools/endpoints/comments.bml
Modified: trunk/cgi-bin/LJ/ExternalComments.pm
===================================================================
--- trunk/cgi-bin/LJ/ExternalComments.pm	2010-12-20 07:28:55 UTC (rev 9864)
+++ trunk/cgi-bin/LJ/ExternalComments.pm	2010-12-20 07:42:44 UTC (rev 9865)
@@ -54,11 +54,6 @@
 
     die "No url" unless $url;
 
-    ## TODO: ensure that url suited to partners domain mask !!!
-    my $mask = $LJ::PARTNER_DOMAIN_MASK{ $u->username };
-    die "No domain mask for the user '" . $u->username . "'." unless $mask;
-    die "Unsupported url" unless $url =~ $mask;
-
     my $flags = {
         u => $u,
         noauth => 1,

Modified: trunk/htdocs/identity/callback-facebook.bml
===================================================================
--- trunk/htdocs/identity/callback-facebook.bml	2010-12-20 07:28:55 UTC (rev 9864)
+++ trunk/htdocs/identity/callback-facebook.bml	2010-12-20 07:42:44 UTC (rev 9865)
@@ -80,7 +80,8 @@
     return LJ::Request->redirect($returl) unless $created;
 
     my $redirect_url = "$LJ::SITEROOT/identity/facebook-interstitial.bml?" .
-                       'ret=' . LJ::eurl($returl);
+                       'ret=' . LJ::eurl($returl) . '&' .
+                       'forwhat=' . LJ::eurl($forwhat);
 
     return LJ::Request->redirect($redirect_url);
 

Modified: trunk/htdocs/identity/callback-twitter.bml
===================================================================
--- trunk/htdocs/identity/callback-twitter.bml	2010-12-20 07:28:55 UTC (rev 9864)
+++ trunk/htdocs/identity/callback-twitter.bml	2010-12-20 07:42:44 UTC (rev 9865)
@@ -58,7 +58,8 @@
     return LJ::Request->redirect($returl) unless $created;
 
     my $redirect_url = "$LJ::SITEROOT/identity/twitter-interstitial.bml?" .
-                       'ret=' . LJ::eurl($returl);
+                       'ret=' . LJ::eurl($returl) . '&' .
+                       'forwhat=' . LJ::eurl($forwhat);
 
     return LJ::Request->redirect($redirect_url);
 }

Modified: trunk/htdocs/identity/facebook-interstitial.bml
===================================================================
--- trunk/htdocs/identity/facebook-interstitial.bml	2010-12-20 07:28:55 UTC (rev 9864)
+++ trunk/htdocs/identity/facebook-interstitial.bml	2010-12-20 07:42:44 UTC (rev 9865)
@@ -18,25 +18,19 @@
         return LJ::Request->redirect("$LJ::SITEROOT/login.bml");
     }
 
-    my $returl = LJ::Request->get_param('ret') || $LJ::SITEROOT;
+    my $returl  = LJ::Request->get_param('ret') || $LJ::SITEROOT;
+    my $forwhat = LJ::Request->get_param('forwhat') || 'login';
 
+    my $external_site_case = ( $forwhat =~ /^external-/ );
+
     my @errors;
 
-    my $handle_post = sub {
-        if (!LJ::check_form_auth()) {
-            push @errors, LJ::Lang::ml('error.invalidform');
-            return;
-        }
+    my $subscribe = sub {
+        my ($u) = @_;
 
-        if (!LJ::Request->post_param('subscribe')) {
-            return LJ::Request->redirect($returl);
-        }
-
         # copypasta from LJ::Hooks::CreatePage with adjustments
         # possibly refactor to reduce code duplication?
 
-        my $u = $remote;
-
         # we are not anymore using this. ugh.
         LJ::update_user($u, {'opt_gettalkemail' => 'N'});
 
@@ -49,7 +43,14 @@
             'Birthday',
             'NewVGift',
         );
-        
+
+        ## LJINT-362 (Comments for side projects):
+        ## in case an account is created on an external site, only
+        ## subscribe them to comment replies
+        if ( $external_site_case ) {
+            @subs = qw( CommentReply );
+        }
+
         my $set = LJ::Subscription::GroupSet->fetch_for_user($u);
         my $newset = $set->clone;
 
@@ -97,6 +98,26 @@
         return LJ::Request->redirect($returl);
     };
 
+    my $handle_post = sub {
+        if (!LJ::check_form_auth()) {
+            push @errors, LJ::Lang::ml('error.invalidform');
+            return;
+        }
+
+        if (!LJ::Request->post_param('subscribe')) {
+            return LJ::Request->redirect($returl);
+        }
+
+        return $subscribe->($remote);
+    };
+
+    ## LJINT-362 (Comments for side projects):
+    ## in case an account is created on an external site, subscribe
+    ## them to comment replies and redirect back immediately
+    if ( $external_site_case ) {
+        return $subscribe->($remote);
+    }
+
     if (LJ::Request->did_post) {
         $handle_post->();
         return if LJ::Request->redirected;

Modified: trunk/htdocs/identity/twitter-interstitial.bml
===================================================================
--- trunk/htdocs/identity/twitter-interstitial.bml	2010-12-20 07:28:55 UTC (rev 9864)
+++ trunk/htdocs/identity/twitter-interstitial.bml	2010-12-20 07:42:44 UTC (rev 9865)
@@ -1,8 +1,6 @@
-<?page
-body<=
 <?_code
 {
-#line 6
+#line 4
     use strict;
     use vars qw($title);
 
@@ -18,8 +16,11 @@
         return LJ::Request->redirect("$LJ::SITEROOT/login.bml");
     }
 
-    my $returl = LJ::Request->get_param('ret') || $LJ::SITEROOT;
+    my $returl  = LJ::Request->get_param('ret') || $LJ::SITEROOT;
+    my $forwhat = LJ::Request->get_param('forwhat') || 'login';
 
+    my $external_site_case = ( $forwhat =~ /^external-/ );
+
     my @errors;
     my $step2 = 0;
 
@@ -100,7 +101,14 @@
             'Birthday',
             'NewVGift',
         );
-        
+
+        ## LJINT-362 (Comments for side projects):
+        ## in case an account is created on an external site, only
+        ## subscribe them to comment replies
+        if ( $external_site_case ) {
+            @subs = qw( CommentReply );
+        }
+
         my $set = LJ::Subscription::GroupSet->fetch_for_user($u);
         my $newset = $set->clone;
 
@@ -148,14 +156,66 @@
         $step2 = 1;
     };
 
+    ## LJINT-362 (Comments for side projects):
+    ## for external sites, we're actually redirecting to
+    ## the iframe page, not to $returl
+    if ( $external_site_case ) {
+        my (undef, $journalid, $ditemid, $dtalkid) = split /-/, $forwhat;
+        my $journal = LJ::load_userid($journalid);
+        my $entry = LJ::Entry->new($journal, 'ditemid' => $ditemid);
+
+        my $uri = URI->new( "$LJ::SITEROOT/gadgets/logcom.bml" );
+        $uri->query_form( $uri->query_form,
+                          'url' => $entry->prop('external_url'),
+                          'rsk' => LJ::Request->get_param('rsk') );
+
+        if ($dtalkid) {
+            $uri->query_form( $uri->query_form, 'replyto' => $dtalkid );
+        }
+
+        $returl = $uri->as_string;
+    }
+
     if (LJ::Request->did_post) {
         $handle_post->();
         return if LJ::Request->redirected;
+
+        ## LJINT-362 (Comments for side projects):
+        ## for external sites, there is no step 2; just
+        ## redirect them to the appropriate iframe
+        if ( $external_site_case && $step2 ) {
+            return LJ::Request->redirect( $returl );
+        }
     }
 
+    my $mode = LJ::Request->get_param('mode');
+    $mode = 'full' unless defined $mode;
+
+    ## LJINT-362 (Comments for side projects):
+    ## in case an account is created on an external site, redirect them
+    ## back to a page that shows the same interstitial in an iframe
+    if ( $external_site_case && $mode ne 'compact' ) {
+        my (undef, $journalid, $ditemid, $dtalkid) = split /-/, $forwhat;
+        my $journal = LJ::load_userid($journalid);
+        my $entry = LJ::Entry->new($journal, 'ditemid' => $ditemid);
+
+        my $uri = URI->new( $entry->prop('external_url') );
+        if ($dtalkid) {
+            $uri->query_form( $uri->query_form, 'replyto' => $dtalkid );
+            $uri->fragment( 't' . $dtalkid );
+        }
+        $uri->query_form( $uri->query_form,
+                          'show' => 'twitter-interstitial/' . $forwhat );
+
+        return LJ::Request->redirect( $uri->as_string );
+    }
+
+    my $template_file = $mode eq 'compact' ? 'TwitterInterstitialCompact.tmpl'
+                                           : 'TwitterInterstitial.tmpl';
+
     my $template = LJ::HTML::Template->new(
         { use_expr => 1 }, # force HTML::Template::Pro with Expr support
-        filename => "$ENV{'LJHOME'}/templates/Identity/TwitterInterstitial.tmpl",
+        filename => "$ENV{'LJHOME'}/templates/Identity/$template_file",
         die_on_bad_params => 0,
         strict => 0,
     ) or die "Can't open template: $!";
@@ -170,12 +230,13 @@
         'ml_validation' => LJ::Lang::ml('.validation', { resend_url => "$LJ::SITEROOT/register.bml" }),
     );
 
-    return $template->output;
+    if ( $mode eq 'compact' ) {
+        return $template->output;
+    } else {
+        return BML::render_page({
+            'body' => $template->output,
+            'title' => $title,
+        });
+    }
 }
 _code?>
-<=body
-title=><?_code return $title; _code?>
-head<=
-<?_code return $headextra; _code?>
-<=head
-page?>

Modified: trunk/htdocs/tools/endpoints/comments.bml
===================================================================
--- trunk/htdocs/tools/endpoints/comments.bml	2010-12-20 07:28:55 UTC (rev 9864)
+++ trunk/htdocs/tools/endpoints/comments.bml	2010-12-20 07:42:44 UTC (rev 9865)
@@ -2,6 +2,7 @@
     ## return a comments to journal entry based on external uniq id
     ##
     use strict;
+    use warnings;
     use Data::Dumper;
     use LJ::ExternalComments;
 
@@ -12,14 +13,16 @@
 
     my $url     = $GET{url};
     my $rskey   = $GET{rsk};
-    my $page    = int $GET{page}    || 0;
-    my $thread  = int $GET{thread}  || undef;
-    my $view    = int $GET{view}    || undef;
+    my $page    = int ( $GET{'page'}    || 0 );
+    my $thread  = int ( $GET{'thread'}  || 0 );
+    my $view    = int ( $GET{'view'}    || 0 ) || undef;
 
     my $format  = $GET{'out'};
     $format = 'JSONP' unless defined $format
                          and $format =~ /^(?:JSON|JSONP)$/;
 
+    my $username;
+
     ## wrapper and serialazer.
     my $answer = sub {
         my $data = shift;
@@ -28,12 +31,41 @@
         if ( $format eq 'JSON' ) {
             return $data_out;
         } elsif ( $format eq 'JSONP' ) {
-            return "JSONP($data_out)";
+            my $domains = $LJ::PARTNER_DOMAINS{$username};
+            my $domains_out = LJ::JSON->to_json($domains);
+
+            my $funcname = LJ::Request->get_param('callback'); 
+            $funcname = 'JSONP'
+                unless defined $funcname && $funcname;
+
+            return qq[
+                var trustedDomains = $domains_out;
+                    
+                var domainMatch = checkDomain(window.location.href, trustedDomains);
+
+                if (domainMatch) {
+                    $funcname($data_out);
+                } else {
+                    window.location.href = 'about:blank';
+                }
+
+                function checkDomain(href, trustedDomains) {
+                    var currentDomain = href.match(] .q{/(http\:\/\/)(?:www\.)?([^\/]*)/} . qq[)[2];
+                    
+                    for (var i = 0, l = trustedDomains.length; i < l; i++) {
+                        if (trustedDomains[i] == currentDomain) {
+                            return true;
+                        }
+                    }
+                    
+                    return false;
+                }
+            ];
         }
     };
 
     ## convert remote-site key to lj.com's community
-    my $username = LJ::ExternalComments->key_to_username($rskey);
+    $username = LJ::ExternalComments->key_to_username($rskey);
     return $answer->({error => "unknown key"})
         unless $username;
 
@@ -41,6 +73,10 @@
     return $answer->({error => "unknown user"})
         unless $journal;
 
+    return 'rate limit exceeded'
+        unless LJ::RateLimit->check( $journal,
+                                     $LJ::PARTNER_RATE_LIMITS{$username} );
+
     ## convert article's uri on partners site to entryid on LJ.com
     my $jitemid = LJ::ExternalComments->url_to_jitemid($journal, $url);
     return $answer->({ comments => [] })

Tags: andy, bml, ljcom, 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