#!/usr/bin/perl
#
# DW::Hooks::SiteSearch
#
# Hooks for Site Search functionality.
#
# Authors:
#      Mark Smith <mark@dreamwidth.org>
#
# Copyright (c) 2009 by Dreamwidth Studios, LLC.
#
# This program is free software; you may redistribute it and/or modify it under
# the same terms as Perl itself.  For a copy of the license, please reference
# 'perldoc perlartistic' or 'perldoc perlgpl'.
#

package DW::Hooks::SiteSearch;

use strict;
use LJ::Hooks;
use Carp;
use DW::Task::SphinxCopier;

sub _sphinx_db {

    # ensure we can talk to our system
    return unless @LJ::SPHINX_SEARCHD;
    my $dbh = LJ::get_dbh('sphinx_search')
        or carp "Unable to get sphinx_search database handle.";
    return $dbh;
}

LJ::Hooks::register_hook(
    'setprop',
    sub {
        my %opts = @_;
        my $bool;
        if ( $opts{prop} eq 'opt_blockglobalsearch' ) {
            $bool = $opts{value} eq 'Y' ? 0 : 1;
        }
        elsif ( $opts{prop} eq 'not_approved' ) {

            # only act on this if it is being cleared from a visible user
            return if $opts{value};
            return if $opts{u}->is_suspended;
            $bool = 1;
        }
        else {
            return;
        }

        my $dbh = _sphinx_db() or return 0;
        $dbh->do(
            q{UPDATE items_raw SET allow_global_search = ?, touchtime = UNIX_TIMESTAMP()
          WHERE journalid = ?},
            undef, $bool, $opts{u}->id
        );
        die $dbh->errstr if $dbh->err;

        # looks good
        return 1;
    }
);

# set when the user's status(vis) changes
# the user may still undelete or be unsuspended
# so we don't want to remove from indexing just yet
sub _mark_deleted {
    my ( $u, $is_deleted ) = @_;

    my $dbh = _sphinx_db() or return 0;
    $dbh->do(
        q{UPDATE items_raw SET is_deleted = ?, touchtime = UNIX_TIMESTAMP()
          WHERE journalid = ?},
        undef, $is_deleted, $u->id
    );
    die $dbh->errstr if $dbh->err;

    return 1;
}

LJ::Hooks::register_hook( 'account_delete', sub { _mark_deleted( $_[0], 1 ) } );
LJ::Hooks::register_hook( 'account_cancel', sub { _mark_deleted( $_[0], 1 ) } );
LJ::Hooks::register_hook(
    'account_makevisible',
    sub {
        my ( $u, %opts ) = @_;

        my $old = $opts{old_statusvis};
        _mark_deleted( $u, 0 ) if $old eq "D" || $old eq "S";
    }
);

LJ::Hooks::register_hook(
    'purged_user',
    sub {
        my ($u) = @_;

       # queue up a copier job, which will notice that the entries by this user have been deleted...
        DW::TaskQueue->dispatch(
            DW::Task::SphinxCopier->new( { userid => $u->id, source => "purghook" } ) );

    }
);

1;
