Skip to content

Commit

Permalink
fix: prefer real user userid to app uuid #- an app userid if the app …
Browse files Browse the repository at this point in the history
…did not provide an app uuid #6847 (#6851)
  • Loading branch information
stephanegigandet authored Jun 6, 2022
1 parent b8913a1 commit 367913c
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 12 deletions.
35 changes: 24 additions & 11 deletions lib/ProductOpener/Products.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1513,6 +1513,18 @@ sub compute_completeness_and_missing_tags($$$) {
For a specific change, analyze change identifiers (comment, user agent, userid etc.)
to determine if the change was done through an app, the OFF userid, or an app specific UUID
=head3 Parameters
=head4 $change_ref reference to a change record
=head3 Return value
The function returns by order of preerence:
- a real user userid if we have an userid which is not the userid of an app
- an appid + app uuid (e.g. some-app.Z626FZF4RTFSG6)
- an app userid if the app did not provide an app uuid
- openfoodfacts-contributors
=cut

sub get_change_userid_or_uuid($) {
Expand All @@ -1525,22 +1537,23 @@ sub get_change_userid_or_uuid($) {
my $app_userid_prefix;
my $uuid;

# Is it an app that sent a app_name?
if (defined $change_ref->{app_name}) {
$app = get_string_id_for_lang("no_language", $change_ref->{app_name});
}
# or is the userid specific to an app?
elsif (defined $userid) {
# Is the userid the userid of an app?
if (defined $userid) {
$app = deep_get(\%options, "apps_userids", $userid);
if (defined $app) {
# If the userid is an an account for an app, unset the userid,
# so that it can be replaced by the app + an app uuid if provided
$userid = undef;
}
}

# If the userid is an an account for an app, unset the userid,
# so that it can be replaced by the app + an app uuid if provided
if (defined $app) {
$userid = undef;
# Is it an app that sent an app_name?
if ((not defined $app) and (defined $change_ref->{app_name})) {
$app = get_string_id_for_lang("no_language", $change_ref->{app_name});
}

# Set the app field for the Open Food Facts app
elsif ((defined $options{official_app_comment}) and ($change_ref->{comment} =~ /$options{official_app_comment}/i)) {
if ((not defined $app) and (defined $options{official_app_comment}) and ($change_ref->{comment} =~ /$options{official_app_comment}/i)) {
$app = $options{official_app_id};
}

Expand Down
38 changes: 37 additions & 1 deletion t/products.t
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,44 @@ my @get_change_userid_or_uuid_tests = (
app_name => "Some App",
app_uuid => "423T42fFST423",
expected_app => "some-app",
expected_userid => "some-app.423T42fFST423",
# if someuser is not registered as an app user in Config_off.pm
# we assume that it is the real userid (not the app's userid), and we ignore the app_uuid
expected_userid => "someuser",
},
{
userid => "waistline-app",
comment => "some comment",
app_name => "Waistline",
app_uuid => "423T42fFST423",
# waistline-app is registered as an app user for the app waistline
# so we use the app_uuid provided
expected_app => "waistline",
expected_userid => "waistline.423T42fFST423",
},
{
userid => "waistline-app",
comment => "some comment",
app_name => "Waistline",
# waistline-app is registered as an app user for the app waistline
# it did not provide an app_uuid, so we return the userid of the app
expected_app => "waistline",
expected_userid => "waistline-app",
},
{
# App that does not send any userid, but sends an app uuid
comment => "some comment",
app_name => "Some App",
app_uuid => "423T42fFST423",
expected_app => "some-app",
expected_userid => "some-app.423T42fFST423",
},
{
# App that does not send any userid, and does not send an app uuid
comment => "some comment",
app_name => "Some App",
expected_app => "some-app",
expected_userid => "openfoodfacts-contributors",
},
);

foreach my $change_ref (@get_change_userid_or_uuid_tests) {
Expand Down

0 comments on commit 367913c

Please sign in to comment.