Skip to content

Commit

Permalink
feat: Run user deletion as a background task (openfoodfacts#8516)
Browse files Browse the repository at this point in the history
* Make user deletion a minion job

* Change message

* Change way enqueue is called to avoid issues with tests

* Perltidy fix

* Rever PO file changes
  • Loading branch information
john-gom authored and MonalikaPatnaik committed Jun 8, 2023
1 parent a0a2e9f commit b58c42d
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 10 deletions.
4 changes: 4 additions & 0 deletions lib/ProductOpener/Producers.pm
Original file line number Diff line number Diff line change
Expand Up @@ -2008,4 +2008,8 @@ sub update_export_status_for_csv_file_task ($job, $args_ref) {
return;
}

sub queue_job { ## no critic (Subroutines::RequireArgUnpacking)
return $minion->enqueue(@_);
}

1;
48 changes: 42 additions & 6 deletions lib/ProductOpener/Users.pm
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ use ProductOpener::Text qw/:all/;

use CGI qw/:cgi :form escapeHTML/;
use Encode;
use JSON::PP;

use Email::Valid;
use Crypt::PasswdMD5 qw(unix_md5_crypt);
Expand Down Expand Up @@ -180,10 +181,9 @@ sub check_password_hash ($password, $hash) {

# we use user_init() now and not create_user()

=head2 delete_user ($password, $hash)
=head2 delete_user ($user_ref)
C<delete_user()> This function is used for deleting a user and uses the user_ref as a parameter.
This function removes the user files, the email and re-assigns product edits to openfoodfacts-contributors-[random number]
C<delete_user()> Creates a background job to delete the user
=head3 Arguments
Expand All @@ -192,8 +192,41 @@ Takes in the $user_ref of the user to be deleted
=cut

sub delete_user ($user_ref) {
my $args_ref = {
userid => get_string_id_for_lang("no_language", $user_ref->{userid}),
email => $user_ref->{email},
};

require ProductOpener::Producers;
ProductOpener::Producers::queue_job(delete_user => [$args_ref] => {queue => $server_options{minion_local_queue}});

return;
}

=head2 delete_user_task ($job, $args_ref)
C<delete_user_task()> Background task that deletes a user.
This function removes the user files, the email and re-assigns product edits to openfoodfacts-contributors-[random number]
=head3 Arguments
my $userid = get_string_id_for_lang("no_language", $user_ref->{userid});
Minion job arguments. $args_ref contains the userid and email
=cut

sub delete_user_task ($job, $args_ref) {
return if not defined $job;

my $job_id = $job->{id};

my $log_message = "delete_user_task - job: $job_id started - args: " . encode_json($args_ref) . "\n";
open(my $minion_log, ">>", "$data_root/logs/minion.log");
print $minion_log $log_message;
close($minion_log);

print STDERR $log_message;

my $userid = $args_ref->{userid};
# Suffix is a combination of seconds since epoch plus a 16 bit random number
my $new_userid = "anonymous-" . lc(encode_base32(pack('LS', time(), rand(65536))));

Expand All @@ -204,7 +237,7 @@ sub delete_user ($user_ref) {

# Remove the e-mail
my $emails_ref = retrieve("$data_root/users/users_emails.sto");
my $email = $user_ref->{email};
my $email = $args_ref->{email};

if ((defined $email) and ($email =~ /\@/)) {

Expand All @@ -214,8 +247,11 @@ sub delete_user ($user_ref) {
}
}

# re-assign product edits to openfoodfacts-contributors-[random number]
# re-assign product edits to anonymous-[random number]
find_and_replace_user_id_in_products($userid, $new_userid);

$job->finish("done");

return;
}

Expand Down
4 changes: 2 additions & 2 deletions po/common/common.pot
Original file line number Diff line number Diff line change
Expand Up @@ -5412,8 +5412,8 @@ msgid "Remove all nutrient values"
msgstr "Remove all nutrient values"

msgctxt "delete_user_process"
msgid "User deleted."
msgstr "User deleted."
msgid "User is being deleted. This may take a few minutes."
msgstr "User is being deleted. This may take a few minutes."

msgctxt "attribute_ecoscore_not_applicable_title"
msgid "Eco-Score not yet applicable"
Expand Down
4 changes: 2 additions & 2 deletions po/common/en.po
Original file line number Diff line number Diff line change
Expand Up @@ -5449,8 +5449,8 @@ msgid "Remove all nutrient values"
msgstr "Remove all nutrient values"

msgctxt "delete_user_process"
msgid "User deleted."
msgstr "User deleted."
msgid "User is being deleted. This may take a few minutes."
msgstr "User is being deleted. This may take a few minutes."

msgctxt "org_list_of_gs1_gln_description"
msgid "GS1 data is automatically associated with an OFF organization identifier that corresponds to the GS1 partyName field. To change the OFF organization identifier, you can directly assign 1 or more GS1 GLN identifiers."
Expand Down
2 changes: 2 additions & 0 deletions scripts/minion_producers.pl
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@
app->minion->add_task(
import_products_categories_from_public_database => \&import_products_categories_from_public_database_task);

app->minion->add_task(delete_user => \&ProductOpener::Users::delete_user_task);

app->config(
hypnotoad => {
listen => [$server_options{minion_daemon_server_and_port}],
Expand Down

0 comments on commit b58c42d

Please sign in to comment.