[Bps-public-commit] rt-extension-excelfeed branch, master, updated. 0.04-1-gc6a35c9

? sunnavy sunnavy at bestpractical.com
Tue Sep 4 14:41:17 EDT 2018


The branch, master has been updated
       via  c6a35c95a8b9432317ce716f18b836850f974152 (commit)
      from  61592b053418ec094ff2a31ec493ca6359e88272 (commit)

Summary of changes:
 lib/RT/Dashboard/Mailer_Vendor.pm | 420 --------------------------------------
 1 file changed, 420 deletions(-)

- Log -----------------------------------------------------------------
commit c6a35c95a8b9432317ce716f18b836850f974152
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Tue Sep 4 23:01:02 2018 +0800

    Remove duplicated code from core RT
    
    Especially because some of them are outdated

diff --git a/lib/RT/Dashboard/Mailer_Vendor.pm b/lib/RT/Dashboard/Mailer_Vendor.pm
index 104689e..0204c83 100644
--- a/lib/RT/Dashboard/Mailer_Vendor.pm
+++ b/lib/RT/Dashboard/Mailer_Vendor.pm
@@ -51,152 +51,8 @@ use strict;
 use warnings;
 no warnings 'redefine';
 
-use HTML::Mason;
-use HTML::RewriteAttributes::Links;
-use HTML::RewriteAttributes::Resources;
-use MIME::Types;
-use POSIX 'tzset';
-use RT::Dashboard;
-use RT::Interface::Web::Handler;
-use RT::Interface::Web;
-use File::Temp 'tempdir';
-use HTML::Scrubber;
-use URI::QueryParam;
-
 use RT::Interface::CLI qw( loc );
 
-sub MailDashboards {
-    my $self = shift;
-    my %args = (
-        All    => 0,
-        DryRun => 0,
-        Time   => time,
-        @_,
-    );
-
-    $RT::Logger->debug("Using time $args{Time} for dashboard generation");
-
-    my $from = $self->GetFrom();
-    $RT::Logger->debug("Sending email from $from");
-
-    # look through each user for her subscriptions
-    my $Users = RT::Users->new(RT->SystemUser);
-    $Users->LimitToPrivileged;
-
-    while (defined(my $user = $Users->Next)) {
-        if ($user->PrincipalObj->Disabled) {
-            $RT::Logger->debug("Skipping over " . $user->Name . " due to having a disabled account.");
-            next;
-        }
-
-        my ($hour, $dow, $dom) = HourDowDomIn($args{Time}, $user->Timezone || RT->Config->Get('Timezone'));
-        $hour .= ':00';
-        $RT::Logger->debug("Checking ".$user->Name."'s subscriptions: hour $hour, dow $dow, dom $dom");
-
-        my $currentuser = RT::CurrentUser->new;
-        $currentuser->LoadByName($user->Name);
-
-        # look through this user's subscriptions, are any supposed to be generated
-        # right now?
-        for my $subscription ($user->Attributes->Named('Subscription')) {
-            next unless $self->IsSubscriptionReady(
-                %args,
-                Subscription => $subscription,
-                User         => $user,
-                LocalTime    => [$hour, $dow, $dom],
-            );
-
-            my $email = $subscription->SubValue('Recipient')
-                     || $user->EmailAddress;
-
-            eval {
-                $self->SendDashboard(
-                    %args,
-                    CurrentUser  => $currentuser,
-                    Email        => $email,
-                    Subscription => $subscription,
-                    From         => $from,
-                )
-            };
-            if ( $@ ) {
-                $RT::Logger->error("Caught exception: $@");
-            }
-            else {
-                my $counter = $subscription->SubValue('Counter') || 0;
-                $subscription->SetSubValues(Counter => $counter + 1)
-                    unless $args{DryRun};
-            }
-        }
-    }
-}
-
-sub IsSubscriptionReady {
-    my $self = shift;
-    my %args = (
-        All          => 0,
-        Subscription => undef,
-        User         => undef,
-        LocalTime    => [0, 0, 0],
-        @_,
-    );
-
-    return 1 if $args{All};
-
-    my $subscription  = $args{Subscription};
-
-    my $counter       = $subscription->SubValue('Counter') || 0;
-
-    my $sub_frequency = $subscription->SubValue('Frequency');
-    my $sub_hour      = $subscription->SubValue('Hour');
-    my $sub_dow       = $subscription->SubValue('Dow');
-    my $sub_dom       = $subscription->SubValue('Dom');
-    my $sub_fow       = $subscription->SubValue('Fow') || 1;
-
-    my ($hour, $dow, $dom) = @{ $args{LocalTime} };
-
-    $RT::Logger->debug("Checking against subscription " . $subscription->Id . " for " . $args{User}->Name . " with frequency $sub_frequency, hour $sub_hour, dow $sub_dow, dom $sub_dom, fow $sub_fow, counter $counter");
-
-    return 0 if $sub_frequency eq 'never';
-
-    # correct hour?
-    return 0 if $sub_hour ne $hour;
-
-    # all we need is the correct hour for daily dashboards
-    return 1 if $sub_frequency eq 'daily';
-
-    if ($sub_frequency eq 'weekly') {
-        # correct day of week?
-        return 0 if $sub_dow ne $dow;
-
-        # does it match the "every N weeks" clause?
-        return 1 if $counter % $sub_fow == 0;
-
-        $subscription->SetSubValues(Counter => $counter + 1)
-            unless $args{DryRun};
-        return 0;
-    }
-
-    # if monthly, correct day of month?
-    if ($sub_frequency eq 'monthly') {
-        return $sub_dom == $dom;
-    }
-
-    # monday through friday
-    if ($sub_frequency eq 'm-f') {
-        return 0 if $dow eq 'Sunday' || $dow eq 'Saturday';
-        return 1;
-    }
-
-    $RT::Logger->debug("Invalid subscription frequency $sub_frequency for " . $args{User}->Name);
-
-    # unknown frequency type, bail out
-    return 0;
-}
-
-sub GetFrom {
-    RT->Config->Get('DashboardAddress') || RT->Config->Get('OwnerEmail')
-}
-
 sub SendDashboard {
     my $self = shift;
     my %args = (
@@ -310,105 +166,6 @@ SUMMARY
     );
 }
 
-sub ObsoleteSubscription {
-    my $self = shift;
-    my %args = (
-        From         => undef,
-        To           => undef,
-        Subscription => undef,
-        CurrentUser  => undef,
-        @_,
-    );
-
-    my $subscription = $args{Subscription};
-
-    my $ok = RT::Interface::Email::SendEmailUsingTemplate(
-        From      => $args{From},
-        To        => $args{Email},
-        Template  => 'Error: Missing dashboard',
-        Arguments => {
-            SubscriptionObj => $subscription,
-        },
-        ExtraHeaders => {
-            'X-RT-Dashboard-Subscription-Id' => $subscription->Id,
-            'X-RT-Dashboard-Id' => $subscription->SubValue('DashboardId'),
-        },
-    );
-
-    # only delete the subscription if the email looks like it went through
-    if ($ok) {
-        my ($deleted, $msg) = $subscription->Delete();
-        if ($deleted) {
-            $RT::Logger->debug("Deleted an obsolete subscription: $msg");
-        }
-        else {
-            $RT::Logger->warning("Unable to delete an obsolete subscription: $msg");
-        }
-    }
-    else {
-        $RT::Logger->warning("Unable to notify ".$args{CurrentUser}->Name." of an obsolete subscription");
-    }
-}
-
-sub EmailDashboard {
-    my $self = shift;
-    my %args = (
-        CurrentUser  => undef,
-        Email        => undef,
-        Dashboard    => undef,
-        Subscription => undef,
-        Content      => undef,
-        Attachments  => undef,
-        @_,
-    );
-
-    my $subscription = $args{Subscription};
-    my $dashboard    = $args{Dashboard};
-    my $currentuser  = $args{CurrentUser};
-    my $email        = $args{Email};
-
-    my $frequency    = $subscription->SubValue('Frequency');
-
-    my %frequency_lookup = (
-        'm-f'     => 'Weekday', # loc
-        'daily'   => 'Daily',   # loc
-        'weekly'  => 'Weekly',  # loc
-        'monthly' => 'Monthly', # loc
-        'never'   => 'Never',   # loc
-    );
-
-    my $frequency_display = $frequency_lookup{$frequency}
-                         || $frequency;
-
-    my $subject = sprintf '[%s] ' .  RT->Config->Get('DashboardSubject'),
-        RT->Config->Get('rtname'),
-        $currentuser->loc($frequency_display),
-        $dashboard->Name;
-
-    my $entity = $self->BuildEmail(
-        %args,
-        To      => $email,
-        Subject => $subject,
-    );
-
-    $entity->head->replace('X-RT-Dashboard-Id', $dashboard->Id);
-    $entity->head->replace('X-RT-Dashboard-Subscription-Id', $subscription->Id);
-
-    $RT::Logger->debug('Mailing dashboard "'.$dashboard->Name.'" to user '.$currentuser->Name." <$email>");
-
-    my $ok = RT::Interface::Email::SendEmail(
-        %{ RT->Config->Get('Crypt')->{'Dashboards'} || {} },
-        Entity => $entity,
-    );
-
-    if (!$ok) {
-        $RT::Logger->error("Failed to email dashboard to user ".$currentuser->Name." <$email>");
-        return;
-    }
-
-    $RT::Logger->debug("Done sending dashboard to ".$currentuser->Name." <$email>");
-}
-
 sub BuildEmail {
     my $self = shift;
     my %args = (
@@ -501,182 +258,5 @@ sub BuildEmail {
     return $entity;
 }
 
-{
-    my $mason;
-    my $outbuf = '';
-    my $data_dir = '';
-
-    sub _mason {
-        unless ($mason) {
-            $RT::Logger->debug("Creating Mason object.");
-
-            # user may not have permissions on the data directory, so create a
-            # new one
-            $data_dir = tempdir(CLEANUP => 1);
-
-            $mason = HTML::Mason::Interp->new(
-                RT::Interface::Web::Handler->DefaultHandlerArgs,
-                out_method => \$outbuf,
-                autohandler_name => '', # disable forced login and more
-                data_dir => $data_dir,
-            );
-            $mason->set_escape( h => \&RT::Interface::Web::EscapeHTML );
-            $mason->set_escape( u => \&RT::Interface::Web::EscapeURI  );
-            $mason->set_escape( j => \&RT::Interface::Web::EscapeJS   );
-        }
-        return $mason;
-    }
-
-    sub RunComponent {
-        _mason->exec(@_);
-        my $ret = $outbuf;
-        $outbuf = '';
-        return $ret;
-    }
-}
-
-{
-    my $scrubber;
-
-    sub _scrubber {
-        unless ($scrubber) {
-            $scrubber = HTML::Scrubber->new;
-            # Allow everything by default, except JS attributes ...
-            $scrubber->default(
-                1 => {
-                    '*' => 1,
-                    map { ("on$_" => 0) }
-                         qw(blur change click dblclick error focus keydown keypress keyup load
-                            mousedown mousemove mouseout mouseover mouseup reset select submit unload)
-                }
-            );
-            # ... and <script>s
-            $scrubber->deny('script');
-        }
-        return $scrubber;
-    }
-
-    sub ScrubContent {
-        my $content = shift;
-        return _scrubber->scrub($content);
-    }
-}
-
-{
-    my %cache;
-
-    sub HourDowDomIn {
-        my $now = shift;
-        my $tz  = shift;
-
-        my $key = "$now $tz";
-        return @{$cache{$key}} if exists $cache{$key};
-
-        my ($hour, $dow, $dom);
-
-        {
-            local $ENV{'TZ'} = $tz;
-            ## Using POSIX::tzset fixes a bug where the TZ environment variable
-            ## is cached.
-            tzset();
-            (undef, undef, $hour, $dom, undef, undef, $dow) = localtime($now);
-        }
-        tzset(); # return back previous value
-
-        $hour = "0$hour"
-            if length($hour) == 1;
-        $dow = (qw/Sunday Monday Tuesday Wednesday Thursday Friday Saturday/)[$dow];
-
-        return @{$cache{$key}} = ($hour, $dow, $dom);
-    }
-}
-
-sub GetResource {
-    my $uri = URI->new(shift);
-    my ($content, $content_type, $filename, $mimetype, $encoding);
-
-    # Avoid trying to inline any remote URIs.  We absolutified all URIs
-    # using WebURL in SendDashboard() above, so choose the simpler match on
-    # that rather than testing a bunch of URI accessors.
-    my $WebURL = RT->Config->Get("WebURL");
-    return unless $uri =~ /^\Q$WebURL/;
-
-    $RT::Logger->debug("Getting resource $uri");
-
-    # strip out the equivalent of WebURL, so we start at the correct /
-    my $path = $uri->path;
-    my $webpath = RT->Config->Get('WebPath');
-    $path =~ s/^\Q$webpath//;
-
-    # add a leading / if needed
-    $path = "/$path"
-        unless $path =~ m{^/};
-
-    # Try the static handler first for non-Mason CSS, JS, etc.
-    my $res = RT::Interface::Web::Handler->GetStatic($path);
-    if ($res->is_success) {
-        RT->Logger->debug("Fetched '$path' from the static handler");
-        $content      = $res->decoded_content;
-        $content_type = $res->headers->content_type;
-    } else {
-        # Try it through Mason instead...
-        $HTML::Mason::Commands::r->path_info($path);
-
-        # grab the query arguments
-        my %args = map { $_ => [ map {Encode::decode("UTF-8",$_)}
-                                     $uri->query_param($_) ] } $uri->query_param;
-        # Convert empty and single element arrayrefs to a non-ref scalar
-        @$_ < 2 and $_ = $_->[0]
-            for values %args;
-
-        $RT::Logger->debug("Running component '$path'");
-        $content = RunComponent($path, %args);
-
-        $content_type = $HTML::Mason::Commands::r->content_type;
-    }
-
-    # guess at the filename from the component name
-    $filename = $1 if $path =~ m{^.*/(.*?)$};
-
-    # the rest of this was taken from Email::MIME::CreateHTML::Resolver::LWP
-    ($mimetype, $encoding) = MIME::Types::by_suffix($filename);
-
-    if ($content_type) {
-        $mimetype = $content_type;
-
-        # strip down to just a MIME type
-        $mimetype = $1 if $mimetype =~ /(\S+);\s*charset=(.*)$/;
-    }
-
-    #If all else fails then some conservative and general-purpose defaults are:
-    $mimetype ||= 'application/octet-stream';
-    $encoding ||= 'base64';
-
-    $RT::Logger->debug("Resource $uri: length=".length($content)." filename='$filename' mimetype='$mimetype', encoding='$encoding'");
-
-    return ($content, $filename, $mimetype, $encoding);
-}
-
-
-{
-    package RT::Dashboard::FakeRequest;
-    sub new { bless {}, shift }
-    sub header_out { return undef }
-    sub headers_out { wantarray ? () : {} }
-    sub err_headers_out { wantarray ? () : {} }
-    sub content_type {
-        my $self = shift;
-        $self->{content_type} = shift if @_;
-        return $self->{content_type};
-    }
-    sub path_info {
-        my $self = shift;
-        $self->{path_info} = shift if @_;
-        return $self->{path_info};
-    }
-}
-
-RT::Base->_ImportOverlays();
-
 1;
 

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


More information about the Bps-public-commit mailing list