| 454 | | # Get an iter for each accessible blog |
| 455 | | my @perms = $app->model('permission')->load( |
| 456 | | { blog_id => '0', author_id => $author->id }, |
| 457 | | { not => { blog_id => 1 } }, |
| 458 | | ); |
| 459 | | if (@perms) { |
| 460 | | @streams = map { |
| 461 | | { |
| 462 | | iter => $class->load_iter( |
| 463 | | { |
| 464 | | blog_id => $_->blog_id, |
| 465 | | %terms |
| 466 | | }, |
| 467 | | \%args |
| 468 | | ) |
| | 448 | |
| | 449 | my @streams; |
| | 450 | if ( $author->is_superuser ) { |
| | 451 | @streams = ( { iter => $class->load_iter( \%terms, \%args ) } ); |
| | 452 | } |
| | 453 | else { |
| | 454 | # Get an iter for each accessible blog |
| | 455 | my @perms = $app->model('permission')->load( |
| | 456 | { blog_id => '0', author_id => $author->id }, |
| | 457 | { not => { blog_id => 1 } }, |
| | 458 | ); |
| | 459 | if (@perms) { |
| | 460 | @streams = map { |
| | 461 | { |
| | 462 | iter => $class->load_iter( |
| | 463 | { |
| | 464 | blog_id => $_->blog_id, |
| | 465 | %terms |
| | 466 | }, |
| | 467 | \%args |
| | 468 | ) |
| | 469 | } |
| | 470 | } @perms; |
| | 471 | } |
| | 472 | } |
| | 473 | |
| | 474 | # Pull out the head of each iterator |
| | 475 | # Next: effectively mergesort the various iterators |
| | 476 | # To call the iterator n times takes time in O(bn) |
| | 477 | # with 'b' the number of blogs |
| | 478 | # we expect to hit the iterator l/p times where 'p' is the |
| | 479 | # prob. of the search term appearing and 'l' is $limit |
| | 480 | $_->{head} = $_->{iter}->() foreach @streams; |
| | 481 | if ( $type ne 'template' ) { |
| | 482 | $iter = sub { |
| | 483 | |
| | 484 | # find the head with greatest created_on |
| | 485 | my $which = \$streams[0]; |
| | 486 | foreach my $iter (@streams) { |
| | 487 | next |
| | 488 | if !exists $iter->{head} |
| | 489 | || !$which |
| | 490 | || !${$which}->{head} |
| | 491 | || !defined( $iter->{head} ); |
| | 492 | if ( $iter->{head}->created_on > |
| | 493 | ${$which}->{head}->created_on ) |
| | 494 | { |
| | 495 | $which = \$iter; |
| | 496 | } |
| 470 | | } @perms; |
| 471 | | } |
| 472 | | } |
| 473 | | |
| 474 | | # Pull out the head of each iterator |
| 475 | | # Next: effectively mergesort the various iterators |
| 476 | | # To call the iterator n times takes time in O(bn) |
| 477 | | # with 'b' the number of blogs |
| 478 | | # we expect to hit the iterator l/p times where 'p' is the |
| 479 | | # prob. of the search term appearing and 'l' is $limit |
| 480 | | $_->{head} = $_->{iter}->() foreach @streams; |
| 481 | | if ( $type ne 'template' ) { |
| 482 | | $iter = sub { |
| 483 | | |
| 484 | | # find the head with greatest created_on |
| 485 | | my $which = \$streams[0]; |
| 486 | | foreach my $iter (@streams) { |
| 487 | | next |
| 488 | | if !exists $iter->{head} |
| 489 | | || !$which |
| 490 | | || !${$which}->{head} |
| 491 | | || !defined( $iter->{head} ); |
| 492 | | if ( $iter->{head}->created_on > |
| 493 | | ${$which}->{head}->created_on ) |
| 494 | | { |
| 495 | | $which = \$iter; |
| | 498 | |
| | 499 | # Advance the chosen one |
| | 500 | my $result = ${$which}->{head}; |
| | 501 | ${$which}->{head} = ${$which}->{iter}->() if $result; |
| | 502 | $result; |
| | 503 | }; |
| | 504 | } |
| | 505 | else { |
| | 506 | $iter = sub { |
| | 507 | return undef unless @streams; |
| | 508 | |
| | 509 | # find the head with greatest created_on |
| | 510 | my $which = \$streams[0]; |
| | 511 | while ( @streams && ( !defined ${$which}->{head} ) ) { |
| | 512 | shift @streams; |
| | 513 | last unless @streams; |
| | 514 | $which = \$streams[0]; |
| 497 | | } |
| 498 | | |
| 499 | | # Advance the chosen one |
| 500 | | my $result = ${$which}->{head}; |
| 501 | | ${$which}->{head} = ${$which}->{iter}->() if $result; |
| 502 | | $result; |
| 503 | | }; |
| 504 | | } |
| 505 | | else { |
| 506 | | $iter = sub { |
| 507 | | return undef unless @streams; |
| 508 | | |
| 509 | | # find the head with greatest created_on |
| 510 | | my $which = \$streams[0]; |
| 511 | | while ( @streams && ( !defined ${$which}->{head} ) ) { |
| 512 | | shift @streams; |
| 513 | | last unless @streams; |
| 514 | | $which = \$streams[0]; |
| 515 | | } |
| 516 | | my $result = ${$which}->{head}; |
| 517 | | ${$which}->{head} = ${$which}->{iter}->() if $result; |
| 518 | | $result; |
| 519 | | }; |
| | 516 | my $result = ${$which}->{head}; |
| | 517 | ${$which}->{head} = ${$which}->{iter}->() if $result; |
| | 518 | $result; |
| | 519 | }; |
| | 520 | } |