From 3ecab1a85f0f8f113094e5d3b1a46ec939a893da Mon Sep 17 00:00:00 2001 From: Victor Boctor Date: Mon, 25 Apr 2016 23:02:19 -0700 Subject: [PATCH] Make mention processing an explicit API action Don't process mentions as part of create API for issues and notes. However, make it a separate APIs and call it from applicable scenarios. --- api/soap/mc_issue_api.php | 11 +++++++++- bug_actiongroup.php | 3 ++- bug_actiongroup_add_note_inc.php | 3 ++- bug_reminder.php | 3 +++ bug_report.php | 4 +++- bug_update.php | 3 ++- bugnote_add.php | 3 +++ core/bug_api.php | 23 ++++++++++++++------ core/bugnote_api.php | 37 +++++++++++++++++++++----------- 9 files changed, 66 insertions(+), 24 deletions(-) diff --git a/api/soap/mc_issue_api.php b/api/soap/mc_issue_api.php index 5c056082e8..e254787ec0 100644 --- a/api/soap/mc_issue_api.php +++ b/api/soap/mc_issue_api.php @@ -787,6 +787,8 @@ function mc_issue_add( $p_username, $p_password, stdClass $p_issue ) { # submit the issue $t_issue_id = $t_bug_data->create(); + $t_bug_data->process_mentions(); + log_event( LOG_WEBSERVICE, 'created new issue id \'' . $t_issue_id . '\'' ); $t_set_custom_field_error = mci_issue_set_custom_fields( $t_issue_id, $p_issue['custom_fields'], false ); @@ -821,6 +823,9 @@ function mc_issue_add( $p_username, $p_password, stdClass $p_issue ) { $t_note_attr, $t_user_id, false ); # don't send mail + + bugnote_process_mentions( $t_issue_id, $t_note_id, $t_note['text'] ); + log_event( LOG_WEBSERVICE, 'bugnote id \'' . $t_note_id . '\' added to issue \'' . $t_issue_id . '\'' ); } } @@ -1239,7 +1244,11 @@ function mc_issue_note_add( $p_username, $p_password, $p_issue_id, stdClass $p_n $t_note_attr = isset( $p_note['note_type'] ) ? $p_note['note_attr'] : ''; log_event( LOG_WEBSERVICE, 'adding bugnote to issue \'' . $p_issue_id . '\'' ); - return bugnote_add( $p_issue_id, $p_note['text'], mci_get_time_tracking_from_note( $p_issue_id, $p_note ), $t_view_state_id == VS_PRIVATE, $t_note_type, $t_note_attr, $t_reporter_id ); + $t_bugnote_id = bugnote_add( $p_issue_id, $p_note['text'], mci_get_time_tracking_from_note( $p_issue_id, $p_note ), $t_view_state_id == VS_PRIVATE, $t_note_type, $t_note_attr, $t_reporter_id ); + + bugnote_process_mentions( $p_issue_id, $t_bugnote_id, $p_note['text'] ); + + return $t_bugnote_id; } /** diff --git a/bug_actiongroup.php b/bug_actiongroup.php index 3c66715e31..3cd8740e5e 100644 --- a/bug_actiongroup.php +++ b/bug_actiongroup.php @@ -212,7 +212,8 @@ # Add bugnote if supplied if( !is_blank( $f_bug_notetext ) ) { - bugnote_add( $t_bug_id, $f_bug_notetext, null, $f_bug_noteprivate ); + $t_bugnote_id = bugnote_add( $t_bug_id, $f_bug_notetext, null, $f_bug_noteprivate ); + bugnote_process_mentions( $t_bug_id, $t_bugnote_id, $f_bug_notetext ); # No need to call email_generic(), bugnote_add() does it } else { email_bug_updated( $t_bug_id ); diff --git a/bug_actiongroup_add_note_inc.php b/bug_actiongroup_add_note_inc.php index b7dfc7ceec..7690be75af 100644 --- a/bug_actiongroup_add_note_inc.php +++ b/bug_actiongroup_add_note_inc.php @@ -150,6 +150,7 @@ function action_add_note_validate( $p_bug_id ) { function action_add_note_process( $p_bug_id ) { $f_bugnote_text = gpc_get_string( 'bugnote_text' ); $f_view_state = gpc_get_int( 'view_state' ); - bugnote_add( $p_bug_id, $f_bugnote_text, '0:00', $f_view_state != VS_PUBLIC ); + $t_bugnote_id = bugnote_add( $p_bug_id, $f_bugnote_text, '0:00', $f_view_state != VS_PUBLIC ); + bugnote_process_mentions( $p_bug_id, $t_bugnote_id, $f_bugnote_text ); return null; } diff --git a/bug_reminder.php b/bug_reminder.php index bc23f7406f..7674fff0f2 100644 --- a/bug_reminder.php +++ b/bug_reminder.php @@ -108,7 +108,10 @@ } $t_attr .= $t_recipient; } + bugnote_add( $f_bug_id, $f_body, 0, config_get( 'default_reminder_view_status' ) == VS_PRIVATE, REMINDER, $t_attr, null, false ); + + # Note: we won't trigger mentions here since reminders are triggered. } form_security_purge( 'bug_reminder' ); diff --git a/bug_report.php b/bug_report.php index 5f3285236a..dcc254123c 100644 --- a/bug_report.php +++ b/bug_report.php @@ -196,9 +196,9 @@ $t_bug_data->handler_id = auth_get_current_user_id(); } - # Create the bug $t_bug_id = $t_bug_data->create(); +$t_bug_data->process_mentions(); # Mark the added issue as visited so that it appears on the last visited list. last_visited_issue( $t_bug_id ); @@ -271,6 +271,8 @@ 0, 0, false ); + + # Note: we won't trigger mentions in the clone scenario. } } diff --git a/bug_update.php b/bug_update.php index 62863a648d..1c9273cda1 100644 --- a/bug_update.php +++ b/bug_update.php @@ -402,7 +402,8 @@ # Add a bug note if there is one. if( $t_bug_note->note || helper_duration_to_minutes( $t_bug_note->time_tracking ) > 0 ) { - bugnote_add( $f_bug_id, $t_bug_note->note, $t_bug_note->time_tracking, $t_bug_note->view_state == VS_PRIVATE, 0, '', null, false ); + $t_bugnote_id = bugnote_add( $f_bug_id, $t_bug_note->note, $t_bug_note->time_tracking, $t_bug_note->view_state == VS_PRIVATE, 0, '', null, false ); + bugnote_process_mentions( $f_bug_id, $t_bugnote_id, $t_bug_note->note ); } # Add a duplicate relationship if requested. diff --git a/bugnote_add.php b/bugnote_add.php index 6bbe2bb4f8..36d63d56b5 100644 --- a/bugnote_add.php +++ b/bugnote_add.php @@ -94,6 +94,9 @@ trigger_error( ERROR_EMPTY_FIELD, ERROR ); } +# Process the mentions in the added note +bugnote_process_mentions( $t_bug->id, $t_bugnote_id, $f_bugnote_text ); + # Handle the reassign on feedback feature. Note that this feature generally # won't work very well with custom workflows as it makes a lot of assumptions # that may not be true. It assumes you don't have any statuses in the workflow diff --git a/core/bug_api.php b/core/bug_api.php index ee13553a22..6b953efd84 100644 --- a/core/bug_api.php +++ b/core/bug_api.php @@ -546,6 +546,15 @@ function create() { history_log_event_direct( $this->id, 'status', $t_original_status, $t_status ); history_log_event_direct( $this->id, 'handler_id', 0, $this->handler_id ); + return $this->id; + } + + /** + * Process mentions in the current issue, for example, after the issue is created. + * @return void + * @access public + */ + function process_mentions() { # Now that the issue is added process the @ mentions $t_all_mentioned_user_ids = array(); @@ -591,8 +600,6 @@ function create() { $t_mention_text, $t_removed_mentions_user_ids ); } - - return $this->id; } /** @@ -1713,7 +1720,8 @@ function bug_assign( $p_bug_id, $p_user_id, $p_bugnote_text = '', $p_bugnote_pri history_log_event_direct( $p_bug_id, 'handler_id', $h_handler_id, $p_user_id ); # Add bugnote if supplied ignore false return - bugnote_add( $p_bug_id, $p_bugnote_text, 0, $p_bugnote_private, 0, '', null, false ); + $t_bugnote_id = bugnote_add( $p_bug_id, $p_bugnote_text, 0, $p_bugnote_private, 0, '', null, false ); + bugnote_process_mentions( $p_bug_id, $t_bugnote_id, $p_bugnote_text ); # updated the last_updated date bug_update_date( $p_bug_id ); @@ -1742,7 +1750,8 @@ function bug_close( $p_bug_id, $p_bugnote_text = '', $p_bugnote_private = false, # Add bugnote if supplied ignore a false return # Moved bugnote_add before bug_set_field calls in case time_tracking_no_note is off. # Error condition stopped execution but status had already been changed - bugnote_add( $p_bug_id, $p_bugnote_text, $p_time_tracking, $p_bugnote_private, 0, '', null, false ); + $t_bugnote_id = bugnote_add( $p_bug_id, $p_bugnote_text, $p_time_tracking, $p_bugnote_private, 0, '', null, false ); + bugnote_process_mentions( $p_bug_id, $t_bugnote_id, $p_bugnote_text ); bug_set_field( $p_bug_id, 'status', config_get( 'bug_closed_status_threshold' ) ); @@ -1772,7 +1781,8 @@ function bug_resolve( $p_bug_id, $p_resolution, $p_fixed_in_version = '', $p_bug # Add bugnote if supplied # Moved bugnote_add before bug_set_field calls in case time_tracking_no_note is off. # Error condition stopped execution but status had already been changed - bugnote_add( $p_bug_id, $p_bugnote_text, $p_time_tracking, $p_bugnote_private, 0, '', null, false ); + $t_bugnote_id = bugnote_add( $p_bug_id, $p_bugnote_text, $p_time_tracking, $p_bugnote_private, 0, '', null, false ); + bugnote_process_mentions( $p_bug_id, $t_bugnote_id, $p_bugnote_text ); $t_duplicate = !is_blank( $p_duplicate_id ) && ( $p_duplicate_id != 0 ); if( $t_duplicate ) { @@ -1857,7 +1867,8 @@ function bug_reopen( $p_bug_id, $p_bugnote_text = '', $p_time_tracking = '0:00', # Add bugnote if supplied # Moved bugnote_add before bug_set_field calls in case time_tracking_no_note is off. # Error condition stopped execution but status had already been changed - bugnote_add( $p_bug_id, $p_bugnote_text, $p_time_tracking, $p_bugnote_private, 0, '', null, false ); + $t_bugnote_id = bugnote_add( $p_bug_id, $p_bugnote_text, $p_time_tracking, $p_bugnote_private, 0, '', null, false ); + bugnote_process_mentions( $p_bug_id, $t_bugnote_id, $p_bugnote_text ); bug_set_field( $p_bug_id, 'status', config_get( 'bug_reopen_status' ) ); bug_set_field( $p_bug_id, 'resolution', config_get( 'bug_reopen_resolution' ) ); diff --git a/core/bugnote_api.php b/core/bugnote_api.php index 9ff3dbdef7..2bbf0d658f 100644 --- a/core/bugnote_api.php +++ b/core/bugnote_api.php @@ -259,11 +259,32 @@ function bugnote_add( $p_bug_id, $p_bugnote_text, $p_time_tracking = '0:00', $p_ history_log_event_special( $p_bug_id, BUGNOTE_ADDED, bugnote_format_id( $t_bugnote_id ) ); } + # Event integration + event_signal( 'EVENT_BUGNOTE_ADD', array( $p_bug_id, $t_bugnote_id ) ); + + # only send email if the text is not blank, otherwise, it is just recording of time without a comment. + if( true == $p_send_email && !is_blank( $t_bugnote_text ) ) { + email_bugnote_add( $p_bug_id ); + } + + return $t_bugnote_id; +} + +/** + * Process mentions in bugnote, typically after its added. + * + * @param int $p_bug_id The bug id + * @param int $p_bugnote_id The bugnote id + * @param string $p_bugnote_text The bugnote text + * @return void + * @access public + */ +function bugnote_process_mentions( $p_bug_id, $p_bugnote_id, $p_bugnote_text ) { # Process the mentions that have access to the issue note - $t_mentioned_user_ids = mention_get_users( $t_bugnote_text ); + $t_mentioned_user_ids = mention_get_users( $p_bugnote_text ); $t_filtered_mentioned_user_ids = access_has_bugnote_level_filter( config_get( 'view_bug_threshold' ), - $t_bugnote_id, + $p_bugnote_id, $t_mentioned_user_ids ); $t_removed_mentions_user_ids = array_diff( $t_mentioned_user_ids, $t_filtered_mentioned_user_ids ); @@ -271,18 +292,8 @@ function bugnote_add( $p_bug_id, $p_bugnote_text, $p_time_tracking = '0:00', $p_ mention_process_user_mentions( $p_bug_id, $t_filtered_mentioned_user_ids, - $t_bugnote_text, + $p_bugnote_text, $t_removed_mentions_user_ids ); - - # Event integration - event_signal( 'EVENT_BUGNOTE_ADD', array( $p_bug_id, $t_bugnote_id ) ); - - # only send email if the text is not blank, otherwise, it is just recording of time without a comment. - if( true == $p_send_email && !is_blank( $t_bugnote_text ) ) { - email_bugnote_add( $p_bug_id ); - } - - return $t_bugnote_id; } /**