madeon (madeon) wrote in changelog,
madeon
madeon
changelog

[livejournal] r21940: LJSUP-11909: New Repost feature

Committer: sbelyaev
LJSUP-11909: New Repost feature
U   trunk/cgi-bin/LJ/Entry/Repost.pm
U   trunk/cgi-bin/LJ/Entry.pm
Modified: trunk/cgi-bin/LJ/Entry/Repost.pm
===================================================================
--- trunk/cgi-bin/LJ/Entry/Repost.pm	2012-05-14 07:02:35 UTC (rev 21939)
+++ trunk/cgi-bin/LJ/Entry/Repost.pm	2012-05-14 07:10:02 UTC (rev 21940)
@@ -9,6 +9,14 @@
 
 sub __get_count {
     my ($u, $jitemid) = @_;
+
+    my $journalid = $u->userid;
+    my $memcache_key = "reposted_count:$journalid:$jitemid";
+    my ($count) = LJ::MemCache::get($memcache_key);
+    if ($count) {
+        return $count;
+    }
+
     my $dbcr = LJ::get_cluster_master($u)
         or die "get cluster for journal failed";
 
@@ -18,12 +26,25 @@
                                                    undef,
                                                    $u->userid,
                                                    $jitemid, );
+
+    LJ::MemCache::set($memcache_key, $count_jitemid, 3600);
+
     return $count_jitemid;
 
 }
 
 sub __get_repostid {
     my ($u, $jitemid, $reposterid) = @_;
+
+    warn "__get_repostid";
+    my $journalid = $u->userid;
+    my $memcache_key = "reposted_itemid:$journalid:$jitemid:$reposterid";
+    my ($itemid) = LJ::MemCache::get($memcache_key);
+    if ($itemid) {
+        warn $itemid;
+        return $itemid;
+    }
+
     my $dbcr = LJ::get_cluster_master($u)
         or die "get cluster for journal failed";
 
@@ -34,12 +55,21 @@
                                                    $u->userid,
                                                    $jitemid,
                                                    $reposterid, );
+
+    LJ::MemCache::set($memcache_key, $repost_jitemid, 3600);
     return $repost_jitemid;
 }
 
 sub __create_repost_record {
     my ($u, $itemid, $repost_journalid, $repost_itemid) = @_;
 
+    my $journalid = $u->userid;
+    my $memcache_key_count = "reposted_count:$journalid:$itemid";
+    my $memcache_key_status = "reposted_itemid:$journalid:$itemid:$repost_journalid";
+   
+    LJ::MemCache::delete($memcache_key_count);
+    LJ::MemCache::delete($memcache_key_status);
+
     $u->do('INSERT INTO repost2 VALUES(?,?,?,?)',
             undef,
             $u->userid,
@@ -51,6 +81,12 @@
 sub __delete_repost_record {
     my ($u, $itemid, $reposterid) = @_;
 
+    my $journalid = $u->userid;
+    my $memcache_key = "reposted_count:$journalid:$itemid";
+    my $memcache_key_status = "reposted_itemid:$journalid:$itemid:$reposterid";
+    LJ::MemCache::delete($memcache_key);
+    LJ::MemCache::delete($memcache_key_status);
+
     $u->do('DELETE FROM repost2 WHERE journalid = ? AND jitemid = ? AND reposterid = ?',
             undef,
             $u->userid,
@@ -104,8 +140,8 @@
         return;
     }
 
-    my $url = $entry_obj->url;
-    $post_obj->convert_to_repost($url);
+    my $mark = $entry_obj->journalid . ":" . $entry_obj->jitemid;
+    $post_obj->convert_to_repost($mark);
 
     # create record
     my $repost_jitemid = $post_obj->jitemid;
@@ -126,11 +162,54 @@
 
     my $reposted = __get_repostid( $entry_obj->journal, $entry_obj->jitemid, $u->userid );
     return  { 'result' => { 
-                  'count'    =>  __get_count($entry_obj->journal, $entry_obj->jitemid), 
-                  'reposted' => !!$reposted, },
+              'count'    =>  __get_count($entry_obj->journal, $entry_obj->jitemid), 
+              'reposted' => !!$reposted, },
             };
 }
 
+sub __reposters {
+    my ($dbcr, $journalid, $jitemid) = @_;
+
+    my $reposted = $dbcr->selectcol_arrayref( 'SELECT reposterid ' .
+                                              'FROM repost2 ' .
+                                              'WHERE journalid = ? AND jitemid = ? LIMIT 1000',
+                                              undef,
+                                              $journalid,
+                                              $jitemid, ); 
+
+    return undef unless scalar @$reposted;
+    return $reposted;
+}
+
+sub delete_all_reposts_records {
+    my ($class, $journalid, $jitemid) = @_;
+
+    my $memcache_key = "reposted_count:$journalid:$jitemid";
+    LJ::MemCache::delete($memcache_key);
+
+    my $u = LJ::want_user($journalid);
+    my $dbcr = LJ::get_cluster_master($u)
+        or die "get cluster for journal failed";
+
+
+    while (my $reposted = __reposters($dbcr, $journalid, $jitemid)) {
+        foreach my $reposterid (@$reposted) {
+            warn  LJ::D($reposterid);
+            my $memcache_key_status = "reposted_itemid:$journalid:$jitemid:$reposterid";
+            LJ::MemCache::delete($memcache_key_status);
+        }
+
+        my $reposters = join(',', @$reposted);
+        warn $reposters;
+
+        $u->do('DELETE FROM repost2 WHERE journalid = ? AND jitemid = ? AND reposterid IN ($reposters)',
+                undef,
+                $u->userid,
+                $jitemid,);
+
+    }
+}
+
 sub delete {
     my ($class, $u, $entry_obj) = @_;
     my $repost_itemid = __get_repostid( $entry_obj->journal, $entry_obj->jitemid, $u->userid );

Modified: trunk/cgi-bin/LJ/Entry.pm
===================================================================
--- trunk/cgi-bin/LJ/Entry.pm	2012-05-14 07:02:35 UTC (rev 21939)
+++ trunk/cgi-bin/LJ/Entry.pm	2012-05-14 07:10:02 UTC (rev 21940)
@@ -9,6 +9,7 @@
 use vars qw/ $AUTOLOAD /;
 use Carp qw/ croak /;
 use LJ::TimeUtil;
+use LJ::Entry::Repost;
 
 # internal fields:
 #
@@ -121,9 +122,15 @@
     # save the singleton if it doesn't exist
     $singletons{$journalid}->{$jitemid} = $self;
 
-    my $url = $self->prop('repost_link');
-    if ($url && ($url ne $self->url) ) {
-        $self->{'original_post_obj'} = LJ::Entry->new_from_url($url);
+    my $link = $self->prop('repost_link');
+    if ($link) {
+        my ($journalid, $jitemid) = split(/:/, $link);
+        my $user = int($journalid) ? LJ::want_user(int($journalid)) : undef;
+        if ($user && $jitemid)
+        {        
+            my $reposted_entry = LJ::Entry->new(int($journalid), jitemid => int($jitemid));
+            $self->{'original_post_obj'} = $reposted_entry if $reposted_entry->valid;
+        }
     }
 
     return $self;
@@ -2521,6 +2528,8 @@
     LJ::MemCache::delete([$jid, "log2:$jid:$jitemid"]);
     LJ::MemCache::decr([$jid, "log2ct:$jid"]) if $dc > 0;
 
+    LJ::Entry::Repost->delete_all_reposts_records($uuserid, $jitemid);
+
     if ( $jitemid == $u->get_sticky_entry_id() ){
         $u->remove_sticky_entry_id();
     }

Tags: livejournal, madeon, pm, sbelyaev
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