Ignore:
Timestamp:
Jun 22, 2011, 9:56:47 PM (10 years ago)
Author:
Edward Z. Yang <ezyang@mit.edu>
Children:
2c4c4c4
Parents:
348cffc
git-author:
Edward Z. Yang <ezyang@mit.edu> (06/22/11 18:30:55)
git-committer:
Edward Z. Yang <ezyang@mit.edu> (06/22/11 21:56:47)
Message:
Add autocomplete and wall-posting support.

Signed-off-by: Edward Z. Yang <ezyang@mit.edu>
File:
1 edited

Legend:

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

    r348cffc r68fa539  
    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.