[Bps-public-commit] rt-extension-spamfilter branch, add-spam-notspam-buttons-to-list-display, created. 2b4d570c4467aa9719d25f82472b0293f2bd8907

Dianne Skoll dianne at bestpractical.com
Wed Mar 3 09:27:36 EST 2021


The branch, add-spam-notspam-buttons-to-list-display has been created
        at  2b4d570c4467aa9719d25f82472b0293f2bd8907 (commit)

- Log -----------------------------------------------------------------
commit f3e2969a70cf859c807ba951fba8c33ec92fd485
Author: Dianne Skoll <dianne at bestpractical.com>
Date:   Tue Mar 2 11:47:09 2021 -0500

    Refactor Delete/Release code into methods so the code is reusable
    
    Also, get rid of unnecessarily tricky "map {} grep {} qw/.../" hack and
    just list arguments explicitly.

diff --git a/html/Tools/SpamFilter/Display.html b/html/Tools/SpamFilter/Display.html
index 0c0f730..0d26b03 100644
--- a/html/Tools/SpamFilter/Display.html
+++ b/html/Tools/SpamFilter/Display.html
@@ -64,7 +64,7 @@
 <script type="text/javascript">
     jQuery( function() {
         jQuery('div.actions button.create_user').click( function () {
-            window.location.href = '?id=<% $email->id %>;Action=CreateUser';
+            window.location.href = '?id=<% $email->id %>;Action=Release';
         });
         jQuery('div.actions button.discard').click( function () {
             window.location.href = '?id=<% $email->id %>;Action=Delete';
@@ -95,34 +95,19 @@ my @results;
 
 if ( $Action ) {
     if ( $Action eq 'Delete' ) {
-        my ($ret, $msg) = $email->SetStatus('deleted');
+        my ($ret, $msg) = $email->Delete();
         push @results, $msg;
     }
-    elsif ( $Action eq 'CreateUser' ) { 
-        my $user = RT::User->new($session{CurrentUser});
-        my ( $address, $name ) = RT::Interface::Email::ParseSenderAddressFromHead( $email->MIMEEntity->head );
-        $user->LoadOrCreateByEmail(
-            RealName     => $name,
-            EmailAddress => $address,
-            Comments     => 'Created by SpamFilter',
-        );
-        my ( $ret, $msg, $ticket ) = RT::Interface::Email::Gateway(
-            {   message => $email->Content,
-                map { lc $_ => $email->$_ } grep { $email->$_ } qw/Queue Action Ticket/,
-            }
-        );
+    elsif ( $Action eq 'Release' ) {
+        my ($ret, $msg, $ticket) = $email->Release(\%session);
+        push @results, $msg;
         if ( $ret == 1 && $ticket ) {
-            $email->SetStatus('resolved');
-            push @results, $msg;
             MaybeRedirectForResults(
                 Actions   => \@results,
                 Path      => "/Ticket/Display.html",
                 Arguments => { id => $ticket->id },
             );
         }
-        else {
-            push @results, $msg;
-        }
     }
 }
 
diff --git a/lib/RT/Spam.pm b/lib/RT/Spam.pm
index 5bd7628..845e82c 100644
--- a/lib/RT/Spam.pm
+++ b/lib/RT/Spam.pm
@@ -211,6 +211,53 @@ sub _CoreAccessible {
    }
 };
 
+=head2 Delete
+
+Mark a potential spam as deleted (ie, confirm it as spam.)  Returns
+the usual (success, msg) pair.
+
+=cut
+sub Delete
+{
+    my ($self) = @_;
+    return $self->SetStatus('deleted');
+}
+
+=head2 Release $session_hash
+
+Release a potential spam (ie, mark it as non-spam.)  Returns (success,
+msg, ticket) triplet.
+
+=cut
+sub Release
+{
+    my ($self, $session) = @_;
+
+    # FIXME: Eventually, we won't need to create a user because we'll
+    # have a proper way to indicate that this is a release spam,
+    # rather than relying on the existence of an RT::User for the
+    # sending address.  This will also eliminate the need
+    # to pass in a reference to %session.
+    my $user = RT::User->new($session->{CurrentUser});
+    my ( $address, $name ) = RT::Interface::Email::ParseSenderAddressFromHead( $self->MIMEEntity->head );
+    $user->LoadOrCreateByEmail(
+        RealName     => $name,
+        EmailAddress => $address,
+        Comments     => 'Created by SpamFilter',
+    );
+
+    my ( $ret, $msg, $ticket ) = RT::Interface::Email::Gateway(
+        {   message => $self->Content,
+            queue   => $self->Queue,
+            action  => $self->Action,
+            ticket  => $self->Ticket }
+    );
+    if ( $ret == 1 && $ticket ) {
+        $self->SetStatus('resolved');
+    }
+    return ($ret, $msg, $ticket);
+}
+
 RT::Base->_ImportOverlays();
 
 1;

commit 2b4d570c4467aa9719d25f82472b0293f2bd8907
Author: Dianne Skoll <dianne at bestpractical.com>
Date:   Wed Mar 3 09:26:36 2021 -0500

    Implement "Delete" and "Not Spam" buttons in each row of the list view

diff --git a/html/Elements/RT__Spam/ColumnMap b/html/Elements/RT__Spam/ColumnMap
index 67b79d4..339867d 100644
--- a/html/Elements/RT__Spam/ColumnMap
+++ b/html/Elements/RT__Spam/ColumnMap
@@ -52,6 +52,26 @@ $Attr => undef
 
 
 <%once>
+
+# For readability, ncapsulate the button-generating code into a
+# function here rather than putting it inline in the big hash.
+my $buttons_sub = sub {
+    my ($email) = @_;
+
+    my $ret = '';
+    if ($email->Status ne 'resolved') {
+       $ret .= '<button type="button" class="create_user" name="Release-' . $email->id . '">' . loc('Not Spam') . '</button>';
+    }
+    if ($email->Status ne 'deleted') {
+        $ret .= ' <button type="button" class="discard" name="Delete-' . $email->id . '">' . loc('Delete') . '</button>';
+    }
+
+    # Here's a magical thing... if you return a *reference* to a scalar,
+    # the result is not HTML-escaped.  If you just return a scalar, it is.
+    # Hence the \ before $ret.
+    return \$ret;
+};
+
 my $COLUMN_MAP;
 
 $COLUMN_MAP = {
@@ -91,6 +111,10 @@ $COLUMN_MAP = {
         title     => 'Score', # loc
         value     => sub { return $_[0]->Score }
     },
+    Disposition => {
+        title     => 'Disposition', #loc
+        value     => $buttons_sub,
+    },
 };
 
 </%once>
diff --git a/html/Tools/SpamFilter/List.html b/html/Tools/SpamFilter/List.html
index 87d1e8c..d3f72d3 100644
--- a/html/Tools/SpamFilter/List.html
+++ b/html/Tools/SpamFilter/List.html
@@ -48,6 +48,25 @@
 <& /Elements/Header, Title => $title &>
 <& /Elements/Tabs &>
 
+<script type="text/javascript">
+    jQuery( function() {
+        jQuery('button.create_user').click( function () {
+            url = new URL(window.location.href);
+            status = url.searchParams.get('Status');
+            bits = this.name.split('-');
+            window.location.href = '?emailid=' + bits[1] + '&Action=' + bits[0] + '&Status=' + status;
+        });
+        jQuery('button.discard').click( function () {
+            url = new URL(window.location.href);
+            status = url.searchParams.get('Status');
+            bits = this.name.split('-');
+            window.location.href = '?emailid=' + bits[1] + '&Action=' + bits[0] + '&Status=' + status;
+        });
+    });
+</script>
+
+<& /Elements/ListActions, actions => \@results &>
+
 <div class="spams">
 <& /Elements/CollectionList, 
     Collection => $emails,
@@ -62,7 +81,9 @@
 
 <%init>
 
-$Format ||= qq{'<a href="__WebPath__/Tools/SpamFilter/Display.html?id=__id__">__id__</a>/TITLE:#','<a href="__WebPath__/Tools/SpamFilter/Display.html?id=__id__">__Subject__</a>',__From__,__To__,__Status__,__Date__,__Score__};
+my @results;
+$Format ||= qq{'<a href="__WebPath__/Tools/SpamFilter/Display.html?id=__id__">__id__</a>/TITLE:#','<a href="__WebPath__/Tools/SpamFilter/Display.html?id=__id__">__Subject__</a>',__From__,__To__,__Status__,__Date__,__Score__,__Disposition__};
+
 $Order    ||= 'ASC';
 $OrderBy  ||= 'id';
 
@@ -70,6 +91,27 @@ $Rows ||= 50;
 $Page = 1 unless $Page && $Page > 0;
 
 use RT::Spams;
+use RT::Spam;
+
+if ($Action && $emailid) {
+    my $email = RT::Spam->new($session{'CurrentUser'}) ;
+    $email->Load($emailid);
+    if ($email->id) {
+        my ($ret, $msg, $ticket);
+        if ($Action eq 'Delete') {
+            ($ret, $msg) = $email->Delete();
+            if ($ret) {
+                push(@results, loc('Marked item [_1] as spam', $email->id));
+            }
+        } elsif ($Action eq 'Release') {
+            ($ret, $msg, $ticket) = $email->Release(\%session);
+            if ($ret) {
+                push(@results, loc('Marked item [_1] as non-spam and created ticket #[_2]', $email->id, $ticket->Id));
+            }
+        }
+    }
+}
+
 my $emails = RT::Spams->new($session{'CurrentUser'}) ;
 $emails->Limit(FIELD => 'Status', VALUE => $Status );
 $emails->OrderBy( FIELD => $OrderBy, ORDER => $Order );
@@ -94,4 +136,6 @@ $Page => 1
 $OrderBy => undef
 $Order => undef
 $Status => 'new'
+$Action => undef,
+$emailid => undef
 </%ARGS>

-----------------------------------------------------------------------


More information about the Bps-public-commit mailing list