mark: A photo of Mark kneeling on top of the Taal Volcano in the Philippines. It was a long hike. (Default)
Mark Smith ([staff profile] mark) wrote in [site community profile] changelog2009-09-09 11:07 pm

[dw-free] Support fdata.bml

[commit: http://hg.dwscoalition.org/dw-free/rev/89878cce1e71]

http://bugs.dwscoalition.org/show_bug.cgi?id=857

Add new output so people can get edges/relationship data for an account.
This is available at http://someuser.site.com/edges once it's live. The
output is JSON, which is what we're moving towards to.

Patch by [personal profile] foxfirefey.

Files modified:
  • cgi-bin/Apache/LiveJournal.pm
  • cgi-bin/ljlib.pl
  • htdocs/data/edges.bml
--------------------------------------------------------------------------------
diff -r e75c1481712e -r 89878cce1e71 cgi-bin/Apache/LiveJournal.pm
--- a/cgi-bin/Apache/LiveJournal.pm	Wed Sep 09 11:39:43 2009 -0500
+++ b/cgi-bin/Apache/LiveJournal.pm	Wed Sep 09 23:07:11 2009 +0000
@@ -615,6 +615,11 @@ sub trans
         if ( $opts->{mode} eq "icons" ) {
             $r->notes->{_journal} = $opts->{user};
             return $bml_handler->( "$LJ::HOME/htdocs/allpics.bml" );
+        }
+
+        if ( $opts->{mode} eq "edges" ) {
+            $r->notes->{_journal} = $opts->{user};
+            return $bml_handler->( "$LJ::HOME/htdocs/data/edges.bml" );
         }
 
         %RQ = %$opts;
diff -r e75c1481712e -r 89878cce1e71 cgi-bin/ljlib.pl
--- a/cgi-bin/ljlib.pl	Wed Sep 09 11:39:43 2009 -0500
+++ b/cgi-bin/ljlib.pl	Wed Sep 09 23:07:11 2009 +0000
@@ -181,7 +181,8 @@ LJ::MemCache::init();
                  },
                  "icons" => {
                     "des" => "Icons",
-                 }
+                 },
+                 edges => { des => 'Edges', },
                  );
 
 ## we want to set this right away, so when we get a HUP signal later
diff -r e75c1481712e -r 89878cce1e71 htdocs/data/edges.bml
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/htdocs/data/edges.bml	Wed Sep 09 23:07:11 2009 +0000
@@ -0,0 +1,91 @@
+<?_code
+{
+    #
+    # edges.bml
+    #
+    # Outputs an account's edges in JSON format.
+    #
+    # Authors:
+    #      Thomas Thurman <thomas@thurman.org.uk>
+    #      foxfirefey <skittisheclipse@gmail.com>
+    #      Mark Smith <mark@dreamwidth.org>
+    #
+    # Copyright (c) 2009 by Dreamwidth Studios, LLC.
+    #
+    # This program is free software; you may redistribute it and/or modify it under
+    # the same terms as Perl itself. For a copy of the license, please reference
+    # 'perldoc perlartistic' or 'perldoc perlgpl'.
+    #
+
+    use strict;
+    use warnings;
+    use vars qw/ %GET /;
+    use JSON;
+
+    BML::set_content_type( "application/json" );
+
+    # Outputs an error message
+    my $error_in_json = sub {
+       my ( $code, $message ) = @_;
+       BML::set_status( $code );
+       BML::noparse( objToJson( { error => $message } ) );
+    };
+
+    # make sure we get a user
+    eval { $GET{user} ||= DW::Request->get->note( '_journal' ); };
+
+    # Load the account or error
+    return $error_in_json->(404, 'Need account name as user parameter') unless $GET{user};
+    my $u = LJ::load_user_or_identity( $GET{user} )
+        or return $error_in_json->( 404, "invalid account");
+
+    # Check for other conditions
+    return $error_in_json->( 410, 'expunged' ) if $u->is_expunged;
+    return $error_in_json->( 403, 'suspended' ) if $u->is_suspended;
+    return $error_in_json->( 404, 'deleted' ) if $u->is_deleted;
+
+    # deal with renamed accounts
+    my $renamed_u = $u->get_renamed_user;
+    return $renamed_u->journal_base . '/edges'
+        unless $renamed_u && $u->equals( $renamed_u );
+
+    # Load appropriate usernames for different accounts
+    my $us;
+
+    if ( $u->is_individual ) {
+        $us = LJ::load_userids( $u->trusted_userids, $u->watched_userids, $u->trusted_by_userids, $u->watched_by_userids, $u->member_of_userids );
+    } elsif ( $u->is_community ) {
+        $us = LJ::load_userids( $u->maintainer_userids, $u->moderator_userids, $u->member_userids, $u->watched_by_userids );
+    } elsif ( $u->is_syndicated ) {
+        $us = LJ::load_userids( $u->watched_by_userids );
+    }
+
+    # Contruct the JSON response hash
+    my $response = {};
+
+    # all accounts have this
+    $response->{account_type} = $u->journaltype;
+    $response->{watched_by} = [ $u->watched_by_userids ];
+
+    # different individual and community edges
+    if ( $u->is_individual ) {
+        $response->{trusted} = [ $u->trusted_userids ];
+        $response->{watched} = [ $u->trusted_userids ];
+        $response->{trusted_by} = [ $u->trusted_userids ];
+        $response->{member_of} = [ $u->trusted_userids ];
+
+    } elsif ( $u->is_community ) {
+        $response->{maintainer} = [ $u->maintainer_userids ];
+        $response->{moderator} = [ $u->moderator_userids ];
+        $response->{member} = [ $u->member_userids ];
+    }
+
+    # now dump information about the users we loaded
+    $response->{accounts} = {
+        map { $_ => { name => $us->{$_}->display_name, type => $us->{$_}->journaltype } } keys %$us
+    };
+
+    # Output to BML
+    return BML::noparse( objToJson( $response ) );
+}
+_code?>
--------------------------------------------------------------------------------
voxbaryton: Picture of Old-Fashioned Bass Clef (Default)

[personal profile] voxbaryton 2009-09-10 02:26 pm (UTC)(link)
I'm really quite happy yall decided to use JSON. That'll make client development so much easier. ;)