Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

filter searches and subscriptions by blocked accounts #22

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions html/using.html
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ <h3>Settings</h3>
requested from twitter (in seconds). Default is every 5 minutes. Keep
in mind that twitter will rate limit your user (or IP) if you hit them
too often. Hardcoded minimum is one minute.</li>
<li><code>twitter_friends_poll</code> - How often should the updated friends
list be retrieved from the server. By default, only once every 10
<li><code>twitter_friends_poll</code> - How often should the updated friends and block
lists be retrieved from the server. By default, only once every 10
minutes</li>
<li><code>twitter_timeout</code> - How long should we wait before giving up
on a twitter operation. Should help avoid irssi pinging out of IRC
Expand Down
85 changes: 81 additions & 4 deletions twirssi.pl
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@
my $poll;
my $last_poll;
my $last_friends_poll = 0;
my $last_blocks_poll = 0;
my %nicks;
my %friends;
my %blocks;
my %tweet_cache;
my %state;
my $failstatus = 0;
Expand Down Expand Up @@ -554,7 +556,7 @@ sub cmd_login {
return;
}

%friends = %nicks = ();
%blocks = %friends = %nicks = ();

my $service;
if ( $user =~ /^(.*)@(twitter|identica)$/ ) {
Expand Down Expand Up @@ -758,10 +760,13 @@ sub verify_twitter_object {
Irssi::timeout_remove($poll) if $poll;
$poll = Irssi::timeout_add( &get_poll_time * 1000, \&get_updates, "" );
&notice( [ "tweet", "$user\@$service" ],
"Logged in as $user\@$service, loading friends list..." );
"Logged in as $user\@$service, loading friends list and blocks..." );
&load_friends();
&notice( [ "tweet", "$user\@$service" ],
"loaded friends: " . scalar keys %friends );
&load_blocks();
&notice( [ "tweet", "$user\@$service" ],
"loaded blocks: " . scalar keys %blocks );

%nicks = %friends;
$nicks{$user} = 0;
Expand Down Expand Up @@ -1139,6 +1144,44 @@ sub load_friends {
return ( $added, $removed );
}

sub load_blocks {
my $fh = shift;
my %new_blocks;
eval {
foreach my $t ( keys %twits ) {
print $fh "type:debug Loading blocks page ALL...\n"
if ( $fh and &debug );
my $blocks;
$blocks = $twits{$t}->blocking();
$new_blocks{ lc $_->{screen_name} } = time foreach @$blocks;
}
};

if ($@) {
print $fh "type:debug Error during blocks list update. Aborted.\n"
if $fh;
return;
}

my ( $added, $removed ) = ( 0, 0 );
print $fh "type:debug Scanning for new blocks...\n" if ( $fh and &debug );
foreach ( keys %new_blocks ) {
next if exists $blocks{$_};
$blocks{$_} = time;
$added++;
}

print $fh "type:debug Scanning for removed blocks...\n"
if ( $fh and &debug );
foreach ( keys %blocks ) {
next if exists $new_blocks{$_};
delete $blocks{$_};
$removed++;
}

return ( $added, $removed );
}

sub get_updates {
print scalar localtime, " - get_updates starting" if &debug;

Expand Down Expand Up @@ -1206,6 +1249,23 @@ sub get_updates {
print $fh "$_ $friends{$_}\n";
}

print $fh "__blocks__\n";
if ( time - $last_blocks_poll > $settings{friends_poll} ) {
print $fh "__updated ", time, "\n";
my ( $added, $removed ) = &load_blocks($fh);
if ( $added + $removed ) {
print $fh "type:debug %R***%n Blocks list updated: ",
join( ", ",
sprintf( "%d added", $added ),
sprintf( "%d removed", $removed ) ),
"\n";
}
}

foreach ( sort keys %blocks ) {
print $fh "$_ $blocks{$_}\n";
}

if ($error) {
print $fh "type:debug Update encountered errors. Aborted\n";
print $fh "-- $last_poll";
Expand Down Expand Up @@ -1421,6 +1481,7 @@ sub do_updates {
$search->{max_id}, $username, $topic;

foreach my $t ( reverse @{ $search->{results} } ) {
next if exists $blocks{ lc $t->{from_user} };
my $text = &get_text( $t, $obj );
printf $fh "id:%s account:%s nick:%s type:search topic:%s %s\n",
$t->{id}, $username, $t->{from_user}, $topic, $text;
Expand Down Expand Up @@ -1457,6 +1518,8 @@ sub do_updates {

# TODO: consider applying ignore-settings to search results
my @results = @{ $search->{results} };

@results = grep { not exists $blocks{ lc $_->{from_user} } } @results;
if ( $max_results > 0 ) {
splice @results, $max_results;
}
Expand Down Expand Up @@ -1697,12 +1760,25 @@ sub monitor_child {

%friends = ();
while (<FILE>) {
last if /^__blocks__/;
if (/^__updated (\d+)$/) {
$last_friends_poll = $1;
print "Friend list updated" if &debug;
next;
}

my ( $f, $t ) = split ' ', $_;
$nicks{$f} = $friends{$f} = $t;
}

%blocks = ();
while (<FILE>) {
if (/^__updated (\d+)$/) {
$last_blocks_poll = $1;
print "Block list updated" if &debug;
next;
}

if (/^-- (\d+)$/) {
$new_last_poll = $1;
if ( $new_last_poll >= $last_poll ) {
Expand All @@ -1715,8 +1791,8 @@ sub monitor_child {
next;
}
}
my ( $f, $t ) = split ' ', $_;
$nicks{$f} = $friends{$f} = $t;
my ( $b, $t ) = split ' ', $_;
$blocks{$b} = $t;
}

if ($new_last_poll) {
Expand Down Expand Up @@ -2305,6 +2381,7 @@ sub window_to_account {
map { "u: $_->{username}\@" . ref($_) } values %twits;
print "selected: $user\@$defservice";
print "friends: ", join ", ", sort keys %friends;
print "blocks: ", join ", ", sort keys %blocks;
print "nicks: ", join ", ", sort keys %nicks;
print "searches: ", Dumper \%{ $state{__searches} };
print "windows: ", Dumper \%{ $state{__windows} };
Expand Down