diff --git a/lib/MusicBrainz/Server/Data/Edit.pm b/lib/MusicBrainz/Server/Data/Edit.pm index 248de648ca2..9548d1c6edc 100644 --- a/lib/MusicBrainz/Server/Data/Edit.pm +++ b/lib/MusicBrainz/Server/Data/Edit.pm @@ -25,6 +25,7 @@ use MusicBrainz::Server::Constants qw( :edit_status :vote $AUTO_EDITOR_FLAG + $BEGINNER_FLAG $UNTRUSTED_FLAG $MINIMUM_RESPONSE_PERIOD $LIMIT_FOR_EDIT_LISTING @@ -779,6 +780,34 @@ sub _do_accept my $status = try { $edit->accept; + + # If this was not an autoedit and the editor is a beginner + # who can now lose the flag, do that + unless ($edit->auto_edit) { + my $editor_id = $edit->editor_id; + my $editor = $self->c->model('Editor')->get_by_id($editor_id); + if ($editor->is_beginner && !$editor->is_newbie) { + # We check if this will be the tenth accepted edit + my $has_nine_edits = + $self->c->sql->select_single_value(<<~"SQL"); + SELECT 1 + FROM edit + WHERE edit.editor = $editor_id + AND edit.autoedit = 0 + AND edit.status = $STATUS_APPLIED + OFFSET 8 + SQL + + if ($has_nine_edits) { + $self->c->sql->do(<<~"SQL"); + UPDATE editor + SET privs = privs - $BEGINNER_FLAG + WHERE id = $editor_id + SQL + } + } + } + return $STATUS_APPLIED; } catch { diff --git a/lib/MusicBrainz/Server/EditQueue.pm b/lib/MusicBrainz/Server/EditQueue.pm index 00ba4331072..8525761ba54 100644 --- a/lib/MusicBrainz/Server/EditQueue.pm +++ b/lib/MusicBrainz/Server/EditQueue.pm @@ -8,7 +8,6 @@ use MusicBrainz::Server::Constants qw( :editor :edit_status :vote - $BEGINNER_FLAG $REQUIRED_VOTES $MINIMUM_RESPONSE_PERIOD $MINIMUM_VOTING_PERIOD @@ -158,28 +157,6 @@ sub _process_open_edit $self->log->debug("Applying edit #$edit_id\n"); unless ($self->dry_run) { $self->c->model('Edit')->accept($edit); - - # If the editor is a beginner who can now lose the flag, do that - my $editor_id = $edit->editor_id; - my $editor = $self->c->model('Editor')->get_by_id($editor_id); - if ($editor->is_beginner && !$editor->is_newbie) { - my $has_ten_edits = $self->c->sql->select_single_value(<<~"SQL"); - SELECT 1 - FROM edit - WHERE edit.editor = $editor_id - AND edit.autoedit = 0 - AND edit.status = $STATUS_APPLIED - OFFSET 9 - SQL - - if ($has_ten_edits) { - $self->c->sql->do(<<~"SQL"); - UPDATE editor - SET privs = privs - $BEGINNER_FLAG - WHERE id = $editor_id - SQL - } - } } } elsif ($status == $STATUS_FAILEDVOTE) { diff --git a/t/lib/t/MusicBrainz/Server/Data/Edit.pm b/t/lib/t/MusicBrainz/Server/Data/Edit.pm index 6ef23ad81cd..9172c6ab30c 100644 --- a/t/lib/t/MusicBrainz/Server/Data/Edit.pm +++ b/t/lib/t/MusicBrainz/Server/Data/Edit.pm @@ -204,6 +204,74 @@ test all => sub { }; }; +test 'Beginner flag is removed when editor fulfils requirements' => sub { + my $test = shift; + MusicBrainz::Server::Test->prepare_raw_test_database($test->c, '+edit'); + + note('We add Editor 10, with 9 accepted edits'); + $test->c->sql->do(<<~"SQL"); + INSERT INTO editor (id, name, password, ha1, email, email_confirm_date, member_since, privs) + VALUES (10, 'Editor', '{CLEARTEXT}pass', 'b5ba49bbd92eb35ddb35b5acd039440d', '', now(), now(), 8192); + + -- Dummy edits to put editor on cusp of losing beginner flag + INSERT INTO edit (id, editor, type, status, expire_time) + SELECT x, 10, 1, 2, now() FROM generate_series(1000, 1008) x; + INSERT INTO edit_data (edit, data) + SELECT x, '{}' FROM generate_series(1000, 1008) x; + SELECT setval('edit_id_seq', (SELECT MAX(id) FROM edit)); + SQL + + my $beginner_editor = $test->c->model('Editor')->get_by_id(10); + ok($beginner_editor->is_beginner, 'Editor 10 is a beginner'); + + my $edit_counts = $test->c->model('Editor')->various_edit_counts(10); + my $old_ae_count = $edit_counts->{accepted_auto_count}; + my $old_e_count = $edit_counts->{accepted_count}; + + use Data::Dumper; + print Dumper($old_ae_count); + print Dumper($old_e_count); + + note('We enter an autoedit for the editor'); + $test->c->model('Edit')->create( + edit_type => 123, + editor_id => 10, + privileges => 1, + ); + + $beginner_editor = $test->c->model('Editor')->get_by_id(10); + ok($beginner_editor->is_beginner, 'Editor 10 still is a beginner'); + + note('We enter an open edit for the editor'); + my $edit = $test->c->model('Edit')->create( + edit_type => 123, + editor_id => 10, + privileges => $UNTRUSTED_FLAG, + ); + + is($edit->status, $STATUS_OPEN, 'The edit is open'); + + $beginner_editor = $test->c->model('Editor')->get_by_id(10); + ok($beginner_editor->is_beginner, 'Editor 10 still is a beginner'); + + note('We approve the edit'); + my $approver = $test->c->model('Editor')->get_by_id(1); + $test->c->model('Edit')->approve($edit, $approver); + + $edit = $test->c->model('Edit')->get_by_id($edit->id); + is($edit->status, $STATUS_APPLIED, 'The edit is now applied'); + + $edit_counts = $test->c->model('Editor')->various_edit_counts(10); + $old_ae_count = $edit_counts->{accepted_auto_count}; + $old_e_count = $edit_counts->{accepted_count}; + + print Dumper($old_ae_count); + print Dumper($old_e_count); + + $beginner_editor = $test->c->model('Editor')->get_by_id(10); + ok(!$beginner_editor->is_beginner, 'Editor 10 is no longer a beginner'); +}; + test 'Collections' => sub { my $test = shift;