Committer: vtroitsky
LJSUP-10497: Login/GetPics/GetGalsTree functions added + updates (temp commit)U trunk/lib/FB/Protocol/Fotki/GetGals.pm A trunk/lib/FB/Protocol/Fotki/GetGalsTree.pm A trunk/lib/FB/Protocol/Fotki/GetPics.pm A trunk/lib/FB/Protocol/Fotki/Login.pm U trunk/lib/FB/Protocol/Fotki/UploadPic.pm U trunk/lib/FB/Protocol/Response.pm
Modified: trunk/lib/FB/Protocol/Fotki/GetGals.pm =================================================================== --- trunk/lib/FB/Protocol/Fotki/GetGals.pm 2011-11-25 15:46:00 UTC (rev 1454) +++ trunk/lib/FB/Protocol/Fotki/GetGals.pm 2011-11-28 15:35:57 UTC (rev 1455) @@ -4,7 +4,6 @@ use strict; use LJ::Fotki::Album; -use Data::Dumper; sub handler { my $resp = shift or return undef; @@ -17,16 +16,15 @@ return undef; }; my $lj_userid = FB::get_domain_userid($u); - my $lj_user = LJ::load_userid($lj_userid); # warn "userid: ".$u->userid." -> ljuserid: $lj_userid # $lj_user" ; - my $albums = LJ::Fotki::Album->get_albums($lj_userid); + # TODO: eval to catch all errors + my $albums = LJ::Fotki::Album->get_albums($lj_userid, $lj_user); my $ret = {Gal => [] }; if (scalar(@$albums)) { -# warn "ALB:".Data::Dumper->Dump($albums); for my $album (@$albums) { next unless($album); my $album_ret = { Added: trunk/lib/FB/Protocol/Fotki/GetGalsTree.pm =================================================================== --- trunk/lib/FB/Protocol/Fotki/GetGalsTree.pm (rev 0) +++ trunk/lib/FB/Protocol/Fotki/GetGalsTree.pm 2011-11-28 15:35:57 UTC (rev 1455) @@ -0,0 +1,165 @@ +#!/usr/bin/perl + +package FB::Protocol::Fotki::GetGalsTree; + +use strict; +use LJ::Fotki::Album; + +sub handler { + my $resp = shift or return undef; + my $req = $resp->{req}; + my $vars = $req->{vars}; + my $u = $resp->{u}; + + my $err = sub { + $resp->add_method_error(GetGalsTree => @_); + return undef; + }; + + my $lj_userid = FB::get_domain_userid($u); + my $lj_user = LJ::load_userid($lj_userid); + + my $albums = LJ::Fotki::Album->get_albums($lj_userid, $lj_user); + + return $err->(500) unless ref $albums; + + my $ret = { + RootGals => [ { Gal => [ ] } ], + UnreachableGals => [ { Gal => [ ] } ], + }; + + if (scalar(@$albums)) { + for my $album (@$albums) { + next unless($album); + my $album_ret = { + id => $album->album_id, + Name => [ $album->title ], + Sec => [ 255 ], # default security: public + # Date => [ $album->createtime ], # gallery creation date in 2004-01-01 01:01:01 format + # TimeUpdate => [ $album->updatetime ], # update timestamp + URL => [ $album->url ], + }; + # is this the incoming gallery? +# $gal_ret->{incoming} = 1 if $album->is_unsorted; + + # GalMembers + my $photos = $album->get_all_photos($lj_user); + $album_ret->{GalMembers}->{GalMember} = [ map { { id => $_->photo_id } } @$photos ]; + + # ChildGals + $album_ret->{ChildGals}->{ChildGal} = []; # empty + + push @{$ret->{RootGals}->[0]->{Gal}}, $album_ret; + } + } + + + # register return value with parent Request + $resp->add_method_vars(GetGalsTree => $ret); + + return 1; + + +} + +sub handler_old { + my $resp = shift or return undef; + my $req = $resp->{req}; + my $vars = $req->{vars}; + my $u = $resp->{u}; + + my $err = sub { + $resp->add_method_error(GetGalsTree => @_); + return undef; + }; + + my $gals = FB::gals_of_user($u); + return $err->(500) unless ref $gals; + + my $ret = { + RootGals => [ { Gal => [ ] } ], + UnreachableGals => [ { Gal => [ ] } ], + }; + + if (%$gals) { + + # load gallery rels and pic rels for use later + my @gal_rel = FB::user_galleryrel($u); + my @pic_rel = FB::user_gallerypics($u); + + # build a data structure of parent gallids => array of their children + my %parents = (); + my %children = (); + foreach (grep { $_->{type} eq 'C' } @gal_rel) { + my ($pid, $cid) = ($_->{gallid}, $_->{gallid2}); + push @{$children{$pid}}, $gals->{$cid}; + push @{$parents{$cid}}, $gals->{$pid}; + $gals->{$cid}->{sortorder} = $_->{sortorder}; + } + + # resets every visit to top-level (0) + my %seen = (); + my %unreachable = map { $_ => 1 } keys %$gals; + + my $recurse; + $recurse = sub { + my $gal = shift; + my $gallid = $gal->{gallid}; + + # since we've visited this gallery, it's not unreachable + delete $unreachable{$gallid}; + + my $node = { id => $gallid, + sortorder => $gal->{sortorder}+0, + Name => [ FB::transform_gal_name($gal->{name}) ], + Sec => [ $gal->{secid} ], + Date => [ $gal->{dategal} ], + TimeUpdate => [ $gal->{timeupdate} ], + URL => [ FB::url_gallery($u, $gal) ], + }; + + # is this the incoming gallery? + $node->{incoming} = 1 if FB::gal_is_unsorted($gal); + + # GalMembers + $node->{GalMembers}->{GalMember} = + [ map { { id => $_->{upicid} } } + grep { $_->{gallid} == $gallid } + @pic_rel ]; + + # now the hard part: ChildGals + $node->{ChildGals}->{Gal} = + [ map { $recurse->($_) } + sort { $a->{sortorder} <=> $b->{sortorder} || $a->{gallid} <=> $b->{gallid} } + grep { ! $seen{$_->{gallid}}++ } + @{$children{$gallid}} ]; + + return $node; + }; + + # start at parent (top-level) node and built tree + # of all its children + foreach my $child (@{$children{0}}) { + %seen = ( $child->{gallid} => 1 ); + push @{$ret->{RootGals}->[0]->{Gal}}, $recurse->($child); + } + + # represent any galleries that were unreachable, recursing down + # from each gallery that has no parents + foreach my $gal (map { $gals->{$_} } + sort { $gals->{$a}->{sortorder} <=> $gals->{$b}->{sortorder} || + $gals->{$a}->{gallid} <=> $gals->{$b}->{gallid} } + keys %unreachable) { + + %seen = ( $gal->{gallid} => 1 ); + push @{$ret->{UnreachableGals}->[0]->{Gal}}, $recurse->($gal); + } + } + + # register return value with parent Request + $resp->add_method_vars(GetGalsTree => $ret); + + return 1; +} + +1; Added: trunk/lib/FB/Protocol/Fotki/GetPics.pm =================================================================== --- trunk/lib/FB/Protocol/Fotki/GetPics.pm (rev 0) +++ trunk/lib/FB/Protocol/Fotki/GetPics.pm 2011-11-28 15:35:57 UTC (rev 1455) @@ -0,0 +1,111 @@ +#!/usr/bin/perl + +package FB::Protocol::Fotki::GetPics; + +use strict; +use LJ::Fotki::Photo; + +sub handler { + my $resp = shift or return undef; + my $req = $resp->{req}; + my $vars = $req->{vars}; + my $u = $resp->{u}; + + my $err = sub { + $resp->add_method_error(GetPics => @_); + return undef; + }; + + my $lj_userid = FB::get_domain_userid($u); + my $lj_user = LJ::load_userid($lj_userid); + + # TODO: eval to catch all errors + my $photos = LJ::Fotki::Photo->get_all_photos($lj_userid, $lj_user); + + my $ret = { Pic => [] }; + + if (scalar(@$photos)) { + for my $photo (@$photos) { + next unless($photo); + + push @{$ret->{Pic}}, { + id => $photo->photo_id, + Sec => [ $photo->get_security ], # TODO: convert mask!!! +# Width => [ $pic->{width} ], # TODO: necessary to support original photo sizes +# Height => [ $pic->{height} ], +# Bytes => [ $pic->{bytes} ], # TODO: original photo size +# Format => [ FB::fmtid_to_mime($pic->{fmtid}) ], # TODO: Format of the original photo +# MD5 => [ FB::bin_to_hex($gpic_md5->{$pic->{gpicid}}) ], + URL => [ $photo->orig_url ], + Meta => [ map { + { name => $_->[0], + content => $pic->($_->[1])() } + } ( #['filename' => 'filename' ], # TODO: To be done + ['title' => 'title' ], + ['description' => 'desc' ]) + ], + }; + + } + } + + # register return value with parent Request + $resp->add_method_vars(GetPics => $ret); + + return 1; +} + + +sub handler_old { + my $resp = shift or return undef; + my $req = $resp->{req}; + my $vars = $req->{vars}; + my $u = $resp->{u}; + + my $err = sub { + $resp->add_method_error(GetPics => @_); + return undef; + }; + + my $ret = { Pic => [] }; + my $upics = FB::upics_of_user($u, { props => [ qw(filename pictitle) ] }); + return $err->(500 => FB::last_error) unless ref $upics eq 'HASH'; + + if (%$upics) { + + # get md5s of all the gpics + my $gpic_md5 = FB::get_gpic_md5_multi([ map { $_->{gpicid} } values %$upics ]); + return $err->(500 => FB::last_error()) unless ref $gpic_md5 eq 'HASH'; + + my @pics_with_des = FB::get_des_multi($u, 'P', [ values %$upics ]); + return $err->(500 => FB::last_error()) unless ref $pics_with_des[0] eq 'HASH'; + + foreach my $pic (sort { $a->{upicid} <=> $b->{upicid} } @pics_with_des) { + + push @{$ret->{Pic}}, { + id => $pic->{upicid}, + Sec => [ $pic->{secid} ], + Width => [ $pic->{width} ], + Height => [ $pic->{height} ], + Bytes => [ $pic->{bytes} ], + Format => [ FB::fmtid_to_mime($pic->{fmtid}) ], + MD5 => [ FB::bin_to_hex($gpic_md5->{$pic->{gpicid}}) ], + URL => [ FB::url_picture($u, $pic) ], + Meta => [ map { + { name => $_->[0], + content => $pic->{$_->[1]} } + } (['filename' => 'filename' ], + ['title' => 'pictitle' ], + ['description' => 'des' ]) + ], + }; + } + } + + # register return value with parent Request + $resp->add_method_vars(GetPics => $ret); + + return 1; +} + +1; Added: trunk/lib/FB/Protocol/Fotki/Login.pm =================================================================== --- trunk/lib/FB/Protocol/Fotki/Login.pm (rev 0) +++ trunk/lib/FB/Protocol/Fotki/Login.pm 2011-11-28 15:35:57 UTC (rev 1455) @@ -0,0 +1,93 @@ +#!/usr/bin/perl + +package FB::Protocol::Fotki::Login; + +use strict; +use LJ::Fotki::UserSpace; + +sub handler { + my $resp = shift or return undef; + my $req = $resp->{req}; + my $vars = $req->{vars}->{Login}; + my $u = $resp->{u}; + + my $lj_userid = FB::get_domain_userid($u); + my $lj_user = LJ::load_userid($lj_userid); + + my $err = sub { + $resp->add_method_error(Login => @_); + return undef; + }; + + my $ret = { ServerTime => [ FB::date_unix_to_mysql() ]}; + + # store client version in $r->notes so we can log it optionally + # to keep stats + if (defined $vars->{ClientVersion}) { + LJ::Request->notes(ProtocolClientVersion => $vars->{ClientVersion}); + } + + # Get available space for the user + my $spaces = LJ::Fotki::UserSpace->get_spaces($lj_user); + + if (ref $spaces eq 'ARRAY') { + my $free_space = $spaces->[3] - $spaces->[0]; + $free_space = 0 if $free_space < 0; + $ret->{Quota} = { + Total => [ $spaces->[3] ], # kb -> bytes + Used => [ $spaces->[0] ], + Remaining => [ $free_space ], + }; + } + + # are there any current system messages applying to this user? + $ret->{Message} = [ FB::get_system_message($u) . '' ]; + + # register return value with parent Request + $resp->add_method_vars( Login => $ret ); + + return 1; +} + +sub handler_old { + my $resp = shift or return undef; + my $req = $resp->{req}; + my $vars = $req->{vars}->{Login}; + my $u = $resp->{u}; + + my $err = sub { + $resp->add_method_error(Login => @_); + return undef; + }; + + my $ret = { ServerTime => [ FB::date_unix_to_mysql() ]}; + + # store client version in $r->notes so we can log it optionally + # to keep stats + if (defined $vars->{ClientVersion}) { + LJ::Request->notes(ProtocolClientVersion => $vars->{ClientVersion}); + } + + # look up quota and usage information to return with this request + if (FB::are_hooks('disk_usage_info')) { + + my $qinf = FB::run_hook('disk_usage_info', $u); + if (ref $qinf eq 'HASH') { + $ret->{Quota} = { + Total => [ $qinf->{quota} * (1 << 10) ], # kb -> bytes + Used => [ $qinf->{used} * (1 << 10) ], + Remaining => [ $qinf->{free} * (1 << 10) ], + }; + } + } + + # are there any current system messages applying to this user? + $ret->{Message} = [ FB::get_system_message($u) . '' ]; + + # register return value with parent Request + $resp->add_method_vars( Login => $ret ); + + return 1; +} + +1; Modified: trunk/lib/FB/Protocol/Fotki/UploadPic.pm =================================================================== --- trunk/lib/FB/Protocol/Fotki/UploadPic.pm 2011-11-25 15:46:00 UTC (rev 1454) +++ trunk/lib/FB/Protocol/Fotki/UploadPic.pm 2011-11-28 15:35:57 UTC (rev 1455) @@ -110,15 +110,16 @@ return $err->(402) if $spaces->[3] < $length; - # TODO: process Galleries + # TODO: process Galleries: add uploaded photo into specified galleries (albums) - - # get default upload album + # use default upload album my $album = LJ::Fotki::Album->get_default_album($lj_user); + # upload photo to fotki and create + my $content = ''; seek $img->{spool_fh}, 0,0; read $img->{spool_fh}, $content, $length; Modified: trunk/lib/FB/Protocol/Response.pm =================================================================== --- trunk/lib/FB/Protocol/Response.pm 2011-11-25 15:46:00 UTC (rev 1454) +++ trunk/lib/FB/Protocol/Response.pm 2011-11-28 15:35:57 UTC (rev 1455) @@ -28,8 +28,11 @@ ); %VALID_METHODS_v2 = map { $_ => 1 } qw( + CreateGals GetGals - CreateGals + GetGalsTree + GetPics + Login UploadPic );