-
-
Notifications
You must be signed in to change notification settings - Fork 278
/
Editor.pm
507 lines (422 loc) · 14.2 KB
/
Editor.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
package MusicBrainz::Server::Data::Editor;
use Moose;
use LWP;
use URI::Escape;
use DateTime;
use MusicBrainz::Server::Entity::Preferences;
use MusicBrainz::Server::Entity::Editor;
use MusicBrainz::Server::Data::Utils qw(
load_subobjects
placeholders
query_to_list
query_to_list_limited
query_to_list
type_to_model
);
use MusicBrainz::Server::Types qw( :edit_status :privileges );
extends 'MusicBrainz::Server::Data::Entity';
with 'MusicBrainz::Server::Data::Role::Subscription' => {
table => 'editor_subscribe_editor',
column => 'subscribed_editor',
class => 'MusicBrainz::Server::Entity::EditorSubscription'
};
sub _table
{
return 'editor';
}
sub _columns
{
return 'editor.id, name, password, privs, email, website, bio,
member_since, email_confirm_date, last_login_date, edits_accepted,
edits_rejected, auto_edits_accepted, edits_failed';
}
sub _column_mapping
{
return {
id => 'id',
name => 'name',
email => 'email',
password => 'password',
privileges => 'privs',
website => 'website',
biography => 'bio',
accepted_edits => 'edits_accepted',
rejected_edits => 'edits_rejected',
failed_edits => 'edits_failed',
accepted_auto_edits => 'auto_edits_accepted',
email_confirmation_date => 'email_confirm_date',
registration_date => 'member_since',
last_login_date => 'last_login_date',
};
}
sub _entity_class
{
return 'MusicBrainz::Server::Entity::Editor';
}
sub get_by_name
{
my ($self, $name) = @_;
my $query = 'SELECT ' . $self->_columns .
' FROM ' . $self->_table .
' WHERE lower(name) = ? LIMIT 1';
my $row = $self->sql->select_single_row_hash($query, lc $name);
return $self->_new_from_row($row);
}
sub find_by_name
{
my ($self, $name, $offset, $limit) = @_;
my $query = 'SELECT ' . $self->_columns .
' FROM ' . $self->_table .
" WHERE musicbrainz_unaccent(lower(name)) LIKE musicbrainz_unaccent(lower(?)) || '%'
OFFSET ?";
return query_to_list_limited(
$self->c->sql, $offset, $limit, sub { $self->_new_from_row(@_) },
$query, $name, $offset
);
}
sub _get_ratings_for_type
{
my ($self, $id, $type, $me) = @_;
my $query = "
SELECT $type AS id, rating FROM ${type}_rating_raw
WHERE editor = ? ORDER BY rating DESC, editor";
my $results = $self->c->sql->select_list_of_hashes ($query, $id);
my $entities = $self->c->model(type_to_model($type))->get_by_ids(map { $_->{id} } @$results);
my $ratings = [];
for my $row (@$results) {
if ($me) {
$entities->{$row->{id}}->user_rating($row->{rating});
}
else {
$entities->{$row->{id}}->rating($row->{rating});
$entities->{$row->{id}}->rating_count(1);
}
push @$ratings, {
$type => $entities->{$row->{id}},
rating => $row->{rating},
}
}
# Sort by rating DESC, name ASC
# Sadly we can't do this in SQL, as the name is in a different schema
return [ sort {
($b->{rating} <=> $a->{rating})
or
($a->{$type}->name cmp $b->{$type}->name)
} @$ratings ];
}
sub get_ratings
{
my ($self, $user, $me) = @_;
my $ratings = {};
foreach my $entity ('artist', 'label', 'recording', 'release_group', 'work')
{
my $data = $self->_get_ratings_for_type ($user->id, $entity, $me);
$ratings->{$entity} = $data if @$data;
}
return $ratings;
}
sub _get_tags_for_type
{
my ($self, $id, $type) = @_;
my $query = "SELECT tag, count(tag)
FROM ${type}_tag_raw
WHERE editor = ?
GROUP BY tag";
my $results = $self->c->sql->select_list_of_hashes ($query, $id);
return { map { $_->{tag} => $_ } @$results };
}
sub get_tags
{
my ($self, $user) = @_;
my $tags = {};
my $max = 0;
foreach my $entity ('artist', 'label', 'recording', 'release', 'release_group', 'work')
{
my $data = $self->_get_tags_for_type ($user->id, $entity);
foreach (keys %$data)
{
if ($tags->{$_})
{
$tags->{$_}->{count} += $data->{$_}->{count};
}
else
{
$tags->{$_} = $data->{$_};
}
$max = $tags->{$_}->{count} if $tags->{$_}->{count} > $max;
}
}
my $entities = $self->c->model('Tag')->get_by_ids(keys %$tags);
foreach (keys %$entities)
{
$tags->{$_}->{tag} = $entities->{$_};
}
my @tags = sort { $a->{tag}->name cmp $b->{tag}->name } values %$tags;
return { max => $max, tags => \@tags };
}
sub find_by_email
{
my ($self, $email) = @_;
return values %{$self->_get_by_keys('email', $email)};
}
sub find_by_privileges
{
my ($self, $privs) = @_;
my $query = "SELECT " . $self->_columns . "
FROM " . $self->_table . "
WHERE (privs & ?) > 0
ORDER BY editor.name, editor.id";
return query_to_list (
$self->c->sql, sub { $self->_new_from_row(@_) },
$query, $privs);
}
sub find_by_subscribed_editor
{
my ($self, $editor_id, $limit, $offset) = @_;
my $query = "SELECT " . $self->_columns . "
FROM " . $self->_table . "
JOIN editor_subscribe_editor s ON editor.id = s.subscribed_editor
WHERE s.editor = ?
ORDER BY editor.name, editor.id
OFFSET ?";
return query_to_list_limited(
$self->c->sql, $offset, $limit, sub { $self->_new_from_row(@_) },
$query, $editor_id, $offset || 0);
}
sub find_subscribers
{
my ($self, $editor_id, $limit, $offset) = @_;
my $query = "SELECT " . $self->_columns . "
FROM " . $self->_table . "
JOIN editor_subscribe_editor s ON editor.id = s.editor
WHERE s.subscribed_editor = ?
ORDER BY editor.name, editor.id
OFFSET ?";
return query_to_list_limited(
$self->c->sql, $offset, $limit, sub { $self->_new_from_row(@_) },
$query, $editor_id, $offset || 0);
}
sub insert
{
my ($self, $data) = @_;
return Sql::run_in_transaction(sub {
return $self->_entity_class->new(
id => $self->sql->insert_row('editor', $data, 'id'),
name => $data->{name},
password => $data->{password},
accepted_edits => 0,
rejected_edits => 0,
failed_edits => 0,
accepted_auto_edits => 0,
registration_date => DateTime->now
);
}, $self->sql);
}
sub update_email
{
my ($self, $editor, $email) = @_;
Sql::run_in_transaction(sub {
if ($email) {
my $email_confirmation_date = $self->sql->select_single_value(
'UPDATE editor SET email=?, email_confirm_date=NOW()
WHERE id=? RETURNING email_confirm_date', $email, $editor->id);
$editor->email($email);
$editor->email_confirmation_date($email_confirmation_date);
}
else {
$self->sql->do('UPDATE editor SET email=NULL, email_confirm_date=NULL
WHERE id=?', $editor->id);
delete $editor->{email};
delete $editor->{email_confirmation_date};
}
}, $self->sql);
}
sub update_password
{
my ($self, $editor, $password) = @_;
Sql::run_in_transaction(sub {
$self->sql->do('UPDATE editor SET password=? WHERE id=?',
$password, $editor->id);
}, $self->sql);
}
sub update_profile
{
my ($self, $editor, $website, $bio) = @_;
Sql::run_in_transaction(sub {
$self->sql->do('UPDATE editor SET website=?, bio=? WHERE id=?',
$website || undef, $bio || undef, $editor->id);
}, $self->sql);
}
sub update_privileges
{
my ($self, $editor, $values) = @_;
my $privs = $values->{auto_editor} * $AUTO_EDITOR_FLAG
+ $values->{bot} * $BOT_FLAG
+ $values->{untrusted} * $UNTRUSTED_FLAG
+ $values->{link_editor} * $RELATIONSHIP_EDITOR_FLAG
+ $values->{no_nag} * $DONT_NAG_FLAG
+ $values->{wiki_transcluder} * $WIKI_TRANSCLUSION_FLAG
+ $values->{mbid_submitter} * $MBID_SUBMITTER_FLAG
+ $values->{account_admin} * $ACCOUNT_ADMIN_FLAG;
Sql::run_in_transaction(sub {
$self->sql->do('UPDATE editor SET privs=? WHERE id=?',
$privs, $editor->id);
}, $self->sql);
}
sub make_autoeditor
{
my ($self, $editor_id) = @_;
$self->sql->do('UPDATE editor SET privs = privs | ? WHERE id = ?',
$AUTO_EDITOR_FLAG, $editor_id);
}
sub load
{
my ($self, @objs) = @_;
load_subobjects($self, 'editor', @objs);
}
sub load_preferences
{
my ($self, @editors) = @_;
return unless @editors;
my %editors = map { $_->id => $_ } @editors;
my $query = sprintf "SELECT editor, name, value ".
"FROM editor_preference WHERE editor IN (%s)",
placeholders(keys %editors);
my $prefs = $self->sql->select_list_of_hashes($query, keys %editors);
for my $pref (@$prefs) {
my ($editor_id, $key, $value) = ($pref->{editor}, $pref->{name}, $pref->{value});
next unless $editors{$editor_id}->preferences->can($key);
$editors{$editor_id}->preferences->$key($value);
}
}
sub save_preferences
{
my ($self, $editor, $values) = @_;
Sql::run_in_transaction(sub {
$self->sql->do('DELETE FROM editor_preference WHERE editor = ?', $editor->id);
my $preferences_meta = $editor->preferences->meta;
foreach my $name (keys %$values) {
my $default = $preferences_meta->get_attribute($name)->default;
unless ($default eq $values->{$name}) {
$self->sql->insert_row('editor_preference', {
editor => $editor->id,
name => $name,
value => $values->{$name},
});
}
}
$editor->preferences(MusicBrainz::Server::Entity::Preferences->new(%$values));
}, $self->sql);
}
sub credit
{
my ($self, $editor_id, $status, $as_autoedit) = @_;
my $column;
return if $status == $STATUS_DELETED;
$column = "edits_rejected" if $status == $STATUS_FAILEDVOTE;
$column = "edits_accepted" if $status == $STATUS_APPLIED && !$as_autoedit;
$column = "auto_edits_accepted" if $status == $STATUS_APPLIED && $as_autoedit;
$column ||= "edits_failed";
my $query = "UPDATE editor SET $column = $column + 1 WHERE id = ?";
$self->sql->do($query, $editor_id);
}
sub donation_check
{
my ($self, $obj) = @_;
my $nag = 1;
$nag = 0 if ($obj->is_nag_free || $obj->is_auto_editor || $obj->is_bot ||
$obj->is_relationship_editor || $obj->is_wiki_transcluder);
my $days = 0.0;
if ($nag)
{
my $ua = LWP::UserAgent->new;
$ua->agent("MusicBrainz server");
$ua->timeout(5); # in seconds.
my $response = $ua->request(HTTP::Request->new (GET =>
'http://metabrainz.org/cgi-bin/nagcheck_days?moderator='.
uri_escape_utf8($obj->name)));
if ($response->is_success && $response->content =~ /\s*([-01]+),([-0-9.]+)\s*/)
{
$nag = $1;
$days = $2;
}
else
{
return undef;
}
}
return { nag => $nag, days => $days };
}
sub editors_with_subscriptions
{
my $self = shift;
my @tables = qw(
editor_subscribe_artist
editor_subscribe_editor
editor_subscribe_label
);
my $ids = join(' UNION ALL ', map { "SELECT editor FROM $_" } @tables);
my $query = 'SELECT ' . $self->_columns . ' FROM ' . $self->_table .
" WHERE id IN ($ids)";
return query_to_list (
$self->c->sql, sub { $self->_new_from_row(@_) },
$query);
}
sub delete {
my ($self, $editor_id) = @_;
die "Invalid editor_id: $editor_id" unless $editor_id > 0;
$self->sql->begin;
$self->sql->do(
"UPDATE editor SET name = 'Deleted Editor #' || id,
password = '',
privs = 0,
email = NULL,
website = NULL,
bio = NULL
WHERE id = ?",
$editor_id
);
$self->sql->do("DELETE FROM editor_preference WHERE editor = ?", $editor_id);
$self->c->model('EditorSubscriptions')->delete_editor($editor_id);
$self->c->model('Collection')->delete_editor($editor_id);
$self->c->model('WatchArtist')->delete_editor($editor_id);
$self->c->model($_)->tags->clear($editor_id)
for qw( Artist
Label
Recording
Release
ReleaseGroup
Work
);
$self->c->model($_)->rating->clear($editor_id)
for qw( Artist
Label
Recording
ReleaseGroup
Work
);
$self->sql->commit;
}
no Moose;
__PACKAGE__->meta->make_immutable;
1;
=head1 NAME
MusicBrainz::Server::Data::Editor - database level loading support for
Editors
=head1 DESCRIPTION
Provides support for fetching editors from the database
=head1 COPYRIGHT
Copyright (C) 2009 Oliver Charles
Copyright (C) 2010 MetaBrainz Foundation
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
=cut