summaryrefslogtreecommitdiffstats
path: root/Bugzilla/API/1_0/Resource/BugUserLastVisit.pm
blob: 20fdb158f4588d25d555b2787667339a7dff5a1c (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
# 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::API::1_0::Resource::BugUserLastVisit;

use 5.14.0;
use strict;
use warnings;

use Bugzilla::API::1_0::Util;

use Bugzilla::Bug;
use Bugzilla::Error;
use Bugzilla::Constants;

use Moo;

extends 'Bugzilla::API::1_0::Resource';

##############
# Constants  #
##############

use constant READ_ONLY => qw(
    get
);

use constant PUBLIC_METHODS => qw(
    get
    update
);

sub REST_RESOURCES {
    use re '/a';
    return [
        # bug-id
        qr{^/bug_user_last_visit/(\d+)$}, {
            GET => {
                method => 'get',
                params => sub {
                    return { ids => [$_[0]] };
                },
            },
            POST => {
                method => 'update',
                params => sub {
                    return { ids => [$_[0]] };
                },
            },
        },
    ];
}

############
# Methods  #
############

sub update {
    my ($self, $params) = validate(@_, 'ids');
    my $user = Bugzilla->user;
    my $dbh  = Bugzilla->dbh;

    $user->login(LOGIN_REQUIRED);

    my $ids = $params->{ids} // [];
    ThrowCodeError('param_required', { param => 'ids' }) unless @$ids;

    # Cache permissions for bugs. This highly reduces the number of calls to the
    # DB.  visible_bugs() is only able to handle bug IDs, so we have to skip
    # aliases.
    $user->visible_bugs([grep /^[0-9]+$/, @$ids]);

    $dbh->bz_start_transaction();
    my @results;
    my $last_visit_ts = $dbh->selectrow_array('SELECT NOW()');
    foreach my $bug_id (@$ids) {
        my $bug = Bugzilla::Bug->check({ id => $bug_id, cache => 1 });

        ThrowUserError('user_not_involved', { bug_id => $bug->id })
            unless $user->is_involved_in_bug($bug);

        $bug->update_user_last_visit($user, $last_visit_ts);

        push(
            @results,
            _bug_user_last_visit_to_hash(
                $bug->bug_id, $last_visit_ts, $params
            ));
    }
    $dbh->bz_commit_transaction();

    return \@results;
}

sub get {
    my ($self, $params) = validate(@_, 'ids');
    my $user = Bugzilla->user;
    my $ids  = $params->{ids};

    $user->login(LOGIN_REQUIRED);

    my @last_visits;
    if ($ids) {
        # Cache permissions for bugs. This highly reduces the number of calls to
        # the DB.  visible_bugs() is only able to handle bug IDs, so we have to
        # skip aliases.
        $user->visible_bugs([grep /^[0-9]+$/, @$ids]);

        my %last_visit  = map { $_->bug_id => $_->last_visit_ts } @{ $user->last_visited($ids) };
        @last_visits = map { _bug_user_last_visit_to_hash($_, $last_visit{$_}, $params) } @$ids;
    }
    else {
        @last_visits = map {
            _bug_user_last_visit_to_hash($_->bug_id, $_->last_visit_ts, $params)
        } @{ $user->last_visited };
    }

    return \@last_visits;
}

sub _bug_user_last_visit_to_hash {
    my ($bug_id, $last_visit_ts, $params) = @_;

    my %result = (id            => as_int($bug_id),
                  last_visit_ts => as_datetime($last_visit_ts));

    return filter($params, \%result);
}

1;

__END__
=head1 NAME

Bugzilla::API::1_0::Resource::BugUserLastVisit - Find and Store the last time a
user visited a bug.

=head1 METHODS

=head2 update

=over

=item B<Description>

Update the last visit time for the specified bug and current user.

=item B<REST>

To add a single bug id:

    POST /rest/bug_user_last_visit/<bug-id>

Tp add one or more bug ids at once:

    POST /rest/bug_user_last_visit

The returned data format is the same as below.

=item B<Params>

=over

=item C<ids> (array) - One or more bug ids to add.

=back

=item B<Returns>

=over

=item C<array> - An array of hashes containing the following:

=over

=item C<id> - (int) The bug id.

=item C<last_visit_ts> - (string) The timestamp the user last visited the bug.

=back

=back

=back

=head2 get

=over

=item B<Description>

Get the last visited timestamp for one or more specified bug ids.

=item B<REST>

To return the last visited timestamp for a single bug id:

    GET /rest/bug_user_last_visit/<bug-id>

=item B<Params>

=over

=item C<ids> (integer) - One or more optional bug ids to get.

=back

=item B<Returns>

=over

=item C<array> - An array of hashes containing the following:

=over

=item C<id> - (int) The bug id.

=item C<last_visit_ts> - (string) The timestamp the user last visited the bug.

=back

=back

=back

=head1 B<Methods in need of POD>

=over

=item REST_RESOURCES

=back