# Type: Input # Category: Issue # Description: Allows to vote for/against issue # Author: Andriy Lesyuk new Orangutan::Context( response => sub { my ($context, $user, $message) = @_; my @result = ( ); my $regexp = 'issue'; my $trackers = $main::config->Get('redmine', '_trackers_regexp'); if ($trackers) { $regexp = "(?:issue|$trackers)"; } my @regexps = ( '(?:Now )?(?:I )?(do(?:n\'t| not) |un)?(?:do )?like '. '(?:(?:the )?'.$regexp.' )?(?:#|No\.? ?)?([0-9]+)(?: any more)?!*\.*', '(?:I )?vote (?:for|(against)) (?:(?:the )?'.$regexp.' )?(?:#|No\.? ?)?([0-9]+)!*\.*', '(?:\+|(-))?1 to (?:(?:the )?'.$regexp.' )?(?:#|No\.? ?)?([0-9]+)!*\.*', '(?:(?:the )?'.$regexp.' )?(?:#|No\.? ?)?([0-9]+)\b ?[:\-]? ?(?:\+|(-))?1' # reverse ); for (my $i = 0; $i < scalar @regexps; $i++) { if ($message =~ /^$regexps[$i]$/i) { my ($against, $issue); if ($i < 3) { ($against, $issue) = ($1, $2); } else { ($against, $issue) = ($2, $1); } if ($against) { push(@result, -1); } else { push(@result, 1); } push(@result, $issue); last; } } return @result; }, handler => sub { my ($context, $user, $item, $issueid) = @_; my $issue = $main::query->GetIssue($issueid); if ($issue) { utf8::decode($issue->{'subject'}); if (!$main::query->Can('view_issues', $user, $issue->{'project_id'})) { $user->SendMessage([ "Can't let you vote for the issue you don't have access to...", 'How could you vote if you did not see this issue?..', 'You could not access this issue!' ]); return; } if (!$main::query->Can('issues_vote', $user, $issue->{'project_id'})) { $user->SendMessage([ 'No rights to vote for this issue!', 'No voting rights for this issue!', "You can't vote for this issue!" ]); return; } my $vote = $main::query->GetVote($issueid, $user->GetUserID); if (defined($vote) && ($vote->{'vote'} == $item)) { $user->SendMessage([ 'You already voted...', 'You already told...' ]); return; } $main::query->Vote($user->GetUserID, $issueid, $item, defined($vote) ? $vote->{'id'} : undef); my $message = Orangutan::Context::Random('Ok. ', 'I see... ', 'Got it. '); if ($item == 1) { $message .= Orangutan::Context::Random( 'Glad you like it.', 'Nice.', ':)' ); if (!defined($vote)) { if (($issue->{'author_id'} != $user->GetUserID) && (!defined($issue->{'assigned_to_id'}) || ($issue->{'assigned_to_id'} != $user->GetUserID)) && !$main::query->IsWatching($user->GetUserID, $issueid)) { my $watch = $user->GetRequest('WouldWatch'); if (!$watch && !$user->GetResponse('WouldWatch')) { $watch = $user->AddRequestResponse($main::config->Context('WouldWatch')); } if ($watch) { $watch->SetField('issue', $issue); } } } } else { $message .= Orangutan::Context::Random( 'Sorry for that...', 'Sad.', ':(' ); my $watch = $user->GetRequest('WouldWatch'); if ($watch) { my $field = $watch->GetField('issue'); if ($field->{'id'} == $issue->{'id'}) { $user->RemoveRequestResponse($watch); } } } $user->SendMessage($message); } else { $user->SendMessage([ 'Perhaps you made a mistake in the issue number...', "You can't vote for a non-existent issue!", 'No such issue!' ]); } }, help => { title => 'How can I vote for an issue?', question => '^(?:How )?(?:(?:can|do) I |to )vote (?:for|against) (?:(?:the|an|some) )?issues?\?*!*\.*$', answer => "You can express your attitude towards an issue by voting for or against it.\n". "Check out these samples:\n". " o I vote against #75\n". " o I don't like #568\n". " o Vote for #716\n". " o I like #129\n". " o +1 to #536\n". " o #586: -1", weight => 1090 }, weight => sub { my ($context, $user) = @_; if ($main::query->TableExists('votes')) { return ( undef, 0, undef ); } else { return ( undef, undef, undef ); } } ); # kate: syntax perl