# Type: Output (event based) # Category: Issue # Description: Notifies watchers about changes in issues # Copied: Yes # Author: Andriy Lesyuk use Orangutan::User; new Orangutan::Context( request => sub { my ($context, $user) = @_; my $issues = $context->GetField('issues'); my $journals = $context->GetField('journals'); my @changes = ( ); for (my $i = 0; $i < scalar @{$journals}; $i++) { my $j; for ($j = 0; $j < scalar @changes; $j++) { if ($changes[$j] = $journals->[$i][2]) { last; } } if ($j == scalar @changes) { push(@changes, $journals->[$i][2]); } } my $message = ''; if (scalar @changes > 1) { $message.= Orangutan::Context::Random( 'Here are issues which were modified since the last time I saw you online', 'Since the last time you were online the following issues were modified', 'Changes were made to the following issues' ); $message.= ":\n"; } for (my $i = 0; $i < scalar @changes; $i++) { if (scalar @changes > 1) { $message .= "\n"; $message .= sprintf("#%d) ", $issues->{$changes[$i]}->{'issue'}->{'id'}); $message .= $issues->{$changes[$i]}->{'issue'}->{'tracker_name'}.': '; $message .= $issues->{$changes[$i]}->{'issue'}->{'subject'}; $message .= ' ('.$issues->{$changes[$i]}->{'issue'}->{'project_name'}.')'; $message .= ":\n"; } elsif ($issues->{$changes[0]}->{'count'} > 1) { $message.= Orangutan::Context::Random( 'Since the last time you were online the following changes were made to', 'The following was modified in', 'Here are changes made to' ); $message .= ' '.$issues->{$changes[0]}->{'issue'}->{'tracker_name'}.' '; $message .= sprintf("#%d ", $issues->{$changes[0]}->{'issue'}->{'id'}); $message .= '"'.$issues->{$changes[0]}->{'issue'}->{'subject'}.'"'; $message .= ' ('.$issues->{$changes[0]}->{'issue'}->{'project_name'}.')'; $message .= ":\n"; } for (my $j = 0; $j < scalar @{$journals}; $j++) { if ($journals->[$j][2] == $changes[$i]) { if ((scalar @changes > 1) || ($issues->{$changes[$i]}->{'count'} > 1)) { $message .= ' '; } my $date; if (!defined($journals->[$j][0]->{'status'}) || ($journals->[$j][0]->{'status'} eq 'add')) { $date = $journals->[$j][0]->{'created_on'}; } elsif ($journals->[$j][0]->{'status'} eq 'change') { $date = $journals->[$j][0]->{'updated_on'}; } else { $date = $journals->[$j][0]->{'removed_on'}; } my $timediff = time - $date; 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)); # FIXME: 0 minutes ago } $message .= Orangutan::User::GetDisplayName($journals->[$j][0]->{'login'}).' '; if (scalar @{$journals->[$j][1]} > 1) { $message .= Orangutan::Context::Random( 'made the following changes', 'modified the following', 'changed the following' ); if ((scalar @changes == 1) && ($issues->{$journals->[$j][2]}->{'count'} == 1)) { $message .= ' in '; $message .= $issues->{$journals->[$j][2]}->{'issue'}->{'tracker_name'}.' '; $message .= sprintf("#%d ", $journals->[$j][2]); $message .= '"'.$issues->{$journals->[$j][2]}->{'issue'}->{'subject'}.'"'; $message .= ' ('.$issues->{$journals->[$j][2]}->{'issue'}->{'project_name'}.')'; } $message .= ":\n"; } for (my $d = 0; $d < scalar @{$journals->[$j][1]}; $d++) { if (scalar @{$journals->[$j][1]} > 1) { $message .= ' o '; } if ($journals->[$j][1][$d]->{'property'} eq 'attachment') { if (defined($journals->[$j][1][$d]->{'value'})) { $message .= Orangutan::Context::Random( 'added attachment', 'uploaded file', 'added file', 'attached' ); $message .= ' "'.$journals->[$j][1][$d]->{'value'}.'"'; } else { $message .= Orangutan::Context::Random( 'removed attachment', 'removed file' ); $message .= ' "'.$journals->[$j][1][$d]->{'old_value'}.'"'; } if ((scalar @{$journals->[$j][1]} == 1) && (scalar @changes == 1) && ($issues->{$journals->[$j][2]}->{'count'} == 1)) { if (defined($journals->[$j][1][$d]->{'value'})) { $message .= ' to '; } else { $message .= ' from '; } $message .= $issues->{$journals->[$j][2]}->{'issue'}->{'tracker_name'}.' '; $message .= sprintf("#%d ", $journals->[$j][2]); $message .= '"'.$issues->{$journals->[$j][2]}->{'issue'}->{'subject'}.'"'; $message .= ' ('.$issues->{$journals->[$j][2]}->{'issue'}->{'project_name'}.')'; } } elsif ($journals->[$j][1][$d]->{'property'} eq 'attr') { my $value; my $strings = [ ]; if (defined($journals->[$j][1][$d]->{'value_name'})) { $value = $journals->[$j][1][$d]->{'value_name'}; } else { $value = $journals->[$j][1][$d]->{'value'}; } if ($journals->[$j][1][$d]->{'prop_key'} eq 'assigned_to_id') { if (defined($value)) { $strings = [ 'assigned', undef, 'to' ]; if ($value eq $user->GetLogin) { $value = 'you'; } elsif ($value eq $journals->[$j][0]->{'login'}) { $value = 'self'; } else { $value = Orangutan::User::GetDisplayName($value); } } else { $strings = [ 'unassigned' ]; } } elsif ($journals->[$j][1][$d]->{'prop_key'} eq 'category_id') { if (defined($value)) { $strings = [ 'set category', 'of', 'to' ]; } else { $strings = Orangutan::Context::Random( [ 'removed category', 'of' ], [ 'cleared category', 'of' ] ); } } elsif ($journals->[$j][1][$d]->{'prop_key'} eq 'done_ratio') { $strings = Orangutan::Context::Random( [ 'set done ratio', 'of', 'to' ], [ 'completed', undef, 'at' ] ); $value .= '%'; } elsif ($journals->[$j][1][$d]->{'prop_key'} eq 'due_date') { if (defined($value)) { $strings = Orangutan::Context::Random( [ 'estimated due date', 'of', 'at' ], [ 'set due date', 'of', 'to' ] ); my ($day, $mon, $year, $wday) = (localtime($value))[3..6]; $value = sprintf("%d %s %04d (%s)", $day, $Orangutan::Date::months_short[$mon], $year + 1900, $Orangutan::Date::weekdays_short[$wday]); } else { $strings = Orangutan::Context::Random( [ 'removed due date', 'of' ], [ 'cleared due date', 'of' ] ); } } elsif ($journals->[$j][1][$d]->{'prop_key'} eq 'estimated_hours') { if (defined($value)) { $strings = Orangutan::Context::Random( [ 'set estimated hours', 'of', 'to' ], [ 'estimated', undef, 'in' ] ); $value .= ' hours'; } else { $strings = [ 'removed estimated hours', 'for' ]; } } elsif ($journals->[$j][1][$d]->{'prop_key'} eq 'fixed_version_id') { if (defined($value)) { $strings = [ 'set target version', 'of', 'to' ]; } else { $strings = [ 'removed target version', 'of' ]; } } elsif ($journals->[$j][1][$d]->{'prop_key'} eq 'parent_id') { if (defined($value)) { $strings = Orangutan::Context::Random( [ 'set parent issue', 'of', 'to' ], [ 'moved', undef, 'under' ] ); $value = '#'.$value; } else { $strings = [ 'removed parent issue', 'of' ]; } } elsif ($journals->[$j][1][$d]->{'prop_key'} eq 'priority_id') { $strings = [ 'set priority', 'of', 'to' ]; } elsif ($journals->[$j][1][$d]->{'prop_key'} eq 'project_id') { $strings = Orangutan::Context::Random( [ 'set project', 'of', 'to' ], [ 'moved', undef, 'into' ] ); } elsif ($journals->[$j][1][$d]->{'prop_key'} eq 'start_date') { if (defined($value)) { $strings = [ 'set start date', 'of', 'to' ]; my ($day, $mon, $year, $wday) = (localtime($value))[3..6]; $value = sprintf("%d %s %04d (%s)", $day, $Orangutan::Date::months_short[$mon], $year + 1900, $Orangutan::Date::weekdays_short[$wday]); } else { $strings = Orangutan::Context::Random( [ 'removed start date', 'of' ], [ 'cleared start date', 'of' ] ); } } elsif ($journals->[$j][1][$d]->{'prop_key'} eq 'status_id') { $strings = [ 'set status', 'of', 'to' ]; } elsif ($journals->[$j][1][$d]->{'prop_key'} eq 'subject') { $strings = Orangutan::Context::Random( [ 'set subject', 'of', 'to' ], [ 'renamed', undef, 'to' ] ); } elsif ($journals->[$j][1][$d]->{'prop_key'} eq 'tracker_id') { $strings = [ 'set tracker', 'of', 'to' ]; } else { $main::logger->Log('unknown or unsupported property key: %s', $journals->[$j][1][$d]->{'prop_key'}); } if (scalar @{$strings} > 0) { $message .= $strings->[0]; if ((scalar @{$journals->[$j][1]} == 1) && (scalar @changes == 1) && ($issues->{$journals->[$j][2]}->{'count'} == 1)) { if (defined($strings->[1])) { $message .= ' '.$strings->[1]; } $message .= ' '.$issues->{$journals->[$j][2]}->{'issue'}->{'tracker_name'}.' '; $message .= sprintf("#%d", $journals->[$j][2]); if ($journals->[$j][1][$d]->{'prop_key'} ne 'subject') { $message .= ' "'.$issues->{$journals->[$j][2]}->{'issue'}->{'subject'}.'"'; } $message .= ' ('.$issues->{$journals->[$j][2]}->{'issue'}->{'project_name'}.')'; } if (defined($strings->[2]) && defined($value)) { $message .= ' '.$strings->[2].' '; if ($value =~ /\s/) { $message .= '"'.$value.'"'; } else { $message .= $value; } } } } elsif ($journals->[$j][1][$d]->{'property'} eq 'cf') { if (defined($journals->[$j][1][$d]->{'value_name'})) { if ($journals->[$j][1][$d]->{'value'}) { $message .= 'set '; } else { $message .= Orangutan::Context::Random('unset ', 'removed ', 'cleared '); } $message .= $journals->[$j][1][$d]->{'value_name'}; if ((scalar @{$journals->[$j][1]} == 1) && (scalar @changes == 1) && ($issues->{$journals->[$j][2]}->{'count'} == 1)) { $message .= ' of '; $message .= $issues->{$journals->[$j][2]}->{'issue'}->{'tracker_name'}.' '; $message .= sprintf("#%d ", $journals->[$j][2]); $message .= '"'.$issues->{$journals->[$j][2]}->{'issue'}->{'subject'}.'"'; $message .= ' ('.$issues->{$journals->[$j][2]}->{'issue'}->{'project_name'}.')'; } if ($journals->[$j][1][$d]->{'value'}) { $message .= ' to '; if ($journals->[$j][1][$d]->{'value'} =~ /\s/) { $message .= '"'.$journals->[$j][1][$d]->{'value'}.'"'; } else { $message .= $journals->[$j][1][$d]->{'value'}; } } } } else { $main::logger->Log('unknown or unsupported property: %s', $journals->[$j][1][$d]->{'property'}); } if (scalar @{$journals->[$j][1]} > 1) { $message .= "\n"; } } if ($journals->[$j][0]->{'notes'}) { if (scalar @{$journals->[$j][1]} == 1) { $message .= ' and '; } elsif (scalar @{$journals->[$j][1]} > 1) { $message .= 'And '; } if ((scalar @{$journals->[$j][1]} == 0) && (scalar @changes == 1) && ($issues->{$journals->[$j][2]}->{'count'} == 1)) { if (!defined($journals->[$j][0]->{'status'}) || ($journals->[$j][0]->{'status'} eq 'add')) { $message .= Orangutan::Context::Random( 'wrote the following to', 'added comment to', 'added note to', 'commented' ); } elsif ($journals->[$j][0]->{'status'} eq 'change') { $message .= Orangutan::Context::Random( 'changed the following comment in', 'modified comment in', 'changed comment in' ); } else { $message .= Orangutan::Context::Random( 'removed comment in', 'removed note in' ); } $message .= ' '; $message .= $issues->{$journals->[$j][2]}->{'issue'}->{'tracker_name'}.' '; $message .= sprintf("#%d ", $journals->[$j][2]); $message .= '"'.$issues->{$journals->[$j][2]}->{'issue'}->{'subject'}.'"'; $message .= ' ('.$issues->{$journals->[$j][2]}->{'issue'}->{'project_name'}.')'; } else { if (!defined($journals->[$j][0]->{'status'}) || ($journals->[$j][0]->{'status'} eq 'add')) { $message .= Orangutan::Context::Random( 'wrote the following', 'added comment', 'added note', 'commented' ); } elsif ($journals->[$j][0]->{'status'} eq 'change') { $message .= Orangutan::Context::Random( 'made changes to comment', 'modified comment', 'changed comment', 'changed note' ); } else { $message .= Orangutan::Context::Random( 'deleted comment', 'removed comment', 'removed note' ); } } $message .= ":\n"; ShowIssue_Format(\$journals->[$j][0]->{'notes'}); $message .= $journals->[$j][0]->{'notes'}; $message .= "\n"; } elsif (scalar @{$journals->[$j][1]} == 1) { $message .= '.'; } } } } return $message; }, onissue => sub { my ($context, $user, $type, $status, $operation, $issue, $journal, @details) = @_; if (!$user->IsSet(USER_NONOTIFY) && ($status eq 'change')) { if (($operation ne 'remove') || ((scalar @details == 0) && $journal->{'notes'})) { if ($journal->{'user_id'} != $user->GetUserID) { my $newjournal = $user->GetRequest($context->GetID); if (defined($newjournal)) { my $issues = $newjournal->GetField('issues'); my $newjournals = $newjournal->GetField('journals'); if (defined($issues->{$issue->{'id'}})) { $issues->{$issue->{'id'}}->{'issue'} = $issue; $issues->{$issue->{'id'}}->{'count'}++; } else { $issues->{$issue->{'id'}} = { issue => $issue, count => 1 }; } if ($operation eq 'add') { push(@{$newjournals}, [ $journal, \@details, $issue->{'id'} ]); } else { push(@{$newjournals}, [ $journal, [ ], $issue->{'id'} ]); } } else { my %issues = ( ); my @newjournals = ( ); $issues{$issue->{'id'}} = { issue => $issue, count => 1 }; if ($operation eq 'add') { push(@newjournals, [ $journal, \@details, $issue->{'id'} ]); } else { push(@newjournals, [ $journal, [ ], $issue->{'id'} ]); } $newjournal = $user->AddRequest($context, 10); $newjournal->SetField('issues', \%issues); $newjournal->SetField('journals', \@newjournals); } } } } }, weight => undef, copy => 1 ); # kate: syntax perl