Igor Gariev (gariev) wrote in changelog,
Igor Gariev
gariev
changelog

[ljcom] r9755: LJINT-363: Comments for side projects

Committer: gariev
LJINT-363: Comments for side projects
A   trunk/cgi-bin/LJ/ExternalComments.pm
Added: trunk/cgi-bin/LJ/ExternalComments.pm
===================================================================
--- trunk/cgi-bin/LJ/ExternalComments.pm	                        (rev 0)
+++ trunk/cgi-bin/LJ/ExternalComments.pm	2010-11-25 07:25:57 UTC (rev 9755)
@@ -0,0 +1,110 @@
+package LJ::ExternalComments;
+use strict;
+use Digest::MD5 qw//;
+
+sub key_to_username {
+    my $class = shift;
+    my $key = shift;
+    my $username = $LJ::PARTNERS_WITH_LJCOM_COMMENTS{$key};
+
+    return $username;
+}
+
+sub url_to_jitemid {
+    my $class = shift;
+    my $u     = shift;
+    my $url   = shift;
+
+    my $url_md5 = Digest::MD5::md5_base64($url); # The length of the returned string will be 22.
+
+    ## check cache
+    my $cache_key = [$u->userid, $u->userid . "." . $url_md5];
+    my $jitemid = LJ::MemCache::get($cache_key);
+    return undef if defined $jitemid and 0 == $jitemid; ## negative mapping
+    return $jitemid if $jitemid;
+
+    ## get data from DB
+    my $dbr = LJ::get_db_reader();
+    ($jitemid) = $dbr->selectrow_array("
+                    SELECT jitemid 
+                    FROM exturl2entry_map
+                    WHERE
+                        userid = ? 
+                        AND url_md5 = ?
+                    ", undef, $u->userid, $url_md5);
+    die DBI->errstr if DBI->err;
+
+    ## Cache
+    if ($jitemid){
+        ## positive mapping
+        LJ::MemCache::set($cache_key, $jitemid);
+    } else {
+        ## cache negative mapping
+        LJ::MemCache::set($cache_key, 0, 3600);
+    } 
+    
+    return $jitemid;
+
+}
+
+sub create_entry_for_url {
+    my $class = shift;
+    my $u     = shift;
+    my $url   = shift;
+
+    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,
+        };
+
+    my (undef, $min, $hour, $day, $month, $year) = localtime();
+    $month++;
+    $year += 1900;
+
+    my $req = {
+        event   => "text",
+        subject => "subject text",
+        allowmask   => 0,
+        username => $u->username,
+        security => 'public',
+
+        year => $year,
+        mon  => $month,
+        day  => $day,
+        hour => $hour,
+        min  => $min,
+    };
+
+    my $err = 0;
+
+    ## create new entry
+    my $res = LJ::Protocol::do_request("postevent", $req, \$err, $flags);
+    return 0 unless $res;
+
+    ## Entry created.. get its ID.
+    my $jitemid = $res->{'itemid'};
+
+    my $url_md5 = Digest::MD5::md5_base64($url); # The length of the returned string will be 22.
+
+    ## update DB
+    my $dbw = LJ::get_db_writer();
+    $dbw->do("INSERT INTO exturl2entry_map (userid, jitemid, url_md5) VALUES (?, ?, ?)", undef,
+                $u->userid, $jitemid, $url_md5)
+        or die DBI->errstr;
+
+    ## update cache
+    my $cache_key = [$u->userid, $u->userid . "." . $url_md5];
+    LJ::MemCache::set($cache_key, $jitemid);
+    
+    return $jitemid;
+}
+
+1
+

Tags: gariev, 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