]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - actions/facebookhome.php
extract log-line formatting to its own function
[quix0rs-gnu-social.git] / actions / facebookhome.php
index 3696df90ac534b962e803de50e042be78e6fbfc3..34989c9786aa9814f9396d49bcba0db3934e9af3 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /*
  * Laconica - a distributed open-source microblogging tool
- * Copyright (C) 2008, Controlez-Vous, Inc.
+ * Copyright (C) 2008, 2009, Control Yourself, Inc.
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU Affero General Public License as published by
@@ -24,60 +24,76 @@ require_once INSTALLDIR.'/lib/facebookaction.php';
 class FacebookhomeAction extends FacebookAction
 {
 
+    var $page = null;
+
+    function prepare($argarray)
+    {
+        parent::prepare($argarray);
+
+        $this->page = $this->trimmed('page');
+
+        if (!$this->page) {
+            $this->page = 1;
+        }
+
+        return true;
+    }
+
     function handle($args)
     {
         parent::handle($args);
 
-        $facebook = get_facebook();
-        $fbuid = $facebook->require_login();
-        // Check to see whether there's already a Facebook link for this user
-        $flink = Foreign_link::getByForeignID($fbuid, FACEBOOK_SERVICE);
-
         // If the user has opted not to initially allow the app to have
         // Facebook status update permission, store that preference. Only
         // promt the user the first time she uses the app
-        if ($this->arg('skip')) {
-            $facebook->api_client->data_setUserPreference(
+        if ($this->arg('skip') || $args['fb_sig_request_method'] == 'GET') {
+            $this->facebook->api_client->data_setUserPreference(
                 FACEBOOK_PROMPTED_UPDATE_PREF, 'true');
         }
 
-        if ($flink) {
-
-            if ($_POST['submit'] == 'Send') {       
-                $this->saveNewNotice($flink);
-                return;
-            }
+        if ($this->flink) {
 
-            $user = $flink->getUser();
-            common_set_user($user);
+            $this->user = $this->flink->getUser();
 
             // If this is the first time the user has started the app
             // prompt for Facebook status update permission
-            if (!$facebook->api_client->users_hasAppPermission('status_update')) {
-
-                if ($facebook->api_client->data_getUserPreference(
-                        FACEBOOK_PROMPTED_UPDATE_PREF) != 'true') {
-                    $this->getUpdatePermission();
-                    return;
-                }
-            }
-
-            // Use is authenticated and has already been prompted once for
+            if (!$this->facebook->api_client->users_hasAppPermission('status_update')) {
+
+                 if ($this->facebook->api_client->data_getUserPreference(
+                    FACEBOOK_PROMPTED_UPDATE_PREF) != 'true') {
+                        $this->getUpdatePermission();
+                        return;
+                 }
+             }
+
+             // Make sure the user's profile box has the lastest notice
+             $notice = $this->user->getCurrentNotice();
+             if ($notice) {
+                 $this->updateProfileBox($notice);
+             }
+
+             if ($this->arg('status_submit') == 'Send') {
+                $this->saveNewNotice();
+             }
+
+            // User is authenticated and has already been prompted once for
             // Facebook status update permission? Then show the main page
             // of the app
-            $this->showHome($flink, null);
+            $this->showPage();
 
         } else {
 
             // User hasn't authenticated yet, prompt for creds
-            $this->login($fbuid);
+            $this->login();
         }
 
     }
 
-    function login($fbuid)
+    function login()
     {
+
+        $this->showStylesheets();
+
         $nickname = common_canonical_nickname($this->trimmed('nickname'));
         $password = $this->arg('password');
 
@@ -95,17 +111,16 @@ class FacebookhomeAction extends FacebookAction
 
                 $flink = DB_DataObject::factory('foreign_link');
                 $flink->user_id = $user->id;
-                $flink->foreign_id = $fbuid;
+                $flink->foreign_id = $this->fbuid;
                 $flink->service = FACEBOOK_SERVICE;
                 $flink->created = common_sql_now();
-                $flink->set_flags(true, false, false);
+                $flink->set_flags(true, false, false, false);
 
                 $flink_id = $flink->insert();
 
                 // XXX: Do some error handling here
 
                 $this->setDefaults();
-                //$this->showHome($flink, _('You can now use Identi.ca from Facebook!'));
 
                 $this->getUpdatePermission();
                 return;
@@ -116,157 +131,145 @@ class FacebookhomeAction extends FacebookAction
         }
 
         $this->showLoginForm($msg);
+        $this->showFooter();
 
     }
 
     function setDefaults()
     {
-        $facebook = get_facebook();
-
-        // A default prefix string for notices
-        $facebook->api_client->data_setUserPreference(
-            FACEBOOK_NOTICE_PREFIX, 'dented: ');
-        $facebook->api_client->data_setUserPreference(
+        $this->facebook->api_client->data_setUserPreference(
             FACEBOOK_PROMPTED_UPDATE_PREF, 'false');
     }
 
-    function showHome($flink, $msg)
+    function showNoticeForm()
     {
+        $post_action = "$this->app_uri/index.php";
 
-        $facebook = get_facebook();
-        $fbuid = $facebook->require_login();
-
-        $user = $flink->getUser();
-
-        $notice = $user->getCurrentNotice();
-        update_profile_box($facebook, $fbuid, $user, $notice);
-
-        $this->showHeader($msg);      
-        $this->showNoticeForm($user);
-        $this->showNav('Home');
-        
-        echo $this->showNotices($user);
-
-        $this->showFooter();
+        $notice_form = new FacebookNoticeForm($this, $post_action, null,
+            $post_action, $this->user);
+        $notice_form->show();
     }
 
-    function showNotices($user)
+    function title()
     {
-
-        $page = $this->trimmed('page');
-        if (!$page) {
-            $page = 1;
+        if ($this->page > 1) {
+            return sprintf(_("%s and friends, page %d"), $this->user->nickname, $this->page);
+        } else {
+            return sprintf(_("%s and friends"), $this->user->nickname);
         }
+    }
 
-        $notice = $user->noticesWithFriends(($page-1)*NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1);
+    function showContent()
+    {
+        $notice = $this->user->noticeInbox(($this->page-1) * NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1);
 
-        $cnt = $this->showNoticeList($notice);
+        $nl = new NoticeList($notice, $this);
 
-        facebookPagination($page > 1, $cnt > NOTICES_PER_PAGE,
-            $page, 'all', array('nickname' => $user->nickname));
+        $cnt = $nl->show();
+
+        $this->pagination($this->page > 1, $cnt > NOTICES_PER_PAGE,
+                          $this->page, 'index.php', array('nickname' => $this->user->nickname));
     }
 
     function showNoticeList($notice)
     {
-        $nl = new FacebookNoticeList($notice);
+
+        $nl = new NoticeList($notice, $this);
         return $nl->show();
     }
 
     function getUpdatePermission() {
 
-        $facebook = get_facebook();
-        $fbuid = $facebook->require_login();
-
-        startFBML();
-
         $this->showStylesheets();
-        $this->showScripts();
-
-        $this->showLogo();
-
-        common_element_start('div', array('class' => 'content'));
 
-        // Figure what the URL of our app is.
-        $app_props = $facebook->api_client->Admin_getAppProperties(
-                array('canvas_name', 'application_name'));
-        $app_url = 'http://apps.facebook.com/' . $app_props['canvas_name'] . '/index.php';
-        $app_name = $app_props['application_name'];
+        $this->elementStart('div', array('class' => 'facebook_guide'));
 
         $instructions = sprintf(_('If you would like the %s app to automatically update ' .
             'your Facebook status with your latest notice, you need ' .
-            'to give it permission.'), $app_name);
+            'to give it permission.'), $this->app_name);
 
-        common_element_start('p');
-        common_element('span', array('id' => 'permissions_notice'), $instructions);
-        common_element_end('p');
+        $this->elementStart('p');
+        $this->element('span', array('id' => 'permissions_notice'), $instructions);
+        $this->elementEnd('p');
 
-        common_element_start('form', array('method' => 'post',
-                                           'action' => $app_url,
+        $this->elementStart('form', array('method' => 'post',
+                                           'action' => "index.php",
                                            'id' => 'facebook-skip-permissions'));
 
-        common_element_start('ul', array('id' => 'fb-permissions-list'));
-        common_element_start('li', array('id' => 'fb-permissions-item'));
-        common_element_start('fb:prompt-permission', array('perms' => 'status_update',
-            'next_fbjs' => 'document.setLocation(\'' . $app_url . '\')'));
-        common_element('span', array('class' => 'facebook-button'),
-            _('Allow Identi.ca to update my Facebook status'));
-        common_element_end('fb:prompt-permission');
-        common_element_end('li');
+        $this->elementStart('ul', array('id' => 'fb-permissions-list'));
+        $this->elementStart('li', array('id' => 'fb-permissions-item'));
 
-        common_element_start('li', array('id' => 'fb-permissions-item'));
-        common_submit('skip', _('Skip'));
-        common_element_end('li');
-        common_element_end('ul');
+        $next = urlencode("$this->app_uri/index.php");
+        $api_key = common_config('facebook', 'apikey');
 
-        common_element_end('form');
-        common_element_end('div');
-
-        common_end_xml();
-
-    }
-    
-    function saveNewNotice($flink)
-    {
-        
-        $user = $flink->getUser();
+        $auth_url = 'http://www.facebook.com/authorize.php?api_key=' .
+            $api_key . '&v=1.0&ext_perm=status_update&next=' . $next .
+            '&next_cancel=' . $next . '&submit=skip';
 
-        $content = $_POST['status_textarea']; 
+        $this->elementStart('span', array('class' => 'facebook-button'));
+        $this->element('a', array('href' => $auth_url),
+            sprintf(_('Okay, do it!'), $this->app_name));
+        $this->elementEnd('span');
 
-        if (!$content) {
-            $this->showHome($flink, _('No content!'));
-            return;
-        } else {
-            $content_shortened = common_shorten_links($content);
+        $this->elementEnd('li');
 
-            if (mb_strlen($content_shortened) > 140) {
-                common_debug("Content = '$content_shortened'", __FILE__);
-                common_debug("mb_strlen(\$content) = " . mb_strlen($content_shortened), __FILE__);
-                $this->showHome($flink, _('That\'s too long. Max notice size is 140 chars.'));
-                return;
-            }
-        }
+        $this->elementStart('li', array('id' => 'fb-permissions-item'));
+        $this->submit('skip', _('Skip'));
+        $this->elementEnd('li');
+        $this->elementEnd('ul');
 
-        $inter = new CommandInterpreter();
+        $this->elementEnd('form');
+        $this->elementEnd('div');
 
-        $cmd = $inter->handle_command($user, $content_shortened);
+    }
 
-        if ($cmd) {
-            $cmd->execute(new WebChannel());
-            return;
-        }
+    /**
+     * Generate pagination links
+     *
+     * @param boolean $have_before is there something before?
+     * @param boolean $have_after  is there something after?
+     * @param integer $page        current page
+     * @param string  $action      current action
+     * @param array   $args        rest of query arguments
+     *
+     * @return nothing
+     */
+    function pagination($have_before, $have_after, $page, $action, $args=null)
+    {
 
-        $replyto = $this->trimmed('inreplyto');
+        // Does a little before-after block for next/prev page
 
-        $notice = Notice::saveNew($user->id, $content,
-            'Facebook', 1, ($replyto == 'false') ? null : $replyto);
+        // XXX: Fix so this uses common_local_url() if possible.
 
-        if (is_string($notice)) {
-            $this->showHome($flink, 'Error!');
-            return;
+        if ($have_before || $have_after) {
+            $this->elementStart('div', array('class' => 'pagination'));
+            $this->elementStart('dl', null);
+            $this->element('dt', null, _('Pagination'));
+            $this->elementStart('dd', null);
+            $this->elementStart('ul', array('class' => 'nav'));
+        }
+        if ($have_before) {
+            $pargs   = array('page' => $page-1);
+            $newargs = $args ? array_merge($args, $pargs) : $pargs;
+            $this->elementStart('li', array('class' => 'nav_prev'));
+            $this->element('a', array('href' => "$action?page=$newargs[page]", 'rel' => 'prev'),
+                           _('After'));
+            $this->elementEnd('li');
+        }
+        if ($have_after) {
+            $pargs   = array('page' => $page+1);
+            $newargs = $args ? array_merge($args, $pargs) : $pargs;
+            $this->elementStart('li', array('class' => 'nav_next'));
+            $this->element('a', array('href' => "$action?page=$newargs[page]", 'rel' => 'next'),
+                           _('Before'));
+            $this->elementEnd('li');
+        }
+        if ($have_before || $have_after) {
+            $this->elementEnd('ul');
+            $this->elementEnd('dd');
+            $this->elementEnd('dl');
+            $this->elementEnd('div');
         }
-
-        common_broadcast_notice($notice);
-        $this->showHome($flink, 'Success!');
     }
-    
+
 }