Commit 3e6f1bd6 authored by IKEDA Soji's avatar IKEDA Soji
Browse files

Refactoring.

  - Add Sympa::DataSource::is_external().
  - use Sympa::List::get_exclusion(). As a side work
    Sympa::List::is_member_excluded() was deprecated.
parent 0b192644
......@@ -4,8 +4,8 @@
# Sympa - SYsteme de Multi-Postage Automatique
#
# Copyright 2019 The Sympa Community. See the AUTHORS.md file at
# the top-level directory of this distribution and at
# Copyright 2019, 2021 The Sympa Community. See the
# AUTHORS.md file at the top-level directory of this distribution and at
# <https://github.com/sympa-community/sympa.git>.
#
# This program is free software; you can redistribute it and/or modify
......@@ -98,12 +98,17 @@ sub new {
$options{name} = substr $options{name}, 0, 50
if $options{name} and 50 < length $options{name};
return $type->_new(
my $self = $type->_new(
%options,
_role => $role,
_defkeys => [@defkeys],
_defvals => [@defvals],
);
$self->{_external} = not($self->isa('Sympa::DataSource::List')
and [split /\@/, $self->{listname}, 2]->[1] eq $list->{'domain'})
if ref $list eq 'Sympa::List';
$self;
}
sub _new {
......@@ -270,6 +275,10 @@ sub is_allowed_to_sync {
return 1;
}
sub is_external {
shift->{_external};
}
1;
__END__
......@@ -360,6 +369,26 @@ A new instance, or C<undef> on failure.
I<Instance method>.
Closes backend and does cleanup.
=item is_external ( )
I<Instance method>.
Returns true value if the data source is external data source.
"External" means that it is not C<include_sympa_list> (the instance of
L<Sympa::DataSource::List>) or not including any lists on local domain.
Known bug:
=over
=item *
If a data source is a list included from the other external data source(s),
this method will treat it as non-external so that some requests not allowed
for external data sources, such as C<move_user> request, on corresponding
users may be allowed.
=back
=item next ( )
I<Instance method>.
......
......@@ -1989,55 +1989,8 @@ sub get_exclusion {
return $data_exclu;
}
sub is_member_excluded {
my $self = shift;
my $email = shift;
return undef unless defined $email and length $email;
$email = Sympa::Tools::Text::canonic_email($email);
my $sdm = Sympa::DatabaseManager->instance;
my $sth;
if (defined $self->{'admin'}{'family_name'}
and length $self->{'admin'}{'family_name'}) {
unless (
$sdm
and $sth = $sdm->do_prepared_query(
q{SELECT COUNT(*)
FROM exclusion_table
WHERE (list_exclusion = ? OR family_exclusion = ?) AND
robot_exclusion = ? AND
user_exclusion = ?},
$self->{'name'}, $self->{'admin'}{'family_name'},
$self->{'domain'},
$email
)
) {
#FIXME: report error
return undef;
}
} else {
unless (
$sdm
and $sth = $sdm->do_prepared_query(
q{SELECT COUNT(*)
FROM exclusion_table
WHERE list_exclusion = ? AND robot_exclusion = ? AND
user_exclusion = ?},
$self->{'name'}, $self->{'domain'},
$email
)
) {
#FIXME: report error
return undef;
}
}
my ($count) = $sth->fetchrow_array;
$sth->finish;
return $count || 0;
}
# DEPRECATED. No longer used.
#sub is_member_excluded;
# Mapping between var and field names.
sub _map_list_member_cols {
......@@ -6605,7 +6558,7 @@ Returns true if the indicated user is member of the list.
=item is_member_excluded ( $email )
I<Instance method>.
FIXME @todo doc
B<Deprecated>.
=item is_moderated ()
......
......@@ -378,9 +378,6 @@ sub _update_users {
? ('subscriber', '')
: ('admin', sprintf ' AND role_admin = %s', $sdm->quote($role));
my $is_external_ds = not (ref $ds eq 'Sympa::DataSource::List'
and [ split /\@/, $ds->{listname}, 2 ]->[1] eq $list->{'domain'});
my %result = (added => 0, deleted => 0, updated => 0, kept => 0);
my $time = time;
......@@ -402,17 +399,9 @@ sub _update_users {
$users{$user_email} = [ $user_inclusion, $user_inclusion_ext ];
}
my %exclusion_list;
my $exclusion_list_query = $sdm->do_prepared_query(q{SELECT user_exclusion
FROM exclusion_table
WHERE list_exclusion = ? AND
robot_exclusion = ?},
$list->{'name'},
$list->{'domain'});
while (my @row = $exclusion_list_query->fetchrow_array) {
$exclusion_list{$row[0]} = 1;
}
my %exclusion_list = map { (Sympa::Tools::Text::canonic_email($_) => 1) }
@{$list->get_exclusion->{emails}}
if $role eq 'member';
my %to_be_inserted;
......@@ -432,7 +421,7 @@ sub _update_users {
if (exists $users{$email}) {
# 2. Keep
if ($is_external_ds) {
if ($ds->is_external) {
if ($users{$email}[0] ne "" && $start_time <= int($users{$email}[0])
&& $users{$email}[1] ne "" && $start_time <= int($users{$email}[1])) {
$result{'kept'}++;
......@@ -448,7 +437,7 @@ sub _update_users {
}
# 3. Update
my %res = __update_user($ds, $email, $start_time, $list, $t, $r, $is_external_ds, $time);
my %res = __update_user($ds, $email, $start_time, $list, $t, $r, $time);
unless (%res) {
$ds->close;
......@@ -481,7 +470,7 @@ sub _update_users {
date => $time,
update_date => $time,
inclusion => $time,
($is_external_ds ? (inclusion_ext => $time) : ()),
($ds->is_external ? (inclusion_ext => $time) : ()),
inclusion_label => $ds->name,
};
my @defkeys = @{$ds->{_defkeys} || []};
......@@ -527,7 +516,6 @@ sub __update_user {
my $list = shift;
my $t = shift;
my $r = shift;
my $is_external_ds = shift;
my $time = shift;
my $sdm = Sympa::DatabaseManager->instance;
......@@ -537,7 +525,7 @@ sub __update_user {
# 3. If user (has not been updated by the other data sources and) exists:
# UPDATE inclusion.
if ($is_external_ds) {
if ($ds->is_external) {
# Already updated by the other non-external data source but not yet
# by any other external ones:
# Update inclusion_ext (and inclusion) field, but not inclusion_label.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment