]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - actions/facebookhome.php
Changed @id to @class for notices ul
[quix0rs-gnu-social.git] / actions / facebookhome.php
index 6206fb6c2704498f3e8300aa47cddb6b52dd5f30..14342f944bc59bea670be9f10495f21e1a2e23bc 100644 (file)
@@ -19,7 +19,7 @@
 
 if (!defined('LACONICA')) { exit(1); }
 
-require_once(INSTALLDIR.'/lib/facebookaction.php');
+require_once INSTALLDIR.'/lib/facebookaction.php';
 
 class FacebookhomeAction extends FacebookAction
 {
@@ -28,81 +28,129 @@ class FacebookhomeAction extends FacebookAction
     {
         parent::handle($args);
 
-        $this->login();
-    }
-
-    function login()
-    {
-
-        $user = null;
-
         $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, 2); // 2 == Facebook
+        // 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(
+                FACEBOOK_PROMPTED_UPDATE_PREF, 'true');
+        }
 
         if ($flink) {
 
+            if ($_POST['submit'] == 'Send') {
+                $this->saveNewNotice($flink);
+                return;
+            }
+
             $user = $flink->getUser();
-            $this->show_home($facebook, $fbuid, $user);
+            common_set_user($user);
 
-        } else {
+            // 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')) {
 
-            # Make the user put in her Laconica creds
-            $nickname = common_canonical_nickname($this->trimmed('nickname'));
-            $password = $this->arg('password');
+                if ($facebook->api_client->data_getUserPreference(
+                        FACEBOOK_PROMPTED_UPDATE_PREF) != 'true') {
+                    $this->getUpdatePermission();
+                    return;
+                }
+            }
 
-            if ($nickname) {
+            // Use 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);
 
-                if (common_check_user($nickname, $password)) {
+        } else {
 
+            // User hasn't authenticated yet, prompt for creds
+            $this->login($fbuid);
+        }
 
-                    $user = User::staticGet('nickname', $nickname);
+    }
 
-                    if (!$user) {
-                        echo '<fb:error message="Coudln\'t get user!" />';
-                        $this->show_login_form();
-                    }
+    function login($fbuid)
+    {
+        $nickname = common_canonical_nickname($this->trimmed('nickname'));
+        $password = $this->arg('password');
 
-                    $flink = DB_DataObject::factory('foreign_link');
-                    $flink->user_id = $user->id;
-                    $flink->foreign_id = $fbuid;
-                    $flink->service = 2; # Facebook
-                    $flink->created = common_sql_now();
-                    $flink->set_flags(true, false, false);
+        $msg = null;
 
-                    $flink_id = $flink->insert();
+        if ($nickname) {
 
-                    if ($flink_id) {
-                        echo '<fb:success message="You can now use Identi.ca from Facebook!" />';
-                    }
+            if (common_check_user($nickname, $password)) {
 
-                    $this->show_home($facebook, $fbuid, $user);
+                $user = User::staticGet('nickname', $nickname);
 
-                    return;
-                } else {
-                    echo '<fb:error message="Incorrect username or password." />';
+                if (!$user) {
+                    $this->showLoginForm(_("Server error - couldn't get user!"));
                 }
-            }
 
-            $this->show_login_form();
+                $flink = DB_DataObject::factory('foreign_link');
+                $flink->user_id = $user->id;
+                $flink->foreign_id = $fbuid;
+                $flink->service = FACEBOOK_SERVICE;
+                $flink->created = common_sql_now();
+                $flink->set_flags(true, 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;
+
+            } else {
+                $msg = _('Incorrect username or password.');
+            }
         }
 
+        $this->showLoginForm($msg);
+
     }
 
-    function show_home($facebook, $fbuid, $user)
+    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(
+            FACEBOOK_PROMPTED_UPDATE_PREF, 'false');
+    }
+
+    function showHome($flink, $msg)
+    {
+
+        $facebook = get_facebook();
+        $fbuid = $facebook->require_login();
 
-        $this->show_header('Home');
+        $user = $flink->getUser();
 
-        echo $this->show_notices($user);
-        $this->update_profile_box($facebook, $fbuid, $user);
+        $notice = $user->getCurrentNotice();
+        update_profile_box($facebook, $fbuid, $user, $notice);
 
-        $this->show_footer();
+        $this->showHeader($msg);
+        $this->showNoticeForm($user);
+        $this->showNav('Home');
+
+        echo $this->showNotices($user);
+
+        $this->showFooter();
     }
 
-    function show_notices($user)
+    function showNotices($user)
     {
 
         $page = $this->trimmed('page');
@@ -112,25 +160,113 @@ class FacebookhomeAction extends FacebookAction
 
         $notice = $user->noticesWithFriends(($page-1)*NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1);
 
-        echo '<ul id="notices">';
+        $cnt = $this->showNoticeList($notice);
+
+        facebookPagination($page > 1, $cnt > NOTICES_PER_PAGE,
+            $page, 'all', array('nickname' => $user->nickname));
+    }
+
+    function showNoticeList($notice)
+    {
+        $nl = new FacebookNoticeList($notice);
+        return $nl->show();
+    }
+
+    function getUpdatePermission() {
+
+        $facebook = get_facebook();
+        $fbuid = $facebook->require_login();
+
+        startFBML();
+
+        $this->showStylesheets();
+        $this->showScripts();
+
+        $this->showLogo();
+
+        $this->elementStart('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'];
+
+        $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);
 
-        $cnt = 0;
+        $this->elementStart('p');
+        $this->element('span', array('id' => 'permissions_notice'), $instructions);
+        $this->elementEnd('p');
 
-        while ($notice->fetch() && $cnt <= NOTICES_PER_PAGE) {
-            $cnt++;
+        $this->elementStart('form', array('method' => 'post',
+                                           'action' => $app_url,
+                                           'id' => 'facebook-skip-permissions'));
 
-            if ($cnt > NOTICES_PER_PAGE) {
-                break;
+        $this->elementStart('ul', array('id' => 'fb-permissions-list'));
+        $this->elementStart('li', array('id' => 'fb-permissions-item'));
+        $this->elementStart('fb:prompt-permission', array('perms' => 'status_update',
+            'next_fbjs' => 'document.setLocation(\'' . $app_url . '\')'));
+        $this->element('span', array('class' => 'facebook-button'),
+            _('Allow Identi.ca to update my Facebook status'));
+        $this->elementEnd('fb:prompt-permission');
+        $this->elementEnd('li');
+
+        $this->elementStart('li', array('id' => 'fb-permissions-item'));
+        common_submit('skip', _('Skip'));
+        $this->elementEnd('li');
+        $this->elementEnd('ul');
+
+        $this->elementEnd('form');
+        $this->elementEnd('div');
+
+        common_end_xml();
+
+    }
+
+    function saveNewNotice($flink)
+    {
+
+        $user = $flink->getUser();
+
+        $content = $_POST['status_textarea'];
+
+        if (!$content) {
+            $this->showHome($flink, _('No content!'));
+            return;
+        } else {
+            $content_shortened = common_shorten_links($content);
+
+            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;
             }
+        }
 
-            echo $this->render_notice($notice);
+        $inter = new CommandInterpreter();
+
+        $cmd = $inter->handle_command($user, $content_shortened);
+
+        if ($cmd) {
+            $cmd->execute(new WebChannel());
+            return;
         }
 
-        echo '<ul>';
+        $replyto = $this->trimmed('inreplyto');
 
-        $this->pagination($page > 1, $cnt > NOTICES_PER_PAGE,
-                          $page, 'index.php', array('nickname' => $user->nickname));
+        $notice = Notice::saveNew($user->id, $content,
+            'Facebook', 1, ($replyto == 'false') ? null : $replyto);
+
+        if (is_string($notice)) {
+            $this->showHome($flink, 'Error!');
+            return;
+        }
 
+        common_broadcast_notice($notice);
+        $this->showHome($flink, 'Success!');
     }
 
 }