File Coverage

blib/lib/WebService/Mattermost/V4/API/Resource/Team.pm
Criterion Covered Total %
statement 12 22 54.5
branch 0 2 0.0
condition n/a
subroutine 4 6 66.6
pod 1 1 100.0
total 17 31 54.8


line stmt bran cond sub pod time code
1             package WebService::Mattermost::V4::API::Resource::Team;
2:

3: # ABSTRACT: Wrapped API methods for the team API endpoints. 4:
5: use Moo;
6: use Types::Standard 'InstanceOf';
7:
8: use WebService::Mattermost::V4::API::Resource::Team::Channels;
9: use WebService::Mattermost::Helper::Alias 'v4';
10:
11: extends 'WebService::Mattermost::V4::API::Resource';
12: with qw(
13: WebService::Mattermost::V4::API::Resource::Role::Single
14: WebService::Mattermost::V4::API::Resource::Role::View::Team
15: );
16:
17: ################################################################################
18:
19: has channels => (is => 'lazy', isa => InstanceOf[v4 'Team::Channels']);
20:
21: ################################################################################
22:
23: around [ qw(
24: delete
25: get
26: patch
27: update
28:
29: add_member
30: add_members
31: members
32: members_by_ids
33: invite_by_emails
34:
35: stats
36:
37: get_icon
38: set_icon
39: remove_icon
40:
41: set_scheme
42:
43: search_posts
44: ) ] => sub {
45: my $orig = shift;
46: my $self = shift;
47: my $id = shift || $self->id;
48:
49: return $self->validate_id($orig, $id, @_);
50: };
51:
52: around [ qw(get_by_name exists_by_name) ] => sub {
53: my $orig = shift;
54: my $self = shift;
55: my $name = shift;
56:
57: unless ($name) {
58: return $self->error_return('The first parameter must be a name.');
59: }
60:
61: return $self->$orig($name, @_);
62: };
63:
64: around [ qw(get_member remove_member) ] => sub {
65: my $orig = shift;
66: my $self = shift;
67: my $team_id = shift;
68: my $user_id = shift;
69:
70: unless ($team_id && $user_id) {
71: return $self->error_return('The first parameter should be a team ID and the second a user ID.');
72: }
73:
74: return $self->$orig($team_id, $user_id, @_);
75: };
76:
77: sub get {
78: my $self = shift;
79: my $id = shift;
80:
81: return $self->_single_view_get({
82: endpoint => '%s',
83: ids => [ $id ],
84: });
85: }
86:
87: sub get_by_name {
88: my $self = shift;
89: my $name = shift;
90:
91: return $self->_single_view_get({
92: endpoint => 'name/%s',
93: ids => [ $name ],
94: });
95: }
96:
97: sub update {
98: my $self = shift;
99: my $id = shift;
100: my $args = shift;
101:
102: return $self->_single_view_put({
103: endpoint => '%s',
104: ids => [ $id ],
105: parameters => $args,
106: required => [ qw(
107: display_name
108: description
109: company_name
110: allowed_domains
111: invite_id
112: allow_open_invite
113: ) ],
114: });
115: }
116:
117: sub delete {
118: my $self = shift;
119: my $id = shift;
120:
121: return $self->_delete({
122: endpoint => '%s',
123: ids => [ $id ],
124: view => 'Status',
125: });
126: }
127:
128: sub patch {
129: my $self = shift;
130: my $id = shift;
131: my $args = shift;
132:
133: return $self->_single_view_put({
134: endpoint => '%s/patch',
135: parameters => $args,
136: ids => [ $id ],
137: });
138: }
139:
140: sub exists_by_name {
141: my $self = shift;
142: my $name = shift;
143:
144: return $self->_get({
145: endpoint => 'name/%s/exists',
146: ids => [ $name ],
147: });
148: }
149:
150: sub members {
151: my $self = shift;
152: my $id = shift;
153:
154: return $self->_get({
155: endpoint => '%s/members',
156: ids => [ $id ],
157: view => 'TeamMember',
158: });
159: }
160:
161: sub members_by_ids {
162: my $self = shift;
163: my $team_id = shift;
164: my $user_ids = shift;
165:
166: return $self->_get({
167: endpoint => '%s/members/ids',
168: ids => [ $team_id ],
169: parameters => $user_ids,
170: view => 'TeamMember',
171: });
172: }
173:
174: sub add_member {
175: my $self = shift;
176: my $team_id = shift;
177: my $user_id = shift;
178:
179: return $self->_single_view_post({
180: endpoint => '%s/members',
181: ids => [ $team_id ],
182: view => 'TeamMember',
183: parameters => {
184: team_id => $team_id,
185: user_id => $user_id,
186: },
187: });
188: }
189:
190: sub add_members {
191: my $self = shift;
192: my $team_id = shift;
193: my $users = shift;
194:
195: return $self->_post({
196: endpoint => '%s/members/batch',
197: ids => [ $team_id ],
198: view => 'TeamMember',
199: parameters => [
200: map {
201: {
202: user_id => $_->{id},
203: team_id => $team_id,
204: roles => $_->{roles},
205: }
206: } grep {
207: defined($_->{id}) && defined($_->{roles})
208: } @{$users}
209: ],
210: });
211: }
212:
213: sub get_member {
214: my $self = shift;
215: my $team_id = shift;
216: my $user_id = shift;
217:
218: unless ($user_id) {
219: return $self->error_return('The second parameter should be a user ID');
220: }
221:
222: return $self->_single_view_get({
223: endpoint => '%s/members/%s',
224: ids => [ $team_id, $user_id ],
225: view => 'TeamMember',
226: });
227: }
228:
229: sub remove_member {
230: my $self = shift;
231: my $team_id = shift;
232: my $user_id = shift;
233:
234: return $self->_single_view_delete({
235: endpoint => '%s/members/%s',
236: ids => [ $team_id, $user_id ],
237: view => 'Status',
238: });
239: }
240:
241: sub stats {
242: my $self = shift;
243: my $id = shift;
244:
245: return $self->_single_view_get({
246: endpoint => '%s/stats',
247: ids => [ $id ],
248: view => 'TeamStats',
249: });
250: }
251:
252: sub get_icon {
253: my $self = shift;
254: my $id = shift;
255:
256: return $self->_single_view_get({
257: endpoint => '%s/image',
258: ids => [ $id ],
259: view => 'Icon',
260: });
261: }
262:
263: sub set_icon {
264: my $self = shift;
265: my $id = shift;
266: my $filename = shift;
267:
268: return $self->_single_view_post({
269: endpoint => '%s/image',
270: ids => [ $id ],
271: override_data_type => 'form',
272: parameters => {
273: image => { file => $filename },
274: },
275: });
276: }
277:
278: sub remove_icon {
279: my $self = shift;
280: my $id = shift;
281:
282: return $self->_single_view_delete({
283: endpoint => '%s/image',
284: ids => [ $id ],
285: view => 'Status',
286: });
287: }
288:
289: sub invite_by_emails {
290: my $self = shift;
291: my $id = shift;
292: my $emails = shift;
293:
294: return $self->_single_view_post({
295: endpoint => '%s/invite/email',
296: ids => [ $id ],
297: parameters => $emails,
298: view => 'Status',
299: });
300: }
301:
302: sub import_from_existing {
303: my $self = shift;
304: my $id = shift;
305: my $args = shift;
306:
307: my $filename = $args->{filename};
308:
309: unless ($args->{file}) {
310: return $self->error_return('A filename argument is required');
311: }
312:
313: $args->{file} = { file => { file => $args->{filename} } };
314:
315: return $self->_single_view_post({
316: endpoint => '%s/import',
317: ids => [ $id ],
318: override_data_type => 'form',
319: parameters => $args,
320: required => [ qw(file filesize importFrom) ],
321: view => 'Results',
322: });
323: }
324:
325: sub set_scheme {
326: my $self = shift;
327: my $id = shift;
328: my $scheme = shift;
329:
330: return $self->_single_view_put({
331: endpoint => '%s/scheme',
332: ids => [ $id ],
333: parameters => { scheme_id => $scheme },
334: required => [ 'scheme_id' ],
335: view => 'Status',
336: });
337: }
338:
339: sub search_posts {
340: my $self = shift;
341: my $id = shift;
342: my $args = shift;
343:
344: $args->{is_or_search} ||= \0;
345:
346: return $self->_single_view_post({
347: endpoint => '%s/posts/search',
348: ids => [ $id ],
349: parameters => $args,
350: required => [ qw(terms is_or_search) ],
351: view => 'Thread',
352: });
353: }
354:
355: ################################################################################
356:
357: sub _build_channels {
358: my $self = shift;
359:
360: return $self->new_related_resource('teams', 'Team::Channels');
361: }
362:
363: ################################################################################
364:
365: 1;
366:
367: __END__
368:
369: =pod
370:
371: =encoding UTF-8
372:
373: =head1 NAME
374:
375: WebService::Mattermost::V4::API::Resource::Team - Wrapped API methods for the team API endpoints.
376:
377: =head1 VERSION
378:
379: version 0.28
380:
381: =head1 DESCRIPTION
382:
383: API methods relating to a single team by ID or name.
384:
385: =head2 USAGE
386:
387: use WebService::Mattermost;
388:
389: my $mm = WebService::Mattermost->new({
390: authenticate => 1,
391: username => 'me@somewhere.com',
392: password => 'hunter2',
393: base_url => 'https://my.mattermost.server.com/api/v4/',
394: });
395:
396: my $resource = $mm->api->team;
397:
398: =head2 METHODS
399:
400: =over 4
401:
402: =item C<get()>
403:
404: L<Get a team|https://api.mattermost.com/#tag/teams%2Fpaths%2F~1teams~1%7Bteam_id%7D%2Fget>
405:
406: my $response = $resource->get('TEAM-ID-HERE');
407:
408: =item C<get_by_name()>
409:
410: L<Get a team by name|https://api.mattermost.com/#tag/teams%2Fpaths%2F~1teams~1name~1%7Bname%7D%2Fget>
411:
412: my $response = $resource->get_by_name('TEAM-NAME-HERE');
413:
414: =item C<update()>
415:
416: L<Update a team|https://api.mattermost.com/#tag/teams%2Fpaths%2F~1teams~1%7Bteam_id%7D%2Fput>
417:
418: my $response = $resource->update('TEAM-ID-HERE', {
419: # Required parameters:
420: display_name => '...',
421: description => '...',
422: company_name => '...',
423: allowed_domains => '...',
424: invite_id => '...',
425: });
426:
427: =item C<delete()>
428:
429: L<Delete a team|https://api.mattermost.com/#tag/teams%2Fpaths%2F~1teams~1%7Bteam_id%7D%2Fdelete>
430:
431: my $response = $resource->delete('TEAM-ID-HERE');
432:
433: =item C<patch()>
434:
435: L<Patch a team|https://api.mattermost.com/#tag/teams%2Fpaths%2F~1teams~1%7Bteam_id%7D~1patch%2Fput>
436:
437: my $response = $resource->patch('TEAM-ID-HERE', {
438: # Optional parameters:
439: display_name => '...',
440: description => '...',
441: company_name => '...',
442: allowed_domains => '...',
443: invite_id => '...',
444: });
445:
446: =item C<exists_by_name()>
447:
448: L<Check if team exists|https://api.mattermost.com/#tag/teams%2Fpaths%2F~1teams~1name~1%7Bname%7D~1exists%2Fget>
449:
450: my $response = $resource->exists_by_name('TEAM-NAME-HERE');
451:
452: =item C<members()>
453:
454: L<Get team members|https://api.mattermost.com/#tag/teams%2Fpaths%2F~1teams~1%7Bteam_id%7D~1members%2Fget>
455:
456: my $response = $resource->members('TEAM-ID-HERE');
457:
458: =item C<members_by_ids()>
459:
460: L<Get team members by IDs|https://api.mattermost.com/#tag/teams%2Fpaths%2F~1teams~1%7Bteam_id%7D~1members~1ids%2Fpost>
461:
462: my $response = $resource->members_by_ids('TEAM-ID-HERE', [ qw(
463: USER-ID-HERE
464: USER-ID-HERE
465: USER-ID-HERE
466: ) ]);
467:
468: =item C<add_member()>
469:
470: L<Add user to team|https://api.mattermost.com/#tag/teams%2Fpaths%2F~1teams~1%7Bteam_id%7D~1members%2Fpost>
471:
472: my $response = $resource->add_member('TEAM-ID-HERE', 'USER-ID-HERE');
473:
474: =item C<add_members()>
475:
476: L<Add multiple users to team|https://api.mattermost.com/#tag/teams%2Fpaths%2F~1teams~1%7Bteam_id%7D~1members~1batch%2Fpost>
477:
478: my $response = $resource->add_members('TEAM-ID-HERE', [
479: { user_id => 'USER-ID-HERE', roles => 'ROLES-HERE' },
480: { user_id => 'USER-ID-HERE', roles => 'ROLES-HERE' },
481: { user_id => 'USER-ID-HERE', roles => 'ROLES-HERE' },
482: ]);
483:
484: =item C<remove_member()>
485:
486: L<Remove user from team|https://api.mattermost.com/#tag/teams%2Fpaths%2F~1teams~1%7Bteam_id%7D~1members~1%7Buser_id%7D%2Fdelete>
487:
488: my $response = $resource->remove_member('TEAM-ID-HERE', 'USER-ID-HERE');
489:
490: =item C<stats()>
491:
492: L<Get a team stats|https://api.mattermost.com/#tag/teams%2Fpaths%2F~1teams~1%7Bteam_id%7D~1stats%2Fget>
493:
494: my $response = $resource->stats('TEAM-ID-HERE');
495:
496: =item C<get_icon()>
497:
498: L<Get the team icon|https://api.mattermost.com/#tag/teams%2Fpaths%2F~1teams~1%7Bteam_id%7D~1image%2Fget>
499:
500: my $response = $resource->get_icon('TEAM-ID-HERE');
501:
502: =item C<set_icon()>
503:
504: L<Sets the team icon|https://api.mattermost.com/#tag/teams%2Fpaths%2F~1teams~1%7Bteam_id%7D~1image%2Fpost>
505:
506: my $response = $resource->set_icon('TEAM-ID-HERE', '/path/to/icon/here.png');
507:
508: =item C<remove_icon()>
509:
510: L<Remove the team icon|https://api.mattermost.com/#tag/teams%2Fpaths%2F~1teams~1%7Bteam_id%7D~1image%2Fdelete>
511:
512: my $response = $resource->remove_icon('TEAM-ID-HERE');
513:
514: =item C<invite_by_emails()>
515:
516: L<Invite users to the team by email|https://api.mattermost.com/#tag/teams%2Fpaths%2F~1teams~1%7Bteam_id%7D~1invite~1email%2Fpost>
517:
518: my $response = $resource->invite_by_emails('TEAM-ID-HERE', [
519: EMAIL-HERE
520: EMAIL-HERE
521: EMAIL-HERE
522: ]);
523:
524: =item C<import_from_existing()>
525:
526: L<Import a Team from other application|https://api.mattermost.com/#tag/teams%2Fpaths%2F~1teams~1%7Bteam_id%7D~1import%2Fpost>
527:
528: my $response = $resource->import_from_existing('TEAM-ID-HERE', {
529: filename => 'IMPORT-FILENAME',
530: filesize => 'filesize',
531: importFrom => '...',
532: });
533:
534: =item C<search_posts()>
535:
536: L<Search for team posts|https://api.mattermost.com/#tag/posts%2Fpaths%2F~1teams~1%7Bteam_id%7D~1posts~1search%2Fpost>
537:
538: my $response = $resource->search_posts('TEAM-ID-HERE', {
539: # Required parameters:
540: terms => '...',
541:
542: # Optional parameters
543: is_or_search => \1, # or \0 for false
544: time_zone_offset => 0,
545: include_deleted_channels => \1, # or \0 for false
546: page => 0,
547: per_page => 60,
548: });
549:
550: =back
551:
552: =head1 AUTHOR
553:
554: Mike Jones <mike@netsplit.org.uk>
555:
556: =head1 COPYRIGHT AND LICENSE
557:
558: This software is Copyright (c) 2020 by Mike Jones.
559:
560: This is free software, licensed under:
561:
562: The MIT (X11) License
563:
564: =cut
565: