source: perl/modules/Facebook/lib/BarnOwl/Module/Facebook/Handle.pm @ 2f6c263

Last change on this file since 2f6c263 was 2f6c263, checked in by Edward Z. Yang <ezyang@mit.edu>, 10 years ago
Improve docs, error handling and refactor. Signed-off-by: Edward Z. Yang <ezyang@mit.edu>
  • Property mode set to 100644
File size: 15.4 KB
Line 
1use warnings;
2use strict;
3
4=head1 NAME
5
6BarnOwl::Module::Facebook::Handle
7
8=head1 DESCRIPTION
9
10Contains everything needed to send and receive messages from Facebook
11
12=cut
13
14package BarnOwl::Module::Facebook::Handle;
15
16use Facebook::Graph;
17
18use List::Util qw(reduce);
19
20eval { require Lingua::EN::Keywords; };
21if ($@) {
22    *keywords = sub {
23        # stupidly pick the longest one, and only return one.
24        my $sentence = shift;
25        $sentence =~ s/[[:punct:]]+/ /g;
26        my @words = split(' ', lc($sentence));
27        return () unless @words;
28        return (reduce{ length($a) > length($b) ? $a : $b } @words,);
29    };
30} else {
31    *keywords = \&Lingua::EN::Keywords::keywords;
32}
33
34use JSON;
35use Date::Parse;
36use POSIX;
37use Ouch;
38
39use Scalar::Util qw(weaken);
40
41use BarnOwl;
42use BarnOwl::Message::Facebook;
43
44our $app_id = 235537266461636; # for application 'barnowl'
45
46# Unfortunately, Facebook does not offer a comment stream, in the same
47# way we can get a post stream using the news feed.  This makes it a bit
48# difficult to de-duplicate comments we have already seen.  We use a
49# simple heuristic to fix this: we check if the comment's time is dated
50# from before our last update, and don't re-post if it's dated before.
51# Be somewhat forgiving, since it's better to duplicate a post than to
52# drop one.  Furthermore, we must use Facebook's idea of time, since the
53# server BarnOwl is running on may be desynchronized.  So we need to
54# utilize Facebook's idea of time, not ours.  We do this by looking at
55# all of the timestamps we see while processing an update, and take the
56# latest one and increment it by one second.
57#
58# What properties do we get with this setup?
59#
60#   - We get comment updates only for the latest N posts on a news feed.
61#   Any later ones, you have to use Facebook's usual mechanisms (e.g.
62#   email notifications).
63#
64#   - Processing a poll is relatively expensive, since we have to
65#   iterate over N new posts.  It might be worthwhile polling for new
66#   comments less frequently than polling for new posts.
67
68sub new {
69    my $class = shift;
70    my $cfg = shift;
71
72    my $self = {
73        'cfg'  => $cfg,
74        'facebook' => undef,
75
76        # Ideally this should be done using Facebook realtime updates,
77        # but we can't assume that the BarnOwl lives on a publically
78        # addressable server (XXX maybe we can setup an option for this.)
79        'friend_timer' => undef,
80
81        # Initialized with our 'time', but will be synced to Facebook
82        # soon enough. (Subtractive amount is just to preseed with some
83        # values.) XXX Remove subtraction altogether.
84        'last_poll' => time - 60 * 60,
85        'timer' => undef,
86
87        # Message polling not implemented yet
88        #'last_message_poll' => time,
89        #'message_timer' => undef,
90
91        # yeah yeah, inelegant, I know.  You can try using
92        # $fb->authorize, but at time of writing (1.0300) they didn't support
93        # the response_type parameter.
94        # 'login_url' => 'https://www.facebook.com/dialog/oauth?client_id=235537266461636&scope=read_stream,read_mailbox,publish_stream,offline_access,read_friendlists,rsvp_event,user_events&redirect_uri=http://www.facebook.com/connect/login_success.html&response_type=token',
95        # minified to fit in most terminal windows.
96        # Be careful about updating these values, since BarnOwl will not
97        # notice that it is missing necessary permissions until it
98        # attempt to perform an operation which fails due to lack of
99        # permissions.
100        'login_url' => 'http://goo.gl/rcM9s',
101
102        'logged_in' => 0,
103
104        # would need another hash for topic de-dup
105        'topics' => {},
106
107        # deduplicated map of names to user ids
108        'friends' => {},
109    };
110
111    bless($self, $class);
112
113    $self->{facebook} = Facebook::Graph->new( app_id => $app_id );
114    $self->facebook_do_auth;
115
116    return $self;
117}
118
119=head2 sleep N
120
121Stop polling Facebook for N seconds.
122
123=cut
124
125sub sleep {
126    my $self  = shift;
127    my $delay = shift;
128
129    # prevent reference cycles
130    my $weak = $self;
131    weaken($weak);
132
133    # Stop any existing timers.
134    if (defined $self->{friend_timer}) {
135        $self->{friend_timer}->stop;
136        $self->{friend_timer} = undef;
137    }
138    if (defined $self->{timer}) {
139        $self->{timer}->stop;
140        $self->{timer} = undef;
141    }
142    if (defined $self->{message_timer}) {
143        # XXX doesn't do anything right now
144        $self->{message_timer}->stop;
145        $self->{message_timer} = undef;
146    }
147
148    $self->{friend_timer} = BarnOwl::Timer->new({
149        name     => "Facebook friend poll",
150        after    => $delay,
151        interval => 60 * 60 * 24,
152        cb       => sub { $weak->poll_friends if $weak }
153       });
154    $self->{timer} = BarnOwl::Timer->new({
155        name     => "Facebook poll",
156        after    => $delay,
157        interval => 90,
158        cb       => sub { $weak->poll_facebook if $weak }
159       });
160    # XXX implement message polling
161}
162
163sub check_result {
164    my $self = shift;
165    if (kiss 400) {
166        # Ugh, no easy way of accessing the JSON error type
167        # which is OAuthException.
168        $self->{logged_in} = 0;
169        $self->facebook_do_auth;
170        return 0;
171    } elsif (hug) {
172        my $code = $@->code;
173        warn "Poll failed with $code: $@";
174        return 0;
175    }
176    return 1;
177}
178
179sub poll_friends {
180    my $self = shift;
181
182    return unless BarnOwl::getvar('facebook:poll') eq 'on';
183    return unless $self->{logged_in};
184
185    my $friends = eval { $self->{facebook}->fetch('me/friends'); };
186    return unless $self->check_result;
187
188    $self->{friends} = {};
189
190    for my $friend ( @{$friends->{data}} ) {
191        if (defined $self->{friends}{$friend->{name}}) {
192            # XXX We should try a little harder here, rather than just
193            # tacking on a number.  Ideally, we should be able to
194            # calculate some extra piece of information that the user
195            # needs to disambiguate between the two users.  An old
196            # version of Facebook used to disambiguate with your primary
197            # network (so you might have Edward Yang (MIT) and Edward
198            # Yang (Cambridge), the idea being that users in the same
199            # network would probably have already disambiguated
200            # themselves with middle names or nicknames.  We no longer
201            # get network information, since Facebook axed that
202            # information, but the Education/Work fields may still be
203            # a reasonable approximation (but which one do you pick?!
204            # The most recent one.)  Since getting this information
205            # involves extra queries, there are also caching and
206            # efficiency concerns (though hopefully you don't have too
207            # many friends with the same name).  Furthermore, accessing
208            # this information requires a pretty hefty extra set of
209            # permissions requests, which we don't currently ask for.
210            #   It may just be better to let users specify custom
211            # aliases for Facebook users, which are added into this
212            # hash.  See also username support.
213            warn "Duplicate friend name " . $friend->{name};
214            my $name = $friend->{name};
215            my $i = 2;
216            while (defined $self->{friends}{$friend->{name} . ' ' . $i}) { $i++; }
217            $self->{friends}{$friend->{name} . ' ' . $i} = $friend->{id};
218        } else {
219            $self->{friends}{$friend->{name}} = $friend->{id};
220        }
221    }
222
223    # XXX We should also have support for usernames, and not just real
224    # names. However, since this data is not returned by the friends
225    # query, it would require a rather expensive set of queries. We
226    # might try to preserve old data, but all-in-all it's a bit
227    # complicated.  One possible way of fixing this is to construct a
228    # custom FQL query that joins the friends table and the users table.
229}
230
231sub poll_facebook {
232    my $self = shift;
233
234    return unless BarnOwl::getvar('facebook:poll') eq 'on';
235    return unless $self->{logged_in};
236
237    # XXX Oh no! This blocks the user interface.  Not good.
238    # Ideally, we should have some worker thread for polling facebook.
239    # But BarnOwl is probably not thread-safe >_<
240
241    my $old_topics = $self->{topics};
242    $self->{topics} = {};
243
244    my $updates = eval {
245        $self->{facebook}
246             ->query
247             ->from("my_news")
248             # Not using this, because we want to pick up comment
249             # updates. We need to manually de-duplicate, though.
250             # ->where_since( "@" . $self->{last_poll} )
251             # Facebook doesn't actually give us that many results.
252             # But it can't hurt to ask!
253             ->limit_results( 200 )
254             ->request
255             ->as_hashref
256    };
257    return unless $self->check_result;
258
259    my $new_last_poll = $self->{last_poll};
260    for my $post ( reverse @{$updates->{data}} ) {
261        # No app invites, thanks! (XXX make configurable)
262        if ($post->{type} eq 'link' && $post->{application}) {
263            next;
264        }
265
266        # XXX Filtering out interest groups for now
267        # A more reasonable strategy may be to show their
268        # posts, but not the comments.
269        if (defined $post->{from}{category}) {
270            next;
271        }
272
273        # There can be multiple recipients! Strange! Pick the first one.
274        my $name    = $post->{to}{data}[0]{name} || $post->{from}{name};
275        my $name_id = $post->{to}{data}[0]{id} || $post->{from}{id};
276        my $post_id  = $post->{id};
277
278        my $topic;
279        if (defined $old_topics->{$post_id}) {
280            $topic = $old_topics->{$post_id};
281            $self->{topics}->{$post_id} = $topic;
282        } else {
283            my @keywords = keywords($post->{name} || $post->{message});
284            $topic = $keywords[0] || 'personal';
285            $topic =~ s/ /-/g;
286            $self->{topics}->{$post_id} = $topic;
287        }
288
289        # Only handle post if it's new
290        my $created_time = str2time($post->{created_time});
291        if ($created_time >= $self->{last_poll}) {
292            # XXX indexing is fragile
293            my $msg = BarnOwl::Message->new(
294                type      => 'Facebook',
295                sender    => $post->{from}{name},
296                sender_id => $post->{from}{id},
297                name      => $name,
298                name_id   => $name_id,
299                direction => 'in',
300                body      => $self->format_body($post),
301                post_id   => $post_id,
302                topic     => $topic,
303                time      => asctime(localtime $created_time),
304                # XXX The intent is to get the 'Comment' link, which also
305                # serves as a canonical link to the post.  The {name}
306                # field should equal 'Comment'.
307                zsig      => $post->{actions}[0]{link},
308               );
309            BarnOwl::queue_message($msg);
310        }
311
312        # This will interleave times (they'll all be organized by parent
313        # post), but since we don't expect too many updates between
314        # polls this is pretty acceptable.
315        my $updated_time = str2time($post->{updated_time});
316        if ($updated_time >= $self->{last_poll} && defined $post->{comments}{data}) {
317            for my $comment ( @{$post->{comments}{data}} ) {
318                my $comment_time = str2time($comment->{created_time});
319                if ($comment_time < $self->{last_poll}) {
320                    next;
321                }
322                my $msg = BarnOwl::Message->new(
323                    type      => 'Facebook',
324                    sender    => $comment->{from}{name},
325                    sender_id => $comment->{from}{id},
326                    name      => $name,
327                    name_id   => $name_id,
328                    direction => 'in',
329                    body      => $comment->{message},
330                    post_id   => $post_id,
331                    topic     => $topic,
332                    time      => asctime(localtime $comment_time),
333                   );
334                BarnOwl::queue_message($msg);
335            }
336        }
337        if ($updated_time + 1 > $new_last_poll) {
338            $new_last_poll = $updated_time + 1;
339        }
340    }
341    # old_topics gets GC'd
342
343    $self->{last_poll} = $new_last_poll;
344}
345
346sub format_body {
347    my $self = shift;
348
349    my $post = shift;
350
351    # XXX implement optional URL minification
352    if ($post->{type} eq 'status') {
353        return $post->{message};
354    } elsif ($post->{type} eq 'link' || $post->{type} eq 'video' || $post->{type} eq 'photo') {
355        return $post->{name}
356          . ($post->{caption} ? " (" . $post->{caption} . ")\n" : "\n")
357          . $post->{link}
358          . ($post->{description} ? "\n\n" . $post->{description} : "")
359          . ($post->{message} ? "\n\n" . $post->{message} : "");
360    } else {
361        return "(unknown post type " . $post->{type} . ")";
362    }
363}
364
365# Invariant: we don't become logged out between entering text field
366# and actually processing the request.  XXX I don't think this actually
367# holds, but such a case would rarely happen.
368
369sub facebook {
370    my $self = shift;
371
372    my $user = shift;
373    my $msg = shift;
374
375    if (defined $user) {
376        $user = $self->{friends}{$user} || $user;
377        eval { $self->{facebook}->add_post( $user )->set_message( $msg )->publish; };
378        return unless $self->check_result;
379    } else {
380        eval { $self->{facebook}->add_post->set_message( $msg )->publish; };
381        return unless $self->check_result;
382    }
383    $self->sleep(0);
384}
385
386sub facebook_comment {
387    my $self = shift;
388
389    my $post_id = shift;
390    my $msg = shift;
391
392    eval { $self->{facebook}->add_comment( $post_id )->set_message( $msg )->publish; };
393    return unless $self->check_result;
394    $self->sleep(0);
395}
396
397sub facebook_auth {
398    my $self = shift;
399
400    my $url = shift;
401
402    # http://www.facebook.com/connect/login_success.html#access_token=TOKEN&expires_in=0
403    $url =~ /access_token=([^&]+)/; # XXX Ew regex
404
405    if (!defined $1) {
406        BarnOwl::message("Invalid URL.");
407        return;
408    }
409
410    $self->{cfg}->{token} = $1;
411    if ($self->facebook_do_auth) {
412        my $raw_cfg = to_json($self->{cfg});
413        BarnOwl::admin_message('Facebook', "Add this as the contents of your ~/.owl/facebook file:\n$raw_cfg");
414    }
415}
416
417sub facebook_do_auth {
418    my $self = shift;
419    if ( ! defined $self->{cfg}->{token} ) {
420        BarnOwl::admin_message('Facebook', "Login to Facebook at ".$self->{login_url}
421            . "\nand run command ':facebook-auth URL' with the URL you are redirected to."
422            . "\n\nWhat does Barnowl use these permissions for?  As a desktop"
423            . "\nmessaging application, we need persistent read/write access to your"
424            . "\nnews feed and your inbox.  Other permissions are for pending"
425            . "\nfeatures: we intend on adding support for event streaming, RSVP,"
426            . "\nand BarnOwl filtering on friend lists."
427        );
428        return 0;
429    }
430    $self->{facebook}->access_token($self->{cfg}->{token});
431    # Do a quick check to see if things are working
432    my $result = eval { $self->{facebook}->query()->find('me')->select_fields('name')->request->as_hashref; };
433    if ($@) {
434        BarnOwl::admin_message('Facebook', "Failed to authenticate! Login to Facebook at ".$self->{login_url}
435            . "\nand run command ':facebook-auth URL' with the URL you are redirected to.");
436        return 0;
437    } else {
438        my $name = $result->{'name'};
439        BarnOwl::admin_message('Facebook', "Successfully logged in to Facebook as $name!");
440        $self->{logged_in} = 1;
441        $self->sleep(0); # start polling
442        return 1;
443    }
444}
445
4461;
Note: See TracBrowser for help on using the repository browser.