summaryrefslogtreecommitdiffstats
path: root/extensions/ComponentWatching/lib/WebService.pm
blob: 331842f7bc49dccff491fa40b159f00035100904 (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
# 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::ComponentWatching::WebService;

use 5.10.1;
use strict;
use warnings;

use base qw(Bugzilla::WebService);

use Bugzilla;
use Bugzilla::Component;
use Bugzilla::Constants;
use Bugzilla::Error;
use Bugzilla::Product;
use Bugzilla::User;

sub rest_resources {
  return [
    qr{^/component-watching$},
    {GET => {method => 'list',}, POST => {method => 'add',},},
    qr{^/component-watching/(\d+)$},
    {
      GET => {
        method => 'get',
        params => sub {
          return {id => $_[0]};
        },
      },
      DELETE => {
        method => 'remove',
        params => sub {
          return {id => $_[0]};
        },
      },
    },
  ];
}

#
# API methods based on Bugzilla::Extension::ComponentWatching->user_preferences
#

sub list {
  my ($self, $params) = @_;
  my $user = Bugzilla->login(LOGIN_REQUIRED);

  return Bugzilla::Extension::ComponentWatching::_getWatches($user);
}

sub add {
  my ($self, $params) = @_;
  my $user = Bugzilla->login(LOGIN_REQUIRED);
  my $result;

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

  my $ra_componentNames = $params->{'component'};
  $ra_componentNames = [$ra_componentNames || ''] unless ref($ra_componentNames);

  if (grep { $_ eq '' } @$ra_componentNames) {

    # watching a product
    $result
      = Bugzilla::Extension::ComponentWatching::_addProductWatch($user, $product);

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

  Bugzilla::Extension::ComponentWatching::_addDefaultSettings($user);

  return $result;
}

sub get {
  my ($self, $params) = @_;
  my $user = Bugzilla->login(LOGIN_REQUIRED);

  return Bugzilla::Extension::ComponentWatching::_getWatches($user,
    $params->{'id'});
}

sub remove {
  my ($self, $params) = @_;
  my $user   = Bugzilla->login(LOGIN_REQUIRED);
  my %result = (status =>
      Bugzilla::Extension::ComponentWatching::_deleteWatch($user, $params->{'id'}));

  return \%result;
}

1;