summaryrefslogtreecommitdiffstats
path: root/extensions/BugmailFilter/lib/Filter.pm
blob: 7f2f4cb87f72bef5dc00d0cbbba4354522aa26f7 (plain)
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
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# This Source Code Form is "Incompatible With Secondary Licenses", as
# defined by the Mozilla Public License, v. 2.0.

package Bugzilla::Extension::BugmailFilter::Filter;

use base qw(Bugzilla::Object);

use 5.10.1;
use strict;
use warnings;

use Bugzilla::Component;
use Bugzilla::Error;
use Bugzilla::Extension::BugmailFilter::Constants;
use Bugzilla::Extension::BugmailFilter::FakeField;
use Bugzilla::Field;
use Bugzilla::Product;
use Bugzilla::User;
use Bugzilla::Util qw(trim);

use constant DB_TABLE => 'bugmail_filters';

use constant DB_COLUMNS => qw(
    id
    user_id
    product_id
    component_id
    field_name
    relationship
    changer_id
    action
);

use constant LIST_ORDER => 'id';

use constant UPDATE_COLUMNS => ();

use constant VALIDATORS => {
    user_id     => \&_check_user,
    field_name  => \&_check_field_name,
    action      => \&Bugzilla::Object::check_boolean,
};
use constant VALIDATOR_DEPENDENCIES => {
    component_id    => [ 'product_id' ],
};

use constant AUDIT_CREATES => 0;
use constant AUDIT_UPDATES => 0;
use constant AUDIT_REMOVES => 0;
use constant USE_MEMCACHED => 0;

# getters

sub user {
    my ($self) = @_;
    return Bugzilla::User->new({ id => $self->{user_id}, cache => 1 });
}

sub product {
    my ($self) = @_;
    return $self->{product_id}
        ? Bugzilla::Product->new({ id => $self->{product_id}, cache => 1 })
        : undef;
}

sub product_name {
    my ($self) = @_;
    return $self->{product_name} //= $self->{product_id} ? $self->product->name : '';
}

sub component {
    my ($self) = @_;
    return $self->{component_id}
        ? Bugzilla::Component->new({ id => $self->{component_id}, cache => 1 })
        : undef;
}

sub component_name {
    my ($self) = @_;
    return $self->{component_name} //= $self->{component_id} ? $self->component->name : '';
}

sub field_name {
    return $_[0]->{field_name} //= '';
}

sub field_description {
    my ($self, $value) = @_;
    $self->{field_description} = $value if defined($value);
    return $self->{field_description};
}

sub field {
    my ($self) = @_;
    return unless $self->{field_name};
    if (!$self->{field}) {
        if (substr($self->{field_name}, 0, 1) eq '~') {
            # this should never happen
            die "not implemented";
        }
        foreach my $field (
            @{ Bugzilla::Extension::BugmailFilter::FakeField->fake_fields() },
            @{ Bugzilla::Extension::BugmailFilter::FakeField->tracking_flag_fields() },
        ) {
            if ($field->{name} eq $self->{field_name}) {
                return $self->{field} = $field;
            }
        }
        $self->{field} = Bugzilla::Field->new({ name => $self->{field_name}, cache => 1 });
    }
    return $self->{field};
}

sub relationship {
    return $_[0]->{relationship};
}

sub changer_id {
    return $_[0]->{changer_id};
}

sub changer {
    my ($self) = @_;
    return $self->{changer_id}
        ? Bugzilla::User->new({ id => $self->{changer_id}, cache => 1 })
        : undef;
}

sub relationship_name {
    my ($self) = @_;
    foreach my $rel (@{ FILTER_RELATIONSHIPS() }) {
        return $rel->{name}
            if $rel->{value} == $self->{relationship};
    }
    return '?';
}

sub is_exclude {
    return $_[0]->{action} == 1;
}

sub is_include {
    return $_[0]->{action} == 0;
}

# validators

sub _check_user {
    my ($class, $user) = @_;
    $user || ThrowCodeError('param_required', { param => 'user' });
}

sub _check_field_name {
    my ($class, $field_name) = @_;
    return undef unless $field_name;
    if (substr($field_name, 0, 1) eq '~') {
        $field_name = lc(trim($field_name));
        $field_name =~ /^~[a-z0-9_\.\-]+$/
            || ThrowUserError('bugmail_filter_invalid');
        length($field_name) <= 64
            || ThrowUserError('bugmail_filter_too_long');
        return $field_name;
    }
    foreach my $rh (@{ FAKE_FIELD_NAMES() }) {
        return $field_name if $rh->{name} eq $field_name;
    }
    return $field_name
        if $field_name =~ /^tracking\./;
    Bugzilla::Field->check({ name => $field_name, cache => 1});
    return $field_name;
}

# methods

sub matches {
    my ($self, $args) = @_;

    if (my $field_name = $self->{field_name}) {
        if ($args->{field}->{field_name} && substr($field_name, 0, 1) eq '~') {
            my $substring = quotemeta(substr($field_name, 1));
            if ($args->{field}->{filter_field} !~ /$substring/i) {
                return 0;
            }
        }
        elsif ($field_name eq 'flagtypes.name') {
            if ($args->{field}->{field_name} ne $field_name) {
                return 0;
            }
        }
        elsif ($field_name ne $args->{field}->{filter_field}) {
            return 0;
        }
    }

    if ($self->{product_id} && $self->{product_id} != $args->{product_id}) {
        return 0;
    }

    if ($self->{component_id} && $self->{component_id} != $args->{component_id}) {
        return 0;
    }

    if ($self->{relationship} && !$args->{rel_map}->[$self->{relationship}]) {
        return 0;
    }

    if ($self->{changer_id} && $self->{changer_id} != $args->{changer_id}) {
        return 0;
    }

    return 1;
}

1;