Ignore:
Timestamp:
Sep 20, 2011, 11:15:31 PM (13 years ago)
Author:
Edward Z. Yang <ezyang@mit.edu>
Branches:
master, release-1.10, release-1.9
Children:
65c2b3c
Parents:
eb20731
git-author:
Edward Z. Yang <ezyang@mit.edu> (06/22/11 18:30:55)
git-committer:
Edward Z. Yang <ezyang@mit.edu> (09/20/11 23:15:31)
Message:
Add autocomplete and wall-posting support.

Signed-off-by: Edward Z. Yang <ezyang@mit.edu>
Location:
perl/modules/Facebook/lib/BarnOwl/Module
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • perl/modules/Facebook/lib/BarnOwl/Module/Facebook.pm

    reb497a9 r8b62088  
    123123sub cmd_facebook {
    124124    my $cmd = shift;
    125     BarnOwl::start_edit_win("What's on your mind?", sub{ facebook(shift) });
    126     # User prompt for other person's wall is "Write something..." which
    127     # we will ostensibly check for soon.
     125    my $user = shift;
     126
     127    BarnOwl::start_edit_win(
     128        defined $user ? "Write something to $user..." : "What's on your mind?",
     129        sub{ facebook($user, shift) }
     130    );
    128131}
    129132
     
    156159BarnOwl::filter(qw{facebook type ^facebook$});
    157160
     161# Autocompletion support
     162
     163sub complete_user { return keys %{$facebook_handle->{friends}}; }
     164BarnOwl::Completion::register_completer(facebook => sub { complete_user(@_) });
     165
    1581661;
  • perl/modules/Facebook/lib/BarnOwl/Module/Facebook/Handle.pm

    reb20731 r8b62088  
    8080        'facebook' => undef,
    8181
     82        # Ideally this should be done using Facebook realtime updates,
     83        # but we can't assume that the BarnOwl lives on a publically
     84        # addressable server (XXX maybe we can setup an option for this.)
     85        'last_friend_poll' => 0,
     86        'friend_timer' => undef,
     87
    8288        # Initialized with our 'time', but will be synced to Facebook
    83         # soon enough.
     89        # soon enough. (Subtractive amount is just to preseed with some
     90        # values.)
    8491        'last_poll' => time - 60 * 60 * 24 * 2,
    8592        'timer' => undef,
     
    100107        # would need another hash for topic de-dup
    101108        'topics' => {},
     109
     110        # deduplicated map of names to user ids
     111        'friends' => {},
    102112    };
    103113
     
    125135
    126136    # Stop any existing timers.
     137    if (defined $self->{friend_timer}) {
     138        $self->{friend_timer}->stop;
     139        $self->{friend_timer} = undef;
     140    }
    127141    if (defined $self->{timer}) {
    128142        $self->{timer}->stop;
     
    135149    }
    136150
     151    $self->{friend_timer} = BarnOwl::Timer->new({
     152        name     => "Facebook friend poll",
     153        after    => $delay,
     154        interval => 60 * 60 * 24,
     155        cb       => sub { $weak->poll_friends if $weak }
     156       });
    137157    $self->{timer} = BarnOwl::Timer->new({
    138158        name     => "Facebook poll",
     
    142162       });
    143163    # XXX implement message polling
     164}
     165
     166sub poll_friends {
     167    my $self = shift;
     168
     169    return unless BarnOwl::getvar('facebook:poll') eq 'on';
     170    return unless $self->{logged_in};
     171
     172    my $friends = eval { $self->{facebook}->fetch('me/friends'); };
     173    if ($@) {
     174        warn "Poll failed $@";
     175        return;
     176    }
     177
     178    $self->{last_friend_poll} = time;
     179    $self->{friends} = {};
     180
     181    for my $friend ( @{$friends->{data}} ) {
     182        if (defined $self->{friends}{$friend->{name}}) {
     183            # XXX We should try a little harder here, rather than just
     184            # tacking on a number.  Ideally, we should be able to
     185            # calculate some extra piece of information that the user
     186            # needs to disambiguate between the two users.  An old
     187            # version of Facebook used to disambiguate with your primary
     188            # network (so you might have Edward Yang (MIT) and Edward
     189            # Yang (Cambridge), the idea being that users in the same
     190            # network would probably have already disambiguated
     191            # themselves with middle names or nicknames.  We no longer
     192            # get network information, since Facebook axed that
     193            # information, but the Education/Work fields may still be
     194            # a reasonable approximation (but which one do you pick?!
     195            # The most recent one.)  Since getting this information
     196            # involves extra queries, there are also caching and
     197            # efficiency concerns.
     198            #   We may want a facility for users to specify custom
     199            # aliases for Facebook users, which are added into this
     200            # hash.  See also username support.
     201            warn "Duplicate friend name " . $friend->{name};
     202            my $name = $friend->{name};
     203            my $i = 2;
     204            while (defined $self->{friends}{$friend->{name} . ' ' . $i}) { $i++; }
     205            $self->{friends}{$friend->{name} . ' ' . $i} = $friend->{id};
     206        } else {
     207            $self->{friends}{$friend->{name}} = $friend->{id};
     208        }
     209    }
     210
     211    # XXX We should also have support for usernames, and not just real
     212    # names. However, since this data is not returned by the friends
     213    # query, it would require a rather expensive set of queries. We
     214    # might try to preserve old data, but all-in-all it's a bit
     215    # complicated, so we don't bother.
    144216}
    145217
     
    283355    my $self = shift;
    284356
     357    my $user = shift;
    285358    my $msg = shift;
    286     my $reply_to = shift;
    287359
    288360    if (!defined $self->{facebook} || !$self->{logged_in}) {
     
    290362        return;
    291363    }
    292     $self->{facebook}->add_post->set_message( $msg )->publish;
     364    if (defined $user) {
     365        $user = $self->{friends}{$user} || $user;
     366        $self->{facebook}->add_post( $user )->set_message( $msg )->publish;
     367    } else {
     368        $self->{facebook}->add_post->set_message( $msg )->publish;
     369    }
    293370    $self->sleep(0);
    294371}
Note: See TracChangeset for help on using the changeset viewer.