$me->db_init();
}
-sub _check_dup {
- my ($me, $args, $uri) = @_;
-
- my $found = 0;
-
+sub _db_check_dup {
+ my ($me, $db_args) = @_;
my $sth = $me->{dbh}->prepare('select * from posts where uri = ? order by post_time desc limit 1');
- my $rv = $sth->execute($uri);
- my $res = $sth->fetchrow_hashref;
- if ($res) {
- if ($res->{post_time} < time() - 7 * 24 * 60 * 60) {
- $me->_response($args, 'だいぶ前 '.$me->_format_submit($res).'にいってたにゃー '.$me->_fb_post_uri($res->{fb_post_id}));
- } else {
- $me->_response($args, '既に '.$me->_format_submit($res).'に言ってますよ? '.$me->{cfg}->{fb_page_url}.'posts/'.$res->{fb_post_id});
- $found = 1;
- }
- }
- $sth->finish;
+ my $rv = $sth->execute($db_args->{uri});
+ my $ret = $sth->fetchrow_hashref;
- return $found;
-};
+ $sth->finish;
+ return $ret;
+}
sub _db_insert {
my ($me, $db_args) = @_;
}
sub _db_search {
- my ($me, $word) = @_;
+ my ($me, $db_args) = @_;
- my $column = $word =~ /:\/\// ? 'uri' : 'path';
- my $w = '%' . $word . '%';
+ my $column = $db_args->{word} =~ /:\/\// ? 'uri' : 'path';
+ my $w = '%' . $db_args->{word} . '%';
my $sth = $me->{dbh}->prepare('select * from posts where prefix like ? or '.$column.' like ? or comment like ? order by post_time desc limit 1000');
$sth->bind_param(1, $w, SQL_VARCHAR);
$sth->bind_param(2, $w, SQL_VARCHAR);
}
sub _db_search_lastpost {
- my ($me, $who) = @_;
+ my ($me, $db_args) = @_;
my $sth = $me->{dbh}->prepare('select * from posts where submitter = ? order by post_time desc limit 1');
- $sth->bind_param(1, $who, SQL_VARCHAR);
+ $sth->bind_param(1, $db_args->{who}, SQL_VARCHAR);
$sth->execute();
my $ret = $sth->fetchrow_hashref();
my $comment = $3;
my $text = $args->{who} . '曰く、'.$prefix.' '.$comment;
- return 0 if ($me->_check_dup($args, $uri));
-
- eval{$resp = $me->{fb}->publish($text, $uri)};
- if ($@) {
- $me->fb_init();
+ if (my $res = $me->_db_check_dup({uri =>$uri})) {
+ if ($res->{post_time} < time() - 7 * 24 * 60 * 60) {
+ $resp_msg = 'だいぶ前 '.$me->_format_submit($res).'にいってたにゃー '.$me->{fb}->post_uri($res->{fb_post_id});
+ } else {
+ $resp_msg = '既に '.$me->_format_submit($res).'に言ってますよ? '.$me->{fb}->post_uri($res->{fb_post_id});
+ }
+ } else {
eval{$resp = $me->{fb}->publish($text, $uri)};
- $post_ok = 0 if ($@);
- }
+ if ($@) {
+ $me->fb_init();
+ eval{$resp = $me->{fb}->publish($text, $uri)};
+ $post_ok = 0 if ($@);
+ }
- if ($post_ok) {
- my (undef, $post_id) = split(/_/, $resp->{id});
- $me->_db_insert({submitter => $args->{who},
- fb_post_id => $post_id,
- uri => $uri,
- prefix => $prefix,
- comment => $comment});
- $resp_msg = $args->{who} . ': うい '.$me->{fb}->post_uri($post_id).' で登録';
- } else {
- $resp_msg = 'can not post to facebook';
+ if ($post_ok) {
+ my (undef, $post_id) = split(/_/, $resp->{id});
+ $me->_db_insert({submitter => $args->{who},
+ fb_post_id => $post_id,
+ uri => $uri,
+ prefix => $prefix,
+ comment => $comment});
+ $resp_msg = $args->{who} . ': うい '.$me->{fb}->post_uri($post_id).' で登録';
+ } else {
+ $resp_msg = 'can not post to facebook';
+ }
}
-
return $resp_msg;
}
return 0;
sub _delete_prev {
my ($me, $args) = @_;
- my $last_post = $me->_db_search_lastpost($args->{who});
+ my $last_post = $me->_db_search_lastpost({who => $args->{who}});
if (!defined $last_post) {
return $args->{who}.': いまのっていつの? というか ないし';
if ($args->{body} =~ /^ふみくん\s+(.+)\?\s*$/) {
$me->{last_search}->{$args->{who}} = undef;
- $me->{last_search}->{$args->{who}} = $me->_db_search($1);
+ $me->{last_search}->{$args->{who}} = $me->_db_search({word => $1});
return $me->_search_next($args);
}
}