# Type: Input # Category: Issue # Description: Allows to watch/unwatch issue/project # Author: Andriy Lesyuk new Orangutan::Context( response => sub { my ($context, $user, $message) = @_; my @result = ( ); # Remove ending $message =~ s/!*\.*$//; my @regexps = ( '((?:Do(?:n\'t| not)|stop) )?(?:notify|tell|write?)(?:ing)?(?: (?:me|([^ ]+?)))? (?:when|if) '. '(?:an? )?(?:new )?issues? (?:was|is|are|were) (?:add|submitt)ed to', # project '((?:Do(?:n\'t| not)|stop) )?(?:notify|tell|write?)(?:ing)?(?: (?:me|([^ ]+?)))? (?:when|if) '. '(?:someone|anybody) (?:add|submitt?)(?:s|ed) (?:an? )?(?:new )?issues? to', # project '((?:Do(?:n\'t| not)|stop) )?(?:notify|tell|write?)(?:ing)?(?: (?:me|([^ ]+?)))? '. 'about (?:an? )?(?:new )?issues? (?:on|in)', # project '((?:Do(?:n\'t| not)|stop) )?(?:notify|tell|write?)(?:ing)?(?: (?:me|([^ ]+?)))? '. '(?:about|on) (?:any )?changes(?: (?:made|done))? (?:to|in)', '(?:(?:Add|insert)|(remove|delete)) (?:me|([^ ]+?)) (?:from|(?:in)?to) (?:the )?watchers(?: list)? (?:of|for)', '(?:Start |(stop |un))?(?:watch|follow)(?:ing)?' ); # Check basic request for (my $i = 0; $i < scalar @regexps; $i++) { if ($message =~ /^$regexps[$i] */i) { if ($i < 3) { push(@result, 2); } else { push(@result, undef); } push(@result, $1); if (defined($2)) { my ($id, $login) = $main::query->UserExists($2); if ($id) { push(@result, $id); push(@result, $login); } else { push(@result, 0); push(@result, undef); } } else { push(@result, undef); push(@result, undef); } $message =~ s/^$regexps[$i] *//i; last; } } if (scalar @result > 0) { push(@result, undef); # Extract/remove project my $regexp = '(?:(?:the )?project )?["\']?(.+?)["\']?'; if ($message =~ /^$regexp$/i) { my $project = $main::query->ProjectExists($1); if (defined($project) && (ref $project eq 'HASH')) { $result[0] = 2; $result[4] = $project; $message =~ s/^$regexp$//i; } } # Extract/remove issue if (!defined($result[0])) { # Prepare tracker regexp my $tracker = 'issue'; my $trackers = $main::config->Get('redmine', '_trackers_regexp'); if ($trackers) { $tracker = "(?:issue|$trackers)"; } $regexp = '(?:(?:the )?'.$tracker.' )?(?:#|No\.? ?)?([0-9]+)'; if ($message =~ /^$regexp$/i) { $result[0] = 1; $result[4] = $1; $message =~ s/^$regexp$//i; } } if (!defined($result[4]) || ($message !~ /^ *$/)) { @result = ( ); } } return @result; }, handler => sub { my ($context, $user, $item, $negation, $userid, $username, $watch) = @_; if (!defined($userid)) { $userid = $user->GetUserID; $username = $user->GetLogin; } elsif ($userid == 0) { $user->SendMessage([ 'Looks like the username you specified is invalid...', 'There is no such user...', 'Invalid username!' ]); return; } my $watched; my $projectid; if (ref $watch eq 'HASH') { $watched = $watch; $projectid = $watch->{'id'}; } else { $watched = $main::query->GetIssue($watch); if ($watched) { $projectid = $watched->{'project_id'}; } else { $user->SendMessage([ "Well... I'm sure nothing is going to happen on this issue... Because it does not exist! ;)", 'Nothing to watch - there is no such issue!', 'Please check the issue number...', 'There is no such issue!' ]); return; } } if ($userid == $user->GetUserID) { if (!$main::query->Can('view_issues', $user, $projectid)) { if (ref $watch eq 'HASH') { $user->SendMessage([ 'To watch the project you must be its member!', 'You are not a member of this project!', 'Not a member of the project...' ]); } else { $user->SendMessage([ 'Even if I added you as a watcher you would not be able to access it...', 'You have no permissions to access this issue!', 'You cannot access this issue!' ]); } return; } } else { if (!$main::query->Can(($negation) ? 'delete_issue_watchers' : 'add_issue_watchers', $user, $projectid)) { if (ref $watch eq 'HASH') { $user->SendMessage([ 'You have not enough rights to add watchers to this project...', "You can't add watchers to this project!", 'No permissions to add watchers!' ]); } else { $user->SendMessage([ 'You have no permissions to add watchers to this issue...', 'You cannot add watchers to this issue!', "You can't add watchers to this issue!" ]); } return; } if (!$main::query->Can('view_issues', $userid, $projectid)) { if (ref $watch eq 'HASH') { $user->SendMessage([ 'This user is not a member of the project...', 'The user must be a member of the project!', 'Not a member of the project...' ]); } else { $user->SendMessage([ 'No reason to add the user as a watcher - the user has no access to this issue...', 'The user has no permissions to access this issue!', 'This user cannot access this issue!' ]); } return; } } my %watcher = ( ); if (ref $watch eq 'HASH') { $watcher{'watchable_type'} = 'Project'; $watcher{'watchable_id'} = $watch->{'id'}; } else { $watcher{'watchable_type'} = 'Issue'; $watcher{'watchable_id'} = $watch; } $watcher{'user_id'} = $userid; $watcher{'login'} = $username; $watcher{'author_id'} = $user->GetUserID; $watcher{'author'} = $user->GetLogin; if ($negation) { my $id = $main::query->IsWatching($userid, $watch); if ($id) { $main::query->RemoveWatcher($id); my $notified = $main::users->GetUserID($userid); if (defined($notified)) { $notified->FireEvent('watcher', 'remove', \%watcher, $watched); } $main::config->Context('Ok')->Tell($user); } else { if ($userid == $user->GetUserID) { $user->SendMessage([ 'You are not in watchers list...', 'You are not watching it...', 'Nothing to change...' ]); } else { $user->SendMessage([ 'User is not in watchers list...', 'User is not watching it...', 'Nothing to change...' ]); } } } else { if (!$main::query->IsWatching($userid, $watch)) { $main::query->AddWatcher($userid, $watch); my $notified = $main::users->GetUserID($userid); if (defined($notified)) { $notified->FireEvent('watcher', 'add', \%watcher, $watched); } $main::config->Context('Ok')->Tell($user); } else { if ($userid == $user->GetUserID) { $user->SendMessage([ 'You are already watching it!', 'Already in watchers list.', 'You already watch it!' ]); } else { $user->SendMessage([ 'User is already watching it!', 'Already in watchers list.', 'User already watches it!' ]); } } } }, help => [{ title => 'How can I add myself to watchers list?', question => [ '^(?:How )?(?:(?:can|do) I |to )(?:watch|follow) (?:(?:changes|modifications) '. '(?:made )?(?:in|of|to) )?(?:(?:the|an?|some) )?(?:project|issue)\?*!*\.*$', '^(?:How )?(?:(?:can|do) I |to )add myself (?:to|as) (?:(?:the|a) )?watchers?(?: list)?'. '(?: of (?:(?:the|an?|some) )?(?:project|issue))?\?*!*\.*$' ], answer => "You can ask Orangutan to add you as a watcher to a project or an issue. ". "In addition to issues unlike Redmine itself Orangutan allows watching projects...\n". "Samples:\n". " o Notify me when new issues are added to Kayako\n". " o Notify me about changes in #528\n". " o Add me to watchers list of #685\n". " o Watch Orangutan\n". "Orangutan also lets you remove yourself from watchers list:\n". " o Stop notifying me about new issues in Kayako\n". " o Stop watching Orangutan\n". " o Unfollow #528\n". "Check also: How can I add another user to watchers list?", weight => 1110 }, { title => 'How can I add another user to watchers list?', question => [ '^(?:How )?(?:(?:can|do) I |to )add (?:(?:(?:an)?other|an?|the) )?(?:user|developer|tester|colleague)s? (?:to|as) '. '(?:(?:the|a) )?watchers?(?: list)?(?: of (?:(?:the|an?|some) )?(?:project|issue))?\?*!*\.*$', '^(?:How )?(?:(?:can|do) I |to )add (?:(?:another|an?|the) )?watcher'. '(?: to (?:(?:the|an?|some) )?(?:project|issue))?\?*!*\.*$' ], answer => "You can add other users as watchers to a project or an issue. ". "Just ask Orangutan to do this for you.\n". "Samples:\n". " o Notify s-andy when somebody adds new issue to Orangutan\n". " o Notify s-andy when new issue is added to Orangutan\n". " o Write s-andy about changes made to #1265\n". " o Add s-andy to watchers of #125\n". "The same way you can remove watchers:\n". " o Stop notifying s-andy about any changes made to #869\n". " o Remove s-andy from watchers of #125\n". "See also: How can I add myself to watchers list?", weight => 1111 }] ); # kate: syntax perl