# Type: Output (event based) # Category: Issue # Description: Notifies watchers when new issue is added # Copied: Yes # Author: Andriy Lesyuk use Orangutan::User; new Orangutan::Context( request => sub { my ($context, $user) = @_; my $issues = $context->GetField('issues'); my @projects = ( ); my %project_issues = ( ); for (my $i = 0; $i < scalar @{$issues}; $i++) { if (defined($project_issues{$issues->[$i]->{'project_name'}})) { $project_issues{$issues->[$i]->{'project_name'}}++; } else { $project_issues{$issues->[$i]->{'project_name'}} = 1; push(@projects, $issues->[$i]->{'project_name'}); } } my $message = ''; for (my $i = 0; $i < scalar @projects; $i++) { if (scalar @{$issues} > 1) { if ($project_issues{$projects[$i]} > 1) { my $format .= Orangutan::Context::Random( '%d new issues were added to %s', 'There are %d new issues in %s', 'Here are %d new issues in %s' ); $message .= sprintf($format, $project_issues{$projects[$i]}, $projects[$i]); } else { my $format .= Orangutan::Context::Random( 'A new issue was added to %s', 'There is a new issue in %s', 'A new issue in %s' ); $message .= sprintf($format, $projects[$i]); } $message .= ":\n"; } for (my $j = 0; $j < scalar @{$issues}; $j++) { if ($issues->[$j]->{'project_name'} eq $projects[$i]) { my $timediff = time - $issues->[$j]->{'created_on'}; if (scalar @{$issues} > 1) { $message .= ' o '; } if ($timediff >= 900) { if ($timediff > 86400) { $message .= sprintf("%d days ", int($timediff / 86400)); } if ($timediff > 3600) { $message .= sprintf("%d hours ", int(($timediff % 86400) / 3600)); } $message .= sprintf("%d minutes ago ", int(($timediff % 3600) / 60)); } $message .= Orangutan::User::GetDisplayName($issues->[$j]->{'author_name'}).' '; if ($timediff < 300) { if ($timediff < 60) { $message .= Orangutan::Context::Random( 'has just reported', 'has just created', 'has just added' ).' '; } else { $message .= Orangutan::Context::Random( 'has reported', 'has created', 'has added' ).' '; } } else { $message .= Orangutan::Context::Random('reported', 'created', 'added').' '; } $message .= $issues->[$j]->{'tracker_name'}.' '; $message .= sprintf("#%d ", $issues->[$j]->{'id'}); $message .= '"'.$issues->[$j]->{'subject'}.'" '; $message .= '['.$issues->[$j]->{'priority_name'}.']'; if (scalar @{$issues} == 1) { $message .= ' '.Orangutan::Context::Random('for', 'for project').' '; $message .= $issues->[$j]->{'project_name'}; } if (defined($issues->[$j]->{'assigned_to_id'}) && ($issues->[$j]->{'assigned_to_id'} == $user->GetUserID)) { $message .= Orangutan::Context::Random( '. This issue is assigned to you', ' and assigned it to you' ); } $message .= '.'; if (scalar @{$issues} > 1) { $message .= "\n"; } } } } return $message; }, onissue => sub { my $context = shift; my $user = shift; my $type = shift; my $status = shift; if (!$user->IsSet(USER_NONOTIFY)) { if ($status eq 'add') { my $issue = shift; if ($issue->{'author_id'} != $user->GetUserID) { my $newissue = $user->GetRequest($context->GetID); if (defined($newissue)) { my $newissues = $newissue->GetField('issues'); push(@{$newissues}, $issue); } else { my @newissues = ( ); push(@newissues, $issue); $newissue = $user->AddRequest($context); $newissue->SetField('issues', \@newissues); } } } elsif ($status eq 'change') { my $operation = shift; my $issue = shift; my $newissue = $user->GetRequest($context->GetID); if (defined($newissue)) { my $newissues = $newissue->GetField('issues'); for (my $i = 0; $i < scalar @{$newissues}; $i++) { if ($newissues->[$i]->{'id'} == $issue->{'id'}) { $newissues->[$i] = $issue; last; } } } } elsif ($status eq 'remove') { my $issue = shift; my $newissue = $user->GetRequest($context->GetID); if (defined($newissue)) { my $newissues = $newissue->GetField('issues'); if (scalar @{$newissues} > 1) { for (my $i = 0; $i < scalar @{$newissues}; $i++) { if ($newissues->[$i]->{'id'} == $issue->{'id'}) { splice(@{$newissues}, $i, 1); last; } } } else { $user->RemoveRequest($context); } } } } }, weight => undef, copy => 1 ); # kate: syntax perl