Gerrit: Fixup gr-message to support anon user (ie a user with no name)

It was showing the user with no name. So users that don't have an
avatar would be invisible. Use a fallback name like we do with the
account dropdown.
See https://gerrit-review.googlesource.com/c/111815/?polygerrit=1

Change-Id: I853d450718e7bd7a9b1555a0c93740a0beea2447
This commit is contained in:
Paladox none
2017-07-03 13:45:27 +00:00
parent 28e17217e7
commit f2bbd7c97d
7 changed files with 101 additions and 21 deletions

View File

@@ -0,0 +1,38 @@
<!--
Copyright (C) 2017 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<script>
(function(window) {
'use strict';
const ANONYMOUS_NAME = 'Anonymous';
/** @polymerBehavior Gerrit.AnonymousNameBehavior */
const AnonymousNameBehavior = {
getAnonymousName(config) {
if (config && config.user &&
config.user.anonymous_coward_name !== 'Anonymous Coward') {
return config.user.anonymous_coward_name;
}
return ANONYMOUS_NAME;
},
};
window.Gerrit = window.Gerrit || {};
window.Gerrit.AnonymousNameBehavior = AnonymousNameBehavior;
})(window);
</script>

View File

@@ -14,6 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
-->
<link rel="import" href="../../../behaviors/gr-anonymous-name-behavior/gr-anonymous-name-behavior.html">
<link rel="import" href="../../../bower_components/polymer/polymer.html">
<link rel="import" href="../../shared/gr-account-link/gr-account-link.html">
<link rel="import" href="../../shared/gr-button/gr-button.html">
@@ -143,7 +144,7 @@ limitations under the License.
<span class="name">[[message.real_author.name]]</span>
on behalf of
</span>
<span class="name">[[author.name]]</span>
<span class="name">[[_authorOrAnon(author)]]</span>
</div>
<template is="dom-if" if="[[message.message]]">
<div class="content">

View File

@@ -86,6 +86,10 @@
},
},
behaviors: [
Gerrit.AnonymousNameBehavior,
],
observers: [
'_updateExpandedClass(message.expanded)',
],
@@ -226,5 +230,15 @@
e.preventDefault();
this.fire('reply', {message: this.message});
},
_authorOrAnon(author) {
if (author && author.name) {
return author.name;
} else if (author && author.email) {
return author.email;
}
return this.getAnonymousName(this.config);
},
});
})();

View File

@@ -211,5 +211,27 @@ limitations under the License.
};
assert.isOk(Polymer.dom(element.root).querySelector('.positiveVote'));
});
test('test for Anonymous Coward user and replace with Anonymous', () => {
element.config = {
user: {
anonymous_coward_name: 'Anonymous Coward',
},
};
element.account = {};
assert.deepEqual(
element._authorOrAnon(element.account), 'Anonymous');
});
test('test for anonymous_coward_name', () => {
element.config = {
user: {
anonymous_coward_name: 'TestAnon',
},
};
element.account = {};
assert.deepEqual(
element._authorOrAnon(element.account, element.config), 'TestAnon');
});
});
</script>

View File

@@ -14,6 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
-->
<link rel="import" href="../../../behaviors/gr-anonymous-name-behavior/gr-anonymous-name-behavior.html">
<link rel="import" href="../../../bower_components/polymer/polymer.html">
<link rel="import" href="../../shared/gr-button/gr-button.html">
<link rel="import" href="../../shared/gr-dropdown/gr-dropdown.html">
@@ -40,7 +41,7 @@ limitations under the License.
items=[[links]]
top-content=[[topContent]]
horizontal-align="right">
<span hidden$="[[_hasAvatars]]" hidden>[[_accountName(account, _anonymousName)]]</span>
<span hidden$="[[_hasAvatars]]" hidden>[[_accountName(account)]]</span>
<gr-avatar account="[[account]]" hidden$="[[!_hasAvatars]]" hidden
image-size="56" aria-label="Account avatar"></gr-avatar>
</gr-dropdown>

View File

@@ -16,24 +16,19 @@
const INTERPOLATE_URL_PATTERN = /\$\{([\w]+)\}/g;
const ANONYMOUS_NAME = 'Anonymous';
Polymer({
is: 'gr-account-dropdown',
properties: {
account: Object,
_anonymousName: {
type: String,
value: ANONYMOUS_NAME,
},
config: Object,
links: {
type: Array,
computed: '_getLinks(_switchAccountUrl, _path)',
},
topContent: {
type: Array,
computed: '_getTopContent(account, _anonymousName)',
computed: '_getTopContent(account)',
},
_path: {
type: String,
@@ -47,21 +42,21 @@
this._handleLocationChange();
this.listen(window, 'location-change', '_handleLocationChange');
this.$.restAPI.getConfig().then(cfg => {
this.config = cfg;
if (cfg && cfg.auth && cfg.auth.switch_account_url) {
this._switchAccountUrl = cfg.auth.switch_account_url;
} else {
this._switchAccountUrl = null;
}
this._hasAvatars = !!(cfg && cfg.plugin && cfg.plugin.has_avatars);
if (cfg && cfg.user &&
cfg.user.anonymous_coward_name &&
cfg.user.anonymous_coward_name !== 'Anonymous Coward') {
this._anonymousName = cfg.user.anonymous_coward_name;
}
});
},
behaviors: [
Gerrit.AnonymousNameBehavior,
],
detached() {
this.unlisten(window, 'location-change', '_handleLocationChange');
},
@@ -77,9 +72,9 @@
return links;
},
_getTopContent(account, _anonymousName) {
_getTopContent(account) {
return [
{text: this._accountName(account, _anonymousName), bold: true},
{text: this._accountName(account), bold: true},
{text: account.email ? account.email : ''},
];
},
@@ -97,13 +92,14 @@
});
},
_accountName(account, _anonymousName) {
_accountName(account) {
if (account && account.name) {
return account.name;
} else if (account && account.email) {
return account.email;
}
return _anonymousName;
return this.getAnonymousName(this.config);
},
});
})();

View File

@@ -55,14 +55,22 @@ limitations under the License.
});
test('test for account without a name but using config', () => {
element._anonymousName = 'WikiGerrit';
element.config = {
user: {
anonymous_coward_name: 'WikiGerrit',
},
};
element.account = {id: '0001'};
assert.deepEqual(element.topContent,
[{text: 'WikiGerrit', bold: true}, {text: ''}]);
});
test('test for account name as an email', () => {
element._anonymousName = 'WikiGerrit';
element.config = {
user: {
anonymous_coward_name: 'WikiGerrit',
},
};
element.account = {email: 'john@doe.com'};
assert.deepEqual(element.topContent,
[{text: 'john@doe.com', bold: true}, {text: 'john@doe.com'}]);