Committer: ailyin
LJINT-362 (Comments for side projects); [tags: aralot]A trunk/bin/worker/partner-sites A trunk/cgi-bin/LJ/Hooks/PartnerSites.pm U trunk/cgi-bin/LJ/PartnerSite.pm
Added: trunk/bin/worker/partner-sites =================================================================== --- trunk/bin/worker/partner-sites (rev 0) +++ trunk/bin/worker/partner-sites 2011-01-21 10:33:23 UTC (rev 9934) @@ -0,0 +1,63 @@ +#!/usr/bin/perl +use strict; +use warnings; + +use lib "$ENV{'LJHOME'}/cgi-bin"; +require 'ljlib.pl'; +use LJ::NewWorker::TheSchwartz; + +package LJ::Worker::PartnerSites; +use base qw( LJ::NewWorker::TheSchwartz ); + +sub capabilities { + return qw( + LJ::Worker::PartnerSites::SyncCommentsCount + ); +} + +__PACKAGE__->start; + +package LJ::Worker::PartnerSites::SyncCommentsCount; +use base qw( TheSchwartz::Worker ); + +sub work { + my ( $class, $job ) = @_; + + my $arg = $job->arg; + + my $journal = LJ::load_userid( $arg->{'journalid'} ); + die 'could not load journal #' . $arg->{'journalid'} + unless $journal; + + my $entry = LJ::Entry->new( $journal, 'jitemid' => $arg->{'jitemid'} ); + die 'could not find entry with jitemid=' . $arg->{'jitemid'} . + ' in ' . $journal->username + unless $entry and $entry->valid; + + my $partner + = LJ::PartnerSite->find_by_journal_username( $journal->username ); + die 'could not find partner for ' . $journal->username + unless $partner; + + unless ( $partner->needs_sync_comments_count ) { + warn $partner->name . + ' doesn\'t need to sync comments count, skipping'; + + return $job->completed; + } + + my $url = $partner->sync_comments_url_from_entry($entry); + my $ua = LJ::get_useragent( 'role' => 'partners-sync-comments-count' ); + + my $res = $ua->get($url); + + unless ( $res->is_success ) { + die 'failed to ping ' . $partner->name . + ' about ' . $entry->url . '; ' . + 'error code: ' . $res->status_line; + } + + return $job->completed; +} + +1; Property changes on: trunk/bin/worker/partner-sites ___________________________________________________________________ Added: svn:executable + * Added: trunk/cgi-bin/LJ/Hooks/PartnerSites.pm =================================================================== --- trunk/cgi-bin/LJ/Hooks/PartnerSites.pm (rev 0) +++ trunk/cgi-bin/LJ/Hooks/PartnerSites.pm 2011-01-21 10:33:23 UTC (rev 9934) @@ -0,0 +1,30 @@ +package LJ::Hooks::PartnerSites; +use strict; +use warnings; + +use LJ::PartnerSite; + +LJ::register_hook( 'replycount_change' => sub { + my ( $entry ) = @_; + + my $journal = $entry->journal; + + my $partner + = LJ::PartnerSite->find_by_journal_username( $journal->username ); + + return unless $partner && $partner->needs_sync_comments_count; + + my $sclient = LJ::theschwartz(); + my $job = TheSchwartz::Job->new( + 'funcname' => 'LJ::Worker::PartnerSites::SyncCommentsCount', + 'arg' => { + 'journalid' => $journal->userid, + 'jitemid' => $entry->jitemid, + }, + ); + $sclient->insert($job); + + warn "here"; +}); + +1; Modified: trunk/cgi-bin/LJ/PartnerSite.pm =================================================================== --- trunk/cgi-bin/LJ/PartnerSite.pm 2011-01-21 07:26:22 UTC (rev 9933) +++ trunk/cgi-bin/LJ/PartnerSite.pm 2011-01-21 10:33:23 UTC (rev 9934) @@ -177,6 +177,18 @@ disabled: disable this partner; when this is true, all calls to find_entry_by_docid return undef +=item * + +dochash_salt: a salt needed to calculate dochashes (that are essentially +signatures in cross-server communication) + +=item * + +sync_comments_count_url_pattern: a URL which we should ping every time +comment count to an entry in the partner's journal is updated. This is a +pattern, and [[docid]], [[dochash]], and [[count]] are replaced with the +actual values. + =back =cut @@ -184,7 +196,8 @@ __PACKAGE__->mk_accessors qw( id name journal_username journalid journal api_key domain link_pattern custom_css_url xdreceiver_url rate_limits encoding - mapping_locked disabled ); + mapping_locked disabled dochash_salt + sync_comments_count_url_pattern ); ### CLASS METHODS ### @@ -490,16 +503,18 @@ } function checkDomain(href, trustedDomains) { - var currentDomain = href.match(] .q{/(http\:\/\/)(?:www\.)?([^\/]*)/} . qq[)[2]; + var currentDomain = href.match(] .q{/(http\:\/\/)([^\/]*)/} . qq[)[2].split('.').slice(-2).join('.'), + currentRegExp = new RegExp(currentDomain.replace('.', '\\.') + '\$'); for (var i = 0, l = trustedDomains.length; i < l; i++) { - if (trustedDomains[i] == currentDomain) { + if (trustedDomains[i].search(currentRegExp) != -1) { return true; } } return false; } + </script> ]; } elsif ( $opts->{'mode'} eq 'jsonp' ) { @@ -517,10 +532,11 @@ } function checkDomain(href, trustedDomains) { - var currentDomain = href.match(] .q{/(http\:\/\/)(?:www\.)?([^\/]*)/} . qq[)[2]; + var currentDomain = href.match(] .q{/(http\:\/\/)([^\/]*)/} . qq[)[2].split('.').slice(-2).join('.'), + currentRegExp = new RegExp(currentDomain.replace('.', '\\.') + '\$'); for (var i = 0, l = trustedDomains.length; i < l; i++) { - if (trustedDomains[i] == currentDomain) { + if (trustedDomains[i].search(currentRegExp) != -1) { return true; } } @@ -843,6 +859,58 @@ }; } +=item * + +$partner->needs_sync_comments_count: returns a boolean value indicating +whether we need to ping the partner's site to notify them of any changes +of comments count to their entries + +=cut + +sub needs_sync_comments_count { + my ($self) = @_; + + return $self->sync_comments_count_url_pattern ? 1 : 0; +} + +=item * + +$partner->calculate_dochash($docid): calculate a message signature +for the given docid by concatenating it with the partner-specific +salt string and then computing Digest::MD5::md5_hex + +=cut + +sub calculate_dochash { + my ( $self, $docid ) = @_; + + return Digest::MD5::md5_hex( $docid . $self->dochash_salt ); +} + +=item * + +$partner->sync_comments_url_from_entry($entry): return the URL we need +to ping to notify the partner that comment count to $entry has changed. + +=cut + +sub sync_comments_url_from_entry { + my ( $self, $entry ) = @_; + + my $docid = $self->docid_from_entry($entry); + $docid ||= ''; + + my $dochash = $self->calculate_dochash($docid); + my $count = $entry->reply_count; + + my $url = $self->sync_comments_count_url_pattern; + $url =~ s/\Q[[docid]]\E/$docid/g; + $url =~ s/\Q[[dochash]]\E/$dochash/g; + $url =~ s/\Q[[count]]\E/$count/g; + + return $url; +} + =back =cut