]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Actor/Friends.pm
initial friend (delegates) management code
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Actor / Friends.pm
1 package OpenILS::Application::Actor::Friends;
2 use strict; use warnings;
3 use OpenILS::Application::AppUtils;
4 use OpenILS::Utils::CStoreEditor q/:funcs/;
5 use OpenSRF::Utils::Logger q/$logger/;
6 my $U = "OpenILS::Application::AppUtils";
7
8 # ----------------------------------------------------------------
9 # Shared Friend utilities.  Thar be no methods published here...
10 # ----------------------------------------------------------------
11
12 # export these fields for friend display
13 my @keep_user_fields = qw/usrname first_given_name second_given_name family_name alias/;
14
15 my $out_links_query = {
16     select => {cubi => ['target_user']}, 
17     from => {
18         cub => {
19             cubi => {field => 'bucket', fkey => 'id'}
20         }
21     }, 
22     where => {
23         '+cub' => {btype => 'folks', owner => undef}
24     }
25 };
26
27 my $in_links_query = { 
28     select => {cub =>  ['owner'] }, 
29     from => {
30         cub => {
31             cubi => {field => 'bucket', fkey => 'id'}
32         }
33     }, 
34     where => {
35         '+cubi' => {target_user => undef}, 
36         '+cub' => {btype => 'folks'}
37     }
38 };
39
40
41 sub retrieve_friends {
42     my($self, $e, $user_id) = @_;
43
44     # users I have links to
45     $out_links_query->{where}->{'+cub'}->{owner} = $user_id;
46     my @out_linked = map {$_->{target_user}} @{$e->json_query($out_links_query)};
47
48     # users who link to me
49     $in_links_query->{where}->{'+cubi'}->{target_user} = $user_id;
50     my @in_linked = map {$_->{owner}} @{$e->json_query($in_links_query)};
51
52     my @confirmed;
53     my @pending_out;
54     my @pending_in;
55
56     for my $out_link (@out_linked) {
57         if(grep {$_ == $out_link} @in_linked) {
58             push(@confirmed, $out_link);
59         } else {
60             push(@pending_out, $out_link);
61         }
62     }
63
64     for my $in_link (@in_linked) {
65         push(@pending_in, $in_link)
66             unless grep {$_ == $in_link} @confirmed;
67     }
68
69     my $select = {select => {au => \@keep_user_fields}};
70
71     my $confirmed = (@confirmed) ? 
72         $e->search_actor_user([{id => \@confirmed}, $select]) : [];
73
74     my $pending_out = (@pending_out) ?
75         $e->search_actor_user([{id => \@pending_out}, $select]) : [];
76
77     my $pending_in = (@pending_in) ? 
78         $e->search_actor_user([{id => \@pending_in}, $select]) : [];
79
80     return {
81         confirmed => $confirmed,
82         pending_out => $pending_out,
83         pending_in =>$pending_in
84     };
85 }
86
87 23;