[Bps-public-commit] rt-extension-rest2 01/05: Abstract _add_message to return results instead of touching http response

sunnavy sunnavy at bestpractical.com
Fri Jul 16 19:39:46 UTC 2021


This is an automated email from the git hooks/post-receive script.

jbrandt pushed a commit to branch tickets-bulk-message
in repository rt-extension-rest2.

commit 74939689042fa958a14895ad637e13c99fc7832b
Author: sunnavy <sunnavy at bestpractical.com>
AuthorDate: Thu Jun 17 23:57:24 2021 +0800

    Abstract _add_message to return results instead of touching http response
    
    Thus we can reuse it in TicketsBulk later.
    
    Note that "CustomRoles" argument conversion is moved before input
    validation: as custom role arguments are like "RT::CustomRole-1": "root"
    in web UI(/Ticket/Update.html), the validation code customized in web UI
    is more compatible in REST2 with this change.
---
 lib/RT/Extension/REST2/Resource/Message.pm | 49 +++++++++++++++++++-----------
 1 file changed, 31 insertions(+), 18 deletions(-)

diff --git a/lib/RT/Extension/REST2/Resource/Message.pm b/lib/RT/Extension/REST2/Resource/Message.pm
index 48ffbf6..4421446 100644
--- a/lib/RT/Extension/REST2/Resource/Message.pm
+++ b/lib/RT/Extension/REST2/Resource/Message.pm
@@ -106,26 +106,43 @@ sub from_json {
 sub add_message {
     my $self = shift;
     my %args = @_;
+
+    my ( $return_code, @results ) = $self->_add_message(%args);
+    if ( $return_code != 201 ) {
+        return error_as_json( $self->response, \$return_code, join "\n", @results );
+    }
+
+    $self->response->body( JSON::to_json( \@results, { pretty => 1 } ) );
+    return 1;
+}
+
+sub _add_message {
+    my $self = shift;
+    my %args = @_;
     my @results;
 
-    my $MIME = HTML::Mason::Commands::MakeMIMEEntity(
-        Interface => 'REST',
-        $args{NoContent} ? () : (Body => $args{Content} || $self->request->content),
-        Type      => $args{ContentType} || $self->request->content_type,
-        Subject   => $args{Subject},
-    );
+    # update_role_members wants custom role IDs (like RT::CustomRole-ID)
+    # rather than role names.
+    %args = ( %args, %{ fix_custom_role_ids( $self->record, $args{CustomRoles} ) } ) if $args{CustomRoles};
 
     # Check for any bad input data before making updates
     my ($ok, $errmsg, $return_code) = $self->validate_input(\%args);
     if (!$ok) {
         if ( $return_code ) {
-            return error_as_json($self->response, \$return_code, $errmsg);
+            return ($return_code, $errmsg);
         }
         else {
-            return error_as_json($self->response, \400, $errmsg);
+            return (400, $errmsg);
         }
     }
 
+    my $MIME = HTML::Mason::Commands::MakeMIMEEntity(
+        Interface => 'REST',
+        $args{NoContent} ? () : (Body => $args{Content} || $self->request->content),
+        Type      => $args{ContentType} || $self->request->content_type,
+        Subject   => $args{Subject},
+    );
+
     # Process attachments
     foreach my $attachment (@{$args{Attachments}}) {
         $MIME->attach(
@@ -149,22 +166,19 @@ sub add_message {
         );
     }
     else {
-        return \400;
+        push @results, $self->current_user->loc('Unknown type');
+        return ( 400, @results );
     }
 
     if (!$Trans) {
-        return error_as_json(
-            $self->response,
-            \400, $msg || "Message failed for unknown reason");
+        push @results, $msg || $self->current_user->loc("Message failed for unknown reason");
+        return ( 400, @results );
     }
 
     push @results, $msg;
     push @results, update_custom_fields($self->record, $args{CustomFields});
 
-    # update_role_members wants custom role IDs (like RT::CustomRole-ID)
-    # rather than role names.
-    my $renamed_custom_roles = fix_custom_role_ids($self->record, $args{CustomRoles});
-    push @results, update_role_members($self->record, $renamed_custom_roles);
+    push @results, update_role_members($self->record, \%args);
     push @results, $self->_update_txn_custom_fields( $TransObj, $args{TxnCustomFields} || $args{TransactionCustomFields} );
 
     # Set ticket status if we were passed a "Status":"foo" argument
@@ -174,9 +188,8 @@ sub add_message {
     }
 
     $self->created_transaction($TransObj);
-    $self->response->body(JSON::to_json(\@results, { pretty => 1 }));
 
-    return 1;
+    return ( 201, @results );
 }
 
 sub _update_txn_custom_fields {

-- 
To stop receiving notification emails like this one, please contact
sysadmin at bestpractical.com.


More information about the Bps-public-commit mailing list