Skip to content

Commit

Permalink
Added moderating code so both admin.pl and comments.pl could use it
Browse files Browse the repository at this point in the history
  • Loading branch information
tvroom committed Sep 15, 2003
1 parent 4cf4559 commit 064e8c4
Showing 1 changed file with 205 additions and 1 deletion.
206 changes: 205 additions & 1 deletion Slash/DB/MySQL/MySQL.pm
Expand Up @@ -4430,6 +4430,206 @@ sub countStory {
return $value;
}


##################################################################
# Handles moderation
# Moderates a specific comment.
# Returns 0 or 1 for whether the comment changed
# Returns a negative value when an error was encountered. The
# warning the user sees is handled within the .pl file
#
# Currently defined error types:
# -1 - No points
# -2 - Not enough points
#
##################################################################

sub moderateComment {
my($self, $sid, $cid, $reason) = @_;
return 0 unless $reason;

my $constants = getCurrentStatic();
my $user = getCurrentUser();

my $comment_changed = 0;
my $superAuthor = $constants->{authors_unlimited}
&& $user->{seclev} >= $constants->{authors_unlimited};

if ($user->{points} < 1 && !$superAuthor) {
return -1;
}

my $comment = $self->getComment($cid);
$comment->{time_unixepoch} = timeCalc($comment->{date}, "%s", 0);

# The user should not have been been presented with the menu
# to moderate if any of the following tests trigger, but,
# an unscrupulous user could have faked their submission with
# or without us presenting them the menu options. So do the
# tests again.
unless ($superAuthor) {
# Do not allow moderation of any comments with the same UID as the
# current user (duh!).
return if $user->{uid} == $comment->{uid};
# Do not allow moderation of any comments (anonymous or otherwise)
# with the same IP as the current user.
return if $user->{ipid} eq $comment->{ipid};
# If the var forbids it, do not allow moderation of any comments
# with the same *subnet* as the current user.
return if $constants->{mod_same_subnet_forbid}
and $user->{subnetid} eq $comment->{subnetid};
# Do not allow moderation of comments that are too old.
return unless $comment->{time_unixepoch} >= time() - 3600*
($constants->{comments_moddable_hours}
|| 24*$constants->{archive_delay});
}

# Start putting together the data we'll need to display to
# the user.
my $reasons = $self->getReasons();
my $dispArgs = {
cid => $cid,
sid => $sid,
subject => $comment->{subject},
reason => $reason,
points => $user->{points},
reasons => $reasons,
};

unless ($superAuthor) {
my $mid = $self->getModeratorLogID($cid, $user->{uid});
if ($mid) {
$dispArgs->{type} = 'already moderated';
Slash::slashDisplay('moderation', $dispArgs);
return 0;
}
}

# Add moderation value to display arguments.
my $val = $reasons->{$reason}{val};
$val = "+1" if $val == 1;
$dispArgs->{val} = $val;

my $scorecheck = $comment->{points} + $val;
my $active = 1;
# If the resulting score is out of comment score range, no further
# actions need be performed.
# Should we return here and go no further?
if ( $scorecheck < $constants->{comment_minscore} ||
$scorecheck > $constants->{comment_maxscore})
{
# We should still log the attempt for M2, but marked as
# 'inactive' so we don't mistakenly undo it. Mods get modded
# even if the action didn't "really" happen.
#
$active = 0;
$dispArgs->{type} = 'score limit';
}

# Find out how many mod points this will really cost us. As of
# Oct. 2002, it might be more than 1.
my $pointsneeded = $self->getModPointsNeeded(
$comment->{points},
$scorecheck,
$reason);

# If more than 1 mod point needed, we might not have enough,
# so this might still fail.
if ($pointsneeded > $user->{points} && !$superAuthor) {
return -2;
}

# Write the proper records to the moderatorlog.
$self->createModeratorLog($comment, $user, $val, $reason, $active,
$pointsneeded);

if ($active) {

# If we are here, then the user either has mod points, or
# is an admin (and 'author_unlimited' is set). So point
# checks should be unnecessary here.

# First, update values for the moderator.
my $changes = { };
$changes->{-points} = "GREATEST(points-$pointsneeded, 0)";
my $tcost = $constants->{mod_unm2able_token_cost} || 0;
$tcost = 0 if $reasons->{$reason}{m2able};
$changes->{-tokens} = "tokens - $tcost" if $tcost;
$changes->{-totalmods} = "totalmods + 1";
$self->setUser($user->{uid}, $changes);
$user->{points} -= $pointsneeded;
$user->{points} = 0 if $user->{points} < 0;

# Update stats.
if ($tcost and my $statsSave = getObject('Slash::Stats::Writer')) {
$statsSave->addStatDaily("mod_tokens_lost_unm2able", $tcost);
}

# Next, adjust the appropriate values for the user who
# posted the comment.
my $karma_change = $reasons->{$reason}{karma};
if (!isAnon($comment->{uid})) {
my $lost_tokens_per_downmod = 1; # XXX should be a var
my $cu_changes = { };
if ($val < 0) {
$cu_changes->{-downmods} = "downmods + 1";
} elsif ($val > 0) {
$cu_changes->{-upmods} = "upmods + 1";
}
if ($karma_change < 0) {
$cu_changes->{-karma} = "GREATEST("
. "$constants->{minkarma}, karma - 1)";
$cu_changes->{-tokens} = "tokens - $lost_tokens_per_downmod";
} elsif ($karma_change > 0) {
$cu_changes->{-karma} = "LEAST("
. "$constants->{maxkarma}, karma + 1)";
}
if (scalar keys %$cu_changes) {
$self->setUser($comment->{uid}, $cu_changes);
# Update stats.
if ($karma_change < 0 and my $statsSave = getObject('Slash::Stats::Writer')) {
$statsSave->addStatDaily("mod_tokens_lost_downmod",
$lost_tokens_per_downmod);
}
}
}

# Make sure our changes get propagated back to the comment.
$comment_changed =
$self->setCommentCleanup($cid, $val, $reason,
$comment->{reason});
if (!$comment_changed) {
# This shouldn't happen; the only way we believe it
# could is if $val is 0, the comment is already at
# min or max score, the user's already modded this
# comment, or some other reason making this mod invalid.
# This is really just here as a safety check.
$dispArgs->{type} = 'logic error';
Slash::slashDisplay('moderation', $dispArgs);
return 0;
}

# We know things actually changed, so update points for
# display and send a message if appropriate.
$dispArgs->{points} = $user->{points};
$dispArgs->{type} = 'moderated';
use Slash::Messages;
Slash::Messages::send_mod_msg({
type => 'mod_msg',
sid => $sid,
cid => $cid,
val => $val,
reason => $reason,
comment => $comment
});
}

# Now display the template with the moderation results.
Slash::slashDisplay('moderation', $dispArgs);

return 1;
}

##################################################################
sub metamodEligible {
my($self, $user) = @_;
Expand Down Expand Up @@ -4555,6 +4755,10 @@ sub getStoryByTime {
return $returnable;
}

sub getStorySidFromDiscussion {
my ($self, $discussion) = (@_);
return $self->sqlSelect("sid", "stories", "discussion = ".$self->sqlQuote($discussion));
}
##################################################################
# admin.pl only
sub getStoryByTimeAdmin {
Expand Down Expand Up @@ -6200,7 +6404,7 @@ sub getRecentComments {
$max = $options->{max} if defined $options->{max};
$max = $min if $max < $min;
my $startat = $options->{startat} || 0;
my $num = $options->{num} || 30; # should be a var
my $num = $options->{num} || 100; # should be a var

my $max_cid = $self->sqlSelect("MAX(cid)", "comments");
my $start_cid = $max_cid - ($startat+($num*5-1));
Expand Down

0 comments on commit 064e8c4

Please sign in to comment.