# Type: Input # Category: Issue # Description: Asks a user if he/she wants to watch the issue # Author: Andriy Lesyuk new Orangutan::Context( request => sub { my ($context, $user) = @_; my $issue = $context->GetField('issue'); if (defined($issue)) { my $yesno = $user->GetResponse('YesNo'); if ($yesno) { my $message = Orangutan::Context::Random( 'Should I add you to watchers list of', 'Should I add you as a watcher to', 'Would you like to follow', 'Would you like to watch' ); $message .= sprintf(" #%d?", $issue->{'id'}); $yesno->InsertForeignHandler($context, 300); return $message; } } return undef; }, handler => sub { my ($context, $user, $item, $answer) = @_; if ($answer eq 'Y') { my $issue = $context->GetField('issue'); if (!$main::query->IsWatching($user->GetUserID, $issue->{'id'})) { my %watcher = ( watchable_type => 'Issue', watchable_id => $issue->{'id'}, user_id => $user->GetUserID, login => $user->GetLogin, author_id => $user->GetUserID, author => $user->GetLogin ); $main::query->AddWatcher($user->GetUserID, $issue->{'id'}); $user->FireEvent('watcher', 'add', \%watcher, $issue); my $message = Orangutan::Context::Random('Ok. ', 'Fine!.. ', 'Cool. ', 'Good. '); $message .= Orangutan::Context::Random('Added.', 'Added you.', 'You are a watcher now.'); $user->SendMessage($message); } else { $user->SendMessage([ 'Hm... Perhaps I already added you to watchers list... :S', 'Hm... You are already watching it...', 'Oh... You are already a watcher!' ]); } } else { $user->SendMessage([ 'Ok. Sorry.', 'I see...' ]); } my $yesno = $user->GetResponse('YesNo'); if ($yesno) { $yesno->RemoveForeignHandler($context); } return undef; }, ontimeout => sub { my ($context, $user) = @_; my $yesno = $user->GetResponse('YesNo'); if ($yesno) { $yesno->RemoveForeignHandler($context); } return undef; }, onwatcher => sub { my ($context, $user, $type, $status, $watcher, $watched) = @_; my @stack = $user->GetStack; if ((scalar @stack == 0) || ($stack[0] ne 'WouldWatch')) { my $wouldwatch = $user->GetResponse($context->GetID); if (defined($wouldwatch)) { my $issue = $wouldwatch->GetField('issue'); if (($watcher->{'watchable_type'} eq 'Issue') && ($watcher->{'watchable_id'} == $issue->{'id'})) { $user->RemoveRequestResponse($wouldwatch); my $yesno = $user->GetResponse('YesNo'); if ($yesno) { $yesno->RemoveForeignHandler($wouldwatch); } } } } }, timeout => 1800, weight => undef ); # kate: syntax perl