[livejournal] r11008: Add some methods for creating comments a...

Apr 11, 2007 16:37


Committer: supAdd some methods for creating comments and some other helper methods

U branches/r5/cgi-bin/LJ/Comment.pm

Modified: branches/r5/cgi-bin/LJ/Comment.pm
===================================================================
--- branches/r5/cgi-bin/LJ/Comment.pm 2007-04-11 00:46:45 UTC (rev 11007)
+++ branches/r5/cgi-bin/LJ/Comment.pm 2007-04-11 16:37:46 UTC (rev 11008)
@@ -118,6 +118,78 @@
return undef;
}

+
+#
+# name: LJ::Comment::create
+# class: comment
+# des: Create a new comment. Add them to db.
+# args: !!!!!!!!!
+# returns: A new LJ::Comment object. undef on failure.
+#
+
+sub create {
+ my $class = shift;
+ my %opts = @_;
+
+ my $need_captcha = delete($opts{ need_captcha }) || 0;
+
+ # %talk_opts emulates parameters received from web form.
+ # Fill it with nessesary options.
+ my %talk_opts = map { $_ => delete $opts{$_} }
+ qw/journal ditemid nodetype parenttalkid usertype
+ poster body subject props/;
+
+ # Strictly parameters check. Do not allow any unused params to be passed in.
+ croak (__PACKAGE__ . "->create: Unsupported params: " . join " " => keys %opts )
+ if %opts;
+
+ # LJ::Talk::init uses 'itemid', not 'ditemid'.
+ $talk_opts{itemid} = $talk_opts{ditemid};
+
+ # Move props values to the talk_opts hash.
+ # Because LJ::Talk::Post::init needs this.
+ foreach my $key ( keys %{ $talk_opts{props} } ){
+ my $talk_key = "prop_$key";
+
+ $talk_opts{$talk_key} = delete $talk_opts{props}->{$key}
+ if not exists $talk_opts{$talk_key};
+ }
+
+ # The following 2 options are nessesary for successfull user authentification
+ # in the depth of LJ::Talk::Post::init.
+ $talk_opts{cookieuser} ||= $talk_opts{poster}->user;
+ $talk_opts{usertype} ||= 'cookieuser';
+ $talk_opts{nodetype} ||= 'L';
+
+ ## init. this handles all the error-checking, as well.
+ my @errors = ();
+ my $init = LJ::Talk::Post::init(\%talk_opts, $talk_opts{poster}, \$need_captcha, \@errors);
+ croak( join "\n" => @errors )
+ unless defined $init;
+
+ # check max comments
+ croak ("Sorry, this entry already has the maximum number of comments allowed.")
+ if LJ::Talk::Post::over_maxcomments($init->{journalu}, $init->{item}->{'jitemid'});
+
+ # no replying to frozen comments
+ croak('No reply to frozen thread')
+ if $init->{parent}->{state} eq 'F';
+
+ ## insertion
+ my $wasscreened = ($init->{parent}->{state} eq 'S');
+ my $err;
+ croak ($err)
+ unless LJ::Talk::Post::post_comment($init->{entryu}, $init->{journalu},
+ $init->{comment}, $init->{parent},
+ $init->{item}, \$err,
+ );
+
+ return
+ LJ::Comment->new($init->{journalu}, jtalkid => $init->{comment}->{talkid});
+
+}
+
+
sub absorb_row {
my ($self, %row) = @_;

@@ -216,6 +288,12 @@
return $self->{nodeid};
}

+sub nodetype {
+ my $self = shift;
+ __PACKAGE__->preload_rows([ $self->unloaded_singletons] );
+ return $self->{nodetype};
+}
+
sub parenttalkid {
my $self = shift;
__PACKAGE__->preload_rows([ $self->unloaded_singletons ]);
@@ -473,6 +551,7 @@
return $self->{state};
}

+
sub is_active {
my $self = shift;
return $self->state eq 'A' ? 1 : 0;
@@ -529,6 +608,12 @@
return LJ::Talk::can_delete($targetu, $journalu, $posteru, $poster);
}

+sub mark_as_spam {
+ my $self = shift;
+ LJ::Talk::mark_comment_as_spam($self->poster, $self->jtalkid)
+}
+
+
# returns comment action buttons (screen, freeze, delete, etc...)
sub manage_buttons {
my $self = shift;
@@ -876,4 +961,28 @@
$self->jtalkid );
}

+#
+# Returns true if passed text is a spam.
+#
+# Class method.
+# LJ::Comment->is_text_spam( $some_text );
+#
+sub is_text_spam($\$) {
+ my $class = shift;
+
+ # REF on text
+ my $ref = shift;
+ $ref = \$ref unless ref ($ref) eq 'SCALAR';
+
+ my $plain = $$ref; # otherwise we modify the source text
+ $plain = LJ::CleanHTML::clean_comment(\$plain);
+
+ foreach my $re ($LJ::TALK_ABORT_REGEXP, @LJ::TALKSPAM){
+ return 1 # spam
+ if $re and ($plain =~ /$re/ or $$ref =~ /$re/);
+ }
+
+ return 0; # normal text
+}
+
1;

Previous post Next post
Up