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 use OpenILS::Utils::Fieldmapper;
7 my $U = "OpenILS::Application::AppUtils";
9 # ----------------------------------------------------------------
10 # Shared Friend utilities. Thar be no methods published here...
11 # ----------------------------------------------------------------
13 # export these fields for friend display
14 my @keep_user_fields = qw/usrname first_given_name second_given_name family_name alias/;
16 my $out_links_query = {
17 select => {cubi => ['target_user']},
20 cubi => {field => 'bucket', fkey => 'id'}
24 '+cub' => {btype => 'folks', owner => undef}
28 my $in_links_query = {
29 select => {cub => ['owner'] },
32 cubi => {field => 'bucket', fkey => 'id'}
36 '+cubi' => {target_user => undef},
37 '+cub' => {btype => 'folks'}
42 sub retrieve_friends {
43 my($self, $e, $user_id) = @_;
45 # users I have links to
46 $out_links_query->{where}->{'+cub'}->{owner} = $user_id;
47 my @out_linked = map {$_->{target_user}} @{$e->json_query($out_links_query)};
49 # users who link to me
50 $in_links_query->{where}->{'+cubi'}->{target_user} = $user_id;
51 my @in_linked = map {$_->{owner}} @{$e->json_query($in_links_query)};
57 for my $out_link (@out_linked) {
58 if(grep {$_ == $out_link} @in_linked) {
59 push(@confirmed, $out_link);
61 push(@pending_out, $out_link);
65 for my $in_link (@in_linked) {
66 push(@pending_in, $in_link)
67 unless grep {$_ == $in_link} @confirmed;
70 my $select = {select => {au => \@keep_user_fields}};
72 my $confirmed = (@confirmed) ?
73 $e->search_actor_user([{id => \@confirmed}, $select]) : [];
75 my $pending_out = (@pending_out) ?
76 $e->search_actor_user([{id => \@pending_out}, $select]) : [];
78 my $pending_in = (@pending_in) ?
79 $e->search_actor_user([{id => \@pending_in}, $select]) : [];
82 confirmed => $confirmed,
83 pending_out => $pending_out,
84 pending_in =>$pending_in
88 my $direct_links_query = {
89 select => {cub => ['id'] },
92 cubi => {field => 'bucket', fkey => 'id'}
96 '+cubi' => {target_user => undef},
97 '+cub' => {btype => 'folks', owner => undef}
102 sub confirmed_friends {
103 my($self, $e, $user1_id, $user2_id) = @_;
105 $direct_links_query->{where}->{'+cub'}->{owner} = $user1_id;
106 $direct_links_query->{where}->{'+cubi'}->{target_user} = $user2_id;
108 if($e->json_query($direct_links_query)->[0]) {
110 $direct_links_query->{where}->{'+cub'}->{owner} = $user2_id;
111 $direct_links_query->{where}->{'+cubi'}->{target_user} = $user1_id;
112 return 1 if $e->json_query($direct_links_query)->[0];
119 my $perm_check_query = {
120 select => {cub => ['id'] },
123 cubi => {field => 'bucket', fkey => 'id'}
129 # returns 1 if delegate_user is allowed to perform 'perm' for base_user
130 sub friend_perm_allowed {
131 my($self, $e, $base_user_id, $delegate_user_id, $perm) = @_;
132 return 0 unless $self->confirmed_friends($base_user_id, $delegate_user_id);
133 $perm_check_query->{where} = {
134 '+cubi' => {target_user => $delegate_user_id},
135 '+cub' => {btype => "folks:$perm", owner => $base_user_id}
137 return 1 if $e->json_query($perm_check_query)->[0];
141 sub apply_friend_perm {
142 my($self, $e, $base_user_id, $delegate_user_id, $perm) = @_;
144 my $bucket = $e->search_container_user_bucket(
145 {owner => $base_user_id, btype => "folks:$perm"})->[0];
148 # is the permission already set?
149 return undef if $e->search_container_user_bucket_item(
150 {bucket => $bucket->id, target_user => $delegate_user_id})->[0];
153 # make sure the perm-specific bucket exists for this user
154 $bucket = Fieldmapper::container::user_bucket->new;
155 $bucket->owner($base_user_id);
156 $bucket->btype("folks:$perm");
157 $bucket->name("folks:$perm");
158 $e->create_container_user_bucket($bucket) or return $e->die_event;
161 my $item = Fieldmapper::container::user_bucket_item->new;
162 $item->bucket($bucket->id);
163 $item->target_user($delegate_user_id);
164 $e->create_container_user_bucket_item($item) or return $e->die_event;