# Type: Output (event based) # Category: Issue # Description: Notifies users that they are added as watchers somewhere # Copied: Yes # Author: Andriy Lesyuk use Orangutan::User; new Orangutan::Context( request => sub { my ($context, $user) = @_; my $message = ''; my $watchers = $context->GetField('watchers'); if ($watchers) { $message .= Orangutan::Context::Random( 'From now you will receive notifications about changes in', 'You were added to watchers list of', 'Now you are a watcher of', 'Now you are watching' ); if (scalar @{$watchers} > 1) { $message .= ":\n"; } else { $message .= ' '; } for (my $i = 0; $i < scalar @{$watchers}; $i++) { if (scalar @{$watchers} > 1) { $message .= ' o '; } if ($watchers->[$i][0]->{'watchable_type'} eq 'Project') { $message .= 'project '; $message .= '"'.$watchers->[$i][1]->{'name'}.'"'; } elsif ($watchers->[$i][0]->{'watchable_type'} eq 'Issue') { $message .= $watchers->[$i][1]->{'tracker_name'}.' '; $message .= sprintf("#%d: ", $watchers->[$i][1]->{'id'}); $message .= $watchers->[$i][1]->{'subject'}; } if (defined($watchers->[$i][0]->{'author'})) { $message .= ' ('; $message .= Orangutan::Context::Random( 'say thanks to', 'the idea of', 'added by' ); $message .= ' '; $message .= Orangutan::User::GetDisplayName($watchers->[$i][0]->{'author'}); $message .= ')'; } if (scalar @{$watchers} > 1) { $message .= "\n"; } } if (scalar @{$watchers} == 1) { $message .= '.'; } } my $removed = $context->GetField('removed'); if ($removed) { if ($watchers) { $message .= ' '; } $message .= Orangutan::Context::Random( 'You will no longer receive notifications about changes in', "From now you won't be notified about changes in", 'You were removed from watchers list of' ); if (scalar @{$removed} > 1) { $message .= ":\n"; } else { $message .= ' '; } for (my $i = 0; $i < scalar @{$removed}; $i++) { if (scalar @{$removed} > 1) { $message .= ' o '; } if ($removed->[$i][0]->{'watchable_type'} eq 'Project') { $message .= 'project '; $message .= '"'.$removed->[$i][1]->{'name'}.'"'; } elsif ($removed->[$i][0]->{'watchable_type'} eq 'Issue') { $message .= $removed->[$i][1]->{'tracker_name'}.' '; $message .= sprintf("#%d: ", $removed->[$i][1]->{'id'}); $message .= $removed->[$i][1]->{'subject'}; } if (defined($removed->[$i][0]->{'author'})) { $message .= ' ('; $message .= 'removed by'; $message .= ' '; $message .= Orangutan::User::GetDisplayName($removed->[$i][0]->{'author'}); $message .= ')'; } if (scalar @{$removed} > 1) { $message .= "\n"; } } if (scalar @{$removed} == 1) { $message .= '.'; } } return $message; }, onwatcher => sub { my ($context, $user, $type, $status, $watcher, $watched) = @_; if (!$user->IsSet(USER_NONOTIFY) && (!defined($watcher->{'author_id'}) || ($watcher->{'author_id'} != $watcher->{'user_id'}))) { if ($status eq 'add') { my $newwatcher = $user->GetRequest($context->GetID); if ($newwatcher) { my $oldwatchers = $newwatcher->GetField('removed'); if ($oldwatchers) { my $i; for ($i = 0; $i < scalar @{$oldwatchers}; $i++) { if (($oldwatchers->[$i][0]->{'watchable_type'} eq $watcher->{'watchable_type'}) && ($oldwatchers->[$i][0]->{'watchable_id'} == $watcher->{'watchable_id'}) && ($oldwatchers->[$i][0]->{'user_id'} == $watcher->{'user_id'})) { last; } } if ($i < scalar @{$oldwatchers}) { splice(@{$oldwatchers}, $i, 1); if (scalar @{$oldwatchers} == 0) { $newwatcher->UnsetField('removed'); my $newwatchers = $newwatcher->GetField('watchers'); if (!$newwatchers || (scalar @{$newwatchers} == 0)) { $user->RemoveRequest($newwatcher); } } return; } } } if ($newwatcher && $newwatcher->GetField('watchers')) { my $newwatchers = $newwatcher->GetField('watchers'); push(@{$newwatchers}, [ $watcher, $watched ]); } else { my @newwatchers = ( ); push(@newwatchers, [ $watcher, $watched ]); $newwatcher = $user->AddRequest($context); $newwatcher->SetField('watchers', \@newwatchers); } } elsif ($status eq 'remove') { my $newwatcher = $user->GetRequest($context->GetID); if ($newwatcher) { my $newwatchers = $newwatcher->GetField('watchers'); if ($newwatchers) { my $i; for ($i = 0; $i < scalar @{$newwatchers}; $i++) { if (($newwatchers->[$i][0]->{'watchable_type'} eq $watcher->{'watchable_type'}) && ($newwatchers->[$i][0]->{'watchable_id'} == $watcher->{'watchable_id'}) && ($newwatchers->[$i][0]->{'user_id'} == $watcher->{'user_id'})) { last; } } if ($i < scalar @{$newwatchers}) { splice(@{$newwatchers}, $i, 1); if (scalar @{$newwatchers} == 0) { $newwatcher->UnsetField('watchers'); my $oldwatchers = $newwatcher->GetField('removed'); if (!$oldwatchers || (scalar @{$oldwatchers} == 0)) { $user->RemoveRequest($newwatcher); } } return; } } } if ($newwatcher && $newwatcher->GetField('removed')) { my $oldwatchers = $newwatcher->GetField('removed'); push(@{$oldwatchers}, [ $watcher, $watched ]); } else { my @oldwatchers = ( ); push(@oldwatchers, [ $watcher, $watched ]); $newwatcher = $user->AddRequest($context); $newwatcher->SetField('removed', \@oldwatchers); } } } }, weight => undef, copy => 1 ); # kate: syntax perl