]> git.ipfire.org Git - thirdparty/bugzilla.git/commitdiff
Bug 1500441 - Make site-wide announcement dismissable
authorDylan William Hardison <dylan@hardison.net>
Thu, 10 Jan 2019 15:26:27 +0000 (10:26 -0500)
committerGitHub <noreply@github.com>
Thu, 10 Jan 2019 15:26:27 +0000 (10:26 -0500)
Bugzilla/App.pm
Bugzilla/App/CGI.pm
Bugzilla/Template.pm
js/global.js
skins/standard/global.css
t/mojo-example.t
template/en/default/global/header.html.tmpl

index 1af429f2fda087f69d2fbdcd690fdd5ffeaa8bca..b8c6e6ae5e78dd608667d8f62212788827e88a89 100644 (file)
@@ -12,6 +12,7 @@ use Mojo::Base 'Mojolicious';
 use CGI::Compile;
 use utf8;
 use Encode;
+use FileHandle;    # this is for compat back to 5.10
 
 use Bugzilla          ();
 use Bugzilla::BugMail ();
@@ -52,6 +53,8 @@ sub startup {
 
   push @{ $self->commands->namespaces }, 'Bugzilla::App::Command';
 
+  $self->sessions->cookie_name('bugzilla');
+
   $self->hook(
     before_routes => sub {
       my ($c) = @_;
@@ -134,12 +137,9 @@ sub setup_routes {
   $r->any('/')->to('CGI#index_cgi');
   $r->any('/bug/<id:num>')->to('CGI#show_bug_cgi');
   $r->any('/<id:num>')->to('CGI#show_bug_cgi');
-  $r->get(
-    '/testagent.cgi' => sub {
-      my $c = shift;
-      $c->render(text => "OK Mojolicious");
-    }
-  );
+  $r->get('/testagent.cgi')->to('CGI#testagent');
+  $r->add_type('hex32' => qr/[[:xdigit:]]{32}/);
+  $r->post('/announcement/hide/<checksum:hex32>')->to('CGI#announcement_hide');
 
   $r->any('/rest')->to('CGI#rest_cgi');
   $r->any('/rest.cgi/*PATH_INFO')->to('CGI#rest_cgi' => {PATH_INFO => ''});
index 911b2ea06ac8b1e0c820688e6b465a51ed80475e..53a4ba96b8b9e3967449d1e085f9d46de08aac74 100644 (file)
@@ -23,6 +23,20 @@ use Bugzilla::Constants qw(bz_locations USAGE_MODE_BROWSER);
 our $C;
 my %SEEN;
 
+sub testagent {
+  my ($self) = @_;
+  $self->render(text => "OK Mojolicious");
+}
+
+sub announcement_hide {
+  my ($self) = @_;
+  my $checksum = $self->param('checksum');
+  if ($checksum && $checksum =~ /^[[:xdigit:]]{32}$/) {
+    $self->session->{announcement_checksum} = $checksum;
+  }
+  $self->render(json => {});
+}
+
 sub setup_routes {
   my ($class, $r) = @_;
 
@@ -79,7 +93,6 @@ sub load_one {
   return 1;
 }
 
-
 sub _ENV {
   my ($c, $script_name) = @_;
   my $tx      = $c->tx;
index 23ef28a4d1c271f0e5fb8ddfc4c2b98e2e848a42..1ff8e6c2d3c962da7267dc0b4b63cda7d2a6aca6 100644 (file)
@@ -539,7 +539,11 @@ $Template::Stash::SCALAR_OPS->{lower} = sub {
 our $is_processing = 0;
 
 sub process {
-  my $self = shift;
+  my ($self, $input, $vars, $output) = @_;
+  $vars //= {};
+  if (($ENV{SERVER_SOFTWARE} // '') eq 'Bugzilla::App::CGI') {
+    $vars->{self} = $vars->{c} = $Bugzilla::App::CGI::C;
+  }
 
   # All of this current_langs stuff allows template_inner to correctly
   # determine what-language Template object it should instantiate.
@@ -549,7 +553,7 @@ sub process {
   local $SIG{__DIE__};
   delete $SIG{__DIE__};
   warn "WARNING: CGI::Carp makes templates slow" if $INC{"CGI/Carp.pm"};
-  my $retval = $self->SUPER::process(@_);
+  my $retval = $self->SUPER::process($input, $vars, $output);
   shift @$current_langs;
   return $retval;
 }
@@ -937,6 +941,10 @@ sub create {
         return decode('UTF-8', encode_json($_[0]), Encode::FB_DEFAULT);
       },
 
+      md5 => sub {
+        return md5_hex($_[0]);
+      },
+
       # Function to create date strings
       'time2str' => \&Date::Format::time2str,
 
index b75b40f130515dc235ffdbaac37e8538fde4f3d3..8e18f310bced8a145722924fe25f97676576f476 100644 (file)
@@ -305,3 +305,26 @@ window.addEventListener('DOMContentLoaded', focus_main_content, { once: true });
 window.addEventListener('load', detect_blocked_gravatars, { once: true });
 window.addEventListener('load', adjust_scroll_onload, { once: true });
 window.addEventListener('hashchange', adjust_scroll_onload);
+
+window.addEventListener('DOMContentLoaded', () => {
+  const announcement = document.getElementById('new_announcement');
+  if (announcement) {
+    const hide_announcement = () => {
+      const checksum = announcement.dataset.checksum;
+      const url = `${BUGZILLA.config.basepath}announcement/hide/${checksum}`;
+      fetch(url, { method: "POST" }).then(
+        response => announcement.style.display = "none"
+      );
+      localStorage.setItem("announcement_checksum", checksum);
+    }
+    announcement.addEventListener('click', hide_announcement);
+    window.addEventListener('visibilitychange', () => {
+      if (!window.hidden) {
+        const hidden_checksum = localStorage.getItem("announcement_checksum");
+        if (hidden_checksum && hidden_checksum == announcement.dataset.checksum) {
+          announcement.style.display = "none";
+        }
+      }
+    });
+  }
+}, { once: true });
index 2ff5529631c3e1db1372489c5bc6a87689fae557..4b5932d5bbd0cc643969702dc7fc07cd122f8ab9 100644 (file)
         background-color: #C00;
     }
 
-    #message-container .announcement {
+    #message-container .new_announcement {
         border-bottom: 1px solid rgba(0, 0, 0, 0.2);
         color: #555;
         background-color: lightyellow;
     }
 
-    #message-container .announcement p.warning {
+    #message-container .new_announcement p.warning {
         color: red;
     }
 
     /* Reset legacy style */
-    #message-container .announcement #message {
+    #message-container .new_announcement #message {
         margin: 0;
         border: 0;
         padding: 0;
index 0fa5a81fc5087244ca9a1f8de9b6325f92c667a1..46ab5fbb488b2e35d2573c2a7ecdd813ad47b3b3 100644 (file)
@@ -26,10 +26,8 @@ use Bugzilla::Test::MockLocalconfig (urlbase => 'http://bmo-web.vm');
 use Bugzilla::Test::MockDB;
 
 # This redirects reads and writes from the config file (data/params)
-use Bugzilla::Test::MockParams (
-  phabricator_enabled => 1,
-  announcehtml        => '<div id="announcement">Mojo::Test is awesome</div>',
-);
+use Bugzilla::Test::MockParams (phabricator_enabled => 1,
+  announcehtml => 'Mojo::Test is awesome',);
 
 # Util provides a few functions more making mock data in the DB.
 use Bugzilla::Test::Util qw(create_user issue_api_key);
@@ -61,7 +59,7 @@ $t->get_ok('/bzapi/configuration')->status_is(200)
 
 # for web requests, you use text_like (or text_is) with CSS selectors.
 $t->get_ok('/')->status_is(200)
-  ->text_like('#announcement' => qr/Mojo::Test is awesome/);
+  ->text_like('#new_announcement div' => qr/Mojo::Test is awesome/);
 
 # Chaining is not magical, you can break up longer lines
 # by calling methods on $t, as below.
index d74ef6f6690afefdbabe30a9ecb573a8add7ca89..cb93effeca199a78fee5bb52988c03606314665a 100644 (file)
       </div>
     </div>
   [% END %]
-  [% IF Param('announcehtml') %]
-    <div class="announcement">
+  [% announcehtml = Param('announcehtml') %]
+  [% checksum     = md5(announcehtml) %]
+  [% IF announcehtml AND c.session.announcement_checksum != checksum %]
+    <div id="new_announcement" class="new_announcement" data-checksum="[% checksum FILTER html %]">
       <div class="inner">
-        [% Param('announcehtml') FILTER none %]
+        [% announcehtml FILTER none %]
       </div>
     </div>
   [% END %]