summaryrefslogtreecommitdiffstats
path: root/extensions/ComponentWatching/Extension.pm
blob: d39d9cd552c353f3a996d0e965664a4b609a31f4 (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
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
# -*- Mode: perl; indent-tabs-mode: nil -*-
#
# The contents of this file are subject to the Mozilla Public
# License Version 1.1 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of
# the License at http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
# implied. See the License for the specific language governing
# rights and limitations under the License.
#
# The Original Code is the Component Watching Extension
#
# The Initial Developer of the Original Code is the Mozilla Foundation
# Portions created by the Initial Developers are Copyright (C) 2011 the
# Initial Developer. All Rights Reserved.
#
# Contributor(s):
#   Byron Jones <bjones@mozilla.com>

package Bugzilla::Extension::ComponentWatching;
use strict;
use base qw(Bugzilla::Extension);

use Bugzilla::Constants;
use Bugzilla::Error;
use Bugzilla::Group;
use Bugzilla::User;
use Bugzilla::User::Setting;

our $VERSION = '1.1';

use constant REL_COMPONENT_WATCHER => 15;

#
# installation
#

sub db_schema_abstract_schema {
    my ($self, $args) = @_;
    $args->{'schema'}->{'component_watch'} = {
        FIELDS => [
            user_id => {
                TYPE => 'INT3',
                NOTNULL => 1,
                REFERENCES => {
                    TABLE  => 'profiles',
                    COLUMN => 'userid',
                    DELETE => 'CASCADE',
                }
            },
            component_id => {
                TYPE => 'INT2',
                NOTNULL => 0,
                REFERENCES => {
                    TABLE  => 'components',
                    COLUMN => 'id',
                    DELETE => 'CASCADE',
                }
            },
            product_id => {
                TYPE => 'INT2',
                NOTNULL => 0,
                REFERENCES => {
                    TABLE  => 'products',
                    COLUMN => 'id',
                    DELETE => 'CASCADE',
                }
            },
        ],
    };
}

#
# templates
#

sub template_before_create {
    my ($self, $args) = @_;
    my $config = $args->{config};
    my $constants = $config->{CONSTANTS};
    $constants->{REL_COMPONENT_WATCHER} = REL_COMPONENT_WATCHER;
}

#
# preferences
#

sub user_preferences {
    my ($self, $args) = @_;
    my $tab = $args->{'current_tab'};
    return unless $tab eq 'component_watch';

    my $save = $args->{'save_changes'};
    my $handled = $args->{'handled'};
    my $user = Bugzilla->user;

    if ($save) {
        my ($sth, $sthAdd, $sthDel);

        if (Bugzilla->input_params->{'add'}) {
            # add watch

            my $productName = Bugzilla->input_params->{'add_product'};
            my $ra_componentNames = Bugzilla->input_params->{'add_component'};
            $ra_componentNames = [$ra_componentNames] unless ref($ra_componentNames);

            # load product and verify access
            my $product = Bugzilla::Product->new({ name => $productName });
            unless ($product && $user->can_access_product($product)) {
                ThrowUserError('product_access_denied', { product => $productName });
            }

            if (grep { $_ eq '' } @$ra_componentNames) {
                # watching a product
                _addProductWatch($user, $product);

            } else {
                # watching specific components
                foreach my $componentName (@$ra_componentNames) {
                    my $component = Bugzilla::Component->new({ name => $componentName, product => $product });
                    unless ($component) {
                        ThrowUserError('product_access_denied', { product => $productName });
                    }
                    _addComponentWatch($user, $component);
                }
            }

            _addDefaultSettings($user);

        } else {
            # remove watch(s)

            foreach my $name (keys %{Bugzilla->input_params}) {
                if ($name =~ /^del_(\d+)$/) {
                    _deleteProductWatch($user, $1);
                } elsif ($name =~ /^del_(\d+)_(\d+)$/) {
                    _deleteComponentWatch($user, $1, $2);
                }
            }
        }
    }

    $args->{'vars'}->{'watches'} = _getWatches($user);

    $$handled = 1;
}

#
# bugmail
#

sub bugmail_recipients {
    my ($self, $args) = @_;
    my $bug = $args->{'bug'};
    my $recipients = $args->{'recipients'};
    my $diffs = $args->{'diffs'};

    my ($oldProductId, $newProductId) = ($bug->product_id, $bug->product_id);
    my ($oldComponentId, $newComponentId) = ($bug->component_id, $bug->component_id);

    # notify when the product/component is switch from one being watched
    if (@$diffs) {
        # we need the product to process the component, so scan for that first
        my $product;
        foreach my $ra (@$diffs) {
            next if !(exists $ra->{'old'}
                      && exists $ra->{'field_name'});
            if ($ra->{'field_name'} eq 'product') {
                $product = Bugzilla::Product->new({ name => $ra->{'old'} });
                $oldProductId = $product->id;
            }
        }
        if (!$product) {
            $product = Bugzilla::Product->new($oldProductId);
        }
        foreach my $ra (@$diffs) {
            next if !(exists $ra->{'old'}
                      && exists $ra->{'field_name'});
            if ($ra->{'field_name'} eq 'component') {
                my $component = Bugzilla::Component->new({ name => $ra->{'old'}, product => $product });
                $oldComponentId = $component->id;
            }
        }
    }

    my $dbh = Bugzilla->dbh;
    my $sth = $dbh->prepare("
        SELECT user_id
          FROM component_watch
         WHERE ((product_id = ? OR product_id = ?) AND component_id IS NULL)
               OR (component_id = ? OR component_id = ?)
    ");
    $sth->execute($oldProductId, $newProductId, $oldComponentId, $newComponentId);
    while (my ($uid) = $sth->fetchrow_array) {
        if (!exists $recipients->{$uid}) {
            $recipients->{$uid}->{+REL_COMPONENT_WATCHER} = Bugzilla::BugMail::BIT_WATCHING();
        }
    }
}

sub bugmail_relationships {
    my ($self, $args) = @_;
    my $relationships = $args->{relationships};
    $relationships->{+REL_COMPONENT_WATCHER} = 'Component-Watcher';
}

#
# db
#

sub _getWatches {
    my ($user) = @_;
    my $dbh = Bugzilla->dbh;

    my $sth = $dbh->prepare("
        SELECT product_id, component_id 
          FROM component_watch
         WHERE user_id = ?
    ");
    $sth->execute($user->id);
    my @watches;
    while (my ($productId, $componentId) = $sth->fetchrow_array) {
        my $product = Bugzilla::Product->new($productId);
        next unless $product && $user->can_access_product($product);

        my %watch = ( product => $product );
        if ($componentId) {
            my $component = Bugzilla::Component->new($componentId);
            next unless $component;
            $watch{'component'} = $component;
        }

        push @watches, \%watch;
    }

    @watches = sort { 
        $a->{'product'}->name cmp $b->{'product'}->name
        || $a->{'component'}->name cmp $b->{'component'}->name
    } @watches;

    return \@watches;
}

sub _addProductWatch {
    my ($user, $product) = @_;
    my $dbh = Bugzilla->dbh;

    my $sth = $dbh->prepare("
        SELECT 1 
          FROM component_watch
         WHERE user_id = ? AND product_id = ? AND component_id IS NULL
    ");
    $sth->execute($user->id, $product->id);
    return if $sth->fetchrow_array;

    $sth = $dbh->prepare("
        DELETE FROM component_watch
              WHERE user_id = ? AND product_id = ?
    ");
    $sth->execute($user->id, $product->id);

    $sth = $dbh->prepare("
        INSERT INTO component_watch(user_id, product_id)
             VALUES (?, ?)
    ");
    $sth->execute($user->id, $product->id);
}

sub _addComponentWatch {
    my ($user, $component) = @_;
    my $dbh = Bugzilla->dbh;

    my $sth = $dbh->prepare("
        SELECT 1 
          FROM component_watch
         WHERE user_id = ?
               AND (component_id = ?  OR (product_id = ? AND component_id IS NULL))
    ");
    $sth->execute($user->id, $component->id, $component->product_id);
    return if $sth->fetchrow_array;

    $sth = $dbh->prepare("
        INSERT INTO component_watch(user_id, product_id, component_id)
             VALUES (?, ?, ?)
    ");
    $sth->execute($user->id, $component->product_id, $component->id);
}

sub _deleteProductWatch {
    my ($user, $productId) = @_;
    my $dbh = Bugzilla->dbh;

    my $sth = $dbh->prepare("
        DELETE FROM component_watch
              WHERE user_id = ? AND product_id = ? AND component_id IS NULL
    ");
    $sth->execute($user->id, $productId);
}

sub _deleteComponentWatch {
    my ($user, $productId, $componentId) = @_;
    my $dbh = Bugzilla->dbh;

    my $sth = $dbh->prepare("
        DELETE FROM component_watch
              WHERE user_id = ? AND product_id = ? AND component_id = ?
    ");
    $sth->execute($user->id, $productId, $componentId);
}

sub _addDefaultSettings {
    my ($user) = @_;
    my $dbh = Bugzilla->dbh;

    my $sth = $dbh->prepare("
        SELECT 1 
          FROM email_setting
         WHERE user_id = ? AND relationship = ?
    ");
    $sth->execute($user->id, REL_COMPONENT_WATCHER);
    return if $sth->fetchrow_array;
    
    my @defaultEvents = (
        EVT_OTHER,
        EVT_COMMENT,
        EVT_ATTACHMENT,
        EVT_ATTACHMENT_DATA,
        EVT_PROJ_MANAGEMENT,
        EVT_OPENED_CLOSED,
        EVT_KEYWORD,
        EVT_DEPEND_BLOCK,
        EVT_BUG_CREATED,
    );
    foreach my $event (@defaultEvents) {
        $dbh->do(
            "INSERT INTO email_setting(user_id,relationship,event) VALUES (?,?,?)",
            undef,
            $user->id, REL_COMPONENT_WATCHER, $event
        );
    }
}

__PACKAGE__->NAME;