From: Dylan William Hardison Date: Thu, 10 Jan 2019 15:26:27 +0000 (-0500) Subject: Bug 1500441 - Make site-wide announcement dismissable X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=039deb4da8b2bf769a9708da26c4343ba5c4b79a;p=thirdparty%2Fbugzilla.git Bug 1500441 - Make site-wide announcement dismissable --- diff --git a/Bugzilla/App.pm b/Bugzilla/App.pm index 1af429f2f..b8c6e6ae5 100644 --- a/Bugzilla/App.pm +++ b/Bugzilla/App.pm @@ -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/')->to('CGI#show_bug_cgi'); $r->any('/')->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/')->to('CGI#announcement_hide'); $r->any('/rest')->to('CGI#rest_cgi'); $r->any('/rest.cgi/*PATH_INFO')->to('CGI#rest_cgi' => {PATH_INFO => ''}); diff --git a/Bugzilla/App/CGI.pm b/Bugzilla/App/CGI.pm index 911b2ea06..53a4ba96b 100644 --- a/Bugzilla/App/CGI.pm +++ b/Bugzilla/App/CGI.pm @@ -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; diff --git a/Bugzilla/Template.pm b/Bugzilla/Template.pm index 23ef28a4d..1ff8e6c2d 100644 --- a/Bugzilla/Template.pm +++ b/Bugzilla/Template.pm @@ -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, diff --git a/js/global.js b/js/global.js index b75b40f13..8e18f310b 100644 --- a/js/global.js +++ b/js/global.js @@ -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 }); diff --git a/skins/standard/global.css b/skins/standard/global.css index 2ff552963..4b5932d5b 100644 --- a/skins/standard/global.css +++ b/skins/standard/global.css @@ -112,18 +112,18 @@ 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; diff --git a/t/mojo-example.t b/t/mojo-example.t index 0fa5a81fc..46ab5fbb4 100644 --- a/t/mojo-example.t +++ b/t/mojo-example.t @@ -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 => '
Mojo::Test is awesome
', -); +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. diff --git a/template/en/default/global/header.html.tmpl b/template/en/default/global/header.html.tmpl index d74ef6f66..cb93effec 100644 --- a/template/en/default/global/header.html.tmpl +++ b/template/en/default/global/header.html.tmpl @@ -273,10 +273,12 @@ [% END %] - [% IF Param('announcehtml') %] -
+ [% announcehtml = Param('announcehtml') %] + [% checksum = md5(announcehtml) %] + [% IF announcehtml AND c.session.announcement_checksum != checksum %] +
- [% Param('announcehtml') FILTER none %] + [% announcehtml FILTER none %]
[% END %]