Skip to content

Commit

Permalink
Item14241: add cache maintenance rest handlers
Browse files Browse the repository at this point in the history
... as well as:

- added account_settings, friends, following_timeline, retweets_of_me apis
- fixed encoding on Foswiki-2.x
  • Loading branch information
MichaelDaum committed Nov 30, 2016
1 parent 5bc8f47 commit ba8b713
Show file tree
Hide file tree
Showing 6 changed files with 194 additions and 110 deletions.
14 changes: 7 additions & 7 deletions .gitignore
Expand Up @@ -5,10 +5,10 @@ igp_*
.changes
*.swp
pub/System/TwitterPlugin/twitter.css
TwitterPlugin.md5
TwitterPlugin.sha1
TwitterPlugin.tgz
TwitterPlugin.txt
TwitterPlugin.zip
TwitterPlugin_installer
TwitterPlugin_installer.pl
/TwitterPlugin.md5
/TwitterPlugin.sha1
/TwitterPlugin.tgz
/TwitterPlugin.txt
/TwitterPlugin.zip
/TwitterPlugin_installer
/TwitterPlugin_installer.pl
41 changes: 25 additions & 16 deletions data/System/TwitterPlugin.txt
@@ -1,6 +1,6 @@
%META:TOPICINFO{author="ProjectContributor" comment="" date="1418752834" format="1.1" version="1"}%
---+!! %TOPIC%
%SHORTDESCRIPTION%
%FORMFIELD{"Description"}%

%TOC%

Expand All @@ -15,14 +15,18 @@ The most common ones are:
| *Parameter* | *Description* | *Default* |
| =&lt;method>= | known actions are <p> \
<ul>\
<li>account_settings</li>\
<li>favorites</li>\
<li>friends</li>\
<li>folowers</li>\
<li>following_timeline</li>\
<li>retweets_of_me</li>\
<li>get_lists</li> \
<li>home_timeline</li> \
<li>list_statuses</li> \
<li>mentions_timeline</li> \
<li>search</li> \
<li>rate_limit_status</li> \
<li>search</li> \
<li>user_timeline</li> \
</ul> </p> ... explained [[#Methods][below]]. | |
| =header="..."= | header string to be prepended to the timeline | <verbatim class="html"><ul class='twitter'></verbatim> |
Expand Down Expand Up @@ -312,19 +316,24 @@ Next click on "Create my access token" and record these keys in:
Make sure that you do NOT share this information with anybody else and that these keys are not readable
by unauthorized people inspecting the =LocalSite.cfg= file on your Foswiki server.

---++ Info
<!--
* Set SHORTDESCRIPTION = %$SHORTDESCRIPTION%
-->

| Author(s): | Michael Daum|
| Copyright: | &copy; 2014 Michael Daum http://michaeldaumconsulting.com |
| License: | [[http://www.gnu.org/licenses/gpl.html][GPL (Gnu General Public License)]] |
| Release: | %$RELEASE% |
| Version: | %$VERSION% |
| Change History: | <!-- versions below in reverse order -->&nbsp; |
---++ Dependencies
%$DEPENDENCIES%

---++ Change History
%TABLE{columnwidths="7em" tablewidth="100%"}%
| 30 Nov 2016: | added =account_settings=, =friends=, =following_timeline=, =retweets_of_me= apis; \
fixed encoding on Foswiki-2.x; \
added rest hanlder =purgeCache= and =clearCache= to maintain the cache |
| 16 Dec 2014: | added =followers= and =rate_limit_status= api |
| 20 Nov 2014: | initial release |
| Dependencies: | %$DEPENDENCIES% |
| Home page: | Foswiki:Extensions/%TOPIC% |
| Support: | Foswiki:Support/%TOPIC% |

%META:FORM{name="PackageForm"}%
%META:FIELD{name="Author" title="Author" value="Michael Daum"}%
%META:FIELD{name="Copyright" title="Copyright" value="&copy; 2014-2016 Michael Daum http://michaeldaumconsulting.com"}%
%META:FIELD{name="Description" title="Description" value="%25$SHORTDESCRIPTION%25"}%
%META:FIELD{name="Home" title="Home" value="Foswiki:Extensions/%TOPIC%"}%
%META:FIELD{name="License" title="License" value="[[http://www.gnu.org/licenses/gpl.html][GPL (Gnu General Public License)]]"}%
%META:FIELD{name="Release" title="Release" value="%$RELEASE%"}%
%META:FIELD{name="Repository" title="Repository" value="https://github.com/foswiki/%TOPIC%"}%
%META:FIELD{name="Support" title="Support" value="Foswiki:Support/%TOPIC%"}%
%META:FIELD{name="Version" title="Version" value="%$VERSION%"}%
41 changes: 27 additions & 14 deletions lib/Foswiki/Plugins/TwitterPlugin.pm
@@ -1,6 +1,6 @@
# Plugin for Foswiki - The Free and Open Source Wiki, http://foswiki.org/
#
# TwitterPlugin is Copyright (C) 2014 Michael Daum http://michaeldaumconsulting.com
# TwitterPlugin is Copyright (C) 2014-2016 Michael Daum http://michaeldaumconsulting.com
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
Expand All @@ -20,39 +20,52 @@ use warnings;

use Foswiki::Func ();

our $VERSION = '2.00';
our $RELEASE = '2.00';
our $VERSION = '3.00';
our $RELEASE = '30 Nov 2016';
our $SHORTDESCRIPTION = 'Access Twitter via Foswiki';
our $NO_PREFS_IN_TOPIC = 1;
our $core;

sub core {
unless (defined $core) {
require Foswiki::Plugins::TwitterPlugin::Core;
$core = new Foswiki::Plugins::TwitterPlugin::Core();
}
return $core;
}


sub initPlugin {

Foswiki::Func::registerTagHandler('TWITTER', sub { return core->TWITTER(@_); });
Foswiki::Func::registerTagHandler('TWITTER', sub { return getCore()->TWITTER(@_); });

Foswiki::Func::registerRESTHandler(
'update',
sub {
return core->restUpdate(@_);
return getCore()->restUpdate(@_);
},
authenticate => 1,
validate => 1,
http_allow => 'POST',
description => 'Send a status update to a twitter account.'
);

Foswiki::Func::registerRESTHandler('purgeCache', sub { return getCore()->purgeCache(@_); },
authenticate => 1,
validate => 0,
http_allow => 'GET,POST',
);

Foswiki::Func::registerRESTHandler('clearCache', sub { return getCore()->clearCache(@_); },
authenticate => 1,
validate => 0,
http_allow => 'GET,POST',
);

return 1;
}

sub getCore {
unless (defined $core) {
require Foswiki::Plugins::TwitterPlugin::Core;
$core = Foswiki::Plugins::TwitterPlugin::Core->new();
}
return $core;
}



sub finishPlugin {
undef $core;
}
Expand Down
166 changes: 117 additions & 49 deletions lib/Foswiki/Plugins/TwitterPlugin/Core.pm
@@ -1,6 +1,6 @@
# Plugin for Foswiki - The Free and Open Source Wiki, http://foswiki.org/
#
# TwitterPlugin is Copyright (C) 2014 Michael Daum http://michaeldaumconsulting.com
# TwitterPlugin is Copyright (C) 2014-2016 Michael Daum http://michaeldaumconsulting.com
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
Expand Down Expand Up @@ -78,6 +78,22 @@ sub agent {
return $this->{agent};
}

sub purgeCache {
my $this = shift;

$this->agent->ua->cache->purge;

return;
}

sub clearCache {
my $this = shift;

$this->agent->ua->cache->clear;

return;
}

sub restUpdate {
my ($this, $session, $plugin, $verb, $response) = @_;

Expand Down Expand Up @@ -204,62 +220,19 @@ sub handle_followers {
$oldExpires = $this->agent->ua->expires($params->{expires});
}

my @followers = ();
my @users = ();
my $args = _params2args($params);
for (my $cursor = -1, my $result; $cursor; $cursor = $result->{next_cursor} ) {
$args->{cursor} = $cursor;
$result = $this->agent->followers($args);
push @followers, @{$result->users};
push @users, @{$result->users};
}

$this->agent->ua->expires($oldExpires) if defined $oldExpires;

return "<pre>"._dump(@followers)."</pre>" if Foswiki::Func::isTrue($params->{raw});
return "" unless @followers;

my $format = $params->{format};
my $header = $params->{header} || '';
my $footer = $params->{footer} || '';
my $sep = $params->{separator};

$format = '$screen_name' unless defined $format;
$sep = ', ' unless defined $sep;

my @result = ();

my $index = 0;
foreach my $item (@followers) {
my $line = $format;

foreach my $key (qw(created_at description favorites_count followers_count
friends_count id lang listed_count location name profile_background_color
profile_background_image_url profile_background_image_url_https
profile_background_tile profile_image_url profile_image_url_https
profile_link_color profile_location profile_sidebar_border_color
profile_sidebar_fill_color profile_text_color profile_use_background_image
protected screen_name status statuses_count time_zone url utc_offset verified)) {
my $val;
return "" unless @users;
return $this->renderUsers(\@users, $params);

if ($key eq 'status') {
$val = defined($item->{$key}) ? $item->{$key}->text : '';
} else {
$val = $item->{$key};
}
$line =~ s/\$$key/$val/g;
}

$line =~ s/\$index/$index/g;
push @result, $line;

$index++;
}

return "" unless @result;

my $result = $header.join($sep, @result).$footer;
$result =~ s/\$count/$index/g;

return Foswiki::Func::decodeFormatTokens($result);
}

sub handle_favorites {
Expand Down Expand Up @@ -309,6 +282,35 @@ sub handle_get_lists {
return Foswiki::Func::decodeFormatTokens($header.join($sep, @result).$footer);
}

sub handle_account_settings {
my ($this, $params) = @_;

my $settings = $this->agent->account_settings(_params2args($params));
return "<pre>"._dump($settings)."</pre>" if Foswiki::Func::isTrue($params->{raw});

my $format = $params->{format} || '$screen_name';
foreach my $key (qw(allow_contributor_request allow_dm_groups_from allow_dms_from discoverable_by_email discoverable_by_mobile_phone
display_sensitive_media geo_enabled language protected screen_name smart_mute use_cookie_personalization)) {
$format =~ s/\$$key/$settings->{$key}/g;
}

return $format;
}

sub handle_friends {
my ($this, $params) = @_;

my @users = ();
my $args = _params2args($params);
for (my $cursor = -1, my $result; $cursor; $cursor = $result->{next_cursor} ) {
$args->{cursor} = $cursor;
$result = $this->agent->friends($args);
push @users, @{$result->users};
}

return $this->renderUsers(\@users, $params);
}

sub handle_list_statuses {
my ($this, $params) = @_;

Expand All @@ -325,6 +327,14 @@ sub handle_user_timeline {
return $this->renderTimeline($timeline, $params);
}

sub handle_following_timeline {
my ($this, $params) = @_;

my $timeline = $this->agent->following_timeline(_params2args($params));

return $this->renderTimeline($timeline, $params);
}

sub handle_home_timeline {
my ($this, $params) = @_;

Expand All @@ -341,6 +351,14 @@ sub handle_mentions_timeline {
return $this->renderTimeline($timeline, $params);
}

sub handle_retweets_of_me {
my ($this, $params) = @_;

my $timeline = $this->agent->retweets_of_me(_params2args($params));

return $this->renderTimeline($timeline, $params);
}

sub handle_search {
my ($this, $params) = @_;

Expand All @@ -349,6 +367,56 @@ sub handle_search {
return $this->renderTimeline($result->statuses, $params);
}

sub renderUsers {
my ($this, $users, $params) = @_;

return "<pre>"._dump($users)."</pre>" if Foswiki::Func::isTrue($params->{raw});

my $format = $params->{format};
my $header = $params->{header} || '';
my $footer = $params->{footer} || '';
my $sep = $params->{separator};

$format = '$screen_name' unless defined $format;
$sep = ', ' unless defined $sep;

my @result = ();

my $index = 0;
foreach my $item (@$users) {
my $line = $format;

foreach my $key (qw(created_at description favorites_count followers_count
friends_count id lang listed_count location name profile_background_color
profile_background_image_url profile_background_image_url_https
profile_background_tile profile_image_url profile_image_url_https
profile_link_color profile_location profile_sidebar_border_color
profile_sidebar_fill_color profile_text_color profile_use_background_image
protected screen_name status statuses_count time_zone url utc_offset verified)) {
my $val;

if ($key eq 'status') {
$val = defined($item->{$key}) ? $item->{$key}->text : '';
} else {
$val = $item->{$key};
}
$line =~ s/\$$key/$val/g;
}

$line =~ s/\$index/$index/g;
push @result, $line;

$index++;
}

return "" unless @result;

my $result = $header.join($sep, @result).$footer;
$result =~ s/\$count/$index/g;

return Foswiki::Func::decodeFormatTokens($result);
}

sub renderTimeline {
my ($this, $timeline, $params) = @_;

Expand Down Expand Up @@ -429,7 +497,7 @@ sub renderTimeline {
$line =~ s/\$orig_name/$origUser->name/ge;
$line =~ s/\$id/$item->{id}/g;

$line = Encode::encode($Foswiki::cfg{Site}{CharSet}, $line);
$line = Encode::encode($Foswiki::cfg{Site}{CharSet}, $line) unless $Foswiki::UNICODE;
push @results, $line;
}
return '' unless @results;
Expand Down

0 comments on commit ba8b713

Please sign in to comment.