madeon (madeon) wrote in changelog,
madeon
madeon
changelog

[livejournal] r20130: LJSUP-9819,LJSUP-9307: fix and recent ch...

Committer: sbelyaev
LJSUP-9819,LJSUP-9307: fix and recent changes
U   branches/delayed_entries/cgi-bin/LJ/DelayedEntry.pm
U   branches/delayed_entries/cgi-bin/LJ/User.pm
U   branches/delayed_entries/cgi-bin/LJ/Widget/EntryForm.pm
U   branches/delayed_entries/cgi-bin/htmlcontrols.pl
U   branches/delayed_entries/cgi-bin/ljprotocol.pl
U   branches/delayed_entries/htdocs/editjournal.bml
U   branches/delayed_entries/htdocs/update.bml
Modified: branches/delayed_entries/cgi-bin/LJ/DelayedEntry.pm
===================================================================
--- branches/delayed_entries/cgi-bin/LJ/DelayedEntry.pm	2011-09-26 09:12:29 UTC (rev 20129)
+++ branches/delayed_entries/cgi-bin/LJ/DelayedEntry.pm	2011-09-26 10:49:11 UTC (rev 20130)
@@ -125,6 +125,13 @@
     return $self->{posttime};
 }
 
+sub posttime_as_unixtime {
+    my ($self) = @_;
+    my $dbh = LJ::get_db_writer();
+    my $qposttime = $self->posttime;
+    return $dbh->selectrow_array("SELECT UNIX_TIMESTAMP($qposttime)");
+} 
+
 sub alldatepart {
     my ($self) = @_;
     return $self->{alldatepart};
@@ -517,18 +524,20 @@
     $req->{tz} = $req->{tz} || $self->data->{tz};
 
     my $journalid = $self->journal->userid;
-    my $posterid = $self->poster->userid;
-    my $subject = $req->{subject};
-    my $posttime = __get_datatime($req);
-    my $data_ser = __serialize($self->journal, $req);
+    my $posterid  = $self->poster->userid;
+    my $subject   = $req->{subject};
+    my $posttime  = __get_datatime($req);
+    my $data_ser  = __serialize($self->journal, $req);
     my $delayedid = $self->{delayed_id};
-    my $dbh = LJ::get_db_writer();
+    my $dbh       = LJ::get_db_writer();
 
-    my $security = "public";
+    my $security  = "public";
     my $uselogsec = 0;
+
     if ($req->{'security'} eq "usemask" || $req->{'security'} eq "private") {
         $security = $req->{'security'};
     }
+
     if ($req->{'security'} eq "usemask") {
         $uselogsec = 1;
     }
@@ -650,15 +659,15 @@
                                           "WHERE journalid=$journalid AND ".
                                           "delayedid = $delayedid");
     my $self = bless {}, $class; 
-    $self->{data} = __deserialize($journal, $data_ser);
-    $self->{journal} = $journal;
-    $self->{poster} = LJ::want_user($opts->[2]);
-    $self->{delayed_id} = $delayedid;
-    $self->{posttime} = __get_datatime($self->{data});
-    $self->{alldatepart} = $opts->[3];
-    $self->{logtime} = $opts->[5];
+    $self->{data}               = __deserialize($journal, $data_ser);
+    $self->{journal}            = $journal;
+    $self->{poster}             = LJ::want_user($opts->[2]);
+    $self->{delayed_id}         = $delayedid;
+    $self->{posttime}           = __get_datatime($self->{data});
+    $self->{alldatepart}        = $opts->[3];
+    $self->{logtime}            = $opts->[5];
     $self->{system_alldatepart} = $opts->[4];
-    $self->{taglist} = __extract_tag_list(\$self->{data}->{props}->{taglist});
+    $self->{taglist}            = __extract_tag_list(\$self->{data}->{props}->{taglist});
 
     __assert( $self->{poster} );
     __assert( $self->{journal} );
@@ -1236,10 +1245,10 @@
         my $error = "";
         foreach my $poll (@polls) {
             $poll->save_to_db(
-            journalid => $journalid,
-            posterid  => $posterid,
-            ditemid   => $ditemid,
-            error     => \$error,
+                journalid => $journalid,
+                posterid  => $posterid,
+                ditemid   => $ditemid,
+                error     => \$error,
             );
             
             my $pollid = $poll->pollid;
@@ -1265,6 +1274,7 @@
     $journal->do("$verb INTO logtext2 (journalid, jitemid, subject, event) ".
         "VALUES ($journalid, $jitemid, ?, ?)", undef, $req->{'subject'},
     LJ::text_compress($event));
+
     if ($journal->err) {
         my $msg = $journal->errstr;
         LJ::delete_entry($journal, $jitemid, undef, $anum);   # roll-back
@@ -1291,14 +1301,15 @@
         my $taginput = $req->{props}->{taglist};
         
         my $logtag_opts = {
-            remote => $poster,
+            remote       => $poster,
             skipped_tags => [], # do all possible and report impossible
         };
 
         if (ref $taginput eq 'ARRAY') {
             $logtag_opts->{set} = [@$taginput];
             $req->{props}->{taglist} = join(", ", @$taginput);
-        } else {
+        }
+        else {
             $logtag_opts->{set_string} = $taginput;
         }
 
@@ -1311,10 +1322,11 @@
     ## copyright
     if (LJ::is_enabled('default_copyright', $poster)) {
         $req->{'props'}->{'copyright'} = $poster->prop('default_copyright')
-        unless defined $req->{'props'}->{'copyright'};
+            unless defined $req->{'props'}->{'copyright'};
         $req->{'props'}->{'copyright'} = 'P' # second try
-        unless defined $req->{'props'}->{'copyright'};
-    } else {
+            unless defined $req->{'props'}->{'copyright'};
+    }
+    else {
         delete $req->{'props'}->{'copyright'};
     }
 
@@ -1328,6 +1340,7 @@
     # meta-data
     if (%{$req->{'props'}}) {
         my $propset = {};
+
         foreach my $pname (keys %{$req->{'props'}}) {
             next unless $req->{'props'}->{$pname};
             next if $pname eq "revnum" || $pname eq "revtime";
@@ -1336,6 +1349,7 @@
             next unless $req->{'props'}->{$pname};
             $propset->{$pname} = $req->{'props'}->{$pname};
         }
+
         my %logprops;
         LJ::set_logprop($journal, $jitemid, $propset, \%logprops) if %$propset;
         
@@ -1370,9 +1384,9 @@
          && LJ::get_cap($poster, "weblogscom") &&
          $security eq "public" ) {
             push @jobs, TheSchwartz::Job->new_from_array("LJ::Worker::Ping::WeblogsCom", {
-                'user' => $poster->{'user'},
+                'user'  => $poster->{'user'},
                 'title' => $poster->{'journaltitle'} || $poster->{'name'},
-                'url' => LJ::journal_base($poster) . "/",
+                'url'   => LJ::journal_base($poster) . "/",
         });
     }
 
@@ -1401,8 +1415,8 @@
     LJ::mark_user_active($journal, 'post') unless LJ::u_equals($poster, $journal);
 
     $res->{'itemid'} = $jitemid;  # by request of mart
-    $res->{'anum'} = $anum;
-    $res->{'url'} = $entry->url;
+    $res->{'anum'}   = $anum;
+    $res->{'url'}    = $entry->url;
 
     push @jobs, LJ::Event::JournalNewEntry->new($entry)->fire_job;
     push @jobs, LJ::Event::UserNewEntry->new($entry)->fire_job if (!$LJ::DISABLED{'esn-userevents'} || $LJ::_T_FIRE_USERNEWENTRY);
@@ -1412,6 +1426,7 @@
     LJ::Feed::generate_hubbub_jobs($journal, \@jobs) unless $journal->is_syndicated;
 
     my $sclient = LJ::theschwartz();
+
     if ($sclient && @jobs) {
         my @handles = $sclient->insert_jobs(@jobs);
         # TODO: error on failure?  depends on the job I suppose?  property of the job?
@@ -1525,12 +1540,14 @@
     __assert($req);
     __assert($req->{'tz'});
 
-    my $dt = DateTime->new(   year      => $req->{'year'}, 
-                              month     => $req->{'mon'},
-                              day       => $req->{'day'}, 
-                              hour      => $req->{'hour'},
-                              minute    => $req->{'min'},
-                              time_zone => $req->{tz}, );
+    my $dt = DateTime->new(
+        year      => $req->{'year'}, 
+        month     => $req->{'mon'},
+        day       => $req->{'day'}, 
+        hour      => $req->{'hour'},
+        minute    => $req->{'min'},
+        time_zone => $req->{tz},
+    );
 
     #if ($dt->is_dst) {
     #    $dt->subtract( hours => 1 );
@@ -1548,6 +1565,7 @@
 
 sub __assert {
     my ($statement) = @_;
+
     unless ($statement) {
         die "assertion failed!";
     }

Modified: branches/delayed_entries/cgi-bin/LJ/User.pm
===================================================================
--- branches/delayed_entries/cgi-bin/LJ/User.pm	2011-09-26 09:12:29 UTC (rev 20129)
+++ branches/delayed_entries/cgi-bin/LJ/User.pm	2011-09-26 10:49:11 UTC (rev 20130)
@@ -6151,7 +6151,8 @@
 # return sticky entries existing
 sub has_sticky_entry {
     my ($self) = @_;
-    return !!$self->prop("sticky_entries") || 0;
+    my $has_sticky  = !!$self->prop("sticky_entries") || 0;
+    return int $has_sticky;
 }
 
 # returns sticky entry jitemid

Modified: branches/delayed_entries/cgi-bin/LJ/Widget/EntryForm.pm
===================================================================
--- branches/delayed_entries/cgi-bin/LJ/Widget/EntryForm.pm	2011-09-26 09:12:29 UTC (rev 20129)
+++ branches/delayed_entries/cgi-bin/LJ/Widget/EntryForm.pm	2011-09-26 10:49:11 UTC (rev 20130)
@@ -424,7 +424,7 @@
             }
 
             push @choices, map { $_ => $_ } @{ $login_data->{'usejournals'} };
-            
+
             $out .= "<span class='wrap'>";
             $out .= LJ::html_select(
                 {
@@ -435,6 +435,7 @@
                     'class' => 'select',
                     "onchange" => "changeSubmit('" . $submitprefix . "',this[this.selectedIndex].value, '$BML::ML{'entryform.update4'}');".
                         "getUserTags(this[this.selectedIndex].value);".
+                        "setPostingPermissions(this[this.selectedIndex].value);".
                         "changeSecurityOptions(this[this.selectedIndex].value)"
                 },
                 @choices,
@@ -494,7 +495,7 @@
     my $help_icon = LJ::help_icon("24hourshelp");
 
     if ( $opts->{'mode'} eq "edit" ) {
-        $out .= qq{ <li class='pkg' id='currentdate'><label class='title'>$BML::ML{'entryform.date'}</label> 
+        $out .= qq{ <li class='pkg' id='currentdate'><label class='title'>$BML::ML{'entryform.date'}</label>
                 <span class='wrap'>
                     $monthlong, $mday, $year, $hour:$min
                     <a href='javascript:void(0)' onclick='editdate();' id='currentdate-edit'>$BML::ML{'entryform.date.edit'}</a>
@@ -502,7 +503,7 @@
                   </span>
                 </li> };
     } else {
-        $out .= qq{ <li class='pkg' id='currentdate'><label class='title'>$BML::ML{'entryform.post'}</label> 
+        $out .= qq{ <li class='pkg' id='currentdate'><label class='title'>$BML::ML{'entryform.post'}</label>
                 <span class='wrap'>
                     $BML::ML{'entryform.post.right.now'}
                     <a href='javascript:void(0)' onclick='editdate();' id='currentdate-edit'>$BML::ML{'entryform.date.edit'}</a>
@@ -768,7 +769,7 @@
                 'type' => "check",
                 'class' => 'sticky_type',
                 'value' => 'sticky',
-                'name' => 'type',
+                'name' => 'prop_sticky_type',
                 'id' => 'sticky_type',
                 'selected' => $selected,
                 $opts->{'prop_opt_preformatted'} || $opts->{'event_format'},
@@ -778,7 +779,7 @@
             my $sticky_exists = $journalu ? $journalu->has_sticky_entry && !$selected : undef;
             my $sticky_text = $sticky_exists ? $BML::ML{'entryform.sticky_replace.edit'} :
                                                $BML::ML{'entryform.sticky.edit'};
-            return qq{$sticky_check <label for='sticky_type' class='right options'>
+            return qq{$sticky_check <label for='sticky_type' id='sticky_type_label' class='right options'>
                    $sticky_text
                 </label>};
         },
@@ -1447,6 +1448,40 @@
 sub render_body {
     my ($self) = @_;
 
+    LJ::register_hook('add_to_site_js', sub {
+        my $site = shift;
+
+        my $remote = LJ::get_remote();
+        my $login_data = LJ::Protocol::do_request("login", {
+                            "ver" => $LJ::PROTOCOL_VER,
+                            "username" => $self->remote->username,
+                            "getpickws" => 1,
+                            "getpickwurls" => 1,
+                        }, undef, {
+                            "noauth" => 1,
+                            "u" => $self->remote,
+                        });
+
+        my $logins = $login_data->{'usejournals'};
+        push @$logins, $remote->username ;
+
+        my $site_data;
+        foreach my $login (@$logins) {
+            my $u = LJ::load_user($login);
+
+            my $can_manage = $remote->can_manage($u) || 0;
+            my $moderated = $u->prop('moderated');
+            my $need_moderated = ( $moderated =~ /^[1A]$/ ) ? 1 : 0;
+            my $can_post = ($u->{'journaltype'} eq 'C' && !$need_moderated) ||
+                            $can_manage;
+
+            $site_data->{$login}->{'is_replace_sticky'} = $u->has_sticky_entry;
+            $site_data->{$login}->{'can_create_sticky'} = $can_manage;
+            $site_data->{$login}->{'can_post_delayed'} = int $can_post;
+        }
+        $site->{remote_permissions} = $site_data;
+    });
+
     my $opts = $self->opts;
     my $head = $self->head;
     my $onload = $self->onload;

Modified: branches/delayed_entries/cgi-bin/htmlcontrols.pl
===================================================================
--- branches/delayed_entries/cgi-bin/htmlcontrols.pl	2011-09-26 09:12:29 UTC (rev 20129)
+++ branches/delayed_entries/cgi-bin/htmlcontrols.pl	2011-09-26 10:49:11 UTC (rev 20130)
@@ -236,7 +236,7 @@
     if ($opts->{'selected'}) { $ret .= " checked='checked'"; }
     if ($opts->{'raw'}) { $ret .= " $opts->{'raw'}"; }
     foreach (grep { ! /^(disabled|type|selected|raw|noescape|label)$/ } keys %$opts) {
-        $ret .= " $_=\"" . ($ehtml ? ehtml($opts->{$_}) : $opts->{$_}) . "\"";
+        $ret .= " $_=\"" . ($ehtml ? ehtml($opts->{$_}) : $opts->{$_}) . "\"" if $_;
     }
     $ret .= "$disabled />";
     my $e_label = ($ehtml ? ehtml($opts->{'label'}) : $opts->{'label'});

Modified: branches/delayed_entries/cgi-bin/ljprotocol.pl
===================================================================
--- branches/delayed_entries/cgi-bin/ljprotocol.pl	2011-09-26 09:12:29 UTC (rev 20129)
+++ branches/delayed_entries/cgi-bin/ljprotocol.pl	2011-09-26 10:49:11 UTC (rev 20130)
@@ -83,7 +83,12 @@
      "212" => [ E_PERM, "Message body is too long" ],
      "213" => [ E_PERM, "Message body is empty" ],
      "214" => [ E_PERM, "Message looks like spam" ],
-     "215" => [ E_PERM, "Timezone is not set"],
+     "215" => [ E_PERM, "Timezone is not set" ],
+     "216" => [ E_PERM, "Unknown post type" ],
+     "217" => [ E_PERM, "Can't convert posted item to delayed" ],
+     "218" => [ E_PERM, "Wrong selecttype" ],
+     "219" => [ E_PERM, "Missing mandatory argument (id or ids)" ],
+     "220" => [ E_PERM, "Can't use such view mode" ],
 
      # Access Errors
      "300" => [ E_TEMP, "Don't have access to requested journal" ],
@@ -1821,8 +1826,7 @@
     return 1;
 }
 
-sub postevent
-{
+sub postevent {
     my ($req, $err, $flags) = @_;
     un_utf8_request($req);
 
@@ -1920,6 +1924,8 @@
         }
     }
 
+    warn $req->{'tz'};
+
     if (defined $req->{'tz'} and not grep { defined $req->{$_} } qw(year mon day hour min)) {
         my @ltime = gmtime(time() + ($offset*3600));
         $req->{'year'} = $ltime[5]+1900;
@@ -1969,7 +1975,7 @@
     #    return fail($err, 153, "You have an entry which was posted at $u->{'newesteventtime'}, but you're trying to post an entry before this. Please check the date and time of both entries. If the other entry is set in the future on purpose, edit that entry to use the \"Date Out of Order\" option. Otherwise, use the \"Date Out of Order\" option for this entry instead.");
     #}
     
-    if ( $req->{type} && $req->{type} eq 'sticky' &&
+    if ( $req->{props} && $req->{props}->{sticky} &&
          $uowner->{'journaltype'} eq 'C' &&
           !( LJ::check_rel($ownerid, $posterid, 'S') ||
              LJ::check_rel($ownerid, $posterid, 'M') ) )
@@ -2090,22 +2096,29 @@
         LJ::run_hook('spam_community_detector', $uowner, $req, \$need_moderated);
     }
 
-    if ( $req->{timezone} && LJ::DelayedEntry::is_future_date($req) ) {
-        # if posting to a moderated community, store and bail out here
-        if ($uowner->{'journaltype'} eq 'C' && $need_moderated && !$flags->{'nomod'}) {
-            return fail($err,322);
-        }
+    if ( $req->{ver} > 1 ) {
+        if ( LJ::DelayedEntry::is_future_date($req) ) {
+            return fail($err, 215) unless $req->{tz};
 
-        $req->{ext}->{flags} = $flags;
-        $req->{ext}->{flags}->{u} = undef; # it's no need to be stored
-        $req->{usejournal} = $req->{usejournal} || '';
+            # if posting to a moderated community, store and bail out here
+            if ($uowner->{'journaltype'} eq 'C' && $need_moderated && !$flags->{'nomod'}) {
+                return fail($err, 322);
+            }
+
+            $req->{ext}->{flags} = $flags;
+            $req->{ext}->{flags}->{u} = undef; # it's no need to be stored
+            $req->{usejournal} = $req->{usejournal} || '';
   
-        my $entry = LJ::DelayedEntry->create( $req, { journal => $uowner,
+            my $entry = LJ::DelayedEntry->create( $req, { journal => $uowner,
                                                       poster  => $u,} );
-        return fail($err, 507) unless $entry;
-        $res->{delayedid} = $entry->delayedid;
-        return $res;
-        
+            return fail($err, 507) unless $entry;
+            $res->{delayedid} = $entry->delayedid;
+            $res->{type}      = 'delayed';
+            return $res;
+        }
+        else {
+            $res->{type} = 'posted';
+        }
     }
 
     # if posting to a moderated community, store and bail out here
@@ -2248,7 +2261,7 @@
                      "UNIX_TIMESTAMP($qeventtime), $rlogtime, $anum)");
     return $fail->($err,501,$dberr) if $dberr;
 
-    if ( $req->{type} && $req->{type} eq 'sticky' &&
+    if ( $req->{props} && $req->{props}->{sticky} &&
          $uowner->{'journaltype'} eq 'C' &&
           !( LJ::check_rel($ownerid, $posterid, 'S') ||
              LJ::check_rel($ownerid, $posterid, 'M') ) )
@@ -2257,7 +2270,7 @@
     }
 
     # post become 'sticky post'
-    if ( $req->{type} && $req->{type} eq 'sticky' ) {
+    if ( $req->{props} && $req->{props}->{sticky} ) {
         $uowner->set_sticky($jitemid);
     }
 
@@ -2489,8 +2502,7 @@
     return $res;
 }
 
-sub editevent
-{
+sub editevent {
     my ($req, $err, $flags) = @_;
     un_utf8_request($req);
 
@@ -2552,31 +2564,47 @@
     # NOTE: as in postevent, this requires $req->{event} to be binary data
     # but we've already removed the utf-8 flag in the XML-RPC path, and it
     # never gets set in the "flat" protocol path
-    return fail($err,409) if length($req->{event}) >= LJ::BMAX_EVENT;
+    return fail($err, 409) if length($req->{event}) >= LJ::BMAX_EVENT;
     
-    if ( $req->{delayedid} ) {
+    if ( $req->{ver} > 1 ) {
+        my $delayedid = delete $req->{delayedid};
         my $res = {};
-        my $delayedid = delete $req->{delayedid};
-        
-        $req->{ext}->{flags} = $flags;
-        $req->{ext}->{flags}->{u} = undef; # it's no need to be stored
-        
-        unless ($req->{timezone}) {
-            return fail( $err, 215);
+
+        if ( $delayedid ) {
+            return fail( $err, 217 ) if $req->{itemid} || $req->{anum};
+            return fail( $err, 215 ) unless $req->{timezone};                        
+
+            $req->{ext}->{flags} = $flags;
+            $req->{ext}->{flags}->{u} = undef; # it's no need to be stored
+            $req->{usejournal} = $req->{usejournal}  || '';
+
+            my $entry = LJ::DelayedEntry->get_entry_by_id(
+                $uowner,
+                $delayedid,
+                { userid => $posterid },
+            );
+
+            return fail($err, 508) unless $entry;
+
+            if ($req->{'event'} !~ /\S/ ) {
+                $entry->delete();
+                $res->{delayedid} = $delayedid;
+                return $res;
+            }
+
+            if ( LJ::DelayedEntry::is_future_date($req) ) {
+                $entry->update($req);
+                $res->{type} = 'delayed';
+            }
+            else {
+                my $out = $entry->convert;
+                $res->{type}   = 'posted';
+                $res->{itemid} = $out->{res}->{itemid};
+                $res->{anum}   = $out->{res}->{anum};
+            }
         }
-        $req->{usejournal} = $req->{usejournal}  || '';
-        
-        my $entry = LJ::DelayedEntry->get_entry_by_id( $uowner,
-                                                       $delayedid,
-                                                       { userid => $posterid });
-        return fail($err, 508) unless $entry;
-        if ($req->{'event'} !~ /\S/ ) {
-            $entry->delete();
-        } else {
-            $entry->update($req);
-        }
-        
-        return $res;
+
+        return $res if $res->{type};        
     }
 
     # fetch the old entry from master database so we know what we
@@ -2631,9 +2659,7 @@
     }
 
     # simple logic for deleting an entry
-    if (!$flags->{'use_old_content'} && $req->{'event'} !~ /\S/)
-    {
-
+    if (!$flags->{'use_old_content'} && $req->{'event'} !~ /\S/) {
         ## 23.11.2009. Next code added due to some hackers activities
         ## that use trojans to delete user's entries in theirs journals.
         if ($LJ::DELETING_ENTRIES_IS_DISABLED
@@ -2717,13 +2743,15 @@
     my %curprops;
     LJ::load_log_props2($dbcm, $ownerid, [ $itemid ], \%curprops);
 
+    warn LJ::D($req);
     # make post sticky
-    if ( $req->{type} && $req->{type} eq 'sticky') {
+    if ( $req->{props} && $req->{props}->{sticky} ) {
         if( $uowner->get_sticky_entry() != $itemid ) {
             $uowner->set_sticky($itemid);
             LJ::MemCache::delete([$ownerid, "log2lt:$ownerid"]);
         }
-    } elsif ( $itemid == $uowner->get_sticky_entry() ) {
+    }
+    elsif ( $itemid == $uowner->get_sticky_entry() ) {
         $uowner->remove_sticky();
         LJ::MemCache::delete([$ownerid, "log2lt:$ownerid"]);
     }
@@ -2818,7 +2846,8 @@
     {
         if ($security eq "public" || $security eq "private") {
             $uowner->do("DELETE FROM logsec2 WHERE journalid=$ownerid AND jitemid=$itemid");
-        } else {
+        }
+        else {
             $uowner->do("REPLACE INTO logsec2 (journalid, jitemid, allowmask) ".
                         "VALUES ($ownerid, $itemid, $qallowmask)");
         }
@@ -2867,13 +2896,16 @@
         unless (defined $req->{'props'}->{'copyright'}) { # try 1: previous value
             $req->{'props'}->{'copyright'} = $curprops{$itemid}->{'copyright'};
         }
+
         unless (defined $req->{'props'}->{'copyright'}) { # try 2: global setting
             $req->{'props'}->{'copyright'} = $uowner->prop('default_copyright');
         }
+
         unless (defined $req->{'props'}->{'copyright'}) { # try 3: allow
             $req->{'props'}->{'copyright'} = 'P';
         }
-    } else { # disabled feature
+    }
+    else { # disabled feature
         delete $req->{'props'}->{'copyright'};
     }
 
@@ -3030,13 +3062,104 @@
 
     $skip = 500 if $skip > 500;
     
+    if ( $req->{ver} > 1 ) {
+        my $res = {};
+
+        if ( $req->{delayedid} ) {
+            return fail( $err, 220 ) if $req->{view} && $req->{view} ne 'stored';
+
+            if ( $req->{selecttype} eq 'lastn' ) {
+                my $ids = LJ::DelayedEntry::get_entries_by_journal(
+                    $uowner,
+                    $req->{skip} || 0,
+                    $req->{howmany} || $req->{itemshow} || 20,
+                    $flags->{user}->id,
+                );
+
+                for my $did ( @$ids ) {
+                    my $entry, LJ::DelayedEntry::get_entry_by_id(
+                        $uowner,
+                        $did,
+                        { userid => $flags->{user}->id },
+                    );
+                    
+                    my $re = {};
+
+                    $re->{$_} = $entry->{$_} for qw(delayedid subject event props logtime);
+                                                    
+                    $re->{eventtime}       = $entry->{posttime};
+                    $re->{event_timestamp} = $entry->{posttime_unixtime};
+                    $re->{url}             = $entry->url;
+                    $re->{security}        = $entry->data->{security};
+                    $re->{allowmask}       = $entry->data->{allowmask};
+                    $re->{posterid}        = $entry->poster->userid;
+                    $re->{poster}          = $entry->poster->{user};
+
+                    push @{$res->{events}}, $re;
+                }
+            }
+            elsif ( $req->{selecttype} eq 'one' ) {
+                my $entry, LJ::DelayedEntry::get_entry_by_id(
+                    $uowner,
+                    $req->{delayedid},
+                    { userid => $flags->{user}->id },
+                );
+                    
+                my $re = {};
+
+                $re->{$_} = $entry->{$_} for qw(delayedid subject event props logtime);
+                                                    
+                $re->{eventtime}       = $entry->{posttime};
+                $re->{event_timestamp} = $entry->{posttime_unixtime};
+                $re->{url}             = $entry->url;
+                $re->{security}        = $entry->data->{security};
+                $re->{allowmask}       = $entry->data->{allowmask};
+                $re->{posterid}        = $entry->poster->userid;
+                $re->{poster}          = $entry->poster->{user};
+
+                push @{$res->{events}}, $re;
+            }
+            else {
+                return fail( $err, 218 );
+            }
+        }
+        elsif ( $req->{delayedids} ) {
+            if ( $req->{selecttype} eq 'multiple' ) {
+                for my $did ( @$req->{delayedids} ) {
+                    my $entry, LJ::DelayedEntry::get_entry_by_id(
+                        $uowner,
+                        $did,
+                        { userid => $flags->{user}->id },
+                    );
+                    
+                    my $re = {};
+
+                    $re->{$_} = $entry->{$_} for qw(delayedid subject event props logtime);
+                                                    
+                    $re->{eventtime}       = $entry->{posttime};
+                    $re->{event_timestamp} = $entry->{posttime_unixtime};
+                    $re->{url}             = $entry->url;
+                    $re->{security}        = $entry->data->{security};
+                    $re->{allowmask}       = $entry->data->{allowmask};
+                    $re->{posterid}        = $entry->poster->userid;
+                    $re->{poster}          = $entry->poster->{user};
+
+                    push @{$res->{events}}, $re;
+                }
+            }
+            else {
+                return fail( $err, 218 );
+            }
+        }
+    }
+
     # build the query to get log rows.  each selecttype branch is
     # responsible for either populating the following 3 variables
     # OR just populating $sql
     my ($orderby, $where, $limit, $offset);
     my $sql;
-    if ($req->{'selecttype'} eq "day")
-    {
+
+    if ($req->{'selecttype'} eq "day") {
         return fail($err,203)
             unless ($req->{'year'} =~ /^\d\d\d\d$/ &&
                     $req->{'month'} =~ /^\d\d?$/ &&
@@ -3053,10 +3176,11 @@
         # see note above about why the sort order is different
         $orderby = $is_community ? "ORDER BY logtime" : "ORDER BY eventtime";
     }
-    elsif ($req->{'selecttype'} eq "lastn")
-    {
+    elsif ($req->{'selecttype'} eq "lastn") {
         my $howmany = $req->{'howmany'} || 20;
+
         if ($howmany > 50) { $howmany = 50; }
+
         $howmany = $howmany + 0;
         $limit = "LIMIT $howmany";
 
@@ -3069,6 +3193,7 @@
         # mysql's braindead optimizer to use the right index.
         my $rtime_after = 0;
         my $rtime_what = $is_community ? "rlogtime" : "revttime";
+
         if ($req->{'beforedate'}) {
             return fail($err,203,"Invalid beforedate format.")
                 unless ($req->{'beforedate'} =~
@@ -3076,48 +3201,51 @@
             my $qd = $dbr->quote($req->{'beforedate'});
             $rtime_after = "$LJ::EndOfTime-UNIX_TIMESTAMP($qd)";
         }
+
         $where .= "AND $rtime_what > $rtime_after ";
         $orderby = "ORDER BY $rtime_what";
 
-	unless ($skip) {
-	    $where .= "OR jitemid=$sticky_id";
-	}
+        unless ($skip) {
+            $where .= "OR jitemid=$sticky_id";
+        }
     }
-    elsif ($req->{'selecttype'} eq "one" && $req->{'itemid'} eq "-1") 
-    {
+    elsif ($req->{'selecttype'} eq "one" && $req->{'itemid'} eq "-1") {
         $use_master = 1;  # see note above.
         $limit = "LIMIT 1";
         $orderby = "ORDER BY rlogtime";
     } 
-    elsif ($req->{'selecttype'} eq "one")
-    {
+    elsif ($req->{'selecttype'} eq "one") {
         my $id = $req->{'itemid'} + 0;
         $where = "AND jitemid=$id";
     }
-    elsif ($req->{'selecttype'} eq "syncitems") 
-    {
-        return fail($err,506) if $LJ::DISABLED{'syncitems'};
+    elsif ($req->{'selecttype'} eq "syncitems") {
+        return fail($err, 506) if $LJ::DISABLED{'syncitems'};
         my $date = $req->{'lastsync'} || "0000-00-00 00:00:00";
-        return fail($err,203,"Invalid syncitems date format")
+        return fail($err, 203, "Invalid syncitems date format")
             unless ($date =~ /^\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d/);
-        return fail($err,301,"syncitems is unavailable in anonymous mode") unless($u);
+        return fail($err, 301, "syncitems is unavailable in anonymous mode") unless($u);
 
         my $now = time();
+
         # broken client loop prevention
         if ($req->{'lastsync'}) {
             my $pname = "rl_syncitems_getevents_loop";
             LJ::load_user_props($u, $pname);
+
             # format is:  time/date/time/date/time/date/... so split
             # it into a hash, then delete pairs that are older than an hour
             my %reqs = split(m!/!, $u->{$pname});
+
             foreach (grep { $_ < $now - 60*60 } keys %reqs) { delete $reqs{$_}; }
             my $count = grep { $_ eq $date } values %reqs;
             $reqs{$now} = $date;
+
             if ($count >= 2) {
                 # 2 prior, plus this one = 3 repeated requests for same synctime.
                 # their client is busted.  (doesn't understand syncitems semantics)
                 return fail($err,406);
             }
+
             $u->set_prop( $pname => join( '/', map { $_ => $reqs{$_} }
                                                sort { $b <=> $a }
                                                keys %reqs ) );
@@ -3127,6 +3255,7 @@
         $sth = $dbcr->prepare("SELECT jitemid, logtime FROM log2 WHERE ".
                               "journalid=? and logtime > ? $secwhere");
         $sth->execute($ownerid, $date);
+
         while (my ($id, $dt) = $sth->fetchrow_array) {
             $item{$id} = $dt;
         }
@@ -3137,39 +3266,43 @@
                               "AND propid=$p_revtime->{'id'} ".
                               "AND value+0 > UNIX_TIMESTAMP(?)");
         $sth->execute($ownerid, $date);
+
         while (my ($id, $dt) = $sth->fetchrow_array) {
             $item{$id} = $dt;
         }
 
         my $limit = 100;
         my @ids = sort { $item{$a} cmp $item{$b} } keys %item;
+
         if (@ids > $limit) { @ids = @ids[0..$limit-1]; }
 
         my $in = join(',', @ids) || "0";
         $where = "AND jitemid IN ($in)";
     }
-    elsif ($req->{'selecttype'} eq "multiple")
-    {
+    elsif ($req->{'selecttype'} eq "multiple") {
         my @ids;
+
         foreach my $num (split(/\s*,\s*/, $req->{'itemids'})) {
-            return fail($err,203,"Non-numeric itemid") unless $num =~ /^\d+$/;
+            return fail($err, 203, "Non-numeric itemid") unless $num =~ /^\d+$/;
             push @ids, $num;
         }
+
         my $limit = 100;
-        return fail($err,209,"Can't retrieve more than $limit entries at once") if @ids > $limit;
+        return fail($err, 209, "Can't retrieve more than $limit entries at once") if @ids > $limit;
+
         my $in = join(',', @ids);
         $where = "AND jitemid IN ($in)";
     }
-    elsif ($req->{'selecttype'} eq 'before')
-    {
-        my $before = $req->{'before'};
-        my $itemshow = $req->{'howmany'};
+    elsif ($req->{'selecttype'} eq 'before') {
+        my $before     = $req->{'before'};
+        my $itemshow   = $req->{'howmany'};
         my $itemselect = $itemshow + $skip;
 
         my %item;
         $sth = $dbcr->prepare("SELECT jitemid, logtime FROM log2 WHERE ".
                               "journalid=? AND logtime < ? $secwhere LIMIT $itemselect");
         $sth->execute($ownerid, $before);
+
         while (my ($id, $dt) = $sth->fetchrow_array) {
             $item{$id} = $dt;
             
@@ -3182,23 +3315,25 @@
                               "AND propid=$p_revtime->{'id'} ".
                               "AND value+0 < ? LIMIT $itemselect");
         $sth->execute($ownerid, $before);
+
         while (my ($id, $dt) = $sth->fetchrow_array) {
             $item{$id} = $dt;
         }
 
         my @ids = sort { $item{$a} cmp $item{$b} } keys %item;        
-        if (@ids > $skip){
+
+        if (@ids > $skip) {
             @ids = @ids[$skip..(@ids-1)];
             @ids = @ids[0..$itemshow-1] if @ids > $itemshow;
-        }else{
+        }
+        else {
             @ids = ();
         }
 
         my $in = join(',', @ids) || "0";
         $where = "AND jitemid IN ($in)";
     }
-    else
-    {
+    else {
         return fail($err,200,"Invalid selecttype.");
     }
 
@@ -3211,7 +3346,7 @@
     # whatever selecttype might have wanted us to use the master db.
     $dbcr = LJ::get_cluster_def_reader($uowner) if $use_master;
 
-    return fail($err,502) unless $dbcr;
+    return fail($err, 502) unless $dbcr;
 
     ## load the log rows
     ($sth = $dbcr->prepare($sql))->execute;
@@ -3228,8 +3363,7 @@
     my $events = $res->{'events'} = [];
     my %evt_from_itemid;
 
-    while (my ($itemid, $eventtime, $sec, $mask, $anum, $jposterid, $replycount, $event_timestamp) = $sth->fetchrow_array)
-    {
+    while (my ($itemid, $eventtime, $sec, $mask, $anum, $jposterid, $replycount, $event_timestamp) = $sth->fetchrow_array) {
         $count++;
         my $evt = {};
         $evt->{'itemid'} = $itemid;
@@ -3251,10 +3385,12 @@
 
         $evt->{"eventtime"} = $eventtime;
         $evt->{event_timestamp} = $event_timestamp;
+
         if ($sec ne "public") {
             $evt->{'security'} = $sec;
             $evt->{'allowmask'} = $mask if $sec eq "usemask";
         }
+
         $evt->{'anum'} = $anum;
         $evt->{'poster'} = LJ::get_username($dbr, $jposterid) if $jposterid != $ownerid;
         $evt->{'url'} = LJ::item_link($uowner, $itemid, $anum);
@@ -3265,14 +3401,13 @@
         }
         else {
     	    push @$events, $evt;
-	}
+	    }
     }
 
     # load properties. Even if the caller doesn't want them, we need
     # them in Unicode installations to recognize older 8bit non-UF-8
     # entries.
-    unless ($req->{'noprops'} && !$LJ::UNICODE)
-    {
+    unless ($req->{'noprops'} && !$LJ::UNICODE) {
         ### do the properties now
         $count = 0;
         my %props = ();
@@ -3304,9 +3439,9 @@
                 $evt->{'props'}->{$name} = $value;
             }
 
-	    if ( $itemid == $sticky_id ) {
-		$evt->{'props'}->{'sticky'} = 1;
-	    }
+    	    if ( $itemid == $sticky_id ) {
+        		$evt->{'props'}->{'sticky'} = 1;
+    	    }
         }
     }
 
@@ -3315,8 +3450,7 @@
         return LJ::get_logtext2($uowner, @itemids);
     });
 
-    foreach my $i (@itemids)
-    {
+    foreach my $i (@itemids) {
         my $t = $text->{$i};
         my $evt = $evt_from_itemid{$i};
 
@@ -3374,7 +3508,8 @@
 
         if ($req->{view}) {
             LJ::EmbedModule->expand_entry($uowner, \$t->[1], edit => 1) if $req->{view} eq 'stored';
-        } elsif ($req->{parseljtags}) {
+        }
+        elsif ($req->{parseljtags}) {
             $t->[1] = LJ::convert_lj_tags_to_links(
                 event => $t->[1],
                 embed_url => $evt->{url});
@@ -3848,8 +3983,7 @@
     };
 }
 
-sub list_friends
-{
+sub list_friends {
     my ($u, $opts) = @_;
 
     # do not show people in here
@@ -3857,9 +3991,11 @@
 
     # TAG:FR:protocol:list_friends
     my $sql;
+
     unless ($opts->{'friendof'}) {
         $sql = "SELECT friendid, fgcolor, bgcolor, groupmask FROM friends WHERE userid=?";
-    } else {
+    }
+    else {
         $sql = "SELECT userid FROM friends WHERE friendid=?";
 
         if (my $list = LJ::load_rel_user($u, 'B')) {
@@ -3872,6 +4008,7 @@
     $sth->execute($u->{'userid'});
 
     my @frow;
+
     while (my @row = $sth->fetchrow_array) {
         next if $hide{$row[0]};
         push @frow, [ @row ];
@@ -3881,6 +4018,7 @@
     my $limitnum = $opts->{'limit'}+0;
 
     my $res = [];
+
     foreach my $f (sort { $us->{$a->[0]}{'user'} cmp $us->{$b->[0]}{'user'} }
                    grep { $us->{$_->[0]} } @frow)
     {
@@ -3895,8 +4033,8 @@
 
         if ($u->identity) {
             my $i = $u->identity;
-            $r->{'identity_type'} = $i->pretty_type;
-            $r->{'identity_value'} = $i->value;
+            $r->{'identity_type'}    = $i->pretty_type;
+            $r->{'identity_value'}   = $i->value;
             $r->{'identity_display'} = $u->display_name;
         }
 
@@ -3909,10 +4047,11 @@
         }
 
         unless ($opts->{'friendof'}) {
-            $r->{'fgcolor'} = LJ::color_fromdb($f->[1]);
-            $r->{'bgcolor'} = LJ::color_fromdb($f->[2]);
-            $r->{"groupmask"} = $f->[3] if $f->[3] != 1;
-        } else {
+            $r->{'fgcolor'}   = LJ::color_fromdb($f->[1]);
+            $r->{'bgcolor'}   = LJ::color_fromdb($f->[2]);
+            $r->{'groupmask'} = $f->[3] if $f->[3] != 1;
+        }
+        else {
             $r->{'fgcolor'} = "#000000";
             $r->{'bgcolor'} = "#ffffff";
         }
@@ -3934,61 +4073,84 @@
         $r->{defaultpicurl} = "$LJ::USERPIC_ROOT/$u->{'defaultpicid'}/$u->{'userid'}" if $u->{'defaultpicid'};
         
         push @$res, $r;
+
         # won't happen for zero limit (which means no limit)
         last if @$res == $limitnum;
     }
+
     return $res;
 }
 
-sub syncitems
-{
+sub syncitems {
     my ($req, $err, $flags) = @_;
     return undef unless authenticate($req, $err, $flags);
     return undef unless check_altusage($req, $err, $flags);
-    return fail($err,506) if $LJ::DISABLED{'syncitems'};
+    return fail($err, 506) if $LJ::DISABLED{'syncitems'};
 
     my $ownerid = $flags->{'ownerid'};
-    my $uowner = $flags->{'u_owner'} || $flags->{'u'};
+    my $uowner  = $flags->{'u_owner'} || $flags->{'u'};
     my $sth;
 
     my $db = LJ::get_cluster_reader($uowner);
-    return fail($err,502) unless $db;
+    return fail($err, 502) unless $db;
 
     ## have a valid date?
     my $date = $req->{'lastsync'};
+
     if ($date) {
-        return fail($err,203,"Invalid date format")
+        return fail($err, 203, "Invalid date format")
             unless ($date =~ /^\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d/);
-    } else {
+    }
+    else {
         $date = "0000-00-00 00:00:00";
     }
 
     my $LIMIT = 500;
+    my ( $table, $idfield ) = ( '', 'jitemid');
 
+    if ( $req->{ver} > 1 ) {
+        if ( $req->{type} eq 'posted' ) {
+            $table   = '';
+            $idfield = 'jitemid';
+        }
+        elsif ( $req->{type} eq 'delayed' ) {
+            $table = 'delayed';
+            $idfield = 'delayedid';
+        }
+        else {
+            return fail( $err, 216 );
+        }
+    }
+
     my %item;
-    $sth = $db->prepare("SELECT jitemid, logtime FROM log2 WHERE ".
+    $sth = $db->prepare("SELECT ${idfield}, logtime FROM ${table}log2 WHERE ".
                         "journalid=? and logtime > ?");
     $sth->execute($ownerid, $date);
+
     while (my ($id, $dt) = $sth->fetchrow_array) {
         $item{$id} = [ 'L', $id, $dt, "create" ];
     }
 
     my %cmt;
-    my $p_calter = LJ::get_prop("log", "commentalter");
-    my $p_revtime = LJ::get_prop("log", "revtime");
-    $sth = $db->prepare("SELECT jitemid, propid, FROM_UNIXTIME(value) ".
+
+    unless ( $req->{type} eq 'delayed' ) {
+        my $p_calter  = LJ::get_prop("log", "commentalter");
+        my $p_revtime = LJ::get_prop("log", "revtime");
+        $sth = $db->prepare("SELECT jitemid, propid, FROM_UNIXTIME(value) ".
                         "FROM logprop2 WHERE journalid=? ".
                         "AND propid IN ($p_calter->{'id'}, $p_revtime->{'id'}) ".
                         "AND value+0 > UNIX_TIMESTAMP(?)");
-    $sth->execute($ownerid, $date);
-    while (my ($id, $prop, $dt) = $sth->fetchrow_array) {
-        if ($prop == $p_calter->{'id'}) {
-            $cmt{$id} = [ 'C', $id, $dt, "update" ];
-        } elsif ($prop == $p_revtime->{'id'}) {
-            $item{$id} = [ 'L', $id, $dt, "update" ];
+        $sth->execute($ownerid, $date);
+
+        while (my ($id, $prop, $dt) = $sth->fetchrow_array) {
+            if ($prop == $p_calter->{'id'}) {
+                $cmt{$id} = [ 'C', $id, $dt, "update" ];
+            }
+            elsif ($prop == $p_revtime->{'id'}) {
+                $item{$id} = [ 'L', $id, $dt, "update" ];
+            }
         }
     }
-
     my @ev = sort { $a->[2] cmp $b->[2] } (values %item, values %cmt);
 
     my $res = {
@@ -3996,23 +4158,28 @@
             u => $flags->{'u'}
         }
     };
+
     my $list = $res->{'syncitems'} = [];
     $res->{'total'} = scalar @ev;
     my $ct = 0;
+
     while (my $ev = shift @ev) {
         $ct++;
-        push @$list, { 'item' => "$ev->[0]-$ev->[1]",
-                       'time' => $ev->[2],
-                       'action' => $ev->[3],  };
+        push @$list, {
+            'item'   => "$ev->[0]-$ev->[1]",
+            'time'   => $ev->[2],
+            'action' => $ev->[3],
+            'type'   => $req->{type},
+        };
         last if $ct >= $LIMIT;
     }
+
     $res->{'count'} = $ct;
 
     return $res;
 }
 
-sub consolecommand
-{
+sub consolecommand {
     my ($req, $err, $flags) = @_;
 
     # logging in isn't necessary, but most console commands do require it

Modified: branches/delayed_entries/htdocs/editjournal.bml
===================================================================
--- branches/delayed_entries/htdocs/editjournal.bml	2011-09-26 09:12:29 UTC (rev 20129)
+++ branches/delayed_entries/htdocs/editjournal.bml	2011-09-26 10:49:11 UTC (rev 20130)
@@ -33,6 +33,10 @@
                   'js/jquery-ui-autocomplete.js', 
                   'js/gears_init.js', 
                   'js/scanner.js' );
+
+    LJ::need_string(qw/
+                        entryform.sticky_replace.edit
+                        entryform.sticky.edit/);
     
     # are they asking to be authed as someone else?
     my $authas = $GET{'authas'} || $remote->{'user'};

Modified: branches/delayed_entries/htdocs/update.bml
===================================================================
--- branches/delayed_entries/htdocs/update.bml	2011-09-26 09:12:29 UTC (rev 20129)
+++ branches/delayed_entries/htdocs/update.bml	2011-09-26 10:49:11 UTC (rev 20130)
@@ -106,6 +106,10 @@
            if $tz;
     }
 
+    LJ::need_string(qw/
+                        entryform.sticky_replace.edit
+                        entryform.sticky.edit/);
+
     my ($year, $mon, $mday, $hour, $min) = ($now->year,
                                             sprintf("%02d", $now->month),
                                             sprintf("%02d", $now->day),

Tags: bml, livejournal, madeon, pl, 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