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 @expose_user_fields = qw/id 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'}
41 my $perm_check_query = {
42 select => {cub => ['btype'] },
45 cubi => {field => 'bucket', fkey => 'id'}
51 sub retrieve_friends {
52 my($self, $e, $user_id, $options) = @_;
55 # users I have links to
56 $out_links_query->{where}->{'+cub'}->{owner} = $user_id;
57 my @out_linked = map {$_->{target_user}} @{$e->json_query($out_links_query)};
59 # users who link to me
60 $in_links_query->{where}->{'+cubi'}->{target_user} = $user_id;
61 my @in_linked = map {$_->{owner}} @{$e->json_query($in_links_query)};
63 # determine which users are confirmed, pending outbound
64 # requests, and pending inbound requests
69 for my $out_link (@out_linked) {
70 if(grep {$_ == $out_link} @in_linked) {
71 push(@confirmed, $out_link);
73 push(@pending_out, $out_link);
77 for my $in_link (@in_linked) {
78 push(@pending_in, $in_link)
79 unless grep {$_ == $in_link} @confirmed;
82 if($$options{confirmed_only}) {
84 confirmed => $self->load_linked_user_perms($e, $user_id, @confirmed),
88 confirmed => $self->load_linked_user_perms($e, $user_id, @confirmed),
89 pending_out => $self->load_linked_user_perms($e, $user_id, @pending_out),
90 pending_in => $self->load_linked_user_perms($e, $user_id, @pending_in)
95 # given a base user and set of linked users, returns the trimmed linked user
96 # records, plus the perms (by name) each user has been granted
97 sub load_linked_user_perms {
98 my($self, $e, $user_id, @users) = @_;
101 # use this query to retrieve trimmed linked user objects
103 {select => {au => \@expose_user_fields}, from => 'au', where => undef};
105 for my $d_user (@users) {
107 # fetch all of the bucket items linked from base user to
108 # delegate user with the folks: prefix on the bucket type
109 $perm_check_query->{where} = {
110 '+cubi' => {target_user => $d_user},
111 '+cub' => {btype => {like => 'folks:%'}, owner => $user_id}
114 my $perms_granted = [
115 map {substr($_->{btype}, 6)} @{$e->json_query($perm_check_query)}];
117 # fetch all of the bucket items linked from the delegate user
118 # to the base user with the folks: prefix on the bucket type
119 $perm_check_query->{where} = {
120 '+cubi' => {target_user => $user_id},
121 '+cub' => {btype => {like => 'folks:%'}, owner => $d_user}
124 my $perms_received = [
125 map {substr($_->{btype}, 6)} @{$e->json_query($perm_check_query)}];
127 $user_select->{where} = {id => $d_user};
129 user => $e->json_query($user_select),
130 perms_granted => $perms_granted,
131 perms_received => $perms_received
139 my $direct_links_query = {
140 select => {cub => ['id'] },
143 cubi => {field => 'bucket', fkey => 'id'}
147 '+cubi' => {target_user => undef},
148 '+cub' => {btype => 'folks', owner => undef}
153 sub confirmed_friends {
154 my($self, $e, $user1_id, $user2_id) = @_;
156 $direct_links_query->{where}->{'+cub'}->{owner} = $user1_id;
157 $direct_links_query->{where}->{'+cubi'}->{target_user} = $user2_id;
159 if($e->json_query($direct_links_query)->[0]) {
161 $direct_links_query->{where}->{'+cub'}->{owner} = $user2_id;
162 $direct_links_query->{where}->{'+cubi'}->{target_user} = $user1_id;
163 return 1 if $e->json_query($direct_links_query)->[0];
171 # returns 1 if delegate_user is allowed to perform 'perm' for base_user
172 sub friend_perm_allowed {
173 my($self, $e, $base_user_id, $delegate_user_id, $perm) = @_;
174 return 0 unless $self->confirmed_friends($base_user_id, $delegate_user_id);
175 $perm_check_query->{where} = {
176 '+cubi' => {target_user => $delegate_user_id},
177 '+cub' => {btype => "folks:$perm", owner => $base_user_id}
179 return 1 if $e->json_query($perm_check_query)->[0];
183 sub apply_friend_perm {
184 my($self, $e, $base_user_id, $delegate_user_id, $perm) = @_;
186 my $bucket = $e->search_container_user_bucket(
187 {owner => $base_user_id, btype => "folks:$perm"})->[0];
190 # is the permission already set?
191 return undef if $e->search_container_user_bucket_item(
192 {bucket => $bucket->id, target_user => $delegate_user_id})->[0];
195 # make sure the perm-specific bucket exists for this user
196 $bucket = Fieldmapper::container::user_bucket->new;
197 $bucket->owner($base_user_id);
198 $bucket->btype("folks:$perm");
199 $bucket->name("folks:$perm");
200 $e->create_container_user_bucket($bucket) or return $e->die_event;
203 my $item = Fieldmapper::container::user_bucket_item->new;
204 $item->bucket($bucket->id);
205 $item->target_user($delegate_user_id);
206 $e->create_container_user_bucket_item($item) or return $e->die_event;